$(document).ready(function(){

  equalHeight();
           
  function equalHeight(){
    if($("#content ul#item_brand li").length>2){
      elementHeight = 0;
      lastElementWithHeight = (Math.floor($("#content ul#item_brand li").length/3)*3)-1;
      
      $("#content ul#item_brand li").each(function(index) { 
        indexTemp = index+1;  
        newHeight = $("#content ul#item_brand li:eq("+index+")").height();
        if(newHeight > elementHeight){elementHeight = newHeight;}
        
        if(indexTemp%3==0 && index!=0){
          $("#content ul#item_brand li:eq("+(index-2)+")").css('height', elementHeight+'px');
          $("#content ul#item_brand li:eq("+(index-1)+")").css('height', elementHeight+'px');
          $("#content ul#item_brand li:eq("+index+")").css('height', elementHeight+'px');
          elementHeight = 0;
        }          
      });
        
      elementHeight = 0;
      $("#content ul#item_brand li:gt("+lastElementWithHeight+")").each(function(index) {   
        newHeight = $("#content ul#item_brand li:eq("+(lastElementWithHeight+index+1)+")").height(); 
        if(newHeight > elementHeight){elementHeight = newHeight;}
      });  
      $("#content ul#item_brand li:gt("+lastElementWithHeight+")").css('height', elementHeight+'px');
      
      $("#content ul#item_brand li").find("p").css({position: 'absolute', bottom: '0px'}); 
      
      if($("#content ul#item_brand li p a.wiecej").length>0){   
        $("#content ul#item_brand li h2").next().css({position: 'absolute', bottom: '50px'});
      }   
    }  
  }
  
  
  // first example
	$("#userTreeMenu").treeview({
		animated: "fast",
		collapsed: true,
		unique: true
	});
	
	
	
  // ------------ newsletter form
	$("form.newsletterForm").submit(function() {	
	  var dobryEmail = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    var email = $("#iTextEmailNewsletter").val();
    
    if(dobryEmail.test(email)){
      $.get('ajaxResponse.php?action=addEmailToNewslleter&email='+email, '', function(data){
        $("#iTextEmailNewsletter").val(data);
      });
    }
    else{
      $("#iTextEmailNewsletter").val("Niepoprawny adres e-mail !");
    }
    return false;     
  });




  // start slideshow 
  $('#box_left_0 .photo_list ul').cycle({ 
    fx: 'fade',  
    timeout: 0, 
    speed: 500, 
    prev: '#box_left_0 .left', 
    next: '#box_left_0 .right'
  }); 
  
  // start slideshow 
  $('#box_left_1 .photo_list ul').cycle({ 
    fx: 'fade',  
    timeout: 0, 
    speed: 500, 
    prev: '#box_left_1 .left', 
    next: '#box_left_1 .right'
  }); 
  
  // start slideshow 
  $('#box_left_2 .photo_list ul').cycle({ 
    fx: 'fade',  
    timeout: 0, 
    speed: 500, 
    prev: '#box_left_2 .left', 
    next: '#box_left_2 .right'
  }); 
        
  // ------------ load fancybox
  $("a[rel]='fan'").fancybox({
	 overlayOpacity: 0.8
	});
});
      

// main banner slide show   
  function slideSwitch() {
    var $active = $('#main_banner ul.pic li.active');
  
    if ( $active.length == 0 ) $active = $('#main_banner ul.pic li:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next() : $('#main_banner ul.pic li:first');
      
    $active.addClass('last-active');
      
    var index = $('#main_banner ul.ico li').index($('#main_banner ul.ico li.active'));
    if(index == $('#main_banner ul.ico li').length-1){
      index=-1;
    }
    
    $('#main_banner ul.ico li').removeClass('active');
    $activeLi = $('#main_banner ul.ico li:eq('+(index+1)+')');
      
    $next.css({opacity: 0.0})
      .addClass('active')
      .animate({opacity: 1.0}, 500, function() {
          $active.removeClass('active last-active');
          $activeLi.addClass('active');
      });
  } 
  
  
  function goTo(next) {
     
    if($('#main_banner ul.ico li:eq('+next+')').hasClass('active')) return false;
    
    $activeLi = $('#main_banner ul.ico li:eq('+next+')');
    $('#main_banner ul.ico li').removeClass('active');
     
    clearTimeout(timeout);
    $active = $('#main_banner ul.pic li.active');
    $active.addClass('last-active');
           
    $('#main_banner ul.pic li:eq('+next+')')
      .css({opacity: 0.0})
      .addClass('active')
      .animate({opacity: 1.0}, 500, function() {
        $active.removeClass('active last-active');
        $activeLi.addClass('active');     
      });
        
    timeout = setInterval( "slideSwitch()", 5000 );
  }
  
  $(function() {
    if ($('#main_banner ul li').length > 1 ){
      $('#main_banner').append('<ul class="ico" style="position:absolute; z-index:100;"></ul>');
      
      $('#main_banner ul li').each(function(i) {
        var $el = $(this);        
        $('#main_banner').parent().find("ul.ico").append('<li>'+i+'</li>');    
        $('#main_banner').parent().find("ul.ico li:eq("+i+")").bind('click', function(){return goTo(i)});
      });
      
      $('#main_banner ul.ico li:first').addClass('active');
      
      timeout = setInterval( "slideSwitch()", 5000 );
    }
  });
// end main banner slide show

// Flash map interface
	function mapClick($id, $name)
	{
	  $name = str_replace(["Ą", "Ę", "Ó", "Ł", "Ż", "Ź", "Ć", "Ń", "Ś", "ę", "ł", "ż", "!", "ź", "ą", "ć", "ś", "ń", "ó", "„" , "“", " ", ",", "_", "/", "?", '"', '\'', "&", "+", "%"], ["A", "E", "O", "L", "Z", "Z", "C", "N", "S", "e", "l", "z", "", "z", "a", "c", "s", "n",  "o", "", "", "-", "-", "-", "-", "", "", "", "", "", ""], $name);
	//	alert("MapClicked, id[" + $id + "], name["+$name+"]");
	 $lang = $("#pageLangValue").text();
		if($name=="internetowe"){
      window.location.href = '/'+$lang+'/gdzie-kupic';
    }
    else{
      window.location.href = '/'+$lang+'/gdzie-kupic/'+$name+'-'+$id;
    }		
	};
	
  function str_replace (search, replace, subject, count) {
    f = [].concat(search),
    r = [].concat(replace),
    s = subject,
    ra = r instanceof Array, sa = s instanceof Array;    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
     for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}        }
    }
    return sa ? s : s[0];
  }
// end Flash map interface
