var this_hash = '';
var use_splash = false;
var use_anim = true;
var use_cufon = true;
var force_js = true;
var isService = false;
var t1;
var t2;
var t3;

//if (BrowserDetect.browser=='Explorer')use_cufon=false;

/*
$(window).bind("load", function() {//The load event will only fire if the entire page or document is fully loaded
	
});
*/


$(document).ready(function() {

	$('#nav').hide();

	if (use_anim) $('.animcol').hide();
	if (!use_splash) {
		$('#overlaysplash').hide(); 
	}
	if (use_cufon) Cufon.replace('.trender', {hover: true});
	$('.pics img').hide();
	
	// handle non hashed URL immediately.
	if (location.href.indexOf('#')<0 && force_js) { 
		//location.href = '/#' + location.href.substr(host.length);
	} 
	
	
	
	$('h2.title').each(function(i) {
		text = $(this).text();				
		if (text) {
			im_str = text.toLowerCase().replace(/ /g, '-');
			$(this).html('<img src="/img/titles/' + im_str + '.png" alt="'+ $(this).text() +'" />');
		}
	});
	
	
	
	$('a[rel!="external"]').bind('click',handle_internal_clicks);
	
	updateState(location.hash);
	
	
	function swfcallback(e) {
		if (e.success == false) {
			// didn't load swf for some reason. show image background.
			//$('#flashbg').html('<img src="/img/bgs/1.png" id="stretchy"/>');
			showCols();
			$('#nav').fadeIn();
			if($('.pics').length) {
				$('.pics img').fadeIn(500);
				setTimeout('go_slideshow()',1000);
			}
		}
	}
	
		swfobject.embedSWF("cwm.swf", "wrapper", "100%", "100%", "9.0.0","", {}, {bgcolor:"#000000", wmode:"transparent", scale:"noscale", salign:"lt"}, false, swfcallback);	
		
/*
		$('#splash_cwmsub').fadeIn(500);
		
		$('#splash_cwm1').fadeIn(2000, function (){ 
			$('#n').fadeIn(2000,function() {
				$('#overlaysplash').fadeOut(1500, function () {	
					if (use_flash) {
		
	} 
	*/
				
		
	

	

	
});
//$(window).hashchange( handle_hashchange );

function showCols()
{
	if(use_anim) {
		/*
		t1 = setTimeout(function() { $('#intro .animcol').show('slide',{direction:'up',easing:'easeOutCirc'},500); }, 500);
		t2 = setTimeout(function() { $('#sub .animcol').show('slide',{direction:'up',easing:'easeOutCirc'},500); }, 1000);
		t3 = setTimeout(function() { $('#body .animcol').show('slide',{direction:'up',easing:'easeOutCirc'},500); }, 1500);
		*/	
		
		//t1 = setTimeout(function() { $('#intro .animcol').slideDown({duration:700}); }, 500);
		//t2 = setTimeout(function() { $('#sub .animcol').slideDown({duration:700}); }, 1000);
		//t3 = setTimeout(function() { $('#body .animcol').slideDown({duration:700}); }, 1500);
		
		
		t1 = setTimeout(function() { $('#intro .animcol').fadeIn(700); }, 500);
		if($('#sub .animcol').length) t2 = setTimeout(function() { $('#sub .animcol').fadeIn(700); }, 1000);
		if($('#body .animcol').length) t3 = setTimeout(function() { $('#body .animcol').fadeIn(700); }, 1500);
	}
}

function handle_clients()
{
	if($('.clients-page').length)
	{
		$('.clients-page').last().hide();
		$('#more-clients').show();
	}
}

function handle_hashchange() {
	clearTimeout(t1);
	clearTimeout(t2);
	clearTimeout(t3);
	updateState(location.hash)
}


function handle_internal_clicks(e) {
	
	
	if ($(this).attr('rel')=='external') {
		 this.target = "_blank";
		
	} else if($(this).parent().attr('id') == 'more-clients') {
	
		e.preventDefault();
		$('#more-clients').fadeOut();
		$('.clients-page').first().fadeOut('slow',function()
		{
			
			$('.clients-page').last().fadeIn();
			$('#less-clients').fadeIn();
		});
	
	} else if($(this).parent().attr('id') == 'less-clients') {
	
		e.preventDefault();
		$('#less-clients').fadeOut();
		$('.clients-page').last().fadeOut('slow',function()
		{
			
			$('.clients-page').first().fadeIn();
			$('#more-clients').fadeIn();
		});
	
	} else {
		e.preventDefault();
		// seperate host from path.
		href = $(this).attr('href');
		if (href.substr(0,host.length)==host) {
			page_path = href.substr(host.length-1);
		} else {
			page_path = href;
		}
		var h = '#' + page_path.substr(1);
		//top.location.href = '/#' + page_path.substr(1);
		isService = $(this).hasClass('service') ? true : false;
		updateState(h);
	}
}

function updateState(hash) {
	if (hash != this_hash) {
		url = '/' + hash.substr(1);
		if(!isService) $('#main').fadeOut();
		else {
			$('.servicecol').fadeOut();
		}
		$("#main").load(url + " #main", function(response, status, xhr) {
			//alert($('#main').html());
			handle_clients();
			$(this).fadeIn();
			$('h2.title').each(function(i) {
				text = $(this).text();				
				if (text) {
					im_str = text.toLowerCase().replace(/ /g, '-');
					$(this).html('<img src="img/titles/' + im_str + '.png" alt="'+ $(this).text() +'" />');
				}
			});
			if (use_anim) $('.animcol').hide();
			$('.pics img').hide();
			if (use_cufon) 	Cufon.replace('.trender', {hover: true});
			$('a').bind('click',handle_internal_clicks);
			
			/*
			$('#more-clients a').unbind('click');
			$('#more-clients a').click(function(e)
			{
				e.preventDefault();
				$('.clients-page').first().fadeOut();	
				//$('.clients-page').last().fadeIn();
			});
			*/
			if (use_anim) {
				showCols();
				
			
			}
			if($('.pics img').length) setTimeout($('.pics img').fadeIn(500),3000);			
			setTimeout('go_slideshow()',4000);
			
			
		});
		modifynav(hash);
		this_hash = hash;
		
		
		
	} else {
	}
}

function modifynav(hash) {
	section = hash.substr(1).split('/');
	$('#links li a').removeClass('selected');
	$('#nav_' + section[0]).addClass('selected');
}
function go_slideshow() {
	if($('.pics').length) {
	
	$('.pics').cycle({
		fx: 'scrollHorz',
		speed:    600, 
		timeout:  3000,
		easing: 'easeOutCirc'
	});
	
	}
}



