var ITEMS_PER_PAGE=true; //set to true to automatically guess
var pager;
function nav_select(this_page){
	$("#main_nav").children('.selected').removeClass('selected');
	$("#"+this_page).addClass('selected');
}

// Global js using the jquery library
// var addthis_pub             = 'YOUR-ACCOUNT-ID'; 
// var addthis_logo            = 'http://www.addthis.com/images/yourlogo.png';
// var addthis_logo_background = 'EFEFFF';
// var addthis_logo_color      = '666699';
var addthis_brand           = 'Steve Wilkos';
var addthis_options         = 'email, favorites, digg, delicious, myspace, facebook, google, live, more';
$(document).ready(function(){
/* METAL SCREEN BACKGROUND */
	$(".metal_screen").wrapInner('<div class="metal_screen_content"></div>');
	$(".metal_screen").prepend('<div class="metal_screen_head"></div><div class="metal_screen_foot"></div>');
	$(".metal_screen").wrapInner('<div class="metal_screen_bg"></div>');
	$(".metal_screen_nr").addClass('metal_screen');
	$(".metal_screen_nr").removeClass('metal_screen_nr');
	

/* INNER BOX BACKGROUND */
	$(".inner_box").wrapInner('<div class="inner_box_content"></div>');
	$(".inner_box").prepend('<div class="inner_box_head"></div>');
	$(".inner_box").append('<div class="inner_box_foot"></div>');
	$(".inner_box_nr").addClass('inner_box');
	$(".inner_box_nr").removeClass('inner_box_nr');
/* SITE BUTTON */
	$(".site_btn").wrapInner('<span class="site_btn_mid"></span>');
	$(".site_btn").prepend('<span class="site_btn_left"></span>');
	$(".site_btn").append('<span class="site_btn_right"></span>');

/* IMAGE ITEMS LIST 
	--gives LI the width of the image */
	$(".image_items_list").children("li").each(function(){
		var img_span_width = $(this).children(".image").find("img").outerWidth();
		$(this).width(img_span_width);
	});
	

	//CALLS THE BIND BEHAVIOR FUNCTION
	behavior_binder();
	pager=new Pager(ITEMS_PER_PAGE);
	$("a.div_toggle").bind('click',function(e){toggle_div(e)});
	// $("a.div_toggle").each(function(i){
	// 	var id=$(this).attr('id');
	// 	id=id.replace(/^toggle_/,'photos_');
	// 	// $('#'+id).css('display','none');
	// });
	// $(".share_link").each(function(){
	// 	$(this).html('<a href="http://www.addthis.com/bookmark.php" onmouseover="return addthis_open(this, \'\', \'[URL]\', \'[TITLE]\')" onmouseout="addthis_close()" onclick="return addthis_sendto()"><img src="http://s9.addthis.com/button0-share.gif" width="83" height="16" border="0" alt="" /></a><script type="text/javascript" src="http://s7.addthis.com/js/152/addthis_widget.js"></script>');
	// });



});
function toggle_div(e)
{
	var button_id='';
	if($.browser.msie)
	{
		button_id=e.srcElement.parentElement.id;
	}
	else
	{
		button_id=$(e.currentTarget).attr('id');
	}
	var id=button_id.replace(/^toggle_/,'photos_');
	$('#'+id+' li').width(100);
	$('#'+id+' li').height(75);

		$('#'+id).slideToggle();
	
	var h=$('#'+button_id).html();
	if(h)
	{
		if(h.match(/see/i))
		{
			h=h.replace(/see/i,'hide');
		}
		else
		{
			h=h.replace(/hide/i,'see');
		}
		$('#'+button_id).html(h);
	}
}

/*
Binds behaviors
** can be re-run whenever there are DOM changes
*/
function behavior_binder(){

/* ADD FIRST/LAST TO LI
		adds a class 'first' to the first LI, and a 'last' to the last LI and 'first last' to a single LI
	*/
	$('ul').each(function(i){
		$(this).children('li:first').addClass('first');
		$(this).children('li:last').addClass('last');
	});
// END ADD FIRST/LAST TO LI
	
/* ADD SHADOW TO AN ELEMENT
		add the class "hard_shadow" to an element to give it a right/bottom drop shadow
	*/
	//make_shadow();
// END ADD SHADOW TO AN ELEMENT

}
function make_shadow()
{
	var pre=arguments[0]?(arguments[0]+' '):'';
	if(!$.browser.safari){
	//temporarily turned off for Safari on mac
	var thisElementWidth = "";
	var thisElementHeight = "";
	$(pre+'.hard_shadow').each(function(i){
		thisElementWidth = $(this).outerWidth();
		thisElementHeight = $(this).outerHeight();
		$(this).wrap("<div class='shadow_maker'></div>");
		//$(this).css({ margin:0, display: "block", position: "absolute", height: thisElementHeight, width: thisElementWidth});
		$(this).css({ margin:0, display: "block", position: "absolute"});
	});
	$(".shadow_maker").each(function(i){
		thisElementWidth = $(this).children(pre+".hard_shadow:first").outerWidth();
		thisElementHeight = $(this).children(pre+".hard_shadow:first").outerHeight();
		$(this).css({ height: thisElementHeight, width: thisElementWidth, position: "relative", marginTop:"1px", marginLeft:"1px"});
			/*if(thisElementHeight > 150){
				$(this).children(".hard_shadow:first").css({ left: "-4px", top: "-4px" });
			}else{*/
				$(this).children(pre+".hard_shadow:first").css({ left: "-1px", top: "-1px" });
			//}
	});
	
	}
}

/* LEGACY JS
	*/

var randDARTNumber=0;
function genSetRandDARTNumber()
{
 randDARTNumber = Math.round(Math.random()*1000000000000);
}
genSetRandDARTNumber();

	
// New Window (Pop-up)
// Opens a new window to the passed specifications

var defaultwidth  = 500;
var defaultheight = 350;

function newWindow(url, name, width, height, settings) {

  // URL is required
  if( url == 'undefined' ) { return false; }

  // Set defaults (if data has not been passed)
  name     = (name     == 'undefined') ? 'window' : name;
  width    = (width    == 'undefined') ? defaultwidth  : width;
  height   = (height   == 'undefined') ? defaultheight : height;
  settings = (settings != 'undefined') ? settings : 'toolbar=0';

  // Open the window
  window.open(url, name, 'width=' + width + ', height=' + height + ', ' + settings);

} // ! newWindow


//pager for video and photo pages
function Pager()
{
	var arg=0;
	this.items_per_page=arguments[arg]?arguments[arg]:5;arg++;
	if(window['pager_page_sources'])
	{
		this.page_sources=window['pager_page_sources'];
	}
	else
	{
		this.page_sources=document.location.pathname+'CURRENT_ID/pages/';
	}
	this.page_source_type='mt';
	if(window['pager_source_type'])
	{
		this.page_source_type=window['pager_source_type'];
	}
	if(this.items_per_page==true)
	{
		var per=0;
		$('.use_pager').each(function(i){
			var x=$('#'+this.id+" .image_items_list").children().length;
			if(x>per){per=x};
		});
		this.items_per_page=per;
	}
	this.at_pages=[];
	this.total_pages=[];
	this.item_counts=[];
	var self=this;
	$('.use_pager').each(function(i){
		self.current_id=this.id
		self.item_counts[self.current_id]=$(self.current_html_id()+' h4.head_number_items').html().replace(/\D/g,'');
		self.total_pages[self.current_id]=Math.ceil(self.item_counts[self.current_id]/self.items_per_page);
		self.at_pages[self.current_id]=1;
		self.process_loaded_page();
	})
}
Pager.prototype.current_html_id = function()
{
	return '#'+this.current_id
}
Pager.prototype.process_loaded_page = function()
{
	this.get_pages();
	this.bind_page_links();
	var self=this;
	$(this.current_html_id()+' a.see_more_less').bind('click',function(e){self.toggle_more_less(e)});
	if(this.total_pages[this.current_id]>1)
	{
		$(this.current_html_id()+' div.heading').append('<h4 class="at_page">Page: '+this.at_pages[this.current_id]+' of '+this.total_pages[this.current_id]+'</h4>');
	}
}
Pager.prototype.bind_page_links = function()
{
	var total=this.total_pages[this.current_id];
	var at_page=this.at_pages[this.current_id];
	var prev=this.at_pages[this.current_id]-1;
	var next=this.at_pages[this.current_id]+1;
	if(total>1)
	{
		var self=this; 
		for(var i=1;i<=total;i++)
		{
			$('li.class_'+this.current_id+'.pager_'+i+' a').bind('click',function(e){self.go_page(e)});
		}
		if(prev>=1)
		{
			$('li.class_'+this.current_id+'.pager_previous a').bind('click',function(e){self.go_page(e)});
		}
		if(next<=total)
		{
			$('li.class_'+this.current_id+'.pager_next a').bind('click',function(e){self.go_page(e)});
		}
	}
}
Pager.prototype.unbind_page_links = function()
{
	$('.class_'+this.current_id).unbind();
}
Pager.prototype.go_page = function(e)
{
	var current_class='';
	if($.browser.msie)
	{
		current_class=e.target.parentElement.className;
	}
	else
	{
		current_class=e.currentTarget.parentNode.className
	}
	var type=current_class.replace(/.* [^_]+_/,'');
	this.current_id=current_class.replace(/^class_([^ ]+) .*/,'$1');
	var current_page=this.at_pages[this.current_id];
	switch(type)
	{
		case 'next':
			this.at_pages[this.current_id]++;
			break;
		case 'previous':
			this.at_pages[this.current_id]--;
			break;
		default:
			this.at_pages[this.current_id]=type;	
			break;
	}
	var next_page=this.at_pages[this.current_id];
	var direction='left';
	if(next_page>current_page)
	{
		direction='right';
	}
	var url=this.page_sources;
	if(this.page_source_type=='mt')
	{
		url=url.replace(/CURRENT_ID/g,this.current_id);
		if(this.at_pages[this.current_id]!=1)
		{
			url+=this.at_pages[this.current_id]+'.php';
		}
	}
	else
	{
		var cat=this.current_id.replace(/^category_/,'');
		cat=cat.replace(/-/g,' ');
		url+='?c='+escape(cat)+'&page='+this.at_pages[this.current_id];
	}
	var self=this;
	this.unbind_page_links();
	$.ajax({
		type:'GET',
		url:url,
		dataType: 'html',
		success:function(data){self.display_next_page(data,direction)}
	})
}
Pager.prototype.display_next_page = function(data,direction)
{
	data=data.replace(/^(\s|\n|\r)*<div[^>]*>/,'');
	data=data.replace(/<\/div>(\s|\n\r)*$/,'');
	$(this.current_html_id()).html(data);
	this.process_loaded_page();
	//specific stuff for inner box and shadow
	$(this.current_html_id()).wrapInner('<div class="inner_box_content"></div>');
	$(this.current_html_id()).prepend('<div class="inner_box_head"></div>');
	$(this.current_html_id()).append('<div class="inner_box_foot"></div>');
	make_shadow(this.current_html_id())
	$(this.current_html_id()+" .image_items_list").children("li").each(function(){
		var img_span_width = $(this).children(".image").find("img").outerWidth();
		$(this).width(img_span_width);
	});
}
Pager.prototype.toggle_more_less = function(e)
{
	var curr;
	if($.browser.msie)
	{
		curr=e.target;
	}
	else
	{
		curr=e.currentTarget;
	}
	var parent='#'+$(curr).parents('.use_pager')[0].id;
	if($(curr).hasClass('minus'))
	{
		$(curr).addClass('plus');
		$(curr).removeClass('minus');
		$(parent+' .item_paging_links').css('display','none');
		// if($.browser.msie)
		// {
		// 	$(parent+' ul.image_items_list').toggle()
		// }
		// else
		{
			$(parent+' ul.image_items_list').slideUp();
		}
		
	}
	else
	{
		$(curr).addClass('minus');
		$(curr).removeClass('plus');
		$(parent+' .item_paging_links').css('display','block');
		// if($.browser.msie)
		// {
		// 	$(parent+' ul.image_items_list').toggle()
		// }
		// else
		{
			$(parent+' ul.image_items_list').slideDown();
		}
		
	}
}
Pager.prototype.get_pages = function()
{
	var id=this.current_id;
	var p='';
	if(this.total_pages[id]>1)
	{
		for(var i=1;i<=this.total_pages[id];i++)
		{
			if(this.at_pages[id]!=i)
			{
				p+='<li class="class_'+id+' pager_'+i+'"><a>'+i+'</a></li>';
			}
			else
			{
				p+='<li class="class_'+id+' pager_'+i+'">'+i+'</li>';
			}
		}
		p='<ul class="pager">'+this.get_prev_next(p)+'</ul>';
	}
	$(this.current_html_id()+' .item_paging_links').html(p);
	
}
Pager.prototype.get_prev_next = function(list)
{
	var id=this.current_id;
	var prev=this.at_pages[id]-1;
	var next=this.at_pages[id]+1;
	var link='<a>&lt;&lt;</a>';
	if(prev<=0)
	{
		link='&lt;&lt;';
	}

	list='<li class="class_'+id+' pager_previous">'+link+'</li>'+list;
	link='<a>&gt;&gt;</a>';
	if(next>this.total_pages[this.current_id])
	{
		link='&gt;&gt;';
	}
	list+='<li class="class_'+id+' pager_next">'+link+'</li>';
	return list;
	
}


