var start_sound = this_play = false;
/////////////////////////////////////////////////////////////////////////////////////////////////
function loadHistory() {
	$(".nohover").each(function() {
		$(this).attr("href",$("#url").val()+"#"+$(this).attr("id").replace("p","")+"/"+$(this).attr("title").replace(/ /g, "-"));
		$(this).removeAttr("title");
	});
	
	$.history.init(openThisPr);
	
	$("a[rel='history']").click(function(){
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		$.history.load(hash);
		return false;
	});
}
/////////////////////////////////////////////////////////////////////////////////////////////////
$(document).bind("pageReady", function(e) {
	loadHistory();
	$(document).trigger("projectReady");

	shortcut.add("H",function() { if($(window).scrollTop() > 50) { window.scrollTo(0, 50); doscroll(0,getScrollHeight(),0);}},{ 'type':'keydown', 'disable_in_input':true, 'keycode':72 });
	shortcut.add("J",function() { $(document).trigger("showNextProject"); },{ 'type':'keydown', 'disable_in_input':true, 'keycode':74 });
	shortcut.add("K",function() { $(document).trigger("showPrevProject"); },{ 'type':'keydown', 'disable_in_input':true, 'keycode':75 });
	shortcut.add("R",function() { $(document).trigger("showRandomProject"); },{ 'type':'keydown', 'disable_in_input':true, 'keycode':82 });
	shortcut.add("X",function() { $(document).trigger("closeProject"); },{ 'type':'keydown', 'disable_in_input':true, 'keycode':88 });
	shortcut.add("I",function() { $(document).trigger("closeProject"); },{ 'type':'keydown', 'disable_in_input':true, 'keycode':73 });
	
	var me = (location.host.indexOf("cargocollective.com") >= 0) ? "http://"+location.host+"/"+$("#url").val() : "http://"+location.host;
	shortcut.add("Shift+F",function() { top.document.location.href=me+"/following" },{ 'type':'keydown', 'disable_in_input':true, 'keycode':70 });
	
});
/////////////////////////////////////////////////////////////////////////////////////////////////
$(document).bind("projectReady", function(e, dataObj) {	
	checkForSound();
	if(dataObj && dataObj['slideheight'] > 0) {
		startSlideshow(dataObj['pid']);
	}
});

/////////////////////////////////////////////////////////////////////////////////////////////////
function checkForSound() {
    //console.log("cargo.js:checkForSound");
	// see if there is a sound && see if soundmanager is loaded
	// Check for firefox
	$(".audio_component .play_pause").live('click', function() { 
		var force = false;
	   if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
		  var ffversion=new Number(RegExp.$1); // capture x.x portion and store as a number
		  //console.log("cargo.js:FF detected:" + ffversion);
		  if(ffversion < 3.6) { force = true; }
	   } //else if($("#template").val() == "SC") { force = true; } // test for SpaceCollective
	
		if(($(".audio_component").length > 0 || force == true) && $("#soundmanagerJS").length <= 0) {
			$(this).removeClass("play").addClass("pause");
			
			//$('<div id="onscreen_audio" style="position:fixed; top:30px; right:30px; z-index:10000;"></div>').insertBefore("#content_container");
			
			var ui=document.createElement('script');
			ui.id='jQueryUIJS';
			ui.setAttribute('type','text/javascript');
			ui.setAttribute('src','/_js/jquery-ui-1.8.1.min.js');   
	
			var sm=document.createElement('script');
			sm.id='soundmanagerJS';
			sm.setAttribute('type','text/javascript');
			sm.setAttribute('src','/_js/soundmanager/soundmanager2-nodebug-jsmin.js');

	
			var hd=document.getElementsByTagName('head')[0];
			hd.appendChild(ui);
			hd.appendChild(sm);
		  
			start_sound = $(this).parents(".audio_component").attr("id");
			this_play = $(this);
		}
	});
	
	if(($(".audio_component").length > 0 ) && $("#DroidFont").length <= 0) {
		 // Load the external font from the google api
		  var droidfont=document.createElement('link');
		  droidfont.id='DroidFont';
		  droidfont.setAttribute('type','text/css');
		  droidfont.setAttribute('rel','stylesheet');
		  droidfont.setAttribute('href','http://fonts.googleapis.com/css?family=Droid+Sans+Mono');   
		  
		  var hd=document.getElementsByTagName('head')[0];
		  hd.appendChild(droidfont);
	};
	$(".progress").css("display","none");
	initPlayerSize();
}
/////////////////////////////////////////////////////////////////////////////////////////////////
function initPlayerSize() {
	$(".audio_component").each(function(){
	// Setup playhead/progress width 
       //$('.Href', this).hide();            
       var position = $(".position", this);
       var playhead = $(".playhead", this);
         
       // This makes the player correctly sized based on the .Sound width property
       var soundWidth = $(this).width();
       //console.log("soundWidth:"+soundWidth);
       var soundBordersWidth = $(".border", this).outerWidth() - $(".border", this).width();
       var controlsWidth = $(".controls", this).width(soundWidth - soundBordersWidth).width();
       var volumeWidth = $(".volume", this).width();
       var playpauseWidth = $(".play_pause", this).width();
       var spacerWidths = $(".vertical_border", this).width() * 2;
       $(".border", this).width(soundWidth - soundBordersWidth);
       $(".info", this).width(controlsWidth - volumeWidth - playpauseWidth - spacerWidths);
      
       var refWidth = $(".info", this).width();
       position.width(refWidth);
         
       playhead
         .css("width", refWidth+"px")
         .css("left", 0);

       var progressBordersWidth = $(".progress", this).outerWidth() - $(".progress", this).width();
       $(".progress")
         .css("width", (refWidth - progressBordersWidth) +"px")
         .css("left", 0);
		
		if($("#home_content").length > 0) var prog_left = "35px";
		else var prog_left = "0px";
		
       $(".progress_clip")
         .css("position", "absolute")
         .css("overflow", "hidden")
         .css("width", refWidth+"px")
         .css("left", prog_left);
	   
	   $(".position")
         .css("left", prog_left);

       $(".playhead_container", this)
         .css("position", "relative")
         .css("width", (playhead.outerWidth() * 2) + "px")
         .css("left", ((playhead.outerWidth() * -1)) + "px");

       $(".progress_container", this)
         .css("position", "absolute")
         .css("z-index", "-1")
         .css("width", (playhead.outerWidth() * 2) + "px")
         .css("left", ((playhead.outerWidth() * -1)) + "px");
	});
}

/////////////////////////////////////////////////////////////////////////////////////////////////
function openThisPr(pid,viewtype,url,id,template) {
	if(pid) {
		
		var href=pid.split("/");
		var pid = href[0];
		if(document.getElementById('p'+pid)) var url = document.getElementById('p'+pid).name;
		if(url == '' || !url) var url = $("#url").val();
		
		var viewtype = document.getElementById('viewtype').value;
		var template = document.getElementById('template').value;
		var design = document.getElementById('design').value;
		var designpath = document.getElementById('designpath').value;
		var templatepath = document.getElementById('templatepath').value;
		var spinner = document.getElementById('load_'+pid);
		var nav_spinner = document.getElementById('nav_loadspin');
		if(spinner) spinner.style.display = "block";
		if(nav_spinner && viewtype != "following") nav_spinner.style.display = "block";
		
		
		
		var cur = document.getElementById('current_open');	
		var prev = document.getElementById('prev_open');
		var prev_type = document.getElementById('prev_type');
		var this_spot = document.getElementById('this_spot');
		var send_open = true;

		$("#prev_open").val($("#current_open").val());
		$("#current_open").val(pid);
		var this_spot = pid_list.indexOf(pid);
		if(!this_spot) this_spot = -1;
		$("#this_spot").val(this_spot);
		
		if(document.getElementById('menu_'+prev.value)) document.getElementById('menu_'+prev.value).className = prev_type.value;
		if(document.getElementById('menu_'+pid)) {
			prev_type.value = document.getElementById('menu_'+pid).className;
			document.getElementById('menu_'+pid).className = "nav_active";	
		}
		
		/// SC
		if($('#items_container').length > 0) {
			if(pid == $("#prev_open").val()) send_open = false;
			else {
				$("#moduleholder_c").val($("#moduleholder_o").val());
				$("#moduleholder_o").val($("#item_"+pid).html());
				$("#modulenumber").val($("#prev_open").val());
			}
			
			
		}
		if(send_open) $.post(templatepath+"/entry-detail.php", { pid: pid, viewtype: viewtype, url: url },openThisPrInline);
		
		//if(viewtype == 'single') document.getElementById('item_'+pid).style.display = "none";
		intransition = true;
		
		if(document.getElementById('item_'+pid)) var thispage = document.getElementById('item_'+pid).getAttribute("page");
		var limit = document.getElementById('limit').value;
		var cur = document.getElementById('current_page').value;
		if(thispage && thispage != 0) {
			if(thispage != cur) {
				changePage(thispage,limit);
				if(document.getElementById('item_'+pid)) {
					var thisspot = document.getElementById('item_'+pid).getAttribute("spot");
					document.getElementById('this_spot').value = thisspot;
				}
			}
		}
	
	} else {
		$("#load").empty();
		var location_str = location+" ";
		if($("#start_page").length > 0 && ($('#prev_open').val() == "none" || location_str.indexOf("#") < 0)) openThisPr($("#start_page").val());
		else closeThisPr($('#current_open').val(),false,'list');
	}
	return false;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
function openThisPrInline(data) {
	var starting = data.indexOf("|#=#|");
	var starting2 = data.indexOf("|##=##|");
	var starting3 = data.indexOf("|###=###|");
	var pid = data.substring(0, starting);
	var slideheight = data.substring((starting+5), starting2);
	var slidecount = data.substring((starting2+7), starting3);
	var content = data.substring((starting3+9), data.length);
	var isIE = testIE();
	
	if($('#maincontainer').length > 0) {
		var container=document.getElementById('maincontainer');
		var prev = document.getElementById('prev_open');
		var prev_type = document.getElementById('prev_type');
		var spinner = document.getElementById('load_'+pid);
		var nav_spinner = document.getElementById('nav_loadspin');
		var o_thumb_nav = document.getElementById('o_thumb_nav').value;
		var current_page = document.getElementById('current_page').value;
		if(spinner) spinner.style.display = "none";
		if(nav_spinner) nav_spinner.style.display = "none";
		
		$(".project_thumb").removeClass("active");
		$("#item_"+pid).addClass("active");
		
	} else if(document.getElementById('items_container')) {
		var prev = $("prev_open").val(); 
		if(prev == pid && document.getElementById('item_'+pid).className == "project_feed_full") container = false;
		//else if(prev != "none" && prev != pid) closeFeedPr(prev,true);
		else {
			var container=document.getElementById('item_'+pid);
			container.onmouseover = "";
			container.onmouseout = "";
			container.className = "project_feed_full";
			if(isIE) $('#item_'+pid).addClass("ie");
			try { DD_roundies.addRule(".project_feed_full", 5, true); } catch(err) { }
			//
			if($("#moduleholder_c").val() != "none") {
				var oldpid = $("#modulenumber").val();
				var closecontent = $("#moduleholder_c").val();
				printClosed(oldpid,closecontent,true);
				oldpid = "none";
				closecontent = "none";
			}
		}
		if(!content || content == "") container = false;
		
		
	} else {
		var container=document.getElementById('pRow'+pid);
		container.className = "content_container_open";
	}
	
	if(container) {
	   // Fire an event before this project gets overwritten
      $(document).trigger("projectClose", [container]);  
	   
		var isIE = testIE();
		container.style.display = "block";
		//container.innerHTML=content;
		if($('#maincontainer').length > 0 && isIE) container.innerHTML=content; 
		else if($('#maincontainer').length > 0) $("#maincontainer").html(content);
		else if($('#pRow'+pid).length > 0) $("#pRow"+pid).html(content);
		else if($('#item_'+pid).length > 0) $("#item_"+pid).html(content);
	}
	
	intransition = false;	
	
	//doscroll(0,getScrollHeight(),0);
	if(!document.getElementById('home_gallery') && !document.getElementById('items_container')) {
		if(getScrollHeight() > 50) window.scrollTo(0, 50);	
		doscroll(0,getScrollHeight(),0);
	}
	
	if(o_thumb_nav == "yes") { 
		$("#page_"+current_page).css("display","none");
	}
	
	// Set the ready return
	var readyObj = Array();
	readyObj["pid"] = pid;
	readyObj["slideheight"] = slideheight;
	
	var isHome = document.getElementById('home_gallery');
	if($('#items_container').length > 0) {
		$(document).trigger("projectReady", [ readyObj ]);
		shiftPosition(true);
		var spacerHeight = $('#item_'+pid).height();
		$('#item_'+pid+" #body_container").before('<div style="height:'+spacerHeight+'px" id="cardspacer">&nbsp;</div>');
		$('#item_'+pid+" #body_container").css("display","none");
		if($(".header_img").css("position") == "fixed") var toppad = $(".header_img").height();
		else var toppad = 60;
		if($('#startpage').val() == 'none' || $('#startpage').val() != pid) {
			$.scrollTo( { top:($('#item_'+pid).offset().top-toppad), left:0}, 450, { onAfter:function(){ 
					$('#cardspacer').remove();
					$('#item_'+pid+" #body_container").fadeIn(); 
					//$(document).trigger("projectReady", [ readyObj ]);
				} 
			}); 
		} else {
			$('#cardspacer').remove();
			$('#item_'+pid+" #body_container").fadeIn(); 
		}
		$('#startpage').val('none');
		return false; 
		
		
	} else if(!$("#home_gallery")) {
		if(getScrollHeight() > 50) window.scrollTo(0, 50);	
		doscroll(0,getScrollHeight(),0);
	
	}
	
	// We're ready
	$(document).trigger("projectReady", [ readyObj ]);
	
	if (typeof projectLoadComplete == "function") projectLoadComplete();
}
/////////////////////////////////////////////////////////////////////////////////////////////////
$(document).bind("closeProject", function(e){
	if($("#template").val() == "spacecollective") closeFeedPr($("#current_open").val(),false,'list');
	else closeThisPr($("#current_open").val(),false,'list');
});
/////////////////////////////////////////////////////////////////////////////////////////////////
$(document).bind("initNextProject", function(e){
	$(".project_next a").live("click", function() { 
		$(document).trigger("showNextProject");
		return false;
	});
});
/////////////////////////////////////////////////////////////////////////////////////////////////
$(document).bind("showNextProject", function(e){
	var cur = ($("#current_open").val() != "none") ? $("#current_open").val() : pr_list[pr_list.length-1];
	var spot = array_search(cur, pr_list);
	if(parseInt(cur) > 0 && spot === false) var next = 0;
	else if(cur == pr_list[pr_list.length-1]) var next = 0;
	else var next = parseInt(spot)+1;
	
	var title = stripslashes(prt_list[next]);
	if(title) title = makeDetailLink(title);
	else title = "";
	$.history.load(pr_list[next]+"/"+title);
});
/////////////////////////////////////////////////////////////////////////////////////////////////
$(document).bind("showPrevProject", function(e){
	var cur = ($("#current_open").val() != "none") ? $("#current_open").val() : pr_list[0];
	var spot = array_search(cur, pr_list);
	if(parseInt(spot) > 0 && spot === false) var next = pr_list.length-1;
	else if(parseInt(spot) == 0 || parseInt(cur) == 0) var next = pr_list.length-1;
	else var next = parseInt(spot)-1;
	
	var title = stripslashes(prt_list[next]);
	if(title) title = makeDetailLink(title);
	else title = "";
	$.history.load(pr_list[next]+"/"+title);
});
/////////////////////////////////////////////////////////////////////////////////////////////////
$(document).bind("showRandomProject", function(e){
	var cur = ($("#current_open").val() != "none") ? $("#current_open").val() : false;
	cur = array_search(cur, pr_list);
	var spot = Math.floor(Math.random()*pr_list.length-1);
	if(parseInt(cur) == spot || spot < 0) var next = 0;
	else var next = parseInt(spot);
	
	var title = stripslashes(prt_list[next]);
	if(title) title = makeDetailLink(title);
	else title = "";
	$.history.load(pr_list[next]+"/"+title);
});
/////////////////////////////////////////////////////////////////////////////////////////////////
function makeDetailLink(title) {
	var url = title
		.replace(/^\s+|\s+$/g, "")	
		.replace(/[_|\s]+/g, "-")
		.replace(/[^a-zA-Z0-9-]+/g, "")
		.replace(/[-]+/g, "-")
		.replace(/^-+|-+$/g, "")
		; 
	return url;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
function startSlideshow(pid) {
	var elements = document.getElementsByClassName("slideshow_container_"+pid);
	var template = $("#template").val();
	var design = $("#design").val();
	if(design) var isSC = design.indexOf("SC") >= 0 ? true : false;
	else var isSC = false;
	if(elements) {
		var el_prev = document.getElementsByClassName("slide_prev_"+pid);
		var el_next = document.getElementsByClassName("slide_next_"+pid);
		var el_count = document.getElementsByClassName("slideshow_count_"+pid);
		var el_nav = document.getElementsByClassName("slideshow_nav_"+pid);
		var el_wrapper = document.getElementsByClassName("slideshow_wrapper_"+pid);
		var el_slideclick = document.getElementsByClassName("slideclick_"+pid);
		
		
		for(var i = 0;i < elements.length;i++) {
			elements[i].setAttribute('id','slideshow_container_'+pid+'_'+i);
			elements[i].setAttribute('class','slideshow_container');
			if(el_prev[0]) el_prev[i].setAttribute('id','prev_'+pid+'_'+i);
			if(el_next[0]) el_next[i].setAttribute('id','next_'+pid+'_'+i);
			if(el_count[0]) el_count[i].setAttribute('id','slideshow_count_'+pid+'_'+i);
			if(el_count[0]) el_count[i].setAttribute('class','slideshow_count');
			if(el_nav[0]) el_nav[i].setAttribute('id','slideshow_nav_'+pid+'_'+i);
			if(el_nav[0]) el_nav[i].setAttribute('class','slideshow_nav');
			if(el_wrapper[0]) el_wrapper[i].setAttribute('id','slideshow_wrapper_'+pid+'_'+i);
			if(el_wrapper[0]) el_wrapper[i].setAttribute('class','slideshow_wrapper');
			if(el_slideclick[0]) el_slideclick[i].setAttribute('id','slideclick_'+pid+'_'+i);
			if(el_slideclick[0]) el_slideclick[i].setAttribute('class','slideclick');
			var divs = elements[i].getElementsByTagName("img");
			if(divs[0]) var slideheight = divs[0].height;
			var slidewidth = 0;
			for(var q=0; q<divs.length;q++) {
				if(divs[q] && divs[q].width > slidewidth) slidewidth = divs[q].width;
				else slidewidth = slidewidth;
				if(isSC && divs[q]) {
					if(divs[q].height > slideheight) slideheight = divs[q].height;
					else slideheight = slideheight;
				}
			}
			$('#slideshow_wrapper_'+pid+'_'+i).css("width",slidewidth+"px");
			$('#slideshow_container_'+pid+'_'+i+' br').each(function() { $(this).remove(); });
			
			if($("slideconfig").attr("has_thumbs") == "yes") {
				var thumb_position = $("slideconfig").attr("thumb_position");
				var nav_position = $("slideconfig").attr("nav_position");
				var has_nav = $("slideconfig").attr("has_textnav");
				
				if(thumb_position == "top") {
					$('#slideshow_wrapper_'+pid+'_'+i).before('<ul class="slideshow_thumbs" id="slideshow_thumbs_'+pid+'_'+i+'">');
				} else if(thumb_position == "bottom") {
					if(nav_position == "bottom" && has_nav == "yes") {
						$('#slideshow_nav_'+pid+'_'+i).before('<ul class="slideshow_thumbs" id="slideshow_thumbs_'+pid+'_'+i+'">');
						$('#slideshow_nav_'+pid+'_'+i).css("clear","both");
					} else {
						$('#slideshow_wrapper_'+pid+'_'+i).after('<ul class="slideshow_thumbs" id="slideshow_thumbs_'+pid+'_'+i+'">');
					}
				}
				
				$('#slideshow_thumbs_'+pid+'_'+i).css("width",slidewidth+"px");
				
				
				
			} else var has_thumbs = "";
			
			if($("slideconfig").attr("is_autoplay") == "yes") var timeout = parseFloat($("slideconfig").attr("auto_delay"))*1000;
			else var timeout = 0;
			var transition = $("slideconfig").attr("transition");
			if(transition == "none") {
				var transition_speed = 1;
				var transition = "fade";
			} else {
				var transition_speed = '2000';
			}
			
			$(document).ready(function() { 
				$('#slideshow_container_'+pid+'_'+i).cycle({
					fx: transition, 
					pager:  '#slideshow_thumbs_'+pid+'_'+i,
					height: slideheight,
					width: slidewidth,
					next:'#next_'+pid+'_'+i,
					prev:'#prev_'+pid+'_'+i,
					contain:'#slideclick_'+pid+'_'+i,
					timeout:  timeout,
					speed: transition_speed,
					this_i: i,
					
					// callback fn that adjusts the slideshow's height 
					before:	function(){ 
						var $sh = $(this).height();
						var delay = $("slideconfig").attr("transition") == "none" ? 10 : 400;
						if($sh > 0 && !isSC) $(this).parent().animate({ height: $sh }, delay);
					},
					
					// callback fn that creates a thumbnail to use as pager anchor 
					pagerAnchorBuilder: function(idx, slide) { 
					//return '<li class="slideshow_thumb"><a href="#"><img src="' + getThumbFile(slide.src) + '" /></a></li>'; }
					return '<li class="slideshow_thumb"><a href="#"><img src="' + slide.src + '" /></a></li>'; }
					
				});
			});
			
			if($("slideconfig").attr("count_style") == "parenth" && el_count[0]) var count_nav = '(1 of '+divs.length+')';
			else if($("slideconfig").attr("count_style") == "no_parenth" && el_count[0]) var count_nav = '1 of '+divs.length;
			$('#slideshow_count_'+pid+'_'+i).html(count_nav);
			
			

		}
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
function getThumbFile(file_name) {
	var ext = file_name.substr(file_name.lastIndexOf('.'));
	var name = file_name.substring(0, file_name.lastIndexOf('.'));
	return name+"_t"+ext
}
/////////////////////////////////////////////////////////////////////////////////////////////////
function showStartProject(pid) {
	var container=document.getElementById('maincontainer');
	if(container) container.style.display = "block";

	var cur = document.getElementById('current_open');	
	var prev = document.getElementById('prev_open');
	var this_spot = document.getElementById('this_spot');
	var prev_type = document.getElementById('prev_type');
	
	if(this_spot) {
		this_spot.value = pid_list.indexOf(pid);
		if(prev) prev.value = cur.value;
		cur.value = pid;
		
		if(document.getElementById('menu_'+prev.value)) document.getElementById('menu_'+prev.value).className = prev_type.value;
		if(document.getElementById('menu_'+pid)) {
			prev_type.value = document.getElementById('menu_'+pid).className;
			document.getElementById('menu_'+pid).className = "nav_active";	
		}
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
function closeThisPr(pid,reopen,viewtype) {
   //console.log("cargo.js:closeThisPr");
	var prev_type = document.getElementById('prev_type');
	if(document.getElementById('menu_'+pid)) document.getElementById('menu_'+pid).className = prev_type.value;
	var container=document.getElementById('maincontainer');
	
	// Fire an event: projectClose
   $(document).trigger("projectClose", [container]);  
	
	if(container) {
		container.innerHTML = "";
		container.style.display = "none";
	}
	
	//doscroll(0,getScrollHeight(),0);
	
	if(viewtype == 'single') document.getElementById('item_'+pid).style.display = "block";
	
	$.history.load("");
	if (jQuery.browser.safari) window.scrollTo(0,0);
	
	if(document.getElementById('pr_contain_item_'+pid)) document.getElementById('pr_contain_item_'+pid).className = "";
	
	var o_thumb_nav = document.getElementById('o_thumb_nav').value;
	var current_page = document.getElementById('current_page').value;
	if(o_thumb_nav == "yes") { 
		$("#page_"+current_page).css("display","block");
	}
	
	$(".project_thumb").removeClass("active");
	
}
/////////////////////////////////////////////////////////////////////////////////////////////////
function changePage(newpage,limit) {
	var cur = document.getElementById('current_page').value;
	var total_pages = document.getElementById('total_pages').value;
	var current_page = document.getElementById('page_'+cur);
	var current_nav = document.getElementById('nav_page_'+cur);
	var new_page = document.getElementById('page_'+newpage);
	var new_nav = document.getElementById('nav_page_'+newpage);
	var pagination = document.getElementById('pagination');
	var pid = $('#current_open').val();
	
	if(document.getElementById('pr_contain_item_'+cur)) document.getElementById('pr_contain_item_'+cur).className = "";
	
	// dump detail
	var container=document.getElementById('maincontainer');
	if(container) {
		container.style.display = "none";
		container.innerHTML = "";
	}
	// dump SC page
	var itemcontainer=document.getElementById('items');
	if(itemcontainer) {
		
		if(pid != "none" && $("#item_"+pid).attr("spot") == 0 && $("#item_"+pid).attr("page") == 0) { // is page
			$("#item_"+pid).html("");
			$("#item_"+pid).attr("className","pagecontainer");
		}
	}
	
	
		
	// reset nav status
	var current_open = document.getElementById('current_open').value;
	if(current_open) {
		var prev_type = document.getElementById('prev_type');
		if(document.getElementById('menu_'+current_open)) document.getElementById('menu_'+current_open).className = prev_type.value;
	}
	
	// dump the old nav
	if(current_nav) current_nav.style.display = "none";
	
	// show the new nav
	if(new_nav) new_nav.style.display = "block";
	
	// dump prev page thumbs
	if(current_page) current_page.style.display = "none";
	
	// change pagination
	var pagout = "";
	if(newpage > 1) pagout += "<a href=\"javascript:void(0)\" onclick=\"changePage("+(parseInt(newpage)-1)+","+limit+")\" class=\"prev_page\">Prev page</a>";
	if(newpage > 1 && newpage < total_pages) pagout += "<span>&nbsp;/&nbsp;</span>";
	if(newpage < total_pages) pagout += "<a href=\"javascript:void(0)\" onclick=\"changePage("+(parseInt(newpage)+1)+","+limit+")\" class=\"next_page\">Next page</a>";
	pagout += "&nbsp;<span>("+newpage+" of "+total_pages+")</span>";
	
	if($(".pagination").length > 0) $(".pagination").each(function() { $(this).html(pagout); });
	
	
	// show next page thumbs
	if(new_page) {
		new_page.style.display = "block";
		var itemsList = new_page.childNodes.length;
		for(i=0;i<itemsList;i++) {
			var thisItem = new_page.childNodes[i];
			thisPid = thisItem.getAttribute("name");
			var thumbContainer = document.getElementById("cardthumb_"+thisPid);
			if(thumbContainer && thumbContainer.getAttribute("name") != "") {
				//thumbContainer.innerHTML = "<img src=\""+thumbContainer.getAttribute("name")+"\" border=\"0\" />";
				$(thumbContainer).html("<img src=\""+thumbContainer.getAttribute("name")+"\" border=\"0\" />");
				thumbContainer.setAttribute("name","");
			}
		}
	}
	
	if(document.getElementById('menu_'+pid)) {
		$("#menu_"+pid).addClass("nav_active");	
	}
	
	// set new page values
	document.getElementById('current_page').value = newpage;
	curspot = cur < newpage ? (cur*limit)-1 : ((newpage-1)*limit)-1;
	document.getElementById('this_spot').value = curspot;
	
	if(document.getElementById('items')) shiftPosition(false);
	
	window.scrollTo(0,0);
	
	//changeHorizNav(newpage);
	
}
/////////////////////////////////////////////////////////////////////////////////////////////////
function changeHorizNav(newpage) {
	// move pages, pagination and following if nav is horiz
	if($(".nav_container").hasClass("horizontal")) {
		if($("#pagination").length > 0) $("#nav_page_"+newpage).append($("#pagination"));
		if($(".nav_follow").length > 0) $("#nav_page_"+newpage).append($(".nav_follow"));
		if(($(".page_link").length > 0 || $(".link_link").length > 0) && $("#sticky_page").val() != "none") {
			if($("#sticky_page").val() == "top") $("#nav_page_"+newpage).prepend($(".page_link")).prepend($(".link_link"));
			else {
				$("#nav_page_"+newpage).append($(".page_link")).append($(".link_link"));
				if($("#pagination").length > 0) $("#nav_page_"+newpage).append($("#pagination"));
				if($(".nav_follow").length > 0) $("#nav_page_"+newpage).append($(".nav_follow"));
			}
		}
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
function doFollow(my_url,their_url,type,name,where) {
	if(type == "add") var agree = confirm("Follow "+name+"?");
	else if(type == "remove") var agree = confirm("Unfollow "+stripslashes(name)+"?");
	if(agree) {
		if(where != 'follow') var bolt = document.getElementById('following_header_bolt_'+where);
		else var bolt = document.getElementById('following_header_bolt');
		var module = $("#module_"+their_url);
		if(bolt) bolt.innerHTML = '<img src="../_gfx/loadingAnim.gif" width="15" height="15">';
		if(module) module.css("display","none");
		$.post("includes/following-process.php", { my_url:my_url, their_url:their_url, type:type, name:name, where:where }, followResult);
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
function followResult(data) {
	var starting = data.indexOf("|#=#|");
	var starting2 = data.indexOf("|##=##|");
	var pid = data.substring(0, starting);
	var url = data.substring((starting+5), starting2);
	var content = data.substring((starting2+7), data.length);
	var follow_link = content;
	if(pid != 'follow') {
		var bolt = document.getElementById('following_header_bolt_'+pid);
		var booklink = document.getElementById('following_header_follow_'+pid);
	} else {
		var bolt = document.getElementById('following_header_bolt');
		var booklink = document.getElementById('following_header_follow');
	}
	if(bolt){
		if(document.getElementById('home_gallery')) bolt.innerHTML = '<img src="../_gfx/sc-bolt.png" width="15" height="15">';
		else bolt.innerHTML = '<img src="../_gfx/sc-bolt-white.png" width="15" height="15">';
	} if(booklink) booklink.innerHTML = follow_link;
	if($("#home_gallery").length > 0) {
		$("#home_follow_"+url).html(content);
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
function changeFollowPage(newpage) {
	if(newpage) {
		if(!isNaN(parseInt(newpage))) var ishome = true;
		else var ishome = false;
		if(!ishome) newpage=parseInt(newpage.replace("page",""));
		var cur = document.getElementById('current_page').value;
		var limit = parseInt(document.getElementById('total_pages').value);
		var current_page = document.getElementById('page_'+cur);
		var new_page = document.getElementById('page_'+newpage);
		var pagination = document.getElementById('pagination');
		
		// change pagination
		var pagout = "";
		if(!ishome) {
			var prevlink = "#page"+(parseInt(newpage)-1);
			var nextlink = "#page"+(parseInt(newpage)+1);
		} else {
			var prevlink = "javascript:void(0)\" onclick=\"changeFollowPage('"+(parseInt(newpage)-1)+"')";
			var nextlink = "javascript:void(0)\" onclick=\"changeFollowPage('"+(parseInt(newpage)+1)+"')";
		}
		if(newpage > 1) pagout += "<a href=\""+prevlink+"\" rel=\"history\">Prev page</a>";
		if(newpage > 1 && newpage < limit) pagout += "<span>&nbsp;/&nbsp;</span>";
		if(newpage < limit) pagout += "<a href=\""+nextlink+"\" rel=\"history\">Next page</a>";
		pagout += "&nbsp;<span>("+newpage+" of "+limit+")</span>";
		
		if($(".follow_pagination").length > 0) $(".follow_pagination").each(function() { $(this).html(pagout); });
		$("#page_"+newpage+" img").each(function(){ $(this).attr("src",$(this).attr("name")); });
		
		// show next page thumbs
		if(new_page) {
			new_page.style.display = "block";
			current_page.style.display = "none";
		}
		
		// set new page values
		document.getElementById('current_page').value = newpage;
		
		$("a[rel='history']").click(function(){
			var hash = this.href;
			hash = hash.replace(/^.*#/, '');
			$.history.load(hash);
			return false;
		});
		
		window.scrollTo(0,0);
	}
	
}
/////////////////////////////////////////////////////////////////////////////////////////////////
	function pageload(hash) {
		if(hash) {
			var template = document.getElementById('template').value;
			$.post("designs/"+template+"/entry-detail.php", { pid: pid, viewtype: 'list', url: url },openThisPrInline);
		} else {
			$("#load").empty();
		}
	}
/////////////////////////////////////////////////////////////////////////////////////////////////