

	/*
		Hampton-Smith Limited 
		Page Interaction scripts
		------------------------
		Author:			Sam
		Date:			20/05/2009
		------------------------
		Copyright 2009 hampton-smith limited.
	*/
	
	var t;
	
	$(document).ready(function(){	
		$("#content ul").not(".sitemap").css({paddingTop:(($("#content").height()-$("#content ul").height())/2)-10});
		$("#content.portfolio ul").css({paddingTop:0});
		$("#content ul:not(.sitemap) li").each(function(n){
			$(this).css({"margin-right":-200, marginBottom:"8px",opacity:0.5}).animate({opacity:1,marginRight:10},500+(n*200),function(){$(this).animate({marginRight:0})});
		});			
				
		// Set up width for scrollers

		$("#content ol:not(.sitemap)").each(function(){
			var offset = $(this).find(".panel:last").offset();
			var maxscroll = ((offset.left)-$("#content").width())+180;	
			$.data(this, "offsetval", maxscroll);
		});
		
		// do the scrolling
		$("#content ol:not(.sitemap)").mousemove(function(e) {
			var maxscroll = $.data(this, "offsetval");
			marginleft = parseInt($(this).css("marginLeft"));
			newX = (e.pageX- $("#content").offset().left);
			scaler = parseInt((newX/$("#content").width())*100);
			if (scaler>100) scaler = 100;
			if (scaler<0) scaler = 0;
			clearTimeout(t)
			$(this).stop();
			if (parseInt((49-scaler)/10)!=0) {
				if (scaler<49)
					moveit(this.id,parseInt((49-scaler)/10),parseInt(scaler)*3,0-maxscroll);
				if (scaler>51)
					moveit(this.id,parseInt((49-scaler)/10),parseInt(100-scaler)*3,0-maxscroll);
			}
		});
		
		$(".portfolio").append("<div id=\"rightarrow\"><div id=\"rightarrowinner\"></div></div><div id=\"leftarrow\"><div id=\"leftarrowinner\"></div></div>");
		$(".portfolio #rightarrow").css({opacity:0});
		$(".portfolio #leftarrow").css({opacity:0});
		
		$(".testimonials").append("<div id=\"rightarrow\"><div id=\"rightarrowinner\"></div></div><div id=\"leftarrow\"><div id=\"leftarrowinner\"></div></div>");
		$(".testimonials #rightarrow").css({opacity:0});
		$(".testimonials #leftarrow").css({opacity:0});
		
		// Do a fake move back and forth to demo usage
		$(".portfolio ol:first").animate({marginLeft:0},1200,function(){
			$(".portfolio #leftarrow").css({display:"block"});
			$(".portfolio #rightarrow").css({display:"block"});
			$(".portfolio #rightarrow").animate({opacity:1},1000,function(){
			$(this).animate({opacity:0},2000)
			$(".portfolio #leftarrow").animate({opacity:1},2000,function(){$(this).animate({opacity:0},2000)});																			
		});
			$(this).animate({marginLeft:-200},2000,function(){
				$(this).animate({marginLeft:0},2000)
			})
		});
		
		$(".testimonials ol:first").animate({marginLeft:0},1200,function(){
			$(".testimonials #leftarrow").css({display:"block"});
			$(".testimonials #rightarrow").css({display:"block"});
			$(".testimonials #rightarrow").animate({opacity:1},1000,function(){
			$(this).animate({opacity:0},2000)
			$(".testimonials #leftarrow").animate({opacity:1},2000,function(){$(this).animate({opacity:0},2000)});																			
		});
			$(this).animate({marginLeft:-200},2000,function(){
				$(this).animate({marginLeft:0},2000)
			})
		});
		
		// Portfolio Links
		
		if (location.hash=="#web") {
			$("#wrapper").animate({marginTop:-290},2000);
		}
		
		if (location.hash=="#brand") {
			$("#wrapper").animate({marginTop:-570},2000);
		}
		
		$("#printbtn").click(function(){
			$("#wrapper").animate({marginTop:0},2000);	
			$("#printportfolio").animate({marginLeft:0},2000);
			return false;							  
		});
		
		$("#webbtn").click(function(){
			$("#wrapper").animate({marginTop:-290},2000);	
			$("#webportfolio").animate({marginLeft:0},2000);
			return false;							  
		});
		
		$("#brandbtn").click(function(){
			$("#wrapper").animate({marginTop:-570},2000);	
			$("#brandportfolio").animate({marginLeft:0},2000);
			return false;							  
		});
		
		$("#otherbtn").click(function(){
			$("#wrapper").animate({marginTop:-860},2000);	
			$("#otherportfolio").animate({marginLeft:0},2000);
			return false;							  
		});
		
		// READ MORE LINKS
		
		$("#readmore").click(function(){
			$("#moreinfo").animate({left:34},1800).animate({left:49},300).animate({left:44},500);
			/*
			offset = $(this).parent("div").offset();
			offsetpage = $("#content").offset();
			offsetleft = offset.left-offsetpage.left;
			offsettop = offset.top-offsetpage.top;
			startwidth = parseInt($(this).parent("div").css("width"));
			startheight = parseInt($(this).parent("div").css("height"));
			$("#moreinfo").data("width",startwidth);
			$("#moreinfo").data("height",startheight);
			$("#moreinfo").data("top",offsettop);
			$("#moreinfo").data("left",offsetleft);
			$("#moreinfo p").css({opacity:0});
			$("#moreinfo").css({position:"absolute",top:offsettop, left:offsetleft, width:startwidth, height:startheight, margin:0, opacity:0, display:"block"}).animate({opacity:1},500,function(){$(this).find("p").animate({opacity:1},2000);}).animate({left:44,top:44,width:872, height:252},2000);
			*/
			return false;							  
		});
		
		$(".viewfull").click(function(){
			var idname = $(this).attr("href");
			$.ajax({
  				url: 'testimonials.xml',
				type: 'GET',
				dataType: 'xml',
				error: function(){
					alert('Error loading XML document');
				},
				success: function(xmlData){
					// do something with xml
					xmlDataSet = xmlData;
					$(xmlDataSet).find("testimonial").each(function(){
						if ($(this).attr("id")==idname) {														
							var content = $(this).find("quote").text();
							var author = "<h3>"+$(this).find("author").text()+"</h3>";
							$("#viewfull #inner").html(content +"<br />"+author);
						}
					});
				}
			});

			$("#viewfull").animate({left:34},1800).animate({left:49},300).animate({left:44},500);	
			return false;	
		});
		
		$("#closereadfull").click(function(){
			$("#content ul").animate({opacity:1},1800);
			//$("#content ol").animate({opacity:1},1800);
			$("#viewfull").animate({left:1100},2000);
			return false;
		});
		
		$(".portfoliobtn").click(function(){
			var idname = $(this).attr("href");
			$.ajax({
  				url: 'portfolio.xml',
				type: 'GET',
				dataType: 'xml',
				error: function(){
					alert('Error loading XML document');
				},
				success: function(xmlData){
					// do something with xml
					xmlDataSet = xmlData;
					$(xmlDataSet).find("portfolio").each(function(){
						if ($(this).attr("id")==idname) {														
							var content = $(this).find("detail").text();
							var url = $(this).find("link").text();
							if (url=='') { } else {
								content = '<a href="'+url+'" target="_blank" title="Click here to visit the website">'+content+'</a>';	
							}
							$("#viewfull #inner").html(content);
						}
					});
				}
			});

			$("#viewfull").animate({left:34},1800).animate({left:49},300).animate({left:44},500);	
			//$("#content ol").animate({opacity:0},1800);
			$("#content ul").animate({opacity:0},1800);
			return false;	
		});
		
		
		$("#closereadmore").click(function(){
			$("#moreinfo").animate({left:1100},2000);
			/*
			newleft = $("#moreinfo").data("left");
			newtop = $("#moreinfo").data("top");
			newwidth = $("#moreinfo").data("width");
			newheight = $("#moreinfo").data("height");
			$("#moreinfo p").animate({opacity:0},2000);
			$("#moreinfo").animate({left:newleft,top:newtop,width:newwidth, height:newheight},2000).animate({opacity:0},500,function(){$(this).css({display:"none"})});
			*/
			return false;							  
		});
		
	});
	
	function moveit(obj, amount,timer,limit){
		clearTimeout(t);
		//console.log($("#"+obj).css("marginLeft"));
		//console.log("move:"+amount);
		//console.log("when:"+timer);
		newoffset = parseInt($("#"+obj).css("marginLeft"))+amount;
		if (newoffset>0) newoffset = 0;
		if (newoffset<limit) newoffset = limit;
		
		if (newoffset>limit+250) {
			if($("#rightarrow").css("opacity")==0)		
				$("#rightarrow").not(":animated").css({opacity:0,display:"block"}).animate({opacity:1},1000);}	else {
			if($("#rightarrow").css("opacity")==1)
				$("#rightarrow").not(":animated").animate({opacity:0},1000,function(){$(this).css({display:"none"})});
		}
		if (newoffset<-50) {
			if($("#leftarrow").css("opacity")==0)
				$("#leftarrow").not(":animated").css({opacity:0,display:"block"}).animate({opacity:1},1000);
		} else {
			if($("#leftarrow").css("opacity")==1)
				$("#leftarrow").not(":animated").animate({opacity:0},1000,function(){$(this).css({display:"none"})});
		}
		
		$("#"+obj).css({marginLeft:newoffset});
		if (newoffset<0 && newoffset>limit) {
			t = setTimeout("moveit('"+obj+"',"+amount+","+timer+","+limit+");",timer); } else {
			clearTimeout(t);
		}
	}