// (c) Artopod 2008, Author: Elin Tjerngren
$(document).ready(function(){	
	$("#gallery").hide();
	$("li.casethumb").bind("click",function(){
		if (!$("#gallery:hidden")[0] && $(this).hasClass("current")) {
			// hide gallery
			closeSlides();
		} else {
			// load gallery
			var url=$(this).children("a")[0].href;
			$("li.casethumb").removeClass("current");
			$(this).addClass("current");
			$("#inner").empty().load(url,function(){
				// pic's notes if exists:
				if ($("#gallery:hidden")[0]) {
					$("#gallery").slideDown("slow",function(){  
					});
				}
				var tot=400;
				$("#inner .pic img").each(function(){
					tot+=parseInt($(this).css("width"))+4;
				});
				$(".scrollpane, .jScrollPaneContainer").css("width",tot+"px");
				$("#theslide").css("height","390px");
				// styled scroll
				CSBfleXcroll("theslide");
				$("#theslide")[0].contentScroll(0,0,false);
			});
			pageTracker._trackPageview(url); // google analytics
		}
		return false;
	});

	$("#closewin").click(closeSlides);
	
	// blog - sidebar:
	$(".js_switch a").click(sidebarSwitch);
	$(".js_switch a.current").each(sidebarSwitchStart);
	
	// slider on top, see also window.onload
	$("#slideright").addClass("disabled"); // left arrow
});
		
window.onload=function() { // set with (afterloading images) and fix slider on top of page
	// fix width of the slider....check if it's wider than the viewport. 
	var slidetot=0; // this will hold the total width of inner slide
	var viewportw=parseInt($("#caseslide").css("width")); // width of viewport
	$("#caseslide li").each(function(){
		slidetot+=$(this)[0].offsetWidth; 
	});
	slidetot+=10; // add extra space for margins and such
	// debug error on width
	//if (window.console) window.console.debug(slidetot);
	
	// pilar hö/vä, default-värden
	var curpos=0;
	
	if (slidetot>viewportw) {
		$("#caseslide ul").css("width",slidetot+"px");
		var lastpos=-(slidetot-viewportw);
		var slideinterval=parseInt(0.7*viewportw);// 70% from width
		$("#caseslide ul").css("position","relative");
		// pil till höger
		$("#slideleft").click(function(){
			if (this.className.indexOf("disabled")!=-1) return false;
			var newpos=curpos-slideinterval;
			$("#slideleft").removeClass("disabled");
			$("#slideright").removeClass("disabled");
			if (newpos<lastpos) {
				newpos=lastpos;
				$("#slideleft").addClass("disabled");
			}
			$("#caseslide ul").animate({
				"left":newpos+"px"
			},1000, function() {
				curpos=newpos;	
			});
			pageTracker._trackPageview("/arrow/right"); // google analytics
			return false;
		})
		// pil till vänster
		$("#slideright").click(function(){
			if (this.className.indexOf("disabled")!=-1) return false;
			var newpos=curpos+slideinterval;
			$("#slideleft").removeClass("disabled");
			$("#slideright").removeClass("disabled");
			if (newpos>0) {
				newpos=0;
				$("#slideright").addClass("disabled");
			} 
			$("#caseslide ul").animate({
				"left":newpos+"px"
			},1000, function() {
				curpos=newpos;	
			});
			pageTracker._trackPageview("/arrow/left"); // google analytics
			return false;
		})
	} else {
		viewportw=slidetot;
		$("#slideleft").addClass("disabled"); // right arrow
	}
}
function sidebarSwitch() {
	$(".js_switch a").removeClass("current");
	$(this).addClass("current");
	var n=$(this).parent(".js_switch")[0].className.replace(/.*(list_[^ ]+).*/,"$1");
	$(".js_list").hide("slow");
	$("#"+n).show("slow");
	pageTracker._trackPageview("/sidebarswitch/"+n); // google analytics
	return false;
}
function sidebarSwitchStart() {
	var n=$(this).parent(".js_switch")[0].className.replace(/.*(list_[^ ]+).*/,"$1");
	$(".js_list").hide();
	$("#"+n).show();
	pageTracker._trackPageview("/sidebarswitch/"+n); // google analytics
	return false;
}
function toggleNote(isVis,jQobj) {
	if (isVis) {
		jQobj.hide();			
	} else {
		jQobj.show();
	}			
}
function closeSlides() {
	if ($("#gallery:visible")[0]) {
		$("#gallery").slideUp("slow",function(){
			$("li.casethumb").removeClass("current");
		});
	}
	return false;
}
			
function chP(imgurl,ID) {
	$("#"+ID)[0].src=imgurl;
}
