var _current_background = 1,
	_waiting_seconds = 0,
	_switch_timer = null;

function resizeBg() {

	if (jQuery(window).width() < 960) {
		jQuery("img.bg").css("margin-left", "36px").css("margin-right", "36px");
	} else {
		jQuery("img.bg").css("margin-left", "auto").css("margin-right", "auto");
	}
}
	
function initBackgroundImages() {
	
	var bg_images = jQuery('.bg'),
		bg_image_count = +bg_images.length;
	
	if (bg_image_count === 0) {
		return;
	}

	jQuery(bg_images[0]).addClass('selected');

	resizeBg();
}

function backgroundSwitch(to)
{
	var fade_speed = 'slow',
		fade_delay = 400,
		this_id = to;
		
	_waiting_seconds = 0;
	
	if (jQuery("img.bg").length < 2) {
		if (_switch_timer !== null) {
			clearInterval(_switch_timer);
		}
		return;
	}
	
	if (to === 'next') {
		if (to === 'next' && _waiting_seconds < 7) { //wait 7 sec.
			_waiting_seconds++;
			return;
		}
		_current_background++;
		if (_current_background > 3) {
			if (_switch_timer !== null)
			{
				_current_background = 1;
				clearInterval(_switch_timer);
			}
		}
		to = 'page' + _current_background;
	}
	
	//change paging image
	jQuery("#paging button").removeClass();
	jQuery(this).addClass('selected');
	//hide writing images
	jQuery("#content p.SEOText").fadeOut(fade_speed);
	jQuery("#container img.bgAccesory").fadeOut(fade_speed);
	jQuery("div.bgRibbon").fadeOut(fade_speed);
	
	jQuery("#paging button#" + to).addClass('selected');
	jQuery("img.bg:visible").delay(fade_delay).fadeOut(fade_speed, function () {
		switch (this_id) {
		case "page1":
			_current_background = 1;
			jQuery("#content p#homeWriting1").delay(fade_delay).fadeIn(fade_speed);
			jQuery("img#bg1").fadeIn(fade_speed);
			break;
		case "page2":
			_current_background = 2;
			jQuery("#content p#homeWriting2").delay(fade_delay).fadeIn(fade_speed);
			jQuery("img#bg2").fadeIn(fade_speed);
			break;
		case "page3":
			_current_background = 3;
			jQuery("#content p#homeWriting3").delay(fade_delay).fadeIn(fade_speed);
			jQuery("img#bg3").fadeIn(fade_speed);
			break;
		}	
	});
}

function setEventHandlers() {
	
    jQuery("#perfume")
	    .mouseenter(function (e) {
			jQuery("#popupPerfume").stop(true, true).slideDown();
	    })
	    .mouseleave(function (e) {
			jQuery("#popupPerfume").delay(300).slideUp();
	    });
	    
	jQuery("#shoppingBagButton")
	    .mouseenter(function (e) {
			jQuery("#popupShoppingBag").stop(true, true).slideDown();
	    });
   
	jQuery("#shoppingBagHeader #close")
	    .click(function (e) {
			jQuery("#popupShoppingBag").slideUp();
	    });
				
	jQuery("#paging button")
		.click(function (e) {
			if (_switch_timer !== null) {
				clearInterval(_switch_timer);
			}
			if (jQuery(this).hasClass('selected')) {
				return;
			}
			backgroundSwitch(jQuery(this).attr("id"));
		});
		
	jQuery(".order_submit")
	    .click(function (e) {
			var qty = jQuery(this).prev().val(),
				product_id = jQuery(this).prev().prev().val();
			location.href = '/checkout/cart/add/?product=' + product_id + '&qty=' + qty;
	    })
		.hover(
			function (e) {
				jQuery(this).css('background-position', '-68px 0');
			},
			function (e) {
				jQuery(this).css('background-position', '0 0');
			}
		)
}

jQuery(function () {
	
	// Move trace element from content to where HintCreative put it.
	jQuery('#trace').appendTo('#navMain');
		
	//  Move images to where HintCreative had them.
	jQuery('body').append(jQuery('.bg'));
	jQuery('#container').append(jQuery('.bgAccesory'));
	
	// Magento likes to add/remove attributes for elements defined in the CMS section.
	// Sometimes it removes the empty value setting, which makes 'submit' show up.  Prevent this.
	jQuery('#content input.order_submit').val('');
	
	setEventHandlers();	
});

jQuery(window).load(function () {    
	
	initBackgroundImages();
	
	jQuery(window).resize(function () {
			resizeBg();
		}).trigger("resize");
});

