eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(7($){$.8.9=7(d){g e=$.v({},$.8.9.k,d);g f=e.l;2.w(7(){5(!$(2).3(\'4\')||$(2).3(\'4\')=="#"){$(2).3(\'4\',j.x.4)}5(!$(2).3(\'6\')){$(2).3(\'6\',y.6)}5($.h.z){$(2).3(\'m\',$(2).3(\'4\')).3(\'4\',\'\').3(\'4\',\'A:B(0)\').3(\'C\',\'$.8.9.n(2,"\'+$(2).3(\'6\')+\'","\'+$(2).3(\'m\')+\'")\')}i{$(2).o(p)}});7 p(a){g b=$(2).3(\'4\');g c=$(2).3(\'6\');b=b.E("q:///","F://q/");5($.h.G){j.H.I(b,c)}i 5($.h.J){j.r.K(c,b,\'\')}i 5(L.M.N().O(\'P\')>-1){s(f)}i 5($.h.Q){s(f)}a.R()};t 2};$.8.9.k={l:\'S "T + D" U "V + D" W X, u Y 2 Z u 10 11.\'};$.8.9.n=7(e,a,b){e.12="r";e.6=a;e.4="";e.4=b;e.o();t 13}})(14);',62,67,'||this|attr|href|if|title|function|fn|jFav|||||||var|browser|else|window|defaults|msg|link|OperaBookmark|click|addToFavorites|file|sidebar|alert|return|to|extend|each|location|document|opera|javascript|void|onClick||replace|http|msie|external|AddFavorite|mozilla|addPanel|navigator|userAgent|toLowerCase|indexOf|chrome|safari|preventDefault|Press|Ctrl|or|CMD|for|MAC|add|page|your|bookmarks|rel|false|jQuery'.split('|'),0,{}));

(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(){
	
	// Add to Favourites
	
	$('.add2fav').jFav();
	
	// "Read More"
	
	$("div#intro *").hide();
	$("div#intro .intro, div#intro .intro *").show();
	
	$("a#intro_read_more").click(function(){
		$(this).remove();
		$("div#intro *").show();
	});
	
	// Quote Form
	
	$("div#quote form input[type=image]").css("opacity", "0.4");
	
	$("div#quote div.box_inner").height( $("div#quote div.box_inner").height() );
	
	$("div#quote form .required").keyup(function(){
		if( $(this).hasClass("error") ){
			$(this).removeClass("error");
			$(this).prev("label").removeClass("error");
		}
		
		var error = false;
		
		$("div#quote form .required").each(function(){
			if( $(this).val() == "" ){
				error = true;
			}
		});
		
		if( error ){
			$("div#quote form input[type=image]").css("opacity", "0.4");
		}else{
			$("div#quote form input[type=image]").css("opacity", "1");
		}
		
	});
	
	$("div#quote form").submit(function(){
		
		var error = false;
		
		$("div#quote form .required").each(function(){
			if( $(this).val() == "" ){
				error = true;
				$(this).addClass("error");
				$(this).prev("label").addClass("error");
			}
		});
		
		if( error ){
			
			$("div#form_error").html("Please complete the fields marked in red").fadeIn(300,function(){
				setTimeout(function(){
					$("div#form_error").fadeOut(300);
				},2000);
			});
			
		}else{
			$("div#quote form").hide();
			$("div#form_status").html("Submitting quote request...").show();
			
			$.ajax({
				url: "quote/",
				data: {
					name: $("input#quote_name").val(),
					email: $("input#quote_email").val(),
					phone: $("input#quote_phone").val(),
					details: $("textarea#quote_details").val(),
					budget: $("input#quote_budget").val()
				},
				type: "POST",
				success: function( data, textStatus ){
					$("div#form_status").hide();
					$("div#form_sent").html('<p><strong>Thanks for your quote request.</strong></p><p>We\'ll be in touch with a quote as soon as possible.</p><p>We aim to respond to all enquiries within 3 hours (8am-6pm Monday to Friday).</p>').show();
				},
				error: function(){
					$("div#form_status").html("An error has occured!");
				}
			});
		}
		
		return false;
	});
	
	
	
	// Gallery
	
	if( $("div#gallery_container").length == 1 ){
		
		if( $("div#left").hasClass("gallery_portraits") ){
			var portraits = true;
		}else{
			var portraits = false;
		}
		
		$("ul#gallery_filmstrip > li > 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="" /><div class="caption"><span class="top"></span><span class="bottom"></span>'+$(this).parents("li:first").children("div.caption").html()+'</div></div>');
			if( !$(this).parents("li:first").children("div.caption").html() ){
				$("div.gallery_photo:first div.caption").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") );
		});
		
	}
	
	// Links
	
	$("ul#clients li a img").css("opacity","0.5").hover(function(){
		$(this).stop().fadeTo(200, 1);
	},function(){
		$(this).stop().fadeTo(200, 0.5);
	});
	
	// Latest
	
	latest_width = $("p#latest").width();
	
	function latest_feed(){
		
		$("ul#feed").hide();
		
		$("p#latest").width( latest_width );
		
		$("p#latest").html('<strong>Latest:</strong> ');
		
		$("p#latest").append( '<span>'+$("ul#feed li:first").html()+'</span>' );
		
		$("ul#feed li:first").appendTo("ul#feed");
		
		$("p#latest").animate({"width": "908"},1200);
		
		setTimeout(function(){
			$("p#latest span:first").fadeOut(400, function(){
				latest_feed();
			});
		}, 5000);
		
	}
	
	latest_feed();
	
});
