var objProducts = {sectionMenuMain: 0, sectionMenuDejour: -980, sectionMenuDynamique: -1960, sectionMenuRomantique: -2940, sectionMenuZerlina: -3920};
var objBackgrounds = {sectionMenuMain: 'bg_home_main.jpg', sectionMenuDejour: 'bg_home_dejour.jpg', sectionMenuDynamique: 'bg_home_dynamique.jpg', sectionMenuRomantique: 'bg_home_romantique.jpg', sectionMenuZerlina: 'bg_home_zerlina.jpg'};
var objColors = {sectionMenuMain: '#840654', sectionMenuDejour: '#ffffff', sectionMenuDynamique: '#fdbc56', sectionMenuRomantique: '#c4295c', sectionMenuZerlina: '#fdf3d7'};
var cache = [];
var currentBg = '';
var productGroupSelected = 0;

//(function($) { 
//
//})(jQuery);


$(document).ready(function() {
    $("ul li:last-child").addClass("last");
    $("ul li:first-child").addClass("first");
	
	$("#odourElimination").fancybox();
	
	$('#sectionMenu a, #mainArticle a').click(sectionMenuClick);
	$('.productGroup #btnPrev, .productGroup #btnNext').click(productGroupClick);
	$('.flowerToFind').click(findFlower);
	$('.linkMore').click(moreClick);
	$(window).resize(windowResize);
	
	function sectionMenuClick(){
		var id = $(this).attr('name');
		if(currentBg != id){
		$('#sectionMenu li').removeClass('selected');		
		//document.title = id;
		$('section').animate({left: objProducts[id]+'px'}, 500);
		$('#articleBackground').animate({
			opacity: 0
		  }, 500, function() {
			  	$('#articleBackground').css('background', objColors[id]+' url(i/backgrounds/'+objBackgrounds[id]+') no-repeat 0 0');
				$('#articleBackground').animate({
				opacity: 1
			  }, 500, function() {
				// Animation complete.
			  });
		  });
		$('#sectionMenu li#'+id).addClass('selected');
		//$(this).parent().addClass('selected');
		}
		currentBg = id;
		return false;
	}
	
	function productGroupClick(){
		
		var count = $('.productWrapper').length;
		var arrPos = [];
		for(i = 0; i < count; i++){
			arrPos.push(i * 980);
		}
		if($(this).attr('id') == 'btnPrev'){
			if(productGroupSelected > 0){
				productGroupSelected--;
				$('#btnPrev').css('display', 'block');
				$('#btnNext').css('display', 'block');
			}else{
				productGroupSelected = 0;
				$('#btnPrev').css('display', 'none');
			}
		}else{
			if(productGroupSelected < count - 1){
				document.title += ' < ';
				//alert(productGroupSelected+' < '+count);
				productGroupSelected++;
				$('#btnNext').css('display', 'block');
				$('#btnPrev').css('display', 'block');
			}else{
				document.title += ' > ';
				productGroupSelected = count - 1;
				$('#btnNext').css('display', 'none');
			}
		}
		//alert(arrPos[productGroupSelected]);
		$('#productsWrapper').animate({left: -arrPos[productGroupSelected]}, 500);
		return false;
	}
	
		
	  $.preloadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
		  var cacheImage = document.createElement('img');
		  cacheImage.src = arguments[i];
		  cache.push(cacheImage);
		}
	  }
	  
	  function turnOnFlower(id){
		  $('#flowerFound'+id).addClass('found');
	  }
	  
	  function turnOffFlower(id){
		  $('#flowerFound'+id).removeClass('found');
	  }
	  
	  function isTurnedOn(id){
		  if($('#flowerFound'+id).hasClass('found')){
			  return true;
		  }else{
			  return false;
		  }
	  }
	  
	  function findFlower(){
		  var id = $(this).attr('id').substr(6);//id's like 'flower1', 'flower2', etc.
		  $.ajax({
				type: "GET",
				url: "/ajax/findflower?flowerId=" + id,
				data: "",
				success: function(msg){
					turnOnFlower(id);
					hideSingleFlower(id);
					if (msg!='') {
						hideFlowers(msg);
					}
				}
			});
			return false;
	  }
	  
	  function moreClick(){
		  if($('.pMore').css('height') == '70px'){
			  $('.pMore').css('height', 'auto');
			  $(this).html('&laquo; меньше');
		  }else{
			  $('.pMore').css('height', '70px');
			  $(this).html('больше &raquo;');
		  }
		  return false;
	  }
	  
	  
	  
	  for(i = 0; i < objBackgrounds.length; i++){
	  	jQuery.preloadImages('i/backgrounds/'+objBackgrounds[i]);
	  }
	
	function switchStyleOn(styleName)
	{
			$('head link').each(function(i)
			{
					//this.disabled = true;
					if (this.getAttribute('title') == styleName) this.disabled = false;
			});
			//createCookie('style', styleName, 365);
	}
	
	function switchStyleOff(styleName)
	{
			$('head link').each(function(i)
			{
					//this.disabled = true;
					if (this.getAttribute('title') == styleName) this.disabled = true;
			});
			//createCookie('style', styleName, 365);
	}
	
	function windowResize() {
		var h = $(window).height();
		//document.title = h;
		if(h < 810){
			switchStyleOn('style_below750');
		}else{
			switchStyleOff('style_below750');
		}
		//setTimeout(cufonReplace, 1000);
	}
	
	windowResize();
	
		
        //var c = readCookie('style');
       // if (c) switchStylestyle(c);
});

	function hideFlowers(lnk){
		  $('#asideFlowers em').html('<a href="'+lnk+'">Поздравляем! Нажми здесь, чтобы получить специальный подарок.</a>');
	  }
	
	function hideSingleFlower(id) {
		$('#flower'+id).remove();
	}
	
	
