/*M.A.D site 2009*/

/*back to top for case studies & services*/
$("#shell").append("<div id='navAid'><a href='#shell' title='Return To Top'>Top</a></div>");		
function setMenuOffset(){
	var twifty = 500;
	$(window).scroll(function () { 
		var offset = document.documentElement.scrollTop || document.body.scrollTop;
		if(offset > twifty){$("#navAid").animate({left:"0px"}, twifty);}
		if(offset ==0){$("#navAid").animate({left:"-50px"}, 700);}
	});
}

//global uri for data container
var showcaseFile = "/showcase.htm";
//global array for showcase - 1 for each id
var showcase = [];
showcase[0] = "#sc_cf";
showcase[1] = "#sc_og";
showcase[2] = "#sc_zee";
showcase[3] = "#sc_nm";
showcase[4] = "#sc_je";
showcase[5] = "#sc_sp";

/*showcase loader*/
function sc_loader(){
	// if specific showcase block is needed for a case study, exit
	if (sc_predefined == true){return};
	var source = Math.floor(Math.random() * showcase.length);
	if(source==0){source=5};//avoid repeated showcase
	var showcaseEntry = showcaseFile + ' ' + showcase[source];
	$("#showcase").load(showcaseEntry);
	//sc_highLight(source);
	/*$("#showcase").ajaxStart(function(){ $("#loader").show(); });*/
}

/*footer brands loader
function bl_loader(){
	var brandLists = [];
	brandLists[0] = "1";
	brandLists[1] = "2";
	brandLists[2] = "3";
	var source = Math.floor(Math.random() * brandLists.length);
	$("#brandList_"+brandLists[source]).css("display","none");
}*/

var init = 0;
function sc_carousel(which){
	var maxlength = showcase.length;
	if(which == maxlength){
		which=0;init=0;init++;
	}else{which = init++};
	//sc_highLight(which);
	$("#showcase").load(showcaseFile + ' ' +showcase[which]);
}

/*function sc_highLight(which){
	$("#sc_navr li a").removeClass("on");
	if(which==0){$("#sc_navr li:first-child a").addClass("on");}
	if(which==1){$("#sc_navr li:nth-child(2) a").addClass("on");}
	if(which==2){$("#sc_navr li:nth-child(3) a").addClass("on");}
	if(which==3){$("#sc_navr li:nth-child(4) a").addClass("on");}
	if(which==4){$("#sc_navr li:nth-child(5) a").addClass("on");}
	if(which==5){$("#sc_navr li:nth-child(6) a").addClass("on");}
	if(which==6){$("#sc_navr li:nth-child(7) a").addClass("on");}
	//could do with getting this looped...
}*/

function nav_on(){
	var pageName = [];
	pageName[0] = document.getElementById('aboutPage');
	pageName[1] = document.getElementById('workPage');
	pageName[2] = document.getElementById('servicesPage');
	pageName[3] = document.getElementById('contactPage');
	pageName[4] = document.getElementById('careersPage');
	if(pageName[0]){$("#nav li:nth-child(1) a").addClass("on");}
	if(pageName[1]){$("#nav li:nth-child(2) a").addClass("on");}
	if(pageName[2]){$("#nav li:nth-child(3) a").addClass("on");}
	if(pageName[3]){$("#nav li:nth-child(4) a").addClass("on");}
	if(pageName[4]){$("#header #careersLink a").addClass("on");}	
}

$(function(){
	
	$.fn.wait = function(time, type) {
        time = time || 4000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };
	
	sc_loader();//showcase - no need for random now - cycles through
	//bl_loader();//hide a random brand list
	nav_on();//nav highlight
	setMenuOffset()//content top anchor
	if (sc_predefined == false){
		var startCarousel = setInterval( "sc_carousel(init)", 6000 );//tenuous homage to Groop Dogdrill
		$("#showcase").ajaxComplete(function(){
			$(".show_block").css("display","none").fadeIn(800).wait().fadeOut(800);
		});
	};
	//news/blog switch
	var nbFade = 400;
	$("#switchNews").click(function(e){$("#blogPanel").fadeOut(nbFade);$("#newsPanel").wait(nbFade).fadeIn(nbFade);$("#switchNews").removeClass("off");$("#switchBlog").removeClass("on");e.preventDefault();});
	$("#switchBlog").click(function(e){$("#newsPanel").fadeOut(nbFade);$("#blogPanel").wait(nbFade).fadeIn(nbFade);$("#switchNews").addClass("off");$("#switchBlog").addClass("on");e.preventDefault();});																					   
});

/*text configuration:*/
Cufon.replace('h1');Cufon.replace('#content h2');Cufon.replace('blockquote.large p');Cufon.replace('.next_step h4');Cufon.replace('#leftCol .banner h3');Cufon.replace('.comment_title .dec');Cufon.replace('#postComment') ;
Cufon.replace('#nav a', { hover: true });Cufon.replace('.ft_block h2 a', { hover: true });Cufon.replace('#madNews .tabs li a', { hover: true });