// developed by Rodrigo Fante - http://rodrigofante.com
var loadAllPages=true;
setTimeout(startSafety,30000);

//setTimeout(mLoader,1500,1);
allLoaded=0;
$(document).ready(function () {
    
    $body = $('html,body').attr('scrollTop',0);
    if ($("#home").length > 0) {
	
        if($("#hiddenCF").length<=0){
	    $("<div  />")
		.css({display:"none"})
		.attr("id","hiddenCF")
		.appendTo("body");
	    // adding classes when JS is enabled, so carousel can start properly
	    $(".homeCarousel UL.content").addClass("contentJS").removeClass("content");
	    $(".homeCarousel UL.logos").addClass("logosJS").removeClass("logos");
	    $(".homeCarousel .wrapperLogos").addClass("wrapperLogosJS").removeClass("wrapperLogos");
	    $(".homeCarousel .wrapperContent").addClass("wrapperContentJS").removeClass("wrapperContent");
	    
	    // Juitter - jQuery plugin by Rodrigo Fante - http://juitter.com
	    $.Juitter.start({
		searchType: "fromUser",
		searchObject: "the7stars",
		live: "live-3600", // the number after "live-" indicates the time in seconds to wait before request the Twitter API for updates.
		placeHolder: "juitterContainer", // Set a place holder DIV which will receive the list of tweets example <div id="juitterContainer"></div>
		loadMSG: "Loading...", // Loading message, if you want to show an image, fill it with "image/gif" and go to the next variable to set which image you want to use on 
		imgName: "", // Loading image, to enable it, go to the loadMSG var above and change it to "image/gif"
		total: 20, // number of tweets to be show - max 100
		readMore: "Read it on Twitter", // read more message to be show after the tweet content
		nameUser: "text", // insert "image" to show avatar of "text" to show the name of the user that sent the tweet 
		openExternalLinks: "newWindow", // here you can choose how to open link to external websites, "newWindow" or "sameWindow"
		filter: "sex->*BAD word*,porn->*BAD word*,fuck->*BAD word*,shit->*BAD word*"  // insert the words you want to hide from the tweets followed by what you want to show instead example: "sex->censured" or "porn->BLOCKED WORD" you can define as many as you want, if you don't want to replace the word, simply remove it, just add the words you want separated like this "porn,sex,fuck"... Be aware that the tweets will still be showed, only the bad words will be removed
	    });
	
        
	    blogPagination();
	    pressPagination();
	
	    Cufon.replace('SPAN.color1');
	    Cufon.replace('SPAN.color2');
	    if ($.browser.msie) $.Carousel.start();
	    if(loadAllPages==true){
		$.preLoadFiles.start();
		testLoad();
	    }
	}
    }
    if ($("#home").length <= 0) {
        $("BODY").css({"background":"none","padding-top":"100px"});
        $("#menu").hide();
	$(".pageBGLeft").hide();
	$(".pageBGRight").hide();
    }
   
    if ($("#people").length > 0) {
        var pTotPag = $("#pages > LI").length;
	 $("<ul />")
		.attr("id","peoplePagination")
		.prependTo("#people");
	for(i=1;i<=pTotPag;i++){
	    if(i==1){
		 $("<li />")
		    .attr("id","people_page_"+i)
		    .attr("class","on")
		    .html("<a href='#people_page_"+i+"'>Page "+i+"</a>")
		    .appendTo("#peoplePagination");
	    } else {
		 $("<li />")
		    .attr("id","people_page_"+i)
		    .html("<a href='#people_page_"+i+"'>Page "+i+"</a>")
		    .appendTo("#peoplePagination");
	    }
	}
	$("#pages").jcarousel({
	    scroll: 1,
	    wrap: 'circular',
	    initCallback: mycarousel_initCallback

	});
        $.twitter.start();
    }
    if ($("#jobs").length > 0) {
        $(".see A").click(function () {
            $("#jobsOverlay").fadeIn();
            $("#contact").css("overflow", "visible");
            return false;
        });
        $(".close A").click(function () {
            $("#jobsOverlay").fadeOut();
            $("#contact").css("overflow", "hidden");
            return false;
        });
	$(".goToPrint A").click(function(){
	    window.print();
	    return false;
	});
    }
    
});

function mycarousel_initCallback(carousel) {
    jQuery('#peoplePagination LI A').bind('click', function() {
	jQuery('#peoplePagination LI').removeClass("on");
	jQuery(this).parent().addClass("on");
	var cp = jQuery(this).attr("href").split("_");
	cp  = cp[cp.length-1]
	
	carousel.scroll(jQuery.jcarousel.intval(cp));
	return false;
    });
    jQuery('.jcarousel-scroll select').bind('change', function() {	
	carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
	return false;
    });
    jQuery('.jcarousel-next').bind('click', function() {
	var pTotPagCar = $("#peoplePagination > LI").length;
	var cur = jQuery('#peoplePagination LI.on').attr("id").replace(/people_page_/,"");
	
	if(cur>=pTotPagCar) cur=1;
	else cur = parseInt(cur)+1;
	jQuery('#peoplePagination LI').removeClass("on");
	jQuery('#peoplePagination LI#people_page_'+cur).addClass("on");
	carousel.next();
	return false;
    });
    jQuery('.jcarousel-prev').bind('click', function() {
	var pTotPagCar = $("#peoplePagination > LI").length;
	var cur = jQuery('#peoplePagination LI.on').attr("id").replace(/people_page_/,"");
	
	if(cur>1) cur = parseInt(cur)-1;
	else cur = pTotPagCar;
	jQuery('#peoplePagination LI').removeClass("on");
	jQuery('#peoplePagination LI#people_page_'+cur).addClass("on");
	carousel.prev();
	return false;
    });
};

var arrayMenu = new Array("home","what-we-do","our-work","people","blog","contact");
var pages2LoadName = new Array("whatwedo","ourwork","people","blog","contact");
var pages2Load = new Array("/whatwedo","/ourwork","/people","/posts","/contact");
var loadedStuff=0;
var total2Load=0;
// ===============
// functions here
function sumFrame() {
    loadedStuff++;  
}
function manageScroller(){
    $(window).bind('scroll',on_scroll);
}
function on_scroll(){
    var pos = ($.browser.safari) ? $('body').attr('scrollTop') : $body.attr('scrollTop');
    /*$("<div />")
	.attr("id","testDiv")
	.css({position:"fixed","border":"solid 1px red","height":"20px",right:"0px",bottom:"0px"})
	.appendTo("body");*/
    for (var i=0;i<arrayMenu.length;i++) {
	    var $prevtop 	= (i==0) ? 0 : $('div#iframeWrapper_'+pages2LoadName[i-1]).offset().top;
	    var $curtop 	= $('div#iframeWrapper_-'+pages2LoadName[i]).offset().top;
	   
	    if ($curtop < pos+100 && pos+100>$prevtop){
		// $("#testDiv").html("Prev: "+$prevtop+" - cur "+i+": "+$curtop+" - pos: "+pos);
		 $("#menu LI A").removeClass("on");
		 $("#menu LI."+arrayMenu[i]+" A").addClass("on");
	    }
	    
	    
    }
}
function moveToPage(){
    var url = document.location.href;
    var findGT = url.search("/#gt/");
    if(findGT>=0){
	var gt = url.split("/#gt/")[1];
	var gt = gt.split("/");
	if(gt[0]=="blog" || gt[0]=="whatwedo" || gt[0]=="ourwork" || gt[0]=="blog" || gt[0]=="people" || gt[0]=="contact"){	    
	    goToByScroll("iframeWrapper_"+gt);
	}
    }	
}
function findBlogURL(){    
    var url = document.location.href;
    var findGT = url.search("/#gt/");
    
    if(findGT>=0){
	var agt = url.split("/#gt/")[1];
	var gt = agt.split("/");
	var urlSize = gt.length;
	
	if(gt[0]=="blog" && urlSize>=1 && gt[1]!="" && gt[1]!=undefined){
	   
	   return "http://www.the7stars.co.uk/blog"+(agt.replace(/blog/,"")); 
	}else return "http://www.the7stars.co.uk/blog";
    } else return "http://www.the7stars.co.uk/blog";
}   
function mLoader(c){
    if(allLoaded==0 && $(".preloaderBW").width()<170){
	if($("#loaderMSG").html()=="Oh... Hi there") msg = "Welcome to the7stars";
	else if($("#loaderMSG").html()=="Welcome to the7stars") msg = "Bear with me a second...";
	else if($("#loaderMSG").html()=="Bear with me a second...") msg = "...I'm just updating the website with the latest content";
	else if($("#loaderMSG").html()=="...I'm just updating the website with the latest content") msg = "Jenny's twitter feed";
	else if($("#loaderMSG").html()=="Jenny's twitter feed") msg = "Where we've been in the press this month";
	else if($("#loaderMSG").html()=="Where we've been in the press this month") msg = "The latest blog headlines";
	else if($("#loaderMSG").html()=="The latest blog headlines") msg = "A bit about what we do";
	else if($("#loaderMSG").html()=="A bit about what we do") msg = "The City Jet case study";
	else if($("#loaderMSG").html()=="The City Jet case study") msg = "And finally... the team photos";
	else msg = $("#loaderMSG").html();
	
	$("#loaderMSG").html(msg);
	
	c++;
	var l=Math.floor(Math.random()*41);
	$(".preloaderBW").stop(true, true).animate({width:($(".preloaderBW").width()+l)+"px"});
	//setTimeout(mLoader,4000,c);
	setTimeout(function() {mLoader(c)},1500);
    }
}
function testLoad(){
    if(loadedStuff > 0 && loadedStuff >= total2Load) {      
        
	if($("#home").length>0){
	    $("#home").css({"padding-top":"100px"});
	    if($("#nextCarouselItem").length<=0 && !$.browser.msie) $.Carousel.start();
	}
	
	$("body,html").css("overflow", "visible");
        $("#menu LI A, .dMenu").click(function (i,item) {
            if($("#workOverlay").length>0){
		if($("#workOverlay").css("display")!="none") $("#workOverlay").fadeOut();
            }
	    var p2go = "iframeWrapper_" + $(this).attr("href").replace(/\//,"").replace(/#/,"");
	    goToByScroll(p2go);
	    document.location.href = "/#gt/"+arrayMenu[i];
            return false;
        });
	/*if($("#iframeWrapper_ourwork #pages").length > 0){
	    $("#iframeWrapper_ourwork #pages").jcarousel({
		scroll: 1,
		wrap: 'circular'
	    });
	}*/
	manageScroller();
	//setTimeout(addCarousel2Work,15000);
	setTimeout(removeLoader,500);
	setTimeout(moveToPage,1600);
	$(".pageBGLeft").height($(document).height());
	$(".pageBGRight").height($(document).height());
    } else {
        setTimeout(testLoad, 100);
    }
}
function addCarousel2Work(){
    
    $("#iframeWrapper_ourwork #pages").jcarousel({
	scroll: 1,
	wrap: 'circular'
    });
}
function goToByScroll(id){    
    $('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}
(function($) {
	var conf = {},
	test=1;
	$.preLoadFiles = {
        start: function(){
	    
		total2Load=pages2Load.length-1;
		$("#menu").css("position","fixed");         
		$.each(pages2Load,function(i,item){
		    
		    if($("#iframeWrapper_"+pages2LoadName[i]).length<=0){
			
			if(pages2LoadName[i]!="ourwork"){
			    
			    $('.canvas_home').append('<div id="iframeWrapper_'+pages2LoadName[i]+'" class="iframeWrapper_'+pages2LoadName[i]+' iframesWrapper" />');
			    iUrl = item;
			  
			    $(".iframeWrapper_"+pages2LoadName[i]).append('<iframe id="iframe_'+pages2LoadName[i]+'" frameBorder="0" scrolling="no" />');
			    $('#iframe_'+pages2LoadName[i]).attr('src', iUrl); 
			    $('#iframe_'+pages2LoadName[i]).attr('framespacing',"0");			    
			    $('#iframe_'+pages2LoadName[i]).attr('border',"0");
			    $('#iframe_'+pages2LoadName[i]).css("border","none");
			    
				$('#iframe_'+pages2LoadName[i]).load(function(){
				    sumFrame();
				    
				    $(".preloaderBW").animate({
					width:($(".preloaderBW").width()+30)+"px"
				    });
				    var helpFrame = jQuery('#iframe_'+pages2LoadName[i]);
				    try{
					var innerDoc = (helpFrame.get(0).contentDocument) ? helpFrame.get(0).contentDocument : helpFrame.get(0).contentWindow.document;
				    } catch(e){
					return false;
				    }
				    if(navigator.userAgent.search("Safari")>=0) helpFrame.height(innerDoc.body.scrollHeight-30);
				    else helpFrame.height(innerDoc.body.scrollHeight + 35);
				    
				    
				});
			    
			} else {
			     
				$('.canvas_home').append('<div id="iframeWrapper_'+pages2LoadName[i]+'" class="iframeWrapper_'+pages2LoadName[i]+' iframesWrapper" />');   
			     
				$.ajax({
				    url: item,
				    type: 'GET',
				    dataType: 'html',
				    timeout: 30000,
				    error: function(){  },
				    success: function(data){
					sumFrame();
					
					$("#iframeWrapper_"+pages2LoadName[i]).html(data);
					
					$("#iframeWrapper_"+pages2LoadName[i]+" .pageBGLeft").hide();
					$("#iframeWrapper_"+pages2LoadName[i]+" .pageBGRight").hide();
					
					$("#ourwork P.image SPAN").css({ opacity: "0.9" });
					$("#ourwork DIV.copy").css({ opacity: "0.9" });
				
					$("#ourwork UL#portfolio LI").hover(function () {
					    $(this).find("P.image SPAN").hide();
					    $(this).find("DIV.copy").animate({
						bottom: 0
					    }, 200);
					}, function () {
					    $(this).find("DIV.copy").animate({
						bottom: "-135px"
					    }, 200);
					    $(this).find("P.image SPAN").show();
					});
					$("#ourwork .clientList A").click(function () {
					    $("#ourWorkContent").animate({
						top: "825px"
					    }, 300, function () {
						$("#clientListContent").animate({
						    top: "100px"
						});
					    });
					    return false;
					});
					$("#ourwork .hideclientList A").click(function () {
					    $("#clientListContent").animate({
						top: "825px"
					    }, 300, function () {
						$("#ourWorkContent").animate({
						    top: "100px"
						});
					    });
					    return false;
					});
					$.ourWork.start();
					$(".canvas_ourwork #menu").hide();
					$("#workOverlay .close A").click(function () {
					    $("#workOverlay").fadeOut();
					    return false;
					});
					$("#ourwork #pages").jcarousel({
					    scroll: 1,
					    wrap: 'circular'
					});
					$("#clientListContent").css("top","820px");
					
				    }
				});
			    
			}
		    }
		   
		});
	    
       }
    };	
})(jQuery);

(function($) {
	var conf = {},
	maxElem=9;
        bigCarousel=".contentJS";
        smallCarousel=".logosJS";
        startElement="campaign7";
        startPos=0;
        leftPos=18;
        elemWidth=98; // including margin
        totalElements=0;
        interval=8000;  // 1000 = 1 second
	curItem=0;
	move=0;
	animation=0;
        $.Carousel = {
            start: function(){
                total=this.countElements();
		
                if(this.checkIntegrity(total)){
		    animation=1;
                    totalElements=parseInt(total.split(",")[0]);
		    
		    this.addClasses();
                    this.findStartElement();
                    this.positionSmall();
		    this.positionBig();
		    this.addLinks();
		    $(smallCarousel+" > LI A").click(function(){
			$.Carousel.gotoItem($(this));
			return false;
		    });
                } else displayError("Number of items in the Carousel element does not match");
            },
            countElements: function(){
                return $(bigCarousel+" > LI").length+","+$(smallCarousel+" > LI A").length;
            },
	    addLinks: function(){
		if($("#nextCarouselItem").length<=0){
		    $("<a />")
			.attr("id","nextCarouselItem")
			.appendTo(".homeCarousel");
		    $("<a />")
			.attr("id","prevCarouselItem")
			.appendTo(".homeCarousel");
		    $("#prevCarouselItem").click(function(){if(animation==0) $.Carousel.clickedNextItem()});
		    $("#nextCarouselItem").click(function(){if(animation==0) $.Carousel.clickedPrevItem()});
		}
	    },
	    addClasses: function(){
		$(bigCarousel+" > LI").each(function(i,item){
		    $(this).addClass("bigItem_"+i);
		});
		$(smallCarousel+" > LI > A").each(function(i,item){
		    $(this).parent().addClass("smallItem_"+i);
		});
		$(smallCarousel).width((totalElements*100)+710);
	    },
            checkIntegrity: function(){
                totalBig=parseInt(total.split(",")[0]);
                totalSmall=parseInt(total.split(",")[1]);
                if(totalBig==totalSmall) return 1;
                else return 0;
            },
            findStartElement: function(){
                $(bigCarousel+" > LI").each(function(i,item){
                    if($(this).attr("id")==startElement){
			startPos=i;
			curItem=i;			
		    }
                });
            },
            positionSmall: function(){
		animation=1;
		$(smallCarousel+" > LI IMG.on").hide();
		$(smallCarousel+" > LI.smallItem_"+curItem+" IMG.on").show();
                $(smallCarousel).css({left:"-"+(($(smallCarousel).width()+elemWidth)-368)+"px"});
                newleftPos=leftPos;
                $(smallCarousel).animate({left:newleftPos+"px"},2000,function(){
		    animation=0;
                    setTimeout($.Carousel.nextItem,interval);
                });
		
            },
	    positionBig: function(){
                $(bigCarousel+" > LI").each(function(i,item){
		    if(i!=curItem) $(this).hide();
		})
            },
	    gotoItem: function(item){
		animation=1;
		clickedItem = parseInt(item.parent().attr("class").replace(/smallItem_/,""));
                newleftPos=leftPos-(elemWidth*clickedItem);
                $(bigCarousel+" > LI.bigItem_"+curItem).fadeOut("slow");
		$(smallCarousel+" > LI.smallItem_"+curItem+" IMG.on").hide();
		curItem=clickedItem;
		$(smallCarousel).animate({left:newleftPos+"px"},1000,function(){animation=0;$(smallCarousel+" > LI.smallItem_"+curItem+" IMG.on").show();});
		$(bigCarousel+" > LI.bigItem_"+curItem).fadeIn("slow");
	    },
	    
            clickedNextItem: function(){
		if(animation==0){
		    animation=1;
		    newPos = parseInt($(smallCarousel).css("left").replace(/px/,""))+elemWidth;
		    $(bigCarousel+" > LI.bigItem_"+curItem).fadeOut("slow");
		    $(smallCarousel+" > LI.smallItem_"+curItem+" IMG.on").hide();
		   
		    if(curItem>0) curItem--;
		    else curItem=totalElements-1;
		   
		    if(leftPos>=newPos){
			$(smallCarousel).animate({left:newPos+"px"},1000,function(){animation=0;$(smallCarousel+" > LI.smallItem_"+curItem+" IMG.on").show();});
		    } else {
			$(smallCarousel).animate({left:((elemWidth*(totalElements-1))-leftPos)*-1+"px"},1000,function(){animation=0;$(smallCarousel+" > LI.smallItem_"+curItem+" IMG.on").show();});
		    }
		    $(bigCarousel+" > LI.bigItem_"+curItem).fadeIn("slow");
		    move++;
		}
            },
	    clickedPrevItem: function(){
		if(animation==0){
		    animation=1;
		    newPos = parseInt($(smallCarousel).css("left").replace(/px/,""))-elemWidth;
		    $(bigCarousel+" > LI.bigItem_"+curItem).fadeOut("slow");
		    $(smallCarousel+" > LI.smallItem_"+curItem+" IMG.on").hide();
		   
		    if(curItem>=(totalElements-1)) curItem = 0;
		    else curItem++;
		   
		    if(newPos>(leftPos-(elemWidth*totalElements))){
			$(smallCarousel).animate({left:newPos+"px"},1000,function(){animation=0;$(smallCarousel+" > LI.smallItem_"+curItem+" IMG.on").show();});
		    } else {
			$(smallCarousel).animate({left:leftPos+"px"},1000,function(){animation=0;$(smallCarousel+" > LI.smallItem_"+curItem+" IMG.on").show();});
			curItem=0;
		    }
		    $(bigCarousel+" > LI.bigItem_"+curItem).fadeIn("slow");
		    move++;
		}
            },
	    nextItem: function(){
		
		if(animation==0){
		    animation=1;
		     totalElements=parseInt(total.split(",")[0]);
		    newPos = parseInt($(smallCarousel).css("left").replace(/px/,""))+elemWidth;
		    $(bigCarousel+" > LI.bigItem_"+curItem).fadeOut("slow");
		    $(smallCarousel+" > LI.smallItem_"+curItem+" IMG.on").hide();
		   
		    if(curItem>0) curItem--;
		    else curItem=totalElements-1;
		   
		     
		    
		    if(leftPos>=newPos){
			$(smallCarousel).animate({left:newPos+"px"},1000,function(){animation=0;$(smallCarousel+" > LI.smallItem_"+curItem+" IMG.on").show();});
			setTimeout($.Carousel.nextItem,interval);
		    } else {
			$(smallCarousel).animate({left:((elemWidth*(totalElements-1))-leftPos)*-1+"px"},1000,function(){animation=0;$(smallCarousel+" > LI.smallItem_"+curItem+" IMG.on").show();});
			setTimeout($.Carousel.nextItem,interval);
		    }
		     $(bigCarousel+" > LI.bigItem_"+curItem).fadeIn("slow");
		    move++;
		} else {
		    setTimeout($.Carousel.nextItem,1);
		}
            }
	};	
})(jQuery);

function hT(){
var t = $(".twitter").length;
    var rItem = Math.floor(Math.random()*t+1);
    $(".twitter").each(function(i,item){
        if(i==rItem){
            $(this).parent().parent().parent().find(".frame").find(".tweet").fadeIn();
        }
    });
   
}

(function($) {
	var conf = {},
	test=1;
	$.twitter = {
            start: function(){

            var t = $(".twitter").length;
           var rItem = Math.floor(Math.random()*t+1);
		$(".twitter").each(function(i,item){
		    var container = $(this).parent().parent().parent().find(".frame");
		    var acc = $(this).find("a").attr("href").split("/");
		    var tot = acc.length;
		    var acc = acc[tot-1];
		    $("<div />")
			.attr("class","tweet")
			.appendTo(container);
            
		    $(this).hover(function(){
                $(".tweet").fadeOut();
			    $(this).parent().parent().parent().find(".frame").find(".tweet").fadeIn();
		    },function(){
			$(this).parent().parent().parent().find(".frame").find(".tweet").fadeOut();
		    });
            
            if(i!=rItem){
                
                $(this).find(".tweet").addClass("hideIt");
            }
            
            if(acc.length>1){
		setTimeout(function() {loadTweets(acc,null,container,t,i)}, (2000*i));
		//setTimeout(loadTweets,(2000*i),acc,"",container,t,i);
            }
		});
        setTimeout(hT,10000);
            }
	};	
})(jQuery);
function loadTweets(acc,contDiv,container,t,counter){
    $.ajax({
	url: "http://twitter.com/statuses/user_timeline/"+acc+".json",
	type: 'GET',
	dataType: 'jsonp',
	timeout: 1000,
	error: function(){ $("#"+contDiv).html("fail#"); },
	success: function(json){
	    openLink="target='_blank'";
	    $.each(json,function(c,item) {
		var t = $.Juitter.textFormat(item.text);
		if(c==0) container.find(".tweet").html("<div class='tCont'>"+t+"</div>");
	
	    });
	}
    });
}
(function($) {
	var conf = {},
	route=0;
	$.ourWork = {
	    start: function(){
		$('.canvas_home').append('<div id="workOverlay" />');   
			$("#workOverlay").html('<div class="workOverlayWrapper"><p class="close"><a href=""><span>Close</span></a></p><div id="overlayContent"></div></div>');
					  
			$("#portfolio .copy A, A.findOutHome").click(function(){
				if($(this).hasClass("externalLink")){
				    window.open($(this).attr("href")); 
				} else{
				    $("P.close A").click(function(){ $("#workOverlay").fadeOut();})
				     $("#workOverlay").fadeIn();
				     $.ourWork.loadIt($(this).attr("href"));
				   
				}
				 return false;
			});
	     },
	     resizeIMG: function(){
		var wDif = 220;
		var wHeight = $(window).height();
		var maxIMGHeight = wHeight-wDif;
		$("DIV.image").width(800);
		$("DIV.image .bgDesc").css("opacity","0.5");
		$("DIV.image IMG").each(function(){
		    var imgH = $(this).height();
		    var eDesc = $(this).parent().find(".desc");
		    var bgEDesc = $(this).parent().find(".bgDesc");
		    if(imgH>maxIMGHeight) $(this).height(maxIMGHeight);
		    var imgW = $(this).width();
		    
		    $(this).parent().width(imgW);
		    var descH = $(this).parent().find(".desc").height();
		    eDesc.width(imgW-20);
		    var h=eDesc.height();
		    eDesc.css({"overflow":"hidden","bottom":"3px"});
		    bgEDesc.css({"overflow":"hidden","bottom":"3px"});
		    bgEDesc.width(imgW).height(descH);
		    eDesc.css({"padding":"0px","height":"0px"});
		    bgEDesc.css({"padding":"0px","height":"0px"});
		    $(this).parent().hover(
			function(){
			    eDesc.css("padding","10px");
			    bgEDesc.css("padding","10px 0");
			    eDesc.animate({
				height:h+"px"
				
			    },300,function(){
				eDesc.css("padding","10px");
			    bgEDesc.css("padding","10px 0");
			    });
			    bgEDesc.animate({
				height:h+"px"
			    },300);
			},
			function(){
			   
			    eDesc.animate({
				height:"0px"
			    },300,function(){
				eDesc.css("padding","0px");
				bgEDesc.css("padding","0");
			    });
			    bgEDesc.animate({
				height:"0px"
			    },300);
			}
		    );
		});
	     },
	     loadIt: function(url2Load){
		$("<img />")
		    .attr("class","loader")
		    .attr("src","/content/img/ajax-loader.gif")
		    .prependTo("#workOverlay");
		$.ajax({
			        url: url2Load,
			        type: 'GET',
			        dataType: 'html',
			        timeout: 30000,
			        error: function(){ $("#workOverlay").html("fail#"); },
			        success: function(data){
				    $("#workOverlay IMG.loader").fadeOut();
				    setTimeout($.ourWork.resizeIMG,2000);
				    if($("#workOverlay #workBgRight").length<=0){
					$("<div />")
					    .attr("id","workBgRight")
					    .prependTo("#workOverlay");
					 $("<div />")
					    .attr("id","workBgBottomRight")
					    .prependTo("#workOverlay");					
					$("<div />")
					    .attr("id","workBgLeft")
					    .prependTo("#workOverlay");
					$("<div />")
					    .attr("id","workBgLeft")
					    .prependTo("#workOverlay");
					    
				    }
				    
				    $("#workOverlay #overlayContent").html($(data).find("#loadedOverlay"));
				    
				    $("#loadedOverlay ").addClass("loadedOverlay");
				    if($("#contentPages").length>0){
					    
					    var totalPages = $("#contentPages > LI").length;
					    $("#contentPages LI").css({"left":"-3000px"});
					    $("#contentPages LI#content_page_1").css({"left":"0px"});
					    var curPage=1;
					   
					    $("#loadedOverlay .pagination LI A").click(function(){
						old=curPage;
						curPage = $(this).attr("href").split("_");
						curPage = curPage[curPage.length-1];
						$("#loadedOverlay .pagination LI A").removeClass("on");
						$(this).addClass("on");
						$("#contentPages LI#content_page_"+old).animate({left: "3000px"},1000,function(){$("#contentPages LI#content_page_"+old).css({"left":"-3000px"})});
						$("#contentPages LI#content_page_"+curPage).animate({left: "0px"},1000,function(){});
						
						if(curPage<totalPages && curPage>1){
						    route=1;
						    
						    $("#content_page_"+totalPages+" .links P.seeWork").addClass("back2Work");
						}
						return false;
					    });
					    $(".links .seeWork A").click(function(){
						route=1;
						$("#content_page_"+totalPages+" .links P.seeWork").addClass("back2Work");
						old=curPage;
						curPage = $(this).attr("href").split("_");
						curPage = curPage[curPage.length-1];
						$("#loadedOverlay .pagination LI A").removeClass("on");
						$("#loadedOverlay .pagination LI.page_2 A").addClass("on");
						$("#contentPages LI#content_page_"+old).animate({left: "3000px"},1000,function(){$("#contentPages LI#content_page_"+old).css({"left":"-3000px"})});
						$("#contentPages LI#content_page_"+curPage).animate({left: "0px"},1000,function(){});
						return false;
					    });
					    $(".links .seeResults A").click(function(){
						old=curPage;
						curPage = $(this).attr("href").split("_");
						curPage = curPage[curPage.length-1];
						$("#loadedOverlay .pagination LI A").removeClass("on");
						$("#loadedOverlay .pagination LI.page_"+totalPages+" A").addClass("on");
						$("#contentPages LI#content_page_"+old).animate({left: "3000px"},1000,function(){$("#contentPages LI#content_page_"+old).css({"left":"-3000px"})});
						$("#contentPages LI#content_page_"+curPage).animate({left: "0px"},1000,function(){});
						return false;
					    });
					    $(".links .backBrief A").click(function(){
						old=curPage;
						curPage=1;
						$("#loadedOverlay .pagination LI A").removeClass("on");
						$("#loadedOverlay .pagination LI.page_1 A").addClass("on");
						$("#contentPages LI#content_page_"+old).animate({left: "3000px"},1000,function(){$("#contentPages LI#content_page_"+old).css({"left":"-3000px"})});
						$("#contentPages LI#content_page_"+curPage).animate({left: "0px"},1000,function(){});
						return false;
					    });
					    $(".links .nextCase A").click(function(){
						route=0;
						$.ourWork.loadIt($(this).attr("href"));
						return false;
					    });
					    $("#contentPages .next A").click(function(){
						route=1;
						$("#content_page_"+totalPages+" .links P.seeWork").addClass("back2Work");
						old=curPage;
						curPage++;
						$("#loadedOverlay .pagination LI A").removeClass("on");
						$("#loadedOverlay .pagination LI.page_"+$(this).attr("href").replace(/#content_page_/,"")+" A").addClass("on");
						$("#contentPages LI#content_page_"+old).animate({left: "3000px"},1000,function(){$("#contentPages LI#content_page_"+old).css({"left":"-3000px"})});
						$("#contentPages LI#content_page_"+curPage).animate({left: "0px"},1000,function(){});
						return false;
					    });
					     $("#contentPages .prev A").click(function(){
						route=1;
						$("#content_page_"+totalPages+" .links P.seeWork").addClass("back2Work");
						old=curPage;
						curPage--;
						$("#loadedOverlay .pagination LI A").removeClass("on");
						$("#loadedOverlay .pagination LI.page_"+$(this).attr("href").replace(/#content_page_/,"")+" A").addClass("on");
						$("#contentPages LI#content_page_"+old).animate({left: "3000px"},1000,function(){$("#contentPages LI#content_page_"+old).css({"left":"-3000px"})});
						$("#contentPages LI#content_page_"+curPage).animate({left: "0px"},1000,function(){});
						return false;
					    });
						
						
					   
					}
			        }
			    });
	     }
	};	
})(jQuery);

function blogPagination(){
    cPage = $(".blogList > LI").length;
    totPagesBlog = cPage>5?5:cPage;
    curEntry=1;
    if(totPagesBlog==1) $("#home .homeCarousel #blogContainer .blogPagination").css({left:"83px"})
    if(totPagesBlog==3) $("#home .homeCarousel #blogContainer .blogPagination").css({left:"61px"})
    $(".blogList > LI.page_"+curEntry).fadeIn();
    $(".blogPagination LI A").click(function(){return false});
    $(".blogPagination LI.prev A").click(function(){
	    if(curEntry>1){
		    $(".blogPagination LI.page_"+curEntry+" A").removeClass("on");
		    $(".blogList > LI.page_"+curEntry).fadeOut();
		    curEntry--;
		    $(".blogPagination LI.page_"+curEntry+" A").addClass("on");
		    $(".blogList > LI.page_"+curEntry).fadeIn();
	    } else{
           $(".blogPagination LI.page_"+curEntry+" A").removeClass("on");
		    $(".blogList > LI.page_"+curEntry).fadeOut();
		    curEntry=totPagesBlog;
		    $(".blogPagination LI.page_"+curEntry+" A").addClass("on");
		    $(".blogList > LI.page_"+curEntry).fadeIn();
        }
	    return false;
    });
    $(".blogPagination LI.next A").click(function(){
	    //alert(curEntry+"/"+totPagesBlog);
	    if(curEntry<totPagesBlog){
		    $(".blogPagination LI.page_"+curEntry+" A").removeClass("on");
		    $(".blogList > LI.page_"+curEntry).fadeOut();
		    curEntry++;
		    $(".blogPagination LI.page_"+curEntry+" A").addClass("on");
		    $(".blogList > LI.page_"+curEntry).fadeIn();
	    } else {
            $(".blogPagination LI.page_"+curEntry+" A").removeClass("on");
		    $(".blogList > LI.page_"+curEntry).fadeOut();
		    curEntry=1;
		    $(".blogPagination LI.page_"+curEntry+" A").addClass("on");
		    $(".blogList > LI.page_"+curEntry).fadeIn();
        }
	    return false;
    });
}
function pressPagination(){
    cpPage = $(".pressList > LI").length;
    totPages = cpPage>5?5:cpPage;
    curEntryPress=1;
    $(".pressList > LI.page_"+curEntryPress).fadeIn();
    $(".pressPagination LI A").click(function(){return false});
    $(".pressPagination LI.prev A").click(function(){
	    if(curEntryPress>1){
		    $(".pressPagination LI.page_"+curEntryPress+" A").removeClass("on");
		    $(".pressList > LI.page_"+curEntryPress).fadeOut();
		    curEntryPress--;
		    $(".pressPagination LI.page_"+curEntryPress+" A").addClass("on");
		    $(".pressList > LI.page_"+curEntryPress).fadeIn();
	    } else {
            $(".pressPagination LI.page_"+curEntryPress+" A").removeClass("on");
		    $(".pressList > LI.page_"+curEntryPress).fadeOut();
		    curEntryPress=totPages;
		    $(".pressPagination LI.page_"+curEntryPress+" A").addClass("on");
		    $(".pressList > LI.page_"+curEntryPress).fadeIn();
        }
	    return false;
    });
    $(".pressPagination LI.next A").click(function(){
	    if(curEntryPress<totPages){		
		    $(".pressPagination LI.page_"+curEntryPress+" A").removeClass("on");
		    $(".pressList > LI.page_"+curEntryPress).fadeOut();
		    curEntryPress++;
		    $(".pressPagination LI.page_"+curEntryPress+" A").addClass("on");
		    $(".pressList > LI.page_"+curEntryPress).fadeIn();
	    } else {
            $(".pressPagination LI.page_"+curEntryPress+" A").removeClass("on");
		    $(".pressList > LI.page_"+curEntryPress).fadeOut();
		    curEntryPress=1;
		    $(".pressPagination LI.page_"+curEntryPress+" A").addClass("on");
		    $(".pressList > LI.page_"+curEntryPress).fadeIn();
        }
	    return false;
    });
}
function startSafety(){
    if($("#preloader").length>0){
	
	if($("#home").length>0){
	    $("#home").css({"padding-top":"100px"});
	    if($("#nextCarouselItem").length<=0 && !$.browser.msie) $.Carousel.start();
	}
	
	$("body,html").css("overflow", "visible");
	$("#menu LI A, .dMenu").click(function () {
	    if($("#workOverlay").length>0){
		if($("#workOverlay").css("display")!="none") $("#workOverlay").fadeOut();
	    }
	    var p2go = "iframeWrapper_" + $(this).attr("href").replace(/\//,"").replace(/#/,"");
	    goToByScroll(p2go);
	    return false;
	});
	manageScroller();
	
	removeLoader();
    }
}
// Display JS errors
function displayError(error){
    if($("#jsErrorMessage").length<=0){
        $("<div />")
            .attr("id","jsErrorMessage")
            .css({"display":"none"})
            .prependTo("body");
    } 
    $("#jsErrorMessage").html("<span>Error report: </span> "+error);
    $("#jsErrorMessage").fadeIn();
    setTimeout(hideErrorMessage,4000);
}
function hideErrorMessage(){
    $("#jsErrorMessage").fadeOut();
}
function removeLoader(){
    allLoaded=1;
    $(".preloaderBW").stop(true, true).animate({
	width:"198px"
    },2000,function(){
	$("html,body").css({overflow:"visible"});
	$("#preloader").fadeOut(400,function(){$(this).remove();})
    });
    
}

