/**************************************************************************************
File: 			global.js
Copyright: 		Breakthrough Design Group
Description: 	Global js file
***************************************************************************************/
/************************************
		IMPORTANT
		WHEN ADDING NEW PAGE - ADD IT TO tpages BELOW
*************************************/

thash = 'home';
tpages = ['home','about','portfolio','contact','burl','broke','baro','hearth','peel','teardrop','jh-30','partners'];

$(document).ready(function () {

	/******************************************
		SLIDESHOW
			1. checking cookie
			2. if doesn't exist, show slideshow, else show site
	******************************************/
	thash = gethash();
	if(thash=='home'){
		slideshow_load();
	} 
	else {
		// ANY CHANGES TO THIS SECTION MUST BE MIRRORED BELOW AFTER SLIDESHOW IS FINISHED
		// Loading content into our 3 sections from file w/ same name as hash
		tshowload = false;
		load_sections(thash);
	}
});

/**********************
		load_sections: Page Loader - the brains
**********************/
tshowload = true;
function load_sections(thashtemp){
	// Turn off back button listener while we load the new page
	clearTimeout(bbtimeout);
	if(thashtemp =="home" && thash =='home'){}
	else {
		if(tshowload){
			showLoader(true);
		}
	}
	
	thash = thashtemp;
	// Loading content into our 3 page sections from php files w/ same name
	$(".left-section, .middle-section, .right-section").append("<div class='inner' style='display:none'></div>");
	$('.left-section .inner:last').load(thash+'.php #left-section > *', {limit: 25}, function(){
		if(jQuery.browser.msie)$(this).pause(400).fadeIn('slow');
		else $(this).fadeIn('slow');
		$('.middle-section .inner:last').load(thash+'.php #middle-section > *', {limit: 25}, function(){
			if(jQuery.browser.msie)$(this).pause(400).fadeIn('slow');
			else $(this).fadeIn('slow');
			$('.right-section .inner:last').load(thash+'.php #right-section > *', {limit: 25}, function(){
				if(jQuery.browser.msie){
					$(this).pause(400).fadeIn('slow',function(){
						load_handlers();
						$(".left-section .inner:first, .middle-section .inner:first, .right-section .inner:first").fadeOut('slow',function(){$(this).remove()});
						showLoader(false);
						checkBackButton();
					});
				}
				else $(this).fadeIn('slow',function(){
					load_handlers();
					$(".left-section .inner:first, .middle-section .inner:first, .right-section .inner:first").fadeOut('slow',function(){$(this).remove()});
					showLoader(false);
					checkBackButton();
				});
				// When loading NON-home page first, we fade out white loader, and fadein page from white to dark
				if(tshowload==false){
					$('#TB_load_center').pause(400).fadeOut('fast',function(){
						showmain();
						tshowload=true;
						$(this).remove();
					});
				}
				if(thash=='home')loadTwitter();
			});
		});
	});
}
	
/**********************
	Page Handlers
**********************/
function load_handlers(){
	$('.navigation a').unbind('click').bind('click',function(){
		load_sections(getHashFromHref(this.href));
	});
	tb_init('a.thickbox, area.thickbox, input.thickbox');	//thickbox link handlers
	
	/**********************
		Portfolio Page
	**********************/
	/** Left bar thumb mouseovers
	if($('.portfolio-pieces').length > 0){
		$('.portfolio-pieces img').unbind('mouseover').mouseover(function(){
			var piece = getPieceName(this.src);
			this.src = 'images/portfolio-thumb-'+piece+'-over.jpg';
		});
		$('.portfolio-pieces img').unbind('mouseout').mouseout(function(){
			var piece = getPieceName(this.src);
			this.src = 'images/portfolio-thumb-'+piece+'.jpg';
		});
	}
	**/
	
	// Thumbnail slivers - DEPRECATED, USED FOR OLD PROJECTS ONLY
	if($('.portfolio-thumbs').length > 0){
		$('.portfolio-thumbs img').unbind('mouseover').mouseover(function(){
			var fullsrc = this.src.split('/');
			var imgsrc = fullsrc[fullsrc.length-1];
			var srcspl = imgsrc.split('.')[0];
			var piece = srcspl.split('-')[0];
			var pcnum = srcspl.split('-')[2];			
			this.src = 'images/'+ piece +'-detail-'+pcnum+'-over.jpg';
		});
		$('.portfolio-thumbs img').unbind('mouseout').mouseout(function(){
			var fullsrc = this.src.split('/');
			var imgsrc = fullsrc[fullsrc.length-1];
			var srcspl = imgsrc.split('.')[0];
			var piece = srcspl.split('-')[0];
			var pcnum = srcspl.split('-')[2];				
			this.src = 'images/'+ piece +'-detail-'+pcnum+'.jpg';
		});
	}
	
	// Thumbnail slivers NEW OPACITY CHANGE
	if($('.portfolio-thumbs-new').length > 0){
		$('.portfolio-thumbs-new img').unbind('mouseover').mouseover(function(){
			$(this).removeClass('opacitymed');
		});
		$('.portfolio-thumbs-new img').unbind('mouseout').mouseout(function(){
			$(this).addClass('opacitymed');
		});
	}
	
	// Scrolling Text
	if($('.scroll-controls').length > 0){
		$('.scroll-controls a.down').unbind('click').click(function(){
			scrollTop = $('.scroll').scrollTop();
	  		$('.scroll').animate({scrollTop: scrollTop+150}, 1000);
		    return false;
		});
		$('.scroll-controls a.up').unbind('click').click(function(){
			scrollTop = $('.scroll').scrollTop();
	  		$('.scroll').animate({scrollTop: scrollTop-150}, 1000);
		    return false;
		});
	}
}
var bbtimeout;
function checkBackButton(){
	if(window.location.hash != '#'+thash){
		var newhash = window.location.hash.substring(1,window.location.hash.length);
		load_sections(newhash);
		thash = newhash;
	}
	bbtimeout = setTimeout('checkBackButton()',400);
}
	
function showLoader(state){
	if(state){
		$("body").append("<div id='TB_load'><img src='images/loading-gray-bg.gif' /></div>");
		$('#TB_load').show();
	} else $('#TB_load').fadeOut('slow').remove();
}

/**********************************************
	SLIDESHOW FUNCTIONS
**********************************************/
ss_num_loaded=0;
ss_total_pics=31;	//Change this to add more pics
function slideshow_load(){
	// 1. Show Loading Message
	if(jQuery.browser.msie)var displaytype = 'block'; else var displaytype = 'table'; 
	$('.shim').css({display: displaytype});
	
	/** 2. Load Images add load handlers USED TO BUILD THE IMAGES WHEN THE PAGE LOADED, 
	NOW WE USE PHP TO LOAD THE IMAGES WHEN THE PAGE LOADS - DUE TO IE BUG - WOW. SHOCKING.  
	HERE IS THE OLD STUFF
	for (i=1;i<=ss_total_pics;i++){
	/**
		var ssimg = new Image();
		$(ssimg).load(function(){slideshow_init()});		
		$('.slideshow').append(ssimg);
		$(ssimg).attr({src: 'images/slideshow/'+i+'.jpg'});
	
		ssimg = new Image();
		ssimg.src="images/slideshow/"+i+".jpg";
		ssimg.onload = function(){alert('loaded '+i)};		
		$('.slideshow').append(ssimg);
	}**/
}
function slideshow_init(){
	ss_num_loaded++;
	if(ss_num_loaded==ss_total_pics){
		slideshow_start();
	}
}
function slideshow_start(){
	// load page in the bg
	load_sections(thash);
	
	$('#TB_load_center').remove(); // not using default loader
	$('.slideshow img:first').fadeIn(1000).pause(2500).fadeOut(1000);
	setTimeout('slideshow_middle()',4500);	//sum of above - starts after above logo fades in & out
}
function slideshow_middle(){
	for(i=1;i<=ss_total_pics;i++){
		setTimeout('slideshow_showimg('+i+')',300*i);
	}
}
function slideshow_showimg(ssnum){
	$(".slideshow img:eq("+ssnum+")").fadeIn(300-100);
	if(ssnum==ss_total_pics)slideshow_end();
}
function slideshow_end(){
	showmain();
}
function showmain(mode){
	$('body').animate({ backgroundColor: "#333333" }, 2000,function(){
		if(jQuery.browser.msie)var displaytype = 'block'; else var displaytype = 'table-cell'; // Classic ie crap
		$('#container').css({display: displaytype});
		$('.shim').hide();
		$('.main').fadeIn(1000);
		showLoader(false);
	});
}

function getPieceName(imgsrc){
	var fullsrc = imgsrc.split('/');
	var imgsrc = fullsrc[fullsrc.length-1];
	var pathlesssrc = imgsrc.split('.')[0];
	var piece = pathlesssrc.split('-')[2];
	return piece;
}
function gethash(){	
	// Getting the hash (which is our page)
	if(window.location.hash == '')window.location.hash = '#home'; 									// setting no hash to home
	else var shash = window.location.hash.substring(1,window.location.hash.length); 	// stripping out the #
	if(!arraycontains(tpages,shash))shash='home';	
	return shash;								// if page doesnt exist, set to home
}
function getHashFromHref(fullhref){
	var fullhref = fullhref.split('/');
	var hrefshort = fullhref[fullhref.length-1];
	var hash = hrefshort.substring(1,hrefshort.length);
	return hash;
}

function arraycontains(arr,val){
	var ret = false;
	$.each(arr,function(theindex,thevalue){
		if(thevalue == val)ret=true;
	});
	return ret;
}

function loadScript(url){
	var e = document.createElement("script");
	e.src = url;
	e.type="text/javascript";
	document.getElementsByTagName("head")[0].appendChild(e); 
}

function loadTwitter(){
	loadScript('http://twitter.com/javascripts/blogger.js');
	loadScript('http://twitter.com/statuses/user_timeline/jahuffman.json?callback=twitterCallback2&amp;count=5');
}
