//Javascript Code for Projects Pages

//Redirect to JS URL
/*for(var i=0;i<content.length;i++) {
	if(window.location.href.indexOf('/'+content[i])>0) {
		window.location.href = '/projects/'+pagetitle+'#'+content[i];
	}		
}*/

function undoLinks() {
	for(var i=0;i<document.links.length;i++) {
		var thislink =document.links[i];
		thislink.onclick=null;
	}
}

if(pagetitle!='darkslide') {
	Shadowbox.init({
		displayNav: false,
		displayCounter: false
	});
} else {
	Shadowbox.init();
}
//Loading content into the page

//Check if the user has told us where to go via hashtag
var goto;
if(goto = window.location.hash.substr(1)) {
	for(var i=0;i<content.length;i++) {
		if(content[i]==goto) {
			thisid=i;
		}
	}
}
if(typeof thisid == 'undefined') {
	var thisid = 0;
}

//load content into page
function getContent(id) {
	if(id<0) {
		window.location.href=prev_page;
	}
	if(id>=content.length) {
		window.location.href=next_page;
	}
	$.post('/projects/'+pagetitle+'_content/'+content[id]+'.php','',function(response) {
		/*if(!response) {
			return false;//safety check that response isn't empty...
		}*/
		$("#hidden_container").html(response);
		//var mc_width = $("#hidden_container").width();
		//$("#main_container").css("width",mc_width);
		$("#main_container").html($("#hidden_container").html());
		$("#hidden_container").html("");
		initiateFilmRollovers();
		window.location.hash = content[id];
		if(pagetitle=='collection' && content[id]=='px100') {
			//load slider
			initSlider();
		}
	});
	//Nav buttons
	if(id<content.length-1) {
		//$("#navigation .next").css("visibility","visible");
		$("#navigation .next img").attr("title",content_titles[id+1].toUpperCase());
	} else {
		//$("#navigation .next a").unbind('click');
		//$("#navigation .next a").get(0).onclick=null;
		$("#navigation .next a").attr("href",next_page);
		//$("#navigation .next a").get(0).onclick=none;
		//$("#navigation .next").css("visibility","hidden");
	}
	if(id==0) {
		//$("#navigation .back a").unbind('click');
		//$("#navigation .back a").get(0).onclick=null;
		$("#navigation .back a").attr("href",prev_page);
		//$("#navigation .back").css("visibility","hidden");
		//$("#navigation .next a").get(0).onclick=none;
	} else {
		//$("#navigation .back").css("visibility","visible");
		$("#navigation .back img").attr("title",content_titles[id-1].toUpperCase());
	}
	//Section title
	if(thisid>0) {
		$("#section_title .title").html(content_titles[id]);
		$("#section_title").show();
	} else {
		$("#section_title").hide();
	}
	//urhere in navigation
	addClassToMenuItem(".subsection",content[id],"urhere");
	hideQuick();
	thisid = id;
}

function setUpNavButtons() {
	/*$("#navigation .next a").click(function(e){
		e.preventDefault();
		nextContent();
	});
	$("#navigation .back a").click(function(e){
		e.preventDefault();
		prevContent();
	});*/
}

function nextContent() {
	thisid++;
	getContent(thisid);
}
function prevContent() {
	thisid--;
	getContent(thisid);
}

//For the Movies Page: film rollovers
function initiateFilmRollovers() {
	//hide the move overlays
	/*$(".moreinfo").css("display","none");
	//Film 1
	$("div#movie1").hover(
		function(){
			$("#movie1 .moreinfo").css("display","");
		},
		function() {
			$("#movie1 .moreinfo").css("display","none");
		}
	);
	//Film 2
	$("div#movie2").hover(
		function(){
			$("#movie2 .moreinfo").css("display","");
		},
		function() {
			$("#movie2 .moreinfo").css("display","none");
		}
	);
	//Film 3
	$("div#movie3").hover(
		function(){
			$("#movie3 .moreinfo").css("display","");
		},
		function() {
			$("#movie3 .moreinfo").css("display","none");
		}
	);*/
	//Set up shadowbox launch
	Shadowbox.clearCache();
	Shadowbox.setup();
	//QuickFix for shadowbox: launch once so the margin calculation works.
	Shadowbox.open({
        content:    '',
        player:     "iframe",
        title:      "",
        height:     350,
        width:      350,
        animate:	false,
        fadeDuration:0
    });
    Shadowbox.close();
    if(pagetitle=='darkslide') {
	    $("#slide_container").jScrollPane();
	}
	/*if(pagetitle=='collection') {
		$("#slider").jScrollPane();
	}*/
}

function toggleQuick() {
	$("#q_open").click(function() {
		$("#quick").toggle();
	});
}

function showQuick() {
	$("#q_open").html("Sections&nbsp;&nbsp;X");
	$("#quick").show();
}
function hideQuick() {
	$("#q_open").html('Sections&nbsp;<img src="img/down.gif" border="0">');
	$("#quick").hide();
}

function addClassToMenuItem(parent,section,classname) {
	var regex = new RegExp(section+'$',"g");
	$(parent).find("a")
		.removeClass(classname)
		.filter(function() {
			return section==this.href.match(regex);
		})
		.addClass(classname)
	;
}
function resetMenuItems(parent,classname) {
	$(parent).find("a").removeClass(classname);
}

//show a particular quick subsection
function toggleQuickSub(section) {
	QuickSubReset();
	$("#sub_"+section).show();
	var width = $("#sections").width()+$("#sub_"+section).width()+40;//adjust width of quick
	$("#quick").width(width);
}

//hide the subsections
function QuickSubReset() {
	$(".subsection").hide();
	$("#quick").width("");
}


function QuickSubSetup() {
	$("#sections li a").hover(
		function() {
			toggleQuickSub($(this).html());//show this section
		},
		function() {
			resetMenuItems("#sections","active");//get rid of the little arrow
		}
	);
}

//make sure the parent of a subsection is displayed correctly (arrow)
function isInQuickSub() {
	$(".subsection").hover(
		function() {
			var thisid = $(this).attr("id");
			var idparts = thisid.split("_");
			var section = idparts[1];
			addClassToMenuItem("#sections",section,"active");
		}
	)
}

function makeJSLinks() {
	//replace all links in the document with JS-compliant ones
	//ich bin so gut!
	/*$("body").find("a").map(function() {
		var oldlink = $(this).attr("href");
		var newlink = oldlink.replace("?section=","#");
		$(this).attr("href",newlink);
	});*/
	for(var i=0;i<document.links.length;i++) {
		var oldlink =document.links[i].href;
		var newlink = oldlink.replace("?section=","#");
		document.links[i].href=newlink;
	}
}

//Get page content on load
$(document).ready(function() {
	//Hide the section title - only show when we're in a subsection
	$("#section_title").hide();
	undoLinks();
	/*setUpNavButtons();*/
	//getContent(thisid);
	//hide quick menu - this way it remains useable without JS
	toggleQuick();
	$("#q_open").click(function(event) {
		event.preventDefault();
	});
	$("#quick").hide();
	QuickSubSetup();
	isInQuickSub();
	//makeJSLinks();
	initSlider();
});

//Stuff for Darkslide
function clearSearch() {
	//clear highlighted items
	$(".slide").css("border","1px solid #161616");
}
function highlightSlide(num) {
	//highlight item and scroll to it in the div
	$("#slide"+num).css("border","1px solid white");
	$("#slide_container").scrollTo($("#slide"+num));
}
function doSearch() {
	clearSearch();
	var num = $("#slidesearch").val();
	highlightSlide(num);
}
