//jQuery.noConflict();

// preload image function
$.preloadImages = function() {
  for(var i = 0; i<arguments.length; i++) {
    $("<img>").attr("src", arguments[i]);
  }
}

// delay function
$.fn.delay = function(time,func) {
	this.each(function() {
		setTimeout(func,time);
	})
	return this;
};

// Gallery image hover	
function image_hover($hover_class) {
	$('.'+$hover_class).hover(
		function() {
				$(this).find('.hover_fade').stop().animate({opacity:0.4},400);
			},
			function() {
				$(this).find('.hover_fade').stop().animate({opacity:1},400);
			});
}

$(document).ready(function() {

	
	// image loader
	
		var $gallery = true;
		var $load='a.load_image'; 
		var $delay_time=500; 
		var $hover_class='image_holder';
		
		
		// add preloader image class
		$(".loading").each(function(index) {
			$(this).addClass('img_pre_'+index);
		});
	
		$(this).delay($delay_time,function() {
	
			// id of the div containers
			var $imgContainerId = "div[class^='image_loader']";
	
			// grab the images
			var $images = $($imgContainerId+' span img');
	
			// image length
			var $max = $images.length;
	
			// remove them from DOM to prevent normal load
			$('.rm_img').remove();

			// start loading
			if($max>0) {
				LoadImage(0,$max);
			}

		// loading function handler
		function LoadImage(index,$max) {

			if(index<$max) {

				// add list to div
				$('<span id="img'+(index+1)+'"></span>').each(function() {
				   $(this).appendTo($('.image_loader '+$load).eq(index));
				});
				
				// new image object
				var $img = new Image();
		
				// current image
				var $curr = $("#img"+(index+1));
		
				// load current image
				$($img).load(function () {
			
					// hide it first + .hide() failed in safari
					$(this).css('display','none');
			
					//add alt attr
					//$(this).attr({alt: ""});
					
		 
					// insert the image into div
					$($curr).append(this);
			
					// fade it in
					$(this).fadeIn(250,function() {

						$('.img_pre_'+index).remove();

						if(index == ($max-1)) {
								// remove loading div after all images loaded then start slider
								//image_hover($hover_class); $('.roll_over').css('display','block');
								
								
						
						}else{
						  // loading next item
						  LoadImage(index+1,$max);
						}
					});
					
					

				}).error(function () {
					// if loading error remove div
					$($curr).remove();
					// try to load next item
					LoadImage(index+1,$max);
				}).attr({
					src: $($images[index]).attr('src'),
					alt: $($images[index]).attr('alt'),
					title: $($images[index]).attr('title')
				});
		   	  }
			}
		});
});
