$(document).ready(function(){

	$("#menu ul").treeview({
		animated: "slow",
		collapsed: true,
		unique: true
	});
	
	$("a.targetBlank").attr('target', '_blank');
	
 $("a[rel='lightbox']").lightBox({
			imageLoading:			'img/lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
			imageBtnPrev:			'img/lightbox-btn-prev.gif',			// (string) Path and the name of the prev button image
			imageBtnNext:			'img/lightbox-btn-next.gif',			// (string) Path and the name of the next button image
			imageBtnClose:		'img/lightbox-btn-close.gif',		// (string) Path and the name of the close btn
			imageBlank:				'img/lightbox-blank.gif'	
  });

     // styling  input type file
    $("input.iFile").filestyle({ 
      image: "img/button_przegladaj.jpg",
      imageheight : 21,
      imagewidth : 98,
      leftimage : 312,
      width : 160
    });
    
    
	$(".sendNewVipPhoto").validate({  	
  rules: {
  			formNazwa: {
  				required: true
  			},
  			formEmail: {
  				required: true,
  	      email: true
  			},
  			formPhotoTemp: {
  				required: true
  			},
  			formTekst: {
  				required: true
  			}
  	}
  });	
  
  var elementsList = $('ul#hiddenPhotosProductList li');
  var activeElement;
  
    $("#productPhotoPagination ul").pagination(elementsList.length,
      {
        num_edge_entries: 1,
        num_display_entries: 10,
        callback: pageselectCallback,
        items_per_page:1,
        prev_text: false,
        next_text: false
      });
                

  function pageselectCallback(page_index, jq){	
    if($('#productPhoto ul li').length>0){
    
      $('#productPhoto ul li').animate({left: 205}, 1000 , '', function(){
        var tempElement = elementsList.eq(page_index);
        tempElement.css("left", -205);
        $('#productPhoto ul').empty().html(tempElement);
        tempElement.animate({left: 0}, 1000 , '', '')
      });
    }
    else{
      $('#productPhoto ul').empty().html(elementsList[page_index]);
    }
    
    activeElement = page_index;
    	    
    return false;
  }
  
  

  
    // ------------------------------------------ comment secion
    var activCommentElement = "";
    
    $("#addComment").attr('href', 'javascript:void(0);');
    $("#showComment").attr('href', 'javascript:void(0);');
    
    if($("#showComment").length>0){
      $("#showComment").parent().removeClass('greenHeadLow');
      $("#commentAjaxContent").show(); 
      activCommentElement="list";
      $.get('ajaxResponse.php?action=commentList&id_parrent='+idParrent, '', function(data){
          $("#commentAjaxContent").html(data);
      });
    }
    
    $("#showComment").click(function () {
    if(($("#showComment").parent().hasClass('greenHeadLow')) || (activCommentElement=="add")){
        $("#commentAjaxContent").show(); 
        $("#showComment").parent().removeClass('greenHeadLow');
        activCommentElement="list";
        $("#commentAjaxContent").html('<div style="text-align:center"><img src="img/lightbox-ico-loading.gif" alt="" style="margin:0px auto;" /></div>');
        
        $.get('ajaxResponse.php?action=commentList&id_parrent='+idParrent, '', function(data){
          $("#commentAjaxContent").html(data);
        });
      }
      else{
        $("#commentAjaxContent").hide(); 
        $("#showComment").parent().addClass('greenHeadLow')
        activCommentElement="";
      }
    });
    
    $("#addComment").click(function () {
    if(($("#showComment").parent().hasClass('greenHeadLow')) || (activCommentElement=="list")){
        $("#commentAjaxContent").show(); 
        $("#showComment").parent().removeClass('greenHeadLow')
        activCommentElement="add";
        
        $.get('ajaxResponse.php?action=addComment&id_parrent='+idParrent+'&breadcrumb='+breadcrumb, '', function(data){
          $("#commentAjaxContent").html(data);
        });
      }
      else{
        $("#commentAjaxContent").hide(); 
        activCommentElement="";
        $("#showComment").parent().addClass('greenHeadLow')
      }
    });
  

    // ------------------------------------------ newsletter secion
    $(".iButtonAddToNewsletter").click(function (){ 
      addToNewsletter(this); 
    });
    $(".iButtonDelFromNewsletter").click(function (){ 
      delFromNewsletter(this); 
    });

    $("form.newsletter").submit(function (){ return false }); 
    
    $("input.newsletterEmail").focus(function (){ $("input.newsletterEmail").val(""); });
    $("input.newsletterEmail").blur(function (){ 
      if($("input.newsletterEmail").val()==""){
        $("input.newsletterEmail").val("adres e-mail");
      }
     }); 
     
    // ------------------------------------------ search secion
    $("input.formSearchString").focus(function (){ $("input.formSearchString").val(""); });
    $("input.formSearchString").blur(function (){ 
      if($("input.formSearchString").val()==""){
        $("input.formSearchString").val("wpisz szukaną frazę");
      }
     }); 
  


});

  function addToNewsletter(ele){
    var dobryEmail=/^[^@]+@([a-z0-9\-]+\.)+[a-z]{2,4}$/i;
    var email = $(".newsletterEmail").val();
      
    if (dobryEmail.test(email)){
      $.get('ajaxResponse.php?action=addEmailToNewslleter&email='+email, '', function(data){
        $(".newsletterEmail").val(data);
      });
    }
    else{
      $(".newsletterEmail").val("Niepoprawny adres email !");
    }
  }
  
  function delFromNewsletter(ele){
    var dobryEmail=/^[^@]+@([a-z0-9\-]+\.)+[a-z]{2,4}$/i;
    var email = $(".newsletterEmail").val();
      
    if (dobryEmail.test(email)){
      $.get('ajaxResponse.php?action=delEmailToNewslleter&email='+email, '', function(data){
        $(".newsletterEmail").val(data);
      });
    }
    else{
      $(".newsletterEmail").val("Niepoprawny adres email !");
    }
  }
