// JavaScript Document

var upto,
	fridayonly,
	biggest,
	brf,
	brefbg,
	big,
	red,
	friday,
	kettle,
	save20,
	r80,
	pampers,
	save90,
	r100,
	more;

var allLoaded = false;
var imageCount = 0;
var totalImages = 0;
var loaderSteps;
var loaderWidth = 1;

$(document).ready(init);
$(window).load(callAnim);

function callAnim() {
	animate();
}

function init(e) {
	// Let's set some variables
	upto = $('.up-to');
	fridayonly = $('.friday-only');
	biggest = $('.biggest');
	brf = $('.brf');
	brefbg = $('.bref-bg');
	big = $('.big');
	red = $('.red');
	friday = $('.friday');
	kettle = $('.kettle');
	save20 = $('.save-20');
	r80 = $('.r80');
	pampers = $('.pampers');
	save90 = $('.save-90');
	r100 = $('.r100');
	more = $('.viewdeals');

	upto.hide();
	biggest.hide();
	fridayonly.hide();
	more.hide();
	big.hide();
	red.hide();
	friday.hide();

	preload();
}

function preload() {
	totalImages = $('.wrapper img').length;
	loaderSteps = 100/totalImages;
	$('.wrapper img').each(function(a,b) {
		var img = new Image();
		$(img).attr('src', $(b).attr('src'));
		if (img.complete || img.readyState === 4) {
			loadOne();
		} else {
			$(img).load(loadOne);
		}

	})
	//$('.wrapper img').load(loadOne);
}

function loadOne() {
	imageCount++;
	loaderWidth+=loaderSteps;
	$('.preloader').css('width', loaderWidth+"%");
	if (imageCount == totalImages) {
		$('.preloader').css('width', "100%");
		$('.preloader').fadeOut();
		animate();
	}
}

function animate() {
	// Friday Only
	fridayonly.show();
	fridayonly.animate({left: '180', top: '70', width: '60%', opacity: 1}, 800).delay(600).fadeOut(200);

	window.setTimeout(function() {
		biggest.show();
		biggest.animate({left: '180', top: '100', width: '60%', opacity: 1}, 800).delay(600).fadeOut(200);
	}, 1600);

	window.setTimeout(function() {
		brf.animate({top: '0'}, 600);
	}, 2900);

	window.setTimeout(function() {
		big.css({top: 27, left: 276, width: 10}).show().animate({top: '27', left: '161', width: '240', opacity: 1}, 500);
	}, 3300);

	window.setTimeout(function() {
		red.css({top: 136, left: 276, width: 10}).show().animate({top: 136, left: 152, width: 266, opacity: 1}, 500);
	}, 3900);

	window.setTimeout(function() {
		friday.css({top: 250, left: 276, width: 10}).show().animate({top: 250, left: 148, width: 270, opacity: 1}, 500);
	}, 4500);

	window.setTimeout(function() {
		upto.css({top: 420, left: 474, width: 10}).show().animate({top: 400, left: 355, width: 248, opacity: 1}, 400);
	}, 4900);

	window.setTimeout(function() {
		kettle.css({left: -350}).show().animate({left: 13}, 600);
	}, 6000);

	window.setTimeout(function() {
		r80.css({left: -220}).show().animate({left: 30}, 600);
	}, 6700);

	window.setTimeout(function() {
		save20.css({left: -120}).show().animate({left: 130}, 600);
	}, 6800);

	window.setTimeout(function() {
		pampers.css({left: 1000}).show().animate({left: 650}, 600);
	}, 7500);

	window.setTimeout(function() {
		r100.css({left: 1000}).show().animate({left: 670}, 600);
	}, 8200);

	window.setTimeout(function() {
		save90.css({left: 1000}).show().animate({left: 790}, 600);
	}, 8300);

	window.setTimeout(function() {
		upto.fadeOut(400);
	}, 9000);

	window.setTimeout(function() {
		more.css({left: 454, top: 420, width: 10}).show().animate({left: 335, top: 400, width: 290, opacity:1}, 400);
	}, 9200);
}