$(document).ready(function(){
 
 //IMAGE PREVIEW
 yOffset = 260;
  xOffset = 20;
  
  // these 2 variable determine popup's distance from the cursor
  // you might want to adjust to get the right result
  
  /* END CONFIG */
  $("img#product_miniature").hover(function(e){
    this.t = this.title;
    this.title = "";
    var tmp=this.src.split("/");
    var filename=tmp[tmp.length-1];
   
    $("body").append("<p id='preview'><img src='/public/themes/frontend/gfx/pop/" + filename + "' alt='' /></p>");
    $("#preview").css("top", (e.pageY - yOffset) + "px").css("left", (e.pageX + xOffset) + "px").fadeIn("fast");
  }, function(){
    this.title = this.t;
    $("#preview").remove();
  });
  
  $("img#product_miniature").mousemove(function(e){
    $("#preview").css("top", (e.pageY - yOffset) + "px").css("left", (e.pageX + xOffset) + "px");
  });
  
  $("img#product_miniature").click(function(e){
    return false;
  });
  
  //End Image preview
  //
    // moduł opacity selection - made by J.Krolewski
    
    $("#innerNavigation li a").mouseover(function(){
        $("#innerNavigation li a").css("opacity", 0.8);
        $(this).css("opacity", 1);
    })
    
    $("#innerNavigation").mouseout(function(){
        $("#innerNavigation li a").css("opacity", 1);
    })
    
    $("a.hover").mouseover(function(){
        $("a.hover").css("opacity", 0.8);
        $(this).css("opacity", 1);
    })
    
    $("#informationModule span").mouseout(function(){
        $("a.hover").css("opacity", 1);
    })
    
    // flash implementation
    
    $('#fmMan').flash({
        swf: 'swf/video.swf',
        height: 292,
        width: 360,
        params: {
            play: true,
            loop: true,
            wmode: "transparent"
        }
    });
	
	
    // policy pop up
	
	  $("#inputDevelop").hide();
	  $(".newsletterRight fieldset .look").hover(
      function () {
	    $("#inputDevelop").fadeIn('normal');
      }
    );
	  $("#inputDevelop a").click(function(){
		$("#inputDevelop").fadeOut('normal');
		return false;
	  });

    
	  //szukajka
	  
	 
	  $('#birthday').keydown(function(event){
	    	if(event.keyCode == 13) 
			{
	    		ValidateForm();
	    		return false;
			}
	    	
	    });
	  
	  $('#searchSubmit').click(function(){
		  $('#bio_1, #bio_2, #bio_3, #plus_1, #plus_2, #plus_3').hide();
		  ValidateForm();
		  return false;
	  });
	  
	  $('.searchAnswer').click(function(){
		  
		  var answer = $(this).attr('rel');
		  searchForm(answer);
		  
		  return false;
	  });
	  
	  $('.notifAnswer').click(function(){
		 
		  var answer = $(this).attr('rel');
		  
		  if (answer == 'n') {
			  parent.history.back()
		  } else if (answer == 'y'){
			  
			  $(this).parent().fadeOut(1000);
			  $('.firstMonthsMilk, .plusContent, .bioContent, .klauzula, #bio_1_content, #plus_1_content, #ratgeberText').css('display', 'block').hide().fadeIn(500);
		  }
		  
		  return false;
	  });
	  
});

	function CalculateDiff(d1) {
	    
		var date1 = new Date(d1);
	    var date2 = new Date();
	    
	    if ( date1 > date2 ) {
	      var tmp = date1;
	      date1 = date2
	      date2 = tmp;
	    }
	    var years  = ( date2.getFullYear() - date1.getFullYear() ) * 12;
	    var months = ( date2.getMonth()    - date1.getMonth() );
	 
	   // alert( 'Delta: ' + ( years + months ) );
	    
	    return years + months;
	  }



	function searchForm(answer){
		$('#bio_1, #bio_2, #bio_3, #plus_1, #plus_2, #plus_3').hide();
		var birthday = $('#birthday').val();
		var type = ''; 
		
		var bd = birthday;
			bd = bd.split('/');
  		var month = bd[1];
  		var day = bd[0];
  		var year = bd[2];
		
		var diff = CalculateDiff(month+'/'+day+'/'+year);
		  
		if (answer == 'n') type = 'bio_'; 
		if (answer == 'y') type = 'plus_';  
		  

    	if (diff < 7){
    		 $('#'+type+'1').slideDown(500);
    	} else if (diff >= 7 && diff <= 10){
    		 $('#'+type+'2').slideDown(500);
    	} else if (diff >= 11 && diff <= 48){
    		 $('#'+type+'3').slideDown(500);
    	} else if (diff > 48){
    		 alert('Wiek dziecka przekracza 4 lat');
    	}

		return false;
	}

	

	function ValidateForm(){
		
		var birthday = $('#birthday').val();
		
		var bd = birthday;
		
		var patt1 = new RegExp(/^([0-9]{2})[\/\s]?([0-9]{2})[\/\s]?([0-9]{4})$/);
		
	  	if(patt1.test(birthday) == false){
	  		alert("Wprowadź poprawną datę (dd/mm/rrrr)");
			return false;
	  	} else {
	  		bd = bd.split('/');
	  		var month = bd[1];
	  		var day = bd[0];
	  		var year = bd[2];
	  		if (month > 12 || month == '00'){
	  			alert("Wprowadź poprawną datę (dd/mm/rrrr)");
	  			return false;
	  		}
	  		if (day > 31 || day == '00'){
	  			alert("Wprowadź poprawną datę (dd/mm/rrrr)");
	  			return false;
	  		}
	  		if (year < 1950){
	  			alert("Wprowadź poprawną datę (dd/mm/rrrr)");
	  			return false;
	  		}
	  		
	  		var date1 = new Date(month+'/'+day+'/'+year);
		    var date2 = new Date();
		    
		    if ( date1 > date2 ) {
		    	alert("Wprowadź poprawną datę (dd/mm/rrrr)");
	  			return false;
		    }
		    
		    var months = new Array();
			    months["m01"] = "31";
			    months["m02"] = "29";
			    months["m03"] = "31";
			    months["m04"] = "30";
			    months["m05"] = "31";
			    months["m06"] = "30";
			    months["m07"] = "31";
			    months["m08"] = "31";
			    months["m09"] = "30";
			    months["m10"] = "31";
			    months["m11"] = "30";
			    months["m12"] = "31";
			    
			if (day > months["m"+month]){
				alert("Wprowadź poprawną datę (dd/mm/rrrr)");
	  			return false;
			}
			
		    
	  	}
		$('#searchQuestion').slideDown(500);
		return false
	 }
	


