//Image pre-loader plug-in
jQuery.preloadImages = function() {
var a = (typeof arguments[0] == 'object')? arguments[0] : arguments;
for(var i = a.length -1; i > 0; i--) {
jQuery("<img>").attr("src", a[i]);
}
}

// Pre-load the images
$.preloadImages("wp-content/themes/manamansour/images/loader.gif");
$.preloadImages("wp-content/themes/manamansour/images/light.png");

$(document).ready(function(){
   
	onload();
	$('img#light_spot').fadeIn(5000);
	
	//MATTHEW FINLASON STUFF ******************
	
	
	function fnStartGrid(sUrl) {
	var nScript = document.createElement('script');nScript.setAttribute('language','JavaScript');nScript.setAttribute('src',sUrl);document.body.appendChild(nScript);
	}

$('#grid_overlay').click(function(){ 
	fnStartGrid('http://www.sprymedia.co.uk/design/grid/media/js/grid-loader.js');
	return false;
});




$('ul.nav li a.inactive').click(function () {
	$('li#coming_soon a h4').fadeIn(2000).fadeOut(2000);
	return false;
})

//$('#content').css('display', 'none').fadeIn(400);

	
	
	//END OF MATTHEW FINLASON STUFF ******************
	
	
	

	
	

	$('span.gallery-icon a').click(function(){
		
		
		if($('#loading').hasClass('active')){
			return false;
		}else{
			
			$('<div id="loading" class="active"></div>').html("LOADING").appendTo('#gallery_canvas').fadeIn(1000);
			var link = $(this).attr("href");

			var img = new Image();

			$(img).load(function() {
					$(this).hide();
					//imgHeight = $('#canvase_image img').height();
					//alert(imgHeight);
					$('#gallery_canvas').append(this);
					$('#loading').fadeOut(2000,function(){$(this).remove();});
					$(this).css({"height": 'auto', "width": 620});
					iHeight = $(this).height();
					lHeight = $('#canvas_image').height();
					if(lHeight == iHeight) {
							$(this).fadeIn(2000);
					} else {
							tempImg = $(this);
							$('#gallery_canvas').animate({height: iHeight}, 500).animate({width: 620}, 500, function() {
										$(tempImg).fadeIn(1000);
									});
							$('#slideshow_gallery').animate({height: iHeight+100}, 500)
							}


					$("#canvas_image:last").fadeOut(1000,function(){$(this).remove();});						
					})
				.attr({src: link, id: "canvas_image"})

		//	$("#canvas_image:last").fadeOut(2000).remove();	


			return false;
			
		}
		
		
	});
	


	
	
	function onload(){
		
		//This if() checks if there are images loaded in the gallery, if not, it hides the gallery, and changes the width of the text.	
		
		$('img#light_spot').fadeIn(500);
		
		
		if($('div.gallery-item').length < 1){
			$('#slideshow_gallery').hide();
			$('div.entry').css('width','620px')
		}else{
			var link = $('span.gallery-icon a:first').attr("href");



			$('<div id="loading" class="active"></div>').html("LOADING").appendTo('#gallery_canvas').fadeIn();
			var img = new Image();

			$(img)
				.hide()
				.load(function() {
					$('#gallery_canvas').append(img).css("height","270px");
					$('#loading').fadeOut(2000,function(){$(this).remove();});						
					})
				.attr({src: link, id: "canvas_image"})
				.fadeIn(2000);
			
		}
		
		
		
			//hides the projects categories pane on load.
		//	$('div#projects').hide();
			$('div#project_art').hide();
			$('div#project_graphic').hide();
			$('div#project_spatial').hide();
	}


	
	


	
	
//	$('span.gallery-icon a').attr("href")

 });