(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(){
	
	// Preload some images
	
	jQuery.preLoadImages(
		"static/img/menu.png",
		"static/img/submenu_bg.png",
		"static/img/menu_bg_2.png",
		"static/img/submenu.png"
	);
	
	// Menu
	
	if( $("ul#menu > li > ul:visible").length == 1 ){
		var submenu_default = $("ul#menu > li ul:visible");
	}else{
		var submenu_default = false;
	}
	
	$("ul#menu > li.has_submenu").hover(function(){
		if( $(this).children("a").length != 0 && submenu_default != false ){
			submenu_default.hide().parents("li:first").children("a:first").removeClass("selected");
			$("span#submenu").hide();
		}
		if( $(this).children("ul").length > 0 ){
			$(this).addClass("submenu_show");
			$("span#submenu").show();
			$(this).children("ul").show();
		}
	},function(){
		if( $(this).children("ul").length > 0 ){
			$(this).removeClass("submenu_show");
			$("span#submenu").hide();
			$(this).children("ul").hide();
		}
		if( submenu_default ){
			submenu_default.show().parents("li:first").children("a:first").addClass("selected");
			$("span#submenu").show();
		}
	});
	
	$("ul#menu > li.has_submenu_black").hover(function(){
		$(this).children("ul").show();
	},function(){
		$(this).children("ul").hide();
	});
	
	/* Sub-sub-menu
	
	function measure_subsubmenu(){
		$("ul#menu > li > ul > li > ul").each(function(){
			var longest_li = 0;
			$(this).children("li").children("a").each(function(){
				if( $(this).width() > longest_li ){
					longest_li = $(this).width();
				}
			});
			$("ul#menu > li > ul > li > ul").width(longest_li+48);
			$("ul#menu > li > ul > li > ul > li > a").width(longest_li);
		});
	}
	
	$("ul#menu > li > ul > li").hover(function(){
		$(this).css("overflow","visible");
	},function(){
		$(this).css("overflow","hidden");
	});
	*/
	
	// Home Photos
	
	if( $("div#home_photos").length == 1 ){
		
		var home_photo_curr = $("div#home_photos ul li:first");
		jQuery.preLoadImages( home_photo_curr.next("li").find("img").attr("src") );
		
		setInterval(function(){
			if( home_photo_curr.next("li").length == 1 ){
				home_photo_curr = home_photo_curr.next("li");
			}else{
				home_photo_curr = $("div#home_photos ul li:first");
			}
			jQuery.preLoadImages( home_photo_curr.next("li").find("img").attr("src") );
			$("div#home_photo_container div").before('<div><img src="'+(home_photo_curr.find("img").attr("src"))+'" alt="'+(home_photo_curr.find("a").attr("title"))+'" /><a href="'+(home_photo_curr.find("a").attr("href"))+'">'+(home_photo_curr.find("a").attr("title"))+'<img src="static/img/button_more.png" alt="More" /></a></div>');
			$("div#home_photo_container div:last").fadeOut(500,function(){
				$(this).remove();
			});
		},3000);
		
	}
	
	// History - 1998
	
	$("ul#history_1998 a").fancybox({
		overlayOpacity: 0.8,
		titlePosition: "inside"
	});
	
	// History - 2002
	
	$("ul#history_2002 a").fancybox({
		overlayOpacity: 0.8,
		titlePosition: "inside"
	});
	
	// Work in Print
	
	$("ul.workinprint a").fancybox({
		overlayOpacity: 0.8,
		titlePosition: "inside"
	});
	
	// Case studies
	
	$("ul.case_studies a").fancybox({
		overlayOpacity: 0.8,
		titlePosition: "inside"
	});
	
	// Gallery
	
	if( $("div#gallery_container").length == 1 ){
		
		if( $("div#left").hasClass("gallery_portraits") ){
			var portraits = true;
		}else{
			var portraits = false;
		}
		
		$("ul#gallery_filmstrip a").css("opacity","0.5");
		
		if( !portraits ){
			$("ul#gallery_filmstrip").css("top","-20px");
		}else{
			$("ul#gallery_filmstrip").css("left","5px");
		}
		
		$("ul#gallery_filmstrip a:first").css("opacity","1").addClass("selected");
		
		if( !portraits ){
			$("ul#gallery_filmstrip li:last").prependTo( $("ul#gallery_filmstrip") );
			$("ul#gallery_filmstrip li:last").prependTo( $("ul#gallery_filmstrip") );
		}else{
			$("ul#gallery_filmstrip li:last").prependTo( $("ul#gallery_filmstrip") );
			$("ul#gallery_filmstrip li:last").prependTo( $("ul#gallery_filmstrip") );
			$("ul#gallery_filmstrip li:last").prependTo( $("ul#gallery_filmstrip") );
			$("ul#gallery_filmstrip li:last").prependTo( $("ul#gallery_filmstrip") );
		}
		
		var gallery_animating = false;
		if( !portraits ){
			var curr_element_position = 2;
		}else{
			var curr_element_position = 4;
		}
		
		$("div#county_list select").click(function(){
			clearInterval( gallery_auto );
		});
		
		$("ul#gallery_filmstrip a").click(function(){
			if( !gallery_auto_click ){
				clearInterval( gallery_auto );
			}
			if( gallery_animating ){
				return false;
			}
			gallery_animating = true;
			
			if( !portraits ){
				var element_position = $(this).position().top / 108;
				var element_offset = ( element_position - curr_element_position );
				var new_position = ( element_offset * 108 ) - 20;
			}else{
				var element_position = $(this).position().left / 101;
				var element_offset = ( element_position - curr_element_position );
				var new_position = ( element_offset * 101 ) + 5;
			}
			
			if( !portraits ){
				
				if( element_offset < 0 ){
					$("ul#gallery_filmstrip").css("top",new_position+"px");
					for( i=-1; i>=element_offset; i-- ){
						$("ul#gallery_filmstrip li:last").prependTo( $("ul#gallery_filmstrip") );
					}
					$("ul#gallery_filmstrip").animate( {top: "-20px"}, 400, "swing" );
				}
				
				if( element_offset > 0 ){
					$("ul#gallery_filmstrip").animate( {top: "-"+(new_position+40)+"px"}, 400, "swing", function(){
						for( i=1; i<=element_offset; i++ ){
							$("ul#gallery_filmstrip li:first").appendTo( $("ul#gallery_filmstrip") );
						}
						$("ul#gallery_filmstrip").css("top","-20px");
					});
				}
				
			}else{
				
				if( element_offset < 0 ){
					$("ul#gallery_filmstrip").css("left",new_position+"px");
					for( i=-1; i>=element_offset; i-- ){
						$("ul#gallery_filmstrip li:last").prependTo( $("ul#gallery_filmstrip") );
					}
					$("ul#gallery_filmstrip").animate( {left: "5px"}, 400, "swing" );
				}
				
				if( element_offset > 0 ){
					$("ul#gallery_filmstrip").animate( {left: "-"+(new_position-10)+"px"}, 400, "swing", function(){
						for( i=1; i<=element_offset; i++ ){
							$("ul#gallery_filmstrip li:first").appendTo( $("ul#gallery_filmstrip") );
						}
						$("ul#gallery_filmstrip").css("left","5px");
					});
				}
				
			}
			
			$("ul#gallery_filmstrip a.selected").css("opacity","0.5").removeClass("selected");
			$(this).css("opacity","1").addClass("selected");
			
			$("div.gallery_photo").before('<div class="gallery_photo"><img src="'+$(this).attr("href")+'" alt="" /><p><span class="top"></span><span class="bottom"></span>'+$(this).children("img").attr("alt")+'</p></div>');
			if( !$(this).children("img").attr("alt") ){
				$("div.gallery_photo:first p").hide();
			}
			
			
			if( $("div.gallery_photo:first img").attr("complete") ){
				jQuery.preLoadImages( $("ul#gallery_filmstrip a.selected").parents("li").next("li").children("a").attr("href") );
				jQuery.preLoadImages( $("ul#gallery_filmstrip a.selected").parents("li").next("li").next("li").children("a").attr("href") );
				$("div.gallery_photo:last").fadeOut(400,function(){
					$(this).remove();
					gallery_animating = false;
					gallery_auto_click = false;
				});
			}else{
				$("div#loading_overlay").css("opacity", 0).show().fadeTo(100, 0.8);
				$("div.gallery_photo:first img").load(function(){
					$("div#loading_overlay").stop().fadeOut(100, 0);
					jQuery.preLoadImages( $("ul#gallery_filmstrip a.selected").parents("li").next("li").children("a").attr("href") );
					jQuery.preLoadImages( $("ul#gallery_filmstrip a.selected").parents("li").next("li").next("li").children("a").attr("href") );
					$("div.gallery_photo:last").fadeOut(400,function(){
						$(this).remove();
						gallery_animating = false;
						gallery_auto_click = false;
					});
				});
			}
			
			return false;
		});
		
		$("a#gallery_prev").click(function(){
			if( gallery_animating ){
				return false;
			}
			$("ul#gallery_filmstrip a.selected").parents("li").prev("li").children("a").click();
			return false;
		});
		
		$("a#gallery_next").click(function(){
			if( gallery_animating ){
				return false;
			}
			$("ul#gallery_filmstrip a.selected").parents("li").next("li").children("a").click();
			return false;
		});
		
		var gallery_auto_click = false;
		var gallery_auto = setInterval(function(){
			gallery_auto_click = true;
			$("a#gallery_next").click();
		},4000);
		
		$(window).load(function(){
			jQuery.preLoadImages( $("ul#gallery_filmstrip a.selected").parents("li").next("li").children("a").attr("href") );
			jQuery.preLoadImages( $("ul#gallery_filmstrip a.selected").parents("li").next("li").next("li").children("a").attr("href") );
		});
		
	}
	
	// Clients
	
	if( $("ul#clients_media").length > 0 ){
		
		setInterval(function(){
			
			$("ul#clients_media").animate({top: "-80px"},800,"swing",function(){
				$("ul#clients_media").css("top","0").children("li:lt(4)").appendTo( $("ul#clients_media") );
			});
			
			$("ul#clients_corporate").animate({top: "-80px"},800,"swing",function(){
				$("ul#clients_corporate").css("top","0").children("li:lt(4)").appendTo( $("ul#clients_corporate") );
			});
			
		},2400);
		
	}
	
});