var Query={};
$(document).ready(function(){
		$(".nav-link[rel='"+Page+"']").hide();
		$(".nav-link").each(function(){
			$(this).attr("href","javascript:void(0);");
		});
		$(".nav-link").click(function(){

			var val = $(this).attr("rel");
	        
	        $('html,body').animate({ scrollTop: $("#" + val).parent().offset().top }, 'slow', function () {
	            window.location.hash=val;
	        });
		});
		$("a.load").toggleClass("nolink",true);
		
		$("a.load").each(
			function(){
				var url = $(this).attr("href");
				
				var target = $(this).attr("rel");
				var title = $(this).attr("title");
				
				if(!target)
					target = $(this);
				else{					
					target = $(target);
				}
				
				$(target).html("");
				var hasQ = url.indexOf("?")!=-1;
				if(!hasQ)
					url=url+"?";
				else
					url=url+"&";

				url=url+"short=true";
				
				target.slideDown();
				target.fadeIn();
				target.toggleClass("loading sm padded",true)
				var self = $(this);
				$.get(url,function(result){
					$(target).data("result",result);
					$(target).slideUp("fast",function(){
						
						var target = $(this);
						
						target.toggleClass("loading sm padded",false);
						var result = $(target).data("result");
						target.html(result);
						self.attr("href","javascript:void(0);");
						self.toggleClass("nolink",true);
						target.slideDown();
						//target.fadeIn();
						var href = $(".brand-forge").attr("href");
						$(".brand-forge").fancybox({href:href+"?short=true",onComplete:function(){
							BrandForge.setZoom();
						}});
						
						if(title && sliders[title])
							sliders[title]();
						SignUp.FormHere();
						
					})
					
				});
				
			}		

		//}//end obj

		)//end each
});//End ready







var Slider = {
	init:function(options){

		//vars
		var conveyor = $(options.conveyor, $(options.content)),
		item = $(options.item, $(options.content));
		
		var totalwidth = 0;
		$(item).each(function(i) {
			if(i==0)
				$(conveyor).data("currentslide",$(this));
			$(this).data("pos",totalwidth);
			totalwidth += $(this).outerWidth( true );
		});
		
		conveyor.data("slides",item);
		
		if(options.onchange)
			conveyor.data("onchange",options.onchange);
		//set length of conveyor
		conveyor.width(totalwidth);
				
	    //config
	    var sliderOpts = {
		  max: (totalwidth - $(options.viewer, $(options.content)).outerWidth(true)),
	      slide: function(e, ui) { 
	        conveyor.css("left", "-" + ui.value + "px");
	        var slides = $(conveyor).data("slides");
	        
	        
	      }
	    };
	
	    //create slider    
	    $(options.dragger).slider(sliderOpts);   
    }

}


var sliders = {
		// Case Study Slider Scipt
		index:function(bottom) {
			
			var target="";
			if(bottom)
				target = "#slider4_bottom";
			else
				target = "#slider4";
			var root = $("#slider_block");
			
			var left = root.find(".slider-left-arrow");
			var right = root.find(".slider-right-arrow");
			//$(target).foundryShow({viewer:".viewer4"});
			/*$(".viewer4").foundryShow({slider:target,events:{place:function(o){
				var note;
				var unnote;
				if(o.pos>.5){
					note=$(".slider-note.left-side");
					unnote=$(".slider-note.right-side");
				}else{
					unnote=$(".slider-note.left-side");
					note=$(".slider-note.right-side");
				}
				note.show();
				unnote.hide();
				
				//console.log(o);
				var title = $(".case-link:eq("+o.index+")").data("title")+" case study";
				//console.log(title);
				$(".slider-note").html(title);
				
			}}});*/
			$(".viewer4").foundryShow({controls:{prev:left,next:right}});
			$(".viewer4").hover(function(){
				$(this).parent().parent().find(".slider-arrow").wait('remove');
				$(this).parent().parent().find(".slider-arrow").fadeTo('fast',1);
				
			},function(){
				$(this).parent().parent().find(".slider-arrow").wait(function(){
					$(this).fadeTo('slow',.5);
				},40);
				
				//$(this).foundryShow("play");
			});
			$(".viewer4").parent().parent().find(".slider-arrow").fadeTo('fast',.5);
			if(Query.caseStudy){
				var options={
						callback:function(){
							$(".case-"+Query.caseStudy).find(".case-link").click();
						}
				};
				$(".viewer4").foundryShow("moveto",options,$(".case-"+Query.caseStudy));
				
				
			}
			//$(".viewer4").foundryShow();
			//Slider.init({conveyor:".content-conveyor4",content:"#sliderContent4",item:".item4",dragger:target,viewer:".viewer4"});
			
		},
		// Client Experience Slider Scipt
		client:function(bottom){
			var target="";
			if(bottom)
				target = "#slider3_bottom";
			else
				target = "#slider3";
			
			
			var root = $("#client_block");
			
			var left = root.find(".slider-left-arrow");
			var right = root.find(".slider-right-arrow");
			$(".viewer").foundryShow({slice:4,animate:false,controls:{prev:left,next:right}});
			$(".viewer").hover(function(){
				$(this).parent().parent().find(".slider-arrow").wait('remove');
				$(this).parent().parent().find(".slider-arrow").fadeTo('fast',1);
				
			},function(){
				$(this).parent().parent().find(".slider-arrow").wait(function(){
					$(this).fadeTo('slow',1);
				},40);
				
				//$(this).foundryShow("play");
			});
			$(".viewer").parent().parent().find(".slider-arrow").fadeTo('fast',1);
			//Slider.init({conveyor:".content-conveyor",content:"#sliderContent",item:".item",dragger:target,viewer:".viewer"});
			
		},
		// Team Slider Scipt
		team:function(bottom){
			//vars
			var target="";
			if(bottom)
				target = "#slider2_bottom";
			else
				target = "#slider2";
			
			var root = $("#team_block");
			
			var left = root.find(".slider-left-arrow");
			var right = root.find(".slider-right-arrow");
			//$(target).foundryShow({viewer:".viewer2"});
			$(".viewer2").foundryShow({slice:4,animate:false,controls:{prev:left,next:right}});
			$(".viewer2").hover(function(){
				$(this).parent().parent().find(".slider-arrow").wait('remove');
				$(this).parent().parent().find(".slider-arrow").fadeTo('fast',1);
				
			},function(){
				$(this).parent().parent().find(".slider-arrow").wait(function(){
					$(this).fadeTo('slow',1);
				},40);
				
				//$(this).foundryShow("play");
			});
			$(".viewer2").parent().parent().find(".slider-arrow").fadeTo('fast',1);
			//Slider.init({conveyor:".content-conveyor2",content:"#sliderContent2",item:".item2",dragger:target,viewer:".viewer2"});
			
				
		}
			
			
};

var Cases = {
	canvas:".case-canvas",
	Close:function(){		
		$(Cases.canvas).slideUp(function(){
			$(this).html("");
			$(this).toggleClass("open",false);
			$(".viewer4").foundryShow("play");
		});
	},
	
	Click:function(self){
		self = $(self);
		var href = self.data("href")+"&short=true";
		var target = $(self.attr("rel"));
		if(target.hasClass("open")){
			Cases.Close();
			return;
		}
		target.toggleClass("loading sm",true);
		target.slideDown();
		$(".viewer4").foundryShow("stop");
		
		$.get(href,function(result){
			//return;
			target.html(result);
			target.toggleClass("loading sm",false);
			target.toggleClass("open",true);
			self.toggleClass("nolink",true);
			target.slideDown();
			
			$(".thumb_img").fancybox(
					{'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
					return '<table id="fancybox-title-float-wrap" cellpadding="0" cellspacing="0"><tbody><tr><td id="fancybox-title-float-left"></td><td id="fancybox-title-float-main">' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</td><td id="fancybox-title-float-right"></td></tr></tbody></table>';			}			}
			);
		});
	}
			
};

$(document).ready(function(){
	
	if(sliders[Page]){
		
		sliders[Page](true);
		
	}
	
	$(".slide-up").slideUp(1);
	
	$(".case-link").each(function(){
		
		var href = $(this).attr("href");
		$(this).data("href",href);
		
		$(this).attr("href","javascript:void(0);");
		$(this).click(function(){
			Cases.Click(this);
		});
		
		$(this).hover(function(e){
			//$(this).find(".case-roll-info").find(".case-name").html($(this).attr("title"));
			/*var offX = e.pageX-$(this).offset().left;
			var top = $(this).offset().top;
			var offY = e.pageY-top;
			var posX = offX/$(this).width();
			var posY = offY/$(this).height();
			
			
			$(this).find(".case-roll-info").find(".case-name").html(posX+" "+posY);*/
			$(this).find(".case-roll-info").css({"display":"block"});
			//$(this).find(".case-roll-info").fadeIn();
		},function(){
			$(this).find(".case-roll-info").css({"display":"none"});
			//$(this).find(".case-roll-info").fadeOut();
		});
		$(this).data("title",$(this).attr("title"));
		$(this).attr("title","");
	});
	//$(".case-roll-info").css({"display":"block"});
	//$(".case-roll-info").fadeOut(1);
	
	$(".hidden").each(function(){
		$(this).css({"display":"none"});
		$(this).toggleClass("hidden",false);		
	});
	$(".slider-note").hide();
	var title = $(".case-link:eq(0)").data("title")+" case study";
	
	$(".slider-note").html(title);
});


var BrandForge = {
		zoomOut:function(){
			$(".brand-forge-flip").toggleClass("hidden",true);
			$(".brand-forge-default").toggleClass("hidden",false);
		},
		zoomIn:function(){
			$(".brand-forge-flip").toggleClass("hidden",false);
			$(".brand-forge-default").toggleClass("hidden",true);
		},
		setZoom:function(){
			
			$('.zoom-mini').jqzoom({
					zoomType: 'standard',
					alwaysOn : true,
					target:".zoom-target"
			});
			
		}
		
};

var SignUp = {
	FormHere:function(){
		if(!$("#contact-form").get(0))
			return;
		
		$("#contact-form").validate();
	
	
	},
	Submit:function(form){
		var dataString = $(form).serialize();
		var url = "http://thefoundryagency.com/api/signup.php";
		
		$.ajax({  
			  type: "POST",  
			  url: url,  
			  data: dataString,  
			  dataType: 'json', 
			  success: function(d) {
				
				if(d.success){
					$("#form_block").html(d.msg);
					$("#form_block").css("height","50px");
					$(".signUpDIV_Block").css("min-height","0px");
					var targ = "body";
					if(!$(targ).get(0))
						targ="*:eq(0)";
					
					$(targ).wait({oncomplete:function(){
						if(window.parent)
							window.parent.$.fancybox.close()
						$.fancybox.close();		
						
					},length:30});
					
				}
				else{
					$(".note").css("display","block");
					$(".note").html(d.msg);					
				}
			  },
			  failure: function(d) {  
				console.log(d);  
			  }  
			}); 
		
	}
		
};
