$(function(){

	$("#header h1 span.e").empty().append("&pound;");
	
	$("#header .ttl").hide();
	$("#header .ttl:first").show();
	
	i = 0;
	
	$(".container_12 > .grid_4").each(function(){
		
		if($(this).attr('id') != 'side') {
			i++;

			switch(i) {
				case 1:
					$(this).addClass('left');
					break;
				case 2:
					$(this).addClass('middle');
					break;
				case 3:
					$(this).addClass('right');
					i = 0;
					break;
			}
		}
	
	});
	
	$(".container_12 #side.grid_4 + #content.grid_8").css({
		"width":"610px",
		"margin-left":"20px"
	});
	
	$(".team #content").css({
		"width":"620px",
		"margin-left":"10px"
	});
	
	function changeBG(new_bgp) {
		
		var ua = $.browser;
		
		new_bgp = Math.floor(new_bgp / 960) * 960
		
		if(Math.abs(new_bgp) > Math.abs(new_bgp * 4)) {
			new_bgp = 0;
		}
		
		if($.browser.msie) {
			var v = parseInt($.browser.version);
			
			if(v <= 8) {
				$("#header").animate({"background-position-x":new_bgp+"px"},750);
			} else {
				$("#header").animate({"background-position":new_bgp+"px"},750);
			}
		} else {

			$("#header").animate({"background-position":new_bgp+"px"},750);
		}
	}
	
	function getBackgroundPosition() {

		if($.browser.msie) {
			var bgp = $("#header").css('background-position-x');
		} else {
			var bgp = $("#header").css('background-position');
		}

		bgp = parseInt(bgp);
		
		return bgp;

	}
	
	var hp = $("#header.home1");
	
	if(hp.length > 0) {

		slider = setInterval(function(){

			var bgp = getBackgroundPosition();

			var new_bgp = bgp - 960;

			changeBG(new_bgp);
			
			//reset to start position if looped back to start;
			
			setTimeout(function(){
				bgp = getBackgroundPosition();
				
				if (bgp < -3500) {
				
					new_bgp = 0;
				
					if($.browser.msie) {
						var v = parseInt($.browser.version);

						if(v <= 8) {
							$("#header").css({"background-position-x":new_bgp+"px"});
						} else {
							$("#header").css({"background-position":new_bgp+"px"});
						}
					} else {

						$("#header").css({"background-position":new_bgp+"px"});
					}
				}
				
				
			},1000);
			
			
			var activeTitle = $(".ttl:visible").attr('id');
			
			j = activeTitle.replace("f","");
			
			j = parseInt(j);
			
			if(j == 4) {
				j = 1;
			} else {
				j++;
			}
			
			$("#bannerNav li, #bannerNav li a").removeClass('active');
			$("#bannerNav li a[href='#h"+j+"']").parent().addClass('active');
			
			
			$("#"+activeTitle).fadeOut(375);
			
			setTimeout(function(){
				$("#f"+j).fadeIn(375);
			},375);
			
			

		},5000);
	}

	$("#quickNav a span").hide();
	
	$("#quickNav a").hover(function(){
		$("span",$(this)).fadeIn(250);
	},function(){
		$("span",$(this)).fadeOut(250);
	});
	
	i = 0;
	
	$("#quickNav li a").each(function(){
		$(this).addClass("item_"+i);
		i++;
	});
	
	active = '#h1';
	
	$("#bannerNav li a").click(function(){
		$("#bannerNav li").removeClass('active');
		$(this).parent().addClass('active');
		
		active = $(this).attr('href');

		clearInterval(slider);
		
		var relval = $(this).attr('rel');
		
		var ab = relval.split("_");
		
		new_bgp = parseInt(ab[1]);
		
		new_bgp = new_bgp * -1;
		
		changeBG(new_bgp);
		
		
			var activeTitle = $(".ttl:visible").attr('id');
			
			j = activeTitle.replace("f","");
			
			j = parseInt(j);
			
			if(j == 4) {
				j = 1;
			} else {
				j++;
			}
			
			$("#bannerNav li, #bannerNav li a").removeClass('active');
			$("#bannerNav li a[href='#h"+j+"']").parent().addClass('active');
			
			
			$("#"+activeTitle).fadeOut(375);
			
			setTimeout(function(){
				$("#f"+j).fadeIn(375);
			},375);
		
		
		return false;
	});
	
	$("#bannerNav li a").hover(function(){
		$("#bannerNav li").removeClass('active');
		$(this).parent().addClass('active');		
	},function(){
		$("#bannerNav li").removeClass('active');
		$("#bannerNav li a[href='"+active+"']").parent().addClass('active');
	});
	
	if($.browser.msie) {
		var v = parseInt($.browser.version);
		
		if(v == 8) {		
			$(".pink > .sectionTitle").css({"color":"#000"});
			$(".green > .sectionTitle").css({"color":"#000"});
			$(".blue > .sectionTitle").css({"color":"#000"});	
		} else {
			$(".pink > .sectionTitle").textShadow();
			$(".green > .sectionTitle").textShadow();
			$(".blue > .sectionTitle").textShadow();
		}
	}


	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
		$("#subhead h2 a.info").click(function(){
			$(".popup",$(this).parent().parent()).toggle();			
		});
	} else {
	
		$("#subhead h2 a.info").hover(function(){
			$(".popup",$(this).parent().parent()).show();
		},function(){
			$(".popup",$(this).parent().parent()).hide();
		});
	}
	
	$("#side ul li.active a").append("<span> &gt;</span>");
	
	$("#side ul li a").hover(function(){
	
		if($(this).parent().hasClass('active')) {
			;
		} else {

			$(this).append("<span> &gt;</span>");
			$(this).css({
				"filter":" alpha(opacity=100)",
				"opacity":" 1",
				"-moz-opacity":" 1"
			});
		}
		
		
	},function(){
		
		if($(this).parent().hasClass('active')) {
			;
		} else {

			$("span",$(this)).remove();
			$(this).css({
				"filter":" alpha(opacity=50)",
				"opacity":" 0.5",
				"-moz-opacity":" 0.5"
			});
		}
	});
	
	$("#bc ul li a").append("<span class='sep'> </span>");
	$("#bc ul li a:last span.sep").remove();

	alpha = true;

	$("#content.grid_8 > .grid_4").each(function(){
	
		if(alpha) {
			$(this).addClass('alpha');
			alpha = false;
		} else {
			$(this).addClass('omega');
			alpha = true;
		}
	
	});
	
	$("a.featuredWork").click(function(){
		
		var url = $(this).attr('href');
		
		url = url + "&ajax=1";
		
		var headerColour = $(this).attr('rel');
		
		$.ajax({
			url:url,
			complete:function(a,b) {
				if(b == 'success') {
					var c = a.responseText;
					
					var h = $(window).height();
					var w = $(window).width();
					
					mt = (h - 460) / 2;
					mt = parseInt(mt);
					
					$("body").append("<div id='cs_popup_wrapper'><div id='cs_popup' style='margin-top:"+mt+"px;'><a href='#' title='close' id='cs_close'><img src='/img/x_"+headerColour+".png' /></a>"+c+"</div></div>");
					
					$("#cs_popup_wrapper").css({"width":w+"px","height":h+"px"});
					
					$(".cs_left img").hide();
					$(".cs_left img:first").show();
					
					var i = 0;
					
					$(".cs_right ul li a").each(function(){
						i++;
						$(this).empty().append(i);
					});
					
					
					//$("#cs_popup > div").css({"backgroind-color":"#2A2A2A"});
					
				}
			}
		});
		
		
		//$('html,body').animate({scrollTop: 120},'slow');
		
		return false;
	
	});
	
	$("a#cs_close").livequery("click",function(){
		$("#cs_popup_wrapper").remove();
		return false;
	});
	
	$(".cs_right ul li a").livequery("click",function(){
	
		url = $(this).attr('href');
		
		$(".cs_left img").fadeOut(500);
		
		setTimeout(function(){
			
			$(".cs_left img[src='"+url+"']").fadeIn(500);
			
		},500);
	
		return false;
	});
	
	$("div.tab").hide();
	$("div.tab:first").show();
	
	$("a.tab").click(function(){
		
		idx = $(this).attr('href');
		
		$("div.tab").fadeOut(500);
		
		setTimeout(function(){
			$(idx).fadeIn(500);
			
		},500);
		
		return false;
	});
	
	//footer_pos = false;
	
	flbgp = 0;
	
	setInterval(function(){
		
		var ua = $.browser;
		
		flbgp = flbgp - 1000;
		
		if($.browser.msie) {
			var v = parseInt($.browser.version);
			
			if(v <= 8) {
				$("#clientLogoWrapper").animate({"background-position-x":flbgp+"px"},750);
			} else {
				$("#clientLogoWrapper").animate({"background-position":flbgp+"px"},750);
			}
		} else {

			$("#clientLogoWrapper").animate({"background-position":flbgp+"px"},750);
		}
	},5000);
	
	//*
	$("#quickNav a, a[rel='lightbox']").click(function(){
		//load page through ajax,
		//isolate main part of page
		//show in lightbox?
		
		cls = $(this).attr('class');
		
		$.ajax({
			url:$(this).attr('href'),
			dataType:'html',
			success:function(a,b,c){
				
				$("body").append("<div id='cs_popup_wrapper'><div id='cs_popup'><a href='#' title='close' id='cs_close'><img src='/img/x.png' /></a><div>"+a+"</div></div></div>");
				
				var ph = $("#cs_popup").height();
				var wh = $(window).height();
				
				var tp = parseInt(((wh - ph) / 2));
				
				$("#cs_popup").css({"margin-top":tp+"px"});
				
				if(cls == 'item_0' || cls == 'item_2') {
					$("#cs_popup").css({"width":"450px"});
				}
				
				
				$("input, textarea, select").css({"color":$("body a").css('color')});
			}
		});
		
		return false;
	});
	//*/
	
	$("input, textarea, select").css({"color":$("body a").css('color')});
	
	$("body").append("<div id='servicesdropdown'><div></div></div>");
	
	$("#servicesdropdown div").append($(".footerWrapper").html());
	
	$("#servicesdropdown .footer_about").remove();
	$("#servicesdropdown .smallprint").remove();
	
	i = 0;
	
	$("#servicesdropdown .footer_links").each(function(){
		i++;
		
		switch(i) {
			case 1:
				var clr = "FF009C";
				break;
			case 2:
				var clr = "DAFE2A";
				break;
			case 3:
				var clr = "49BFF1";
				break;				
		}
		
		$("h4",$(this)).css({"color":"#"+clr});
	});
	
	$("div#superhead div.container_16 div.grid_8 ul li a[title='Services']").click(function(){
		$("#servicesdropdown").toggle();
		
		setTimeout(function(){
			$("#servicesdropdown").fadeOut();
		},10000);
		
		return false;
	});
	
	$("#primary.portfolio ul li a").click(function(){
		var hf = $(this).attr('href');
		
		$(".grid_4.section").removeClass('alpha').removeClass('omega');
		
		/*
		switch(hf) {
			case '/fresh-creative-thinking/':
				$("a.featuredWork[rel='pink']").parent().fadeIn(375);
				$("a.featuredWork[rel='green']").parent().fadeOut(375);
				$("a.featuredWork[rel='blue']").parent().fadeOut(375);
				break;
			case '/inspiring-design/':
				$("a.featuredWork[rel='pink']").parent().fadeOut(375);
				$("a.featuredWork[rel='green']").parent().fadeIn(375);
				$("a.featuredWork[rel='blue']").parent().fadeOut(375);
				break;
			case '/digital-marketing/':
				$("a.featuredWork[rel='pink']").parent().fadeOut(375);
				$("a.featuredWork[rel='green']").parent().fadeOut(375);
				$("a.featuredWork[rel='blue']").parent().fadeIn(375);
				break;
			default:
				$("#content .section").fadeIn(375);
		}
		//*/

		//*
		switch(hf) {
			case '/fresh-creative-thinking/':
				$("div[rel='Creative Thinking']").fadeIn(375);
				$("div[rel='Inspiring Design']").fadeOut(375);
				$("div[rel='Digital Marketing']").fadeOut(375);
				break;
			case '/inspiring-design/':
				$("div[rel='Creative Thinking']").fadeOut(375);
				$("div[rel='Inspiring Design']").fadeIn(375);
				$("div[rel='Digital Marketing']").fadeOut(375);
				break;
			case '/digital-marketing/':
				$("div[rel='Creative Thinking']").fadeOut(375);
				$("div[rel='Inspiring Design']").fadeOut(375);
				$("div[rel='Digital Marketing']").fadeIn(375);
				break;
			default:
				$("#content .section").fadeIn(375);
				$("div[rel='Creative Thinking']").fadeIn(375);
				$("div[rel='Inspiring Design']").fadeIn(375);
				$("div[rel='Digital Marketing']").fadeIn(375);
		}
		//*/
		
		setTimeout(function(){
			alpha = true;

			$("#content .section").each(function(){

				if($(this).is(":visible")) {

					if(alpha) {
						$(this).addClass('alpha');
						alpha = false;
					} else {
						$(this).addClass('omega');
						alpha = true;
					}

				}

			});
		
		},400);

		return false;
	});
	
	
	$(".mugshot a").click(function(){
		
		$("#content").fadeOut(500);
		
		url = $(this).attr('href');
		
		setTimeout(function(){
			$("#content").empty().load(url);
			$("#content").fadeIn(500);
		},500);
		
		return false;
	});
	
	
	$("#cs_popup form").livequery("submit",function(){
		
		var d = $(this).serialize();
		
		el = $(this);
		
		if($(this).attr('action') == 'http://email.newsletterhub.co.uk/email/subscribe.php') {
			msg = "Thank you for your interest in our mailing list. You will receive regular updates from us and can unsubscribe at any time";
		} else {
			msg = "Thank you for your enquiry, someone will be in touch with you shortly.";
		}

		$.ajax({
			url:$(this).attr('action'),
			data:d,
			type:$(this).attr('method'),
			complete:function(a,b){
				$(el).parent().empty().append("<h1>Thank you</h1><p>&nbsp;</p><p class='thankyou'>"+msg+"</p>");
			}

		});

		return false;
		
	});
	
	$("body, #cs_popup_wrapper").livequery("click",function(){
		$("#cs_popup_wrapper").remove();
	});


	$("#cs_popup").livequery("click",function(e){
		e.stopPropagation();
	});
	
	/*
	
	$(".twitter_follower a").hover(
		function(){
			
			var tweet = $("span",$(this).parent()).html();
			var tweeter = $(this).attr("title");
			
			$("#tweetreader").empty().append("<h3>"+tweeter+"</h3><p>"+tweet+"</p>");
		},function(){
			$("#tweetreader").empty();
		}
	);
	
	//*/

	$(".xdl").remove();

	var seo_panels = $("#xdlp").html() || '';;
	$("#xdlp").remove();
	seo_panels = seo_panels.replace(/yorkshire/ig,"");
	$("#main").append(seo_panels);

	$(".testimonial .rating").each(function(){
		var r = $(this).html();
		r = r * 1;
		var w = 20 * r;
		$(this).css({"width":w+"px"});
	});
	
	$(".hreview-aggregate").remove();

});

