// Use jQuery via $j(...)
$j(document).ready(function(){
	var t = setTimeout('promotion()', 1000);
});

function promotion() {
	//Load lottery mask
	var img = new Image();
	$j(img).attr('src', 'images/lottery-mask.png');
	$j(img).addClass('mask');
	$j('#lottery-images').append(img);
	
	//Pre-load the first two images, then..
	var img = new Image();
	var img2 = new Image();
	$j(img).load(function() {
		$j(img2).load(function() {
			/***
			//Slide the first promo text in from right
			$j('#promotion-1 .promotion-text').animate({
				marginLeft: parseInt($j('#promotion-1 .promotion-text').css('margin-left'),10) == 0 ?
					$j('#promotion-1 .promotion-text').outerWidth() :
					0,
				opacity: 'toggle'
			}, 2000, function() {
				//start rotating promos
				if ($j("#mainbody .promotion").length>1) {
					thengo();
				}
				if ($j("#lottery li").length>=1) {
					display_lottery();
				}
			});
			***/
			
			//start rotating promos
			if ($j("#mainbody .promotion").length>1) {
				slideSwitch();
				$j('#quality-guarantee').delay(2000).fadeIn(1000);
				$j('#blog-post').fadeIn('slow');
			}
			
			
		}).attr('src', $j("#promotion-2").css('background-image').replace(/url\(\"?|\"?\)/gi, ''));
	}).attr('src', $j("#promotion-1").css('background-image').replace(/url\(\"?|\"?\)/gi, ''));
}

function thengo() {
	setInterval("slideSwitch()", 7000);
}

var loop = 0;
function slideSwitch() {
	if ($j("#mainbody .promotion").length>1) {
		$j("#mainbody .promotion").each(function(i) {
			if (loop==i) {
				$j(this).hide();
				$j(this).children('.promotion-text').hide();
				$j(this).css({"z-index": "2"}).fadeIn(3000);
				$j(this).children('.promotion-text').delay(2000).fadeIn(1000, function() {
					if ($j("#lottery li").length>=1) {
						display_lottery();
					}
				});
			} else {
				$j(this).css({"z-index": "1"});
				$j(this).fadeOut(3000);
			}
		});
		
		loop = loop + 1;
		
		if (loop == $j("#mainbody .promotion").length)
			loop = 0;
	}
}

var lottery_done = false;
function display_lottery() {
	if (!lottery_done) {
		$j('#lottery ul a').each(function(i) {
			$j(this).mouseenter(function() {
				$j('#lottery-images img').not(i).css({'z-index':'9997'});
				$j('#lottery-images img').eq(i).css({'z-index':'9998'});
				$j('#lottery-images .mask').css({'z-index':'9999'});
			});
		});
		
		$j('#lottery ul').slideDown(1000, function() {
			$j('#lottery-images img').each(function(i) {
				if ($j(this).hasClass('mask')) {
					//Do nothing
				} else {
					$j(this).css({'z-index':'9998'});
					$j(this).delay((i*1000)).animate({
						marginTop: parseInt($j(this).css('margin-top'),10) == 0 ?
							$j(this).css('margin-top') :
							0
					}, 2000, function() {
						$j(this).css({'z-index':'9997'});
						if (i==($j('#lottery-images img').length-2)) {
							lottery_done = true;
							slideSwitch();
							thengo();
						}
					});
				}
			});
		});
	}
}
