var car;

function imgpreload(imgs,settings)
{
	if (settings instanceof Function) { settings = {all:settings}; }

	if (typeof imgs == "string") { imgs = [imgs]; }

	var loaded = [];
	var t = imgs.length;
	var i = 0;

	for (i; i<t; i++)
	{
		var img = new Image();
		img.onload = function()
		{
			loaded.push(this);
			if (settings.each instanceof Function) { settings.each.call(this); }
			if (loaded.length>=t && settings.all instanceof Function) { settings.all.call(loaded); }
		};
		img.src = imgs[i];
	}
}


if (typeof jQuery != "undefined")
{
	(function($){

		$.imgpreload = imgpreload;

		$.fn.imgpreload = function(settings)
		{
			settings = $.extend({},$.fn.imgpreload.defaults,(settings instanceof Function)?{all:settings}:settings);

			this.each(function()
			{
				var elem = this;

				imgpreload($(this).attr('src'),function()
				{
					if (settings.each instanceof Function) { settings.each.call(elem); }
				});
			});

			var urls = [];

			this.each(function()
			{
				urls.push($(this).attr('src'));
			});

			var selection = this;

			imgpreload(urls,function()
			{
				if (settings.all instanceof Function) { settings.all.call(selection); }
			});

			return this;
		};

		$.fn.imgpreload.defaults =
		{
			each: null 
			, all: null 
		};

	})(jQuery);
}


$(document).ready(function() {	
	$('#content .image1').cycle({ 
		fx:     'fade', 
		prev:   '#prev', 
		next:   '#next', 
		timeout: 16000,
		speed:  2000,
		delay: -14000
		
	});
	$('#content .image2').cycle({ 
		fx:     'fade', 
		prev:   '#prev', 
		next:   '#next', 
		timeout: 16000,
		speed:  2000,
		delay: -10000
	});
	$('#content .image3').cycle({ 
		fx:     'fade', 
		prev:   '#prev', 
		next:   '#next', 
		timeout: 16000,
		speed:  2000,
		delay: -6000
	});
	$('#content .image4').cycle({ 
		fx:     'fade', 
		prev:   '#prev', 
		next:   '#next', 
		timeout: 16000,
		speed:  2000,
		delay: -2000
	});	
	/*if ($('.homepage #images ul').length)
		$('.homepage #images ul').jcarousel({
			wrap: 'circular',
			initCallback: mycarousel_initCallback,
			scroll: 1
		});
	*/
	if ($('.homepage #images .image div').length)
	{
		$('.homepage #images .image div').cycle({ 
			fx:     'none', 
			timeoutFn: calculateTimeout
		});	
	}
	
	if ($('#aboutimages2 .image div').length)
	{
		$('#aboutimages2 .image div').cycle({ 
			fx:     'none', 
			timeoutFn: calculateTimeout
		});	
	}
	
	if ($('#aboutimage div').length)
	{
		$('#aboutimage div').cycle({ 
			fx:     'scrollHorz', 
			timeoutFn: calculateTimeout,
			speed:  2000,
			easeIn:  'easeOutExpo', 
		    easeOut: 'easeOutExpo'
		});	
	}

	$('.category a img').hover(function() { $(this).parent().parent().find('h2 a').addClass('active');}, function() {$(this).parent().parent().find('h2 a').removeClass('active');})
	$("#content .product .images a").click(change_photo);
	$("#content .product .image .arrow").click(arrow)	
});

function calculateTimeout(currElement, nextElement, opts, isForward) { 
	time = ( (Math.floor(Math.random()*7000)) + 2000 ) ;
	return time;
} 

function next_slide()
{
	car.next();	
	time = ( (Math.floor(Math.random()*7)) + 2 ) * 1000;
	setTimeout(next_slide,time)
}

function mycarousel_initCallback(carousel) {
	
	car=carousel;
	time = ( (Math.floor(Math.random()*7)) + 2 ) * 1000;
	setTimeout(next_slide,time)
	//carousel.next(carousel);
};


function arrow()
{
	hover=$("#content .product .images a.active");
	next=$("#content .product .images a.active").next();
	last=$("#content .product .images a:last");
	if ($(hover).hasClass('last')) {next=$("#content .product .images a:first");}
	var im=new Image();
	im.src=$(next).attr('href');
	image = $(next).attr('href');
	$("#content .product .image .ajax").show();
	$.imgpreload($(next).attr('href'),function() {
		$("#content .product .image .ajax").hide();
		$("#content .product .image img").fadeOut(1000,function() {
			$("#content .product .image img").attr('src',image).attr('width',im.width).attr('height',im.height);			
			$("#content .product .image img").fadeIn(1000);
			});
	});
	$(hover).removeClass('active');
	$(next).addClass('active');
	return false;
}

function change_photo()
{
	$("#content .product .images a").removeClass('active');
	$(this).addClass('active');
	var im=new Image();
	im.src=$(this).attr('href');
	image = $(this).attr('href');
	$("#content .product .image .ajax").show();
	$.imgpreload($(this).attr('href'),function() {
		$("#content .product .image .ajax").hide();
		$("#content .product .image img").fadeOut(1000,function() {
			$("#content .product .image img").attr('src',image).attr('width',im.width).attr('height',im.height);			
			$("#content .product .image img").fadeIn(1000);
			});
	});
	return false;
}

function pausecomp(millis)
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < millis);
} 
