var timeOut;
var timeOutRecom;
var timeNews;
var timeBoxProf;
var timeOutSetTime = 6000;

$(document).ready(function(){

$(".question").click(function(){
  var _hrefTitle = $(this).attr("href");
  _index = _hrefTitle.search("&name=");
  _hrefTitle = _hrefTitle.substr(_index+6);
  var tmp = $(this).attr("href");
  var title = Url.encode(_hrefTitle);
  _index = tmp.search("&name=");
  tmp = tmp.substr(0,_index);
  $(this).attr("href",tmp+"&name="+title);
})

/*czyszczenie cookie do porównywania*/
$("#page #wrap .content .box950 .wrapAll .text ul li a").click(function(){
  Delete_Cookie('Compare','/','location.host');
});


  $("input#mailformsword").val("wyszukaj");
  $("input#mailformsword").focus(function(){if($("input#mailformsword").val() == "wyszukaj") $(this).val("");});
  $("input#mailformsword").blur(function(){if($("input#mailformsword").val() == '') $(this).val("wyszukaj");});
  
  $("input#mailformsword").keypress(function (e) {
    if (e.which == 13){
      $("#searchTop").click();
      return false;
    }
  });
  
  $("input#mailformsword2").keypress(function (e) {
    if (e.which == 13){
      $("#searchTop2").click();
      return false;
    }
  });
  
  $(".tx-indexedsearch-searchbox-button").click(function(){
    $(this).parent().parent().attr("action","index.php?id=27&no_cache=1&search="+$(this).parent().find("input[type='text']").val());
  });
  
  if($("#mailformtytu_wiadomoci").attr("name") != '' && $("#mailformtytu_wiadomoci").attr("name") != undefined){
    var _hrefTitle = location.search;
    _index = _hrefTitle.search("&name=");
    if(_index > 0){
      _hrefTitle = _hrefTitle.replace("%20"," ").substr(_index+6);
      _hrefTitle = Url.decode(_hrefTitle);
      $("#mailformtytu_wiadomoci").val(_hrefTitle.toString())
      $("#mailform_imie").focus();
    }
  }
   
  /* Przycisk nowość belka NEWS */
  $(".boxNews .title").click(function(){
    window.location = "/index.php?id=23";
  });
  
  /*ROtacja newsów*/
  timeNews = setTimeout("rotateNews()",timeOutSetTime);
  
  

  $('input[type=checkbox]').checkbox();
  
  $(".selectBox .titleSearch a").toggle(function(){
      $(this).parent().next().hide();
      $(this).addClass("open");
    },function(){
      $(this).parent().next().show();
      $(this).removeClass("open");
  });
  $(".openClose").toggle(function(){
      $(this).parent().find("ul").hide();
      $(this).parent().find(".details").hide();
      $(this).parent().find(".deleteAll").hide();
      $(this).addClass("closeOpen");
    },function(){
      $(this).parent().find("ul").show();
      $(this).parent().find(".details").show();
      $(this).parent().find(".deleteAll").show();
      $(this).removeClass("closeOpen");
  });
  
  /*--------  opacity na Lightbox-ise  ----------------*/
  /*
  $(".lightbox img").css("opacity","0.7");
  var _fadeTo = $(this) ? '1' : '0.7';
  $(".lightbox img").hover(function(){
			$(this).fadeTo('fast',1);		
  },function() { 
    $(this).fadeTo('fast',0.7);}
  );
  */
  
   /*--------  rotacyjne galeriie ----------------*/
  $(".boxNews ul.news li:first").addClass("active").show();
  $(".box370 ul.wrapAll li:first").addClass("active").show();
  $(".galleryLightBox li:first").addClass("active").show();
  $(".profSite .boxLeft ul li:first").addClass("active").show();
  $(".profSite .boxRight ul li:first").addClass("active").show();
  
  $(".boxNews .next, .boxNews .prev").click(function(){
    clearTimeout(timeNews);
    if(!$(this).hasClass("inactive")){
      setList($(this).parent().find("ul"),$(this), 100);
    } 
    timeNews = setTimeout("rotateNews()",timeOutSetTime);
  });
  $(".box370 .next, .box370 .prev").click(function(){
  clearTimeout(timeOutRecom);
    if(!$(this).hasClass("inactive")){
      setList($(this).parent().next(),$(this), 500);
      timeOutRecom = setTimeout("checkClickGalleryRecom()",timeOutSetTime);
    } 
  });
  $(".rightContent .next, .rightContent .prev").click(function(){
    clearTimeout(timeOut);
    if(!$(this).hasClass("inactive")){
      setList($(this).parent().find("ul"),$(this), 700);
      timeOut = setTimeout("checkClickGallery()",timeOutSetTime);
    } 
  });
  
  $(".profSite .boxLeft .next, .profSite .boxLeft .prev").click(function(){
    if(!$(this).hasClass("inactive")){
      setList($(this).parent().find("ul"),$(this), 700);
      objClick($(this).parent().find("ul li.active"));
    } 
  });
  
  $(".profSite .boxLeft ul li a").click(function(){return false;})
  
  $(".profSite .boxRight .next, .profSite .boxRight .prev").click(function(){
    clearTimeout(timeBoxProf);
    if(!$(this).hasClass("inactive")){
      setList($(this).parent().find("ul"),$(this), 700);
    } 
  });
  timeOutRecom = setTimeout("checkClickGalleryRecom()",timeOutSetTime);
  timeOut = setTimeout("checkClickGallery()",timeOutSetTime);
  
  /* -------  usuwanie zbędnych znaczników  ------------*/
  $(".boxNews ul.news a:first-child, .box ul.wrapAll a:first-child").remove();
  
  for(var i=0; i<350; i++){
    var obj = $("#c"+i);
    $(obj).remove();
  }
  
  /* -------  Strona Profesjonalne SPA  ------------*/
  
  
  timeBoxProf = setTimeout("rotateBoxProf()",timeOutSetTime);
  
  $(".profSite .boxRight ul li a").click(function() {return false;})
  $(".profSite .boxRight .boxListProf:first, .profSite .textContent .boxListProf:first").show().addClass("activeList");
  
  
  function objClick(obj){
    clearTimeout(timeBoxProf);
    var last = false;
    var number = $(".boxLeft ul li").index(obj);
    if($(obj).is(":last-child")){
      last = true;
    }
	  showProfBox(number,last);
  }
    
  
  function showProfBox(number,last){
	 $(".profSite .boxRight .boxListProf, .profSite .textContent .boxListProf").hide().removeClass("activeList");
	 $(".profSite .boxRight .navGalleryProfRight").removeClass("inactive");
	 $(".profSite .boxRight .prev").addClass("inactive");
	 $(".profSite .boxLeft .navGalleryProf").removeClass("inactive");
	 $(".profSite .boxLeft ul li").removeClass("active").hide();
	 $(".profSite .boxLeft ul li").eq(number).addClass("active").show();
	 if(number == 0){
    $(".profSite .boxLeft .prev").addClass("inactive");
   }
   if(number > 0 && last == true){
    $(".profSite .boxLeft .next").addClass("inactive");
    last = false;
   }
	 $(".profSite .boxRight .boxListProf").eq(number).show().addClass("activeList");
	 $(".profSite .boxRight .boxListProf li").removeClass("active").hide();
	 $(".profSite .boxRight .boxListProf").eq(number).children("li:first").addClass("active").show();
	 $(".profSite .textContent .boxListProf").eq(number).show();
	 timeBoxProf = setTimeout("rotateBoxProf()",timeOutSetTime);
  }

  

  /* -------  zakładki na stronie produkty  ------------*/
  $("ul.menuTab li").click(function() {
		$("ul.menuTab li").removeClass("active");
		$(this).addClass("active");
		$(".box710 .wrapAllBox").removeClass("activeBox");
		var activeTab = $(this).find("a").attr("href");
		$(activeTab).addClass("activeBox");
		return false;
	});
	
	
	 /* -------  do linku dodawanie parametru faq np.: link_1_2  ------------*/
  $(".faq_li li a").click(function(){
    if($(this).attr("rel") != ''){
      location = $(this).attr("href") + "&type=" + $(this).attr("rel");
      return false;
    }
    //return false;
  });
  
	
	 /*-------  wyszukwiarka strona FAQ -------------*/
	var option = '<option value="0">Wszystkie</option>'; 
  $(".faqList li a.first").each(function(){
      var  html = $(this).html();
      option += '<option value="'+html+'">'+html+'</option>';
  });
  $(".searchFormFaq select").html(option);
  
  
  /* -------  strona FAQ  ------------*/
  $("ul.faq li a ").click(function() {
		$(this).next().slideToggle(300);
	});
	$("ul.faqList li a.first").click(function() {
		pokaz($(this).next());
	});
  
  $(".openAll").click(function(){
    $(".faqList li span").parent().find("ul").show();
    $(".faqList li span").removeClass("close");
    $(".faqList li span").addClass("open");
    $(this).hide();
    $(".closeAll").show();
  });
  $(".closeAll").click(function(){
    $(".faqList li span").parent().find("ul").hide();
    $(".faqList li span").removeClass("open");
    $(".faqList li span").addClass("close");
    $(this).hide();
    $(".openAll").show();
  });
  $(".closeAll").click();
  
  
  /* ----------- wyszukiwanie parametru faq ------------------------ */
  
  var _hrefFaq = location.search;
  _index = _hrefFaq.search("&type=");
  _hrefFaq = _hrefFaq.substr(_index+6);
  
	if(_hrefFaq.indexOf("link") >= 0){
	 $(".faqList li ul.faq li a[name="+_hrefFaq+"]").parent().parent().prev().prev().click();
	 $(".faqList li ul.faq li a[name="+_hrefFaq+"]").click();
	}
  
	
	/* -------  LightBox  ------------*/
  $('a.lightbox').lightBox(); // Select all links with lightbox class
  
  /* -------  link stopka  ------------*/
  $('.footer p a').attr("href","http://www.koperfam.pl/index.php?id=19"); 
  
 
  /* -------  losowi partnerzy  ------------*/
  
  var lp = 0;
  $(".find_faq .bgBox").each(function(){lp++;});
  var number = Math.floor(Math.random()*(lp-2));
	$(".find_faq .bgBox").eq(number).show();
	$(".find_faq .bgBox").eq(number+1).show();
	$(".find_faq .bgBox").eq(number+2).show().addClass("bgBoxLast");
   
	
});


/* -------  do pobrania  ------------*/
$(document).ready(function(){
  $("select[name='typeProduct']").html($(".searchResult .group select").html());
  $("select[name='typeProduct'] option").each(function(){
    var str = $(this).html();
    var index = str.indexOf("pliki");
    if(index >= 0){
      $(this).remove();
    }
  });
  $(".infoBox a").click(function(){
    $(".searchForm input").val("");
    var type = $(this).attr("rel");
    $(".searchForm select[name='type']").val(type)
    searchFile("all");
  });

  $(".searchForm select, .searchForm input[name='searchWord']").keypress(function (e) {
    if (e.which == 13){
      $(".searchIcon").click();
    }
  });  
})
   
function searchFile(typeSearch){
  $("img.preloader").show();
  $(".searchResult, .searchResult .file").hide();

  var lp = 0;
  var type = $(".searchForm select[name='type'] option:selected").val();
  var typeProduct = $(".searchForm select[name='typeProduct'] option:selected").val();
  var text = $(".searchForm input").val();
  text = text.substr(0).toLowerCase();
  $(".searchResult .file").each(function(){
    var firstSelect = $(this).find("a.icon").attr("rel")
    var secondSelect = $(this).find("a.icon").attr("name")
    
    if(typeSearch == "all"){
      typeProduct = "";
      secondSelect = "";
    }
    var indexName = secondSelect.search(typeProduct);
    var name = $(this).find("a.titleFile").html();
    if(name != null){
      name = name.substr(0).toLowerCase();
      var index = name.search(text);
    }
    else{
      name='';
      index = -1;
    }
    if((type == firstSelect && index >= 0 && indexName >= 0) || (type == firstSelect && indexName >= 0 && text == '')){
      lp++;
      $(this).show();
    }
  });
  
  var resultText = '';
  switch(lp) {
    case 1: resultText = "plik."; 
    break;
    case 2: resultText = "pliki."; 
    break;
    case 3: resultText = "pliki."; 
    break;
    case 4: resultText = "pliki."; 
    break;
    default: resultText = "plików."; 
    break;
  }
  $("img.preloader").hide();
  if(lp > 0) {
    $(".searchResult").show();
    $("p.resultText").html("Znaleziono "+lp+" "+resultText);
    $("p.resultText").show();
  }
  else{
    $("p.resultText").html("Nie znaleziono plików.");
    $("p.resultText").show();
  }
   
  
};

/*-------------- wyszukiwanie faq-----------------------*/

$(document).ready(function(){
  $("input[name='searchKey']").keypress(function (e) {
    if (e.which == 13){
      $(".searchIcon").click();
    }
  });
});

function searchFaq(){
  $(".resultText").hide();
  $(".faqList li").hide();
  $(".faqList li ul.faq li").show();
  var type = $(".searchFormFaq select option:selected").val();
  var searchKey = $(".searchFormFaq input[name='searchKey']").val();
  $(".openAll").show();
    if(searchKey == ''){
      $(".faqList li a.first").each(function(){
        if(type == '0'){
          $(this).parent().show();
          $(this).next().html("");
        }
        else{
          if ($(this).html() == type){
            $(this).parent().show();
            $(this).next().html("");
          }
        }
      });
    }
    else{
      var number = 0;
      var check = 0
      $(".faqList li a.first").each(function(){
        if ($(this).html() == type || type == '0'){
          $(this).parent().find("ul").children().each(function(e){
            var text = $(this).html();
            var _index = text.search(searchKey);
            if(_index < 0){
              $(this).hide();
            }
            else{
              number++;
            }
          });
          if(number > 0){
            number = 0;
            $(this).parent().show();
            $(this).parent().find("span").click();
            $(".openAll").hide();
            $(".closeAll ").show();
            $(this).next().html("");
            check = 1;
          }
        }
      });
	  if(check != 1){
		$(".resultText").show();
		$(".openAll").hide();
	  }
    }
  
  
  
  
  /*
    var text = $(this).parent().find("ul").children().html();
    var _index = text.search(searchKey);
  
    if(type == '0'){
      $(this).parent().show();
      $(this).next().html("");
    }
    else{
      if (($(this).html() == type && _index > 0) || ($(this).html() == type && key == '') ){
        if(_index > 0 ){
          $(this).parent().find("ul").children().each(function(){
            var txt = $(this).html();
            var _indexTxt = txt.search(searchKey);
            if(_index > 0 ){
              $(this).show();
              $(this).parent().parent.show();
            }
            else{
              $(this).hide();
            }
          });
        }
        else{
          $(this).parent().show();
          $(this).next().html("");
        }
          
      }
    }
    */
 
}

/*--------------automatyczne przewijanie-----------------------*/
function checkClickGallery(){
    if(!$(".rightContent .next").hasClass("inactive")){
      $(".rightContent .next").click();
    }
    else{
      $(".rightContent .next").removeClass("inactive");
      $(".rightContent .prev").addClass("inactive");
      $(".galleryLightBox li:last").removeClass("active").hide();
      $(".galleryLightBox li:first").addClass("active").show();
      timeOut = setTimeout("checkClickGallery()",timeOutSetTime);
    }
}

function checkClickGalleryRecom(){
    if(!$(".box370 .next").hasClass("inactive")){
      $(".box370 .next").click();
    }
    else{
      $(".box370 .next").removeClass("inactive");
      $(".box370 .prev").addClass("inactive");
      $(".box370 .wrapAll li:last").removeClass("active").hide();
      $(".box370 .wrapAll li:first").addClass("active").show();
      timeOutRecom = setTimeout("checkClickGalleryRecom()",timeOutSetTime);
    }
}


/*  -------------przyciski lewo prawo--------------*/
function pokaz(obj){
  var cl = $(obj).attr("class");
  if(cl == 'close'){ 
    $(obj).parent().find("ul").show();
    $(obj).removeClass("close");
    $(obj).addClass("open");
  }
  else{
    $(obj).parent().find("ul").hide();
    $(obj).removeClass("open");
    $(obj).addClass("close");
  }
  if($(".faqList li span").hasClass("close")){
    $(".openAll").show();
    $(".closeAll").hide();
  }
  else{
    $(".openAll").hide();
    $(".closeAll").show();
  }
}

function setList(list,obj,speed){
  var active = $(list).find("li.active");
  if($(obj).hasClass("next")){$(active).next().addClass("active").fadeIn(speed);}
  else{$(active).prev().addClass("active").fadeIn(speed);}
  $(active).removeClass("active").hide();
  $(obj).parent().find(".inactive").removeClass("inactive");
  active = $(list).find("li.active");
  
  if($(obj).hasClass("next") && $(active).is(":last-child")){
    $(obj).addClass("inactive");
  }
  if($(obj).hasClass("prev") && $(active).is(":first-child")){
    $(obj).addClass("inactive");
  }
}

function rotateNews(){
  clearTimeout(timeNews);
  if($(".boxNews .next").hasClass("inactive")){
    $("ul.news li").removeClass("active").hide();
    $(".boxNews .next").removeClass("inactive")
    $(".boxNews .prev").addClass("inactive");    
    $("ul.news li:first").addClass("active").show();
    timeNews = setTimeout("rotateNews()",timeOutSetTime);
  }
  else{
    timeNews = setTimeout("rotateNews()",timeOutSetTime);
    $(".boxNews .next").click();
  }
}

function rotateBoxProf(){
  if($(".profSite .boxRight .next").hasClass("inactive")){
    $(".profSite .boxRight .next").removeClass("inactive")
    $(".profSite .boxRight .prev").addClass("inactive");    
    $(".activeList li").removeClass("active").hide();
    $(".activeList li:first-child").addClass("active").show();
  }
  else{
    $(".profSite .boxRight .next").click();
  }
  timeBoxProf = setTimeout("rotateBoxProf()",timeOutSetTime);
}

 function Get_Cookie( check_name ) {
  	var a_all_cookies = document.cookie.split( ';' );
  	var a_temp_cookie = '';
  	var cookie_name = '';
  	var cookie_value = '';
  	var b_cookie_found = false; // set boolean t/f default f
  
  	for ( i = 0; i < a_all_cookies.length; i++ )
  	{
  		a_temp_cookie = a_all_cookies[i].split( '=' );
  
  		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
  
  		if ( cookie_name == check_name )
  		{
  			b_cookie_found = true;
  			if ( a_temp_cookie.length > 1 )
  			{
  				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
  			}
  			return cookie_value;
  			break;
  		}
  		a_temp_cookie = null;
  		cookie_name = '';
  	}
  	if ( !b_cookie_found )
  	{
  		return null;
  	}
  }

function Delete_Cookie( name, path, domain ) {
    if(Get_Cookie(name)){ document.cookie = name + "=" + ( ( path ) ? ";path=" + path : "") + ( ( domain ) ? ";domain=" + domain : "" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";}
}
function blurLink(obj){
  Delete_Cookie('Compare','/','')
}
function printWindow(){
  var style="a{color:#333; text-decoration:none;}table{width:100%; display:block; height:100%; border:1px solid #999; margin:0; padding:0; top:0;}table tr td{padding:2px; border:1px solid #999;}table tr td img{border:0;width:150px;}h3{width:100%; text-align:center; font-family:Tahoma;}table tr td img a{border:0;} .print,.close{display:none;}"
  var DocumentContainer = document.getElementById('TB_ajaxContent');
  var html = "<html><head><title>Drukowanie</title><style type='text/css'>"+style+"</style></head><body>"+DocumentContainer.innerHTML+"</body></html>"
  var WindowObject = window.open("", "PrintWindow","width=750,height=650,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes");
  WindowObject.document.writeln(html);
  WindowObject.document.close();
  WindowObject.focus();
  WindowObject.print();
  WindowObject.close();
}

var Url = {
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	},
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
		while ( i < utftext.length ) {
			c = utftext.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}
