// when html loaded
var stateEnable = false;
var stateObj = { why: "me" };
$(document).ready(function() {
	window.onpopstate = function(event) {
		//console.log(event.target.location.hash);
		if(!stateEnable){
			stateEnable = true;
		}else if(event.target.location.hash == ''){
			changePage(document.location);
		}
	};
	
	$('a').live('click',function(e){
		if($(this).hasClass('external')){
			return true;
		}else if($(this).hasClass('colorbox')){
			e.preventDefault();
			showBox($(this));
			return false;
		}else if($(this).hasClass('closevideo')){
			e.preventDefault();
			$('#thevideo').fadeOut('fast',function(){$('#thevideo').remove();});
			return false;
		}else if( window.history && window.history.pushState ){
			e.preventDefault();
			history.pushState(stateObj, document.title, document.location);
			history.replaceState(stateObj, $(this).attr('title'),  $(this).attr('href'));
			changePage($(this).attr('href'));
			return false;
		}
	});
	
	$('#loading').fadeOut('slow');
	$('#single').fadeIn('slow');
	$('#contentslide').fadeIn('fast');
	checkHash();
});

function showBox($obj){
	var rel = $obj.attr('rel');
	var iniframe = false, boxwidth = false, boxheight = false, boxinline = true;
	if($obj.hasClass('viewmap')){
		iniframe = true;
		boxwidth = '90%';
		boxheight = '70%';
	}else if($obj.hasClass('viewclient')){
		iniframe = true;
		boxinline = false;
		boxwidth = '630px';
		boxheight = '500px';
		if ( $.browser.msie && $.browser.version < 8.0) {
			boxheight = '540px';
  		}
	}
		
	$.colorbox({ opacity: 1, inline: boxinline, href: rel, initialWidth:200,initialHeight:200, iframe: iniframe, width: boxwidth, height: boxheight, close: '<strong>CLOSE</strong> <span>X</span>', fastIframe: false,scrolling: false,onOpen: function(){
		if ( $.browser.msie ) {
			
			$("html, body").animate({ scrollTop: 0 }, "fast");
		    $("html").css( "overflow","hidden" );
		 }
	},
	onLoad: function(){
	},
	onClosed: function(){
		if ( $.browser.msie ) {
			$("html").css( "overflow","auto" );
		}
	} });
}


function changePage(url){
	$('#cboxOverlay, #colorbox').remove(); 
	$.colorbox.remove();
	if (location.href.indexOf("#") != -1) {
		var hashurl = location.href.substr(location.href.indexOf("#"));
		url = location.href.substr(0,location.href.indexOf("#"));
	}
	$.ajax({
		url: url+"?ajax=1"+hashurl,
		success: function(data){
			$.colorbox.init();
			$('body').attr('class','');
			$('.nav a').unbind('.lrcontentslide');
			$(document).unbind('.lrcontentslide');
			$('.tabnav a').unbind('.lrcontentslide');
			$('#container').attr('id','oldcon');
			$('#wrapper').prepend(data);
			$('#oldcon').fadeOut('fast',function(){
				$('#oldcon').remove();
				$('#contentslide').show();
				$('#single').fadeIn('slow');
				checkHash();
			});
		}
	});
}

// when all page elements loaded
$(window).load(function() {
	
});

function checkHash(){
	if (location.href.indexOf("#") != -1) {
		if($('a'+location.href.substr(location.href.indexOf("#"))).length == 1){
			showBox($('a'+location.href.substr(location.href.indexOf("#"))));
		}
    }
}
