(function($){
  var cache = [];
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);

$(document).ready(function(){
	
	// Area menu switching panels on home page
	
	$("#homePanels").each(function(){
		
		var transitioning = false;
		var auto_transitioning = false;
		
		$("#homePanels div.panel").not(":last").hide();
		$("#area_menu.extended li a:first").addClass("selected").click();
		
		$("#area_menu.extended li a").click(function(e){
			if( $(this).hasClass("selected") || transitioning == true ){ return false; }
			transitioning = true;
			if( e.pageX != undefined ){ clearInterval( auto_transitioning ); }
			$("#area_menu.extended li a").removeClass("selected");
			$(this).addClass("selected");
			$("#homePanel_"+$(this).attr("data-id")).prependTo("#homePanels").show();
			$("#homePanels div.panel:visible:last").addClass("nopie").fadeOut(350,function(){
				$(this).removeClass("nopie");
				transitioning = false;
			});
			return false;
		});
		
		auto_transitioning = setInterval(function(){
			var next = $("#area_menu.extended li a.selected").closest("li").next("li").children("a");
			if( next.length == 0 ){
				next = $("#area_menu.extended li a:first");
			}
			next.click();
		},5500);
		
	});
	
	// Case study filter
	
	$("#case_studies_filter ul li a").click(function(){
		var tick = $(this).children("span.tick");
		if( tick.hasClass("checked") ){
			if( $(this).is("#check_all") ){
				$("#case_studies_filter ul li a span.tick").removeClass("checked");
			}else{
				tick.removeClass("checked");
				$("#check_all span.tick").removeClass("checked");
			}
		}else{
			if( $(this).is("#check_all") ){
				$("#case_studies_filter ul li a span.tick").addClass("checked");
			}else{
				tick.addClass("checked");
				if( $("#case_studies_filter a.area_checkbox").length == $("#case_studies_filter a.area_checkbox span.tick.checked").length ){
					$("#check_all span.tick").addClass("checked");
				}
			}
		}
		
		update_filter();
		
		return false;
	});
	
	if( $("#case_studies_filter ul li a span.tick.checked").length == 0 ){
		$("#check_all").click();
	}
	
	update_filter();
	
	function update_filter(){
		$("#case_studies > a").hide();
		
		var area_count = 0;
		
		$("#case_studies_filter a.area_checkbox").each(function(){
			var area_id = $(this).attr("data-id");
			if( $(this).children("span.tick").is(".checked") ){
				area_count++;
				$("#case_studies a.area_"+area_id).show();
			}
		}).length;
		
		$("#filter_counts span.category_count").html( area_count );
		$("#filter_counts span.case_study_count").html( $("#case_studies a:visible").length );
	}
	
	// Photo Gallery
	
	(function(){
		if( $("div.photo_gallery").length > 0 ){
			
			var gallery_transitioning = false;
			var gallery_auto = false;
			
			$("div.photo_gallery ul.nav a").click(function(e){
				if( typeof e.pageX !== 'undefined' ){
					if( gallery_auto !== false ){ clearInterval( gallery_auto ); gallery_auto = false; }
					changePhoto( $(this) );
					return false;
				}
			});
			
			$("div.photo_gallery div.photo a").live("click", function(e){
				e.preventDefault();
				if( gallery_auto !== false ){ clearInterval( gallery_auto ); gallery_auto = false; }
				$("#"+$(this).attr("data-gallery-id")).click();
			});
			
			$("div.photo_gallery ul.nav a").colorbox({
				rel: 'gallery',
				href: $(this).attr('href'),
				title: $(this).attr('data-caption'),
				fixed: true
			});
			
			$(window).load(function(){
				$("div.photo_gallery ul.nav a").each(function(){
					$.preLoadImages( $(this).attr("data-inline") );
				});
			});
			
			if( $("#system_panel div.photo_gallery ul.nav li, #area_intro_panel div.photo_gallery ul.nav li, #case_study_right div.photo_gallery ul.nav li").length > 1 ){
				gallery_auto = setInterval(function(){
					if( $("div.photo_gallery ul.nav a.selected").closest("li").next("li").length == 1 ){
						var next_photo = $("div.photo_gallery ul.nav a.selected").closest("li").next("li").children("a");
					}else{
						var next_photo = $("div.photo_gallery ul.nav a:first");
					}
					changePhoto( next_photo );
				},4500);
			}
			
			function changePhoto( photo_link ){
				if( gallery_transitioning == true ){ return false; }
				gallery_transitioning = true;
				var photo_gallery = photo_link.closest("div.photo_gallery");
				photo_gallery.find("ul.nav li a").removeClass("selected");
				photo_link.addClass("selected");
				photo_gallery.children("div.photo").prepend('<a class="modal" href="' + photo_link.attr("href") + '" data-gallery-id="' + photo_link.attr("id") + '"><span class="caption">' + photo_link.attr("title") + '</span><span class="magnify"></span></a>').children("a:first").css( "background-image", "url('" + photo_link.attr("data-inline") + "')" );
				photo_gallery.children("div.photo a:last").addClass("nopie").fadeTo( 300, 0, function(){
					$(this).remove();
					gallery_transitioning = false;
				});
			}
			
		}
	})();
	
	// Map
	
	$("#map").colorbox({
		html: '<iframe width="740" height="540" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.co.uk/maps?client=opera&amp;oe=utf-8&amp;channel=suggest&amp;ie=UTF8&amp;q=kwik-klik,+HD6+4DJ&amp;fb=1&amp;gl=uk&amp;hq=kwik-klik,&amp;hnear=0x487bddf419eb5ba9:0xa1bcffa46179149a,Brighouse,+West+Yorkshire+HD6+4DJ&amp;cid=0,0,10546741063128146700&amp;ll=53.72143,-1.777146&amp;spn=0.006295,0.006295&amp;iwloc=A&amp;output=embed"></iframe>',
		innerHeight: 550,
		innerWidth: 740
	});
	
	// Products galleries
	
	$("#products a.photo").click(function(){
		$(this).next("ul").find("li:first a").click();
		return false;
	});
	
	$("#products ul.photos a").colorbox({
		fixed: true
	});
	
});
