(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(){
	
	// Newsletter email entry
	
	$("#panelNewsletter form input").blur(function(){
		if( !$(this).val() ){ $(this).addClass("empty").val("enter email address"); }
	}).focus(function(){
		if( $(this).hasClass("empty") ){ $(this).removeClass("empty").val(""); }
	}).blur();
	
	// Newsletter signup
	
	$("#panelNewsletter form").submit(function(){
		
		$("a.nl_back").live("click", function(){
			$("#panelNewsletter form").show();
			$("#newsletterStatus").hide();
		});
		
		$("#panelNewsletter form").hide();
		$("#newsletterStatus").html('<span style="color: #147c00;">Submitting...</span>').show();
		
		$.ajax({
			type: "POST",
			url: "newsletter-signup/",
			data: {
				email: $("#panelNewsletter input[name=email]").val()
			},
			success: function( data, textStatus, XMLHttpRequest ){
				if( data == "1" ){
					$("#newsletterStatus").html('<span style="color: #147c00;">Thanks for subscribing!</span>').show();
				}else{
					$("#newsletterStatus").html('<span style="color: #bb1600;">Email address invalid. Please <a href="javascript:;" class="nl_back" style="color: #7c1700;">go back</a> and try again.</span>').show();
				}
			},
			error: function(){
				$("#newsletterStatus").html('<span style="color: #bb1600;">An error has occured. Please <a href="javascript:;" class="nl_back" style="color: #7c1700;">go back</a> and try again.</span>').show();
			}
		});
		
	});
	
	$("#panelNewsletter form a").click(function(){
		$("#panelNewsletter form").submit();
	});
	
	// News headline top line (done like this for IE's sake)
	
	$("#newsLeft ul.headlines a, ul.news_downloads a, div.download_block li a, .tab_planning ul a").hover(function(){
		$(this).parent("li").find("span.hr, span.hr_top").hide();
		$(this).parent("li").prev("li").find("span.hr").hide();
	},function(){
		$(this).parent("li").find("span.hr, span.hr_top").show();
		$(this).parent("li").prev("li").find("span.hr").show();
	});
	
	// News Photos
	
	$("ul.news_photos a").fancybox({
		titlePosition: 'inside',
		onComplete: function(){
			$("#fancybox-img").bind("contextmenu", function(e){
				alert( "This image is copyright protected and the property of Express Elevators" );
				return false;
			});
		}
	});
	
	// Gallery Photos
	
	$("ul#photoGalleries a").fancybox({
		titlePosition: 'inside',
		onComplete: function(){
			$("#fancybox-img").bind("contextmenu", function(e){
				alert( "This image is copyright protected and the property of Express Elevators" );
				return false;
			});
		}
	});
	
	// Disable right click on gallery images
	
		$("img, #headerPic, ").bind("contextmenu", function(e){
			alert( "This image is copyright protected and the property of Express Elevators" );
			return false;
		});
	
	// Blockquotes
	
	$(".content blockquote").append('<span class="endquote"></span>');
	
	// Home banners
	
	if( $(".home #headerPic li").length > 1 ){
		
		setInterval( function(){
			$(".home #headerPic ul li:last").fadeTo( 1300, 0, function(){
				$(".home #headerPic ul li:last").prependTo(".home #headerPic ul");
				$(".home #headerPic ul li:first").css("opacity", "1");
			} );
		}, 6800 );
		
	}
	
});

