// (c) Artopod 2008-, Hello Future 2010- Author: Elin Tjerngren
function initgallery() {
	$("#gallery").slideUp(1000,function(){ $("#portfoliosign").fadeIn(3000); });
}
function setupGallery() {
	var tot=400;
	$("#inner .pic img,#inner .vid img,#inner .vid_txt 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);
}

$(document).ready(function(){	
	if ($.cookie("portfolio")=="shown") {
	//	$("#theslide").css("height","0px");
		//$("#gallery").slideUp(0);
		$("li.casethumb").removeClass("current");
		$("#portfoliosign").fadeIn(3000);
	} else {
//		$("#gallery").css("visibility","visible");
		setupGallery();
		setTimeout('initgallery()',1000);
		$.cookie("portfolio", "shown", { path: '/' });
	}
	 //{showArrows:true, scrollbarWidth: 15, arrowSize: 16}
	$("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(){  

					});
				}
				setupGallery();
// 				$("#theslide img").each(function(){
// 					$(this).addClass("png");	
// 				});
				// curl thingie
// 				if(curlOnload) curlOnload(); if(isIE){addIECurl(); }else {addCurl(); }
				if (typeof(cvi_curl)!="undefined") {
					$(".curl").each(function(){
						$(this).parent("a").hover(function(){
							cvi_curl.modify($(this).children(".curl")[0], { size: 7 });
						},function(){
							cvi_curl.modify($(this).children(".curl")[0], { size: 3 });
						}).click(function(){
							// se text:
							toggleNote($(this).parent(".pic").children(".txtnote:visible")[0],$(this).parent(".pic").children(".txtnote,.txtnote_bg"));
							return false;
						});
						$(this).parent("a").parent(".pic").children(".txtnote_bg,.txtnote").click(function() { 
							toggleNote($(this).parent(".pic").children(".txtnote:visible")[0],$(this).parent(".pic").children(".txtnote,.txtnote_bg"));
						});
						cvi_curl.add(this, { shadow: 90, color: 0, size: 3 });  
					});
				}
			});
			pageTracker._trackPageview(url); // google analytics
		}
		return false;
	});
	// 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; 
	});
	// pilar hö/vä, default-värden
	var curpos=0;
	$("#slideright").addClass("disabled"); // left arrow
	
	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
	}

	$("#closewin").click(closeSlides);
	
	// blog - sidebar:
	$(".js_switch a").click(sidebarSwitch);
	$(".js_switch a.current").each(sidebarSwitchStart);
});
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;
}

// addition to imageflow.js
// function handleBigPict(url) {
// 	$("#photo").empty().append('<img src="'+url+'" />').fadeIn();
// }
