///// The following script was written entirely by Kyle Farris for Spare-O-Productions.com
///// It was created for working alongside the jQuery Library (www.jquery.com) and will not work otherwise
///// I'd like to thank Cody Lindy for his help scripts used in Thickbox 
///// (http://codylindley.com/Javascript/257/thickbox-one-box-to-rule-them-all)
///// The thickbox scripts at the bottom are slightly modified to fit the needs of this site
//////////////////////////////////////////////////////////////////////////////////////////////
///// Please, feel free to use the code below in anyway that you'd like on your own site. 
///// I'd like it if you would link back to spare-o-productions in your comment tags, though,
///// I know you probably won't, but, it'd be nice! -Kyle Farris
///////////////////////////////////////////////////////////////////////////////////////////

var sliderID = null;

//on page load call Sliders_init
$(document).ready(function() {
	allLinksInit();
	slidersInit();
	correctSizes(1);
	hideBackUpLinks();
	
	// Init StyleSheet Switcher and check for style cookies
	$('.styleswitch').click(function() {
		switchStylestyle(this.getAttribute("rel"));
		return false;
	});
	//var c = readCookie('style');
	//if (c) switchStylestyle(c);
});

//add sliders to href elements that have a class of .slider
function slidersInit() {
	$("a.slider").click(function(){
		sliderID = this.id || null;
		slide_Slider(sliderID,this.href);
		this.blur();
		return false;
	});
	TB_init();
}

//add sliders to href elements that have a class of .topLink
function topLinksInit() {
	$("a.topLink").click(function(){
		sliderID = this.rel || null;
		slide_Slider(sliderID,this.href);
		this.blur();
		return false;
	});
	TB_init();
}

// Initialize the normal links (non-bar links)
function normalLiksInit() {
	$("a.normalLink").click(function(){
		sliderID = this.rel || null;
		slide_Slider(sliderID,this.href);
		this.blur();
		return false;
	});
	TB_init();
}

function allLinksInit() {
	$("a.normalLink, a.topLink").unbind();
	$("a.normalLink, a.topLink").bind("click",function(){
		sliderID = this.rel || null;
		slide_Slider(sliderID,this.href);
		this.blur();
		return false;
	});
	TB_init();
}

// Hide the test inside the portfolio thumbnails
function emptyThumbs() {
	$(".portSectionThumb").empty();
}

// Slide the appropriate bar to the right after new page is loaded.
function slide_Slider(sliderID,url,init) {
	$("#mainSection").animate({opacity: 0.01},"fast",function() {
		$("#mainSection").load(url,function() {
			hideBackUpLinks();
			emptyThumbs();
			var pagesize = TB_getPageSize();
			var pageWidth = pagesize[0];
			var sliderRight = pageWidth - 283;
			
			// Slide the Appropriate Bar
			var sliderIDString = "#"+sliderID;
			$(sliderIDString).animate({left:sliderRight},"slow");
			returnOthers(sliderID);
			correctSizes(2);				
			$("#mainSection").animate({opacity: 0.9999},"fast");
			allLinksInit();
		});
	});
}

// When on bar is clicked, send the others back to the left side.
function returnOthers(sliderID) {
	var pagesize = TB_getPageSize();
	var pageWidth = pagesize[0];
	var sliderLeft1 = 0;
	var sliderLeft2 = 50;
	var sliderLeft3 = 100;
	
	if(sliderID == "blogSlider") {
		$("#portfolioSlider").animate({left:sliderLeft1},"fast",function() {
			$("#latestWorksSlider").animate({left:sliderLeft2},"fast",positionChangeable(1));
		});
	}
	else if(sliderID == "portfolioSlider") {
		$("#blogSlider").animate({left:sliderLeft1},"fast",function() {
			$("#latestWorksSlider").animate({left:sliderLeft2},"fast",positionChangeable(1));
		});
	}
	else if(sliderID == "latestWorksSlider") {
		$("#blogSlider").animate({left:sliderLeft1},"fast",function() {
			$("#portfolioSlider").animate({left:sliderLeft2},"fast",positionChangeable(1));
		});
	}
	else if(sliderID == "home"){
		$("#blogSlider").animate({left:sliderLeft1},"fast",function() {
			$("#portfolioSlider").animate({left:sliderLeft2},"fast",function() {;
				$("#latestWorksSlider").animate({left:sliderLeft3},"fast",positionChangeable(2));
			});
		});
		positionChangeable(2);
	}
}

// Place AJAX content in correct spot when slider is moved
function positionChangeable(situation) {
	if(situation == 1) {
		$("#mainSection").animate({left: 115},"fast"); 
		$("#logo").animate({left: 115},"fast"); 
		$("#normalNav").animate({left: 185},"fast");
	}
	else if (situation == 2) {
		$("#mainSection").animate({left: 165},"fast"); 
		$("#logo").animate({left: 165},"fast"); 
		$("#normalNav").animate({left: 235},"fast");
	}
}

// When the window is resized, make sure sure the height of the right bar is correct
$(window).resize(function() {
	correctSizes(3);
});

// Make sure stuff is aligned correctly
function correctSizes(situation) {
	var pagesize = TB_getPageSize();
	var pageHeight = pagesize[1] - 114;
	var mainSectionWidth = pagesize[0] - 413;
	var grabMainSectionHeight = $("#mainSection").innerHeight();
	
	if(situation == 1 || situation == 3) { // On pageLoad and pageResize
		// Make sure the right vertical "snippet" bar is the full height of the browser window
		var arrayPageScroll = TB_getPageScrollTop();
		$("#rightBar").css({height: (arrayPageScroll[1] + pagesize[1])+"px" });
		$(".slider").css({height: (arrayPageScroll[1] + pagesize[1])+"px" });
	}
	$("#mainSection").css({width: mainSectionWidth+"px"});
	
	if(situation != 1) {
		// position RIGHT SliderBar to lineup correctly with right vertical "snippet" bar
		// I'm doing this because of the scroll bar that shows up on longer pages
		var pageWidth = pagesize[0];
		if(grabMainSectionHeight > pageHeight) {
			var sliderRight = pageWidth - 273;
		}
		else if(grabMainSectionHeight < pageHeight) {
			var sliderRight = pageWidth - 253;
		}
		var sliderIDString = "#"+sliderID;
		$(sliderIDString).animate({left:sliderRight},1);
	}
	returnOthers(sliderID);
}

// Hides the normal links if javascript is enabled
function hideBackUpLinks() {
	$("#backUpLinks").hide();
}

//////////////////////////////////////////////////////////////////////////////////////
////////////////////////// Non-Slider-Related Scripts ////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////

// Change search bar value 
function searchTextClearDefault(self) {
	if (self.value == "Search Blog") self.value = "";
	return true;
}
// Change search bar value
function searchTextSetDefault(self) {
	if (self.value == "") self.value = "Search Blog";
	return true;
}
/////////////////////////////////////////////
// Style Sheet Switcher
function switchStylestyle(styleName) {
	$('link[@rel*=style][@title]').each(function(i) {
		this.disabled = true;
		if (this.getAttribute('title') == styleName) {
			this.disabled = false;
		}
		this.blur();
	});
	createCookie('style', styleName, 365);
	hideStyleLink = "switchTO".styleName;
	$(hideStyleLink).hide();
}

// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}// end cookie functions


function showIconLegend() {
	$("#iconLegend").slideToggle("medium");
	if($("#showIcons").val() == 1) {
		$("#showIcons").val(2);
		$("#showIconLegend").html('<a href="javascript:showIconLegend();">Hide Icon Legend</a>');
	}
	else {
		$("#showIcons").val(1);
		$("#showIconLegend").html('<a href="javascript:showIconLegend();">Show Icon Legend</a>');
	}
	$("#showIconLegend").blur();
}



//////////////////////////////////////////////////////////////////////////////////////
//////////// A little help from Codey Lindey and his ThickBox script...///////////////
//////////////////////////////////////////////////////////////////////////////////////

function TB_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
	arrayPageSize = new Array(w,h) 
	return arrayPageSize;
}

function TB_getPageScrollTop(){
	var yScrolltop;
	var xScrollleft;
	if (self.pageYOffset || self.pageXOffset) {
	yScrolltop = self.pageYOffset;
	xScrollleft = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){   // Explorer 6 Strict
	yScrolltop = document.documentElement.scrollTop;
	xScrollleft = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
	yScrolltop = document.body.scrollTop;
	xScrollleft = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScrollleft,yScrolltop) 
	return arrayPageScroll;
}