// JavaScript Document


jQuery(document).ready(function(){

/* datepicker */
     $('#inputDate').css("color","white");
     var datum = new Date(); // aktuln datum
     d = datum.getDate();
     m = datum.getMonth()+1;
     y = datum.getFullYear();
     x = (d+'/'+m+'/'+y);
     $('#inputDate').attr("value", (d+'/'+m+'/'+y) );
     $('#inputDate, #inputDateIcon').DatePicker({
     	format:'d.m.Y',
     	date: $('#inputDate').val(),
     	current: $('#inputDate').val(),
     	starts: 1,
     	position: 'r',
     	onBeforeShow: function(){
     		$('#inputDate').DatePickerSetDate($('#inputDate').val(), true);
     	},
     	onChange: function(formated, dates){
     		$('#inputDate').val(formated);
     		if ($('#closeOnSelect input').attr('checked')) {
     			$('#inputDate').DatePickerHide();
     		}
     		$('#inputDate, #inputDateIcon').DatePickerHide();
     		$('#inputDate').css("color","black");
     	}
     });
/* end- datepicker */
$(".infoLink, .infoBox").click(function(){
     $(".infoBox").toggle();
});
$(".lendCol .kontakt div.checkdiv, div.lendCol form.puvodniForm table td input[name=kontakt]:last").live('click', function(){
     $("table.cal").toggle();
});


/* calc */

     if ( $("div.Calculator div.calcInput #percentageRate").attr("value")=='' ){
          $("div.Result span").css("color","#E9F1F9");
     }else{
          $("div.Result span").css("color","#004C98");   
     }
     $("div.calcInput div#submit input").click(function(){
          if ( $("div.Calculator div.calcInput #percentageRate").attr("value")=='' ){
               $("div.Result span").css("color","#E9F1F9");
          }else{
               $("div.Result span").css("color","#004C98");   
     }
     }); 


/* right banner mouseover */
     $("div.RightBanner div.moreLink, div.RightBannerMouseover").mouseover(function(){
          $("div.RightBannerMouseover").show();
     });
     $("div.RightBanner div.moreLink, div.RightBannerMouseover").mouseleave(function(){
          $("div.RightBannerMouseover").hide();
     });
     

/* checkboxy */
     $(":checkbox").css({display:"none"}); 
     $("form.favoriteCat div").css("margin-bottom","5px");          
     $(":checkbox").wrap("<div class='checkdiv'></div>");

     $(":checkbox:checked").each(function(){
          $(this).parent("div").addClass("checked");
     });
     
     $("div.checkdiv").click(function(){
          if($(this).children().is(":checked")==true){
               $(this).children().removeAttr("checked");
               $(this).removeClass("checked");
          }else{
               $(this).children().attr("checked","checked");
               $(this).addClass("checked");    
          };
     });
     
/* faq */
     $("div.faq p.a").hide();   
     $("div.faq p strong").css("cursor", "pointer");  
     $("div.faq p.q").click(function(){     
          $("p.a").slideUp(500);

          if ( $(this).parent("div.faqItem").children("p.a").is(":visible") ){
               $(this).parent("div.faqItem").children("p.a").slideUp(500);  
          }else{
               $(this).parent("div.faqItem").children("p.a").slideDown(500);   
          }


     });
     
     
     





/* carousel */	
	/* init */
     if (HomePage == true){ //
          vyskaBoxu = 151;
          vyskaBoxuMin = 150; /* výška mínus border */
          odsazeni = 73;
          $("div.Offer div.OfferInner div.OfferItem").css("height","150px");
     }else{
          vyskaBoxu = 132;
          vyskaBoxuMin = 131; /* výška mínus border */
          odsazeni = 67;
          $("div.Offer div.OfferInner div.OfferItem").css("height","131px");
     }	
	items = productItems; /* počet zobrazených itemů */
	//pocet = (1*131)+1-47;
	wheretop = odsazeni-(items*vyskaBoxu);
	$("div.SliderList").css("top", wheretop+"px");
     spust = 1;

     /* pokud je málo prvků, netřeba scrollovat */
     var pocetPrvku = ($("div.SliderList > div.OfferItem").length);
     if (productItems==pocetPrvku){
          $("div.Down, div.Top").hide();
          var x = parseInt($("div.SliderList").css("top"));
          var x = x-18;
          $("div.SliderList").css("top", x+"px");
          var y = parseInt($("div.Offer").css("height"));
          var y = y - 40;
          $("div.Offer").css("height", y+"px");
     }

     /* kolik  */
     if (productItems==4){
     /* nakopírujem první čtyři dolů*/ 
     d = $("div.SliderList > div.OfferItem:first").next("div.OfferItem").next("div.OfferItem").next("div.OfferItem").clone();
     d.prependTo("div.post");
     c = $("div.SliderList > div.OfferItem:first").next("div.OfferItem").next("div.OfferItem").clone();
     c.prependTo("div.post");
     b = $("div.SliderList > div.OfferItem:first").next("div.OfferItem").clone();
     b.prependTo("div.post");
     a = $("div.SliderList > div.OfferItem:first").clone();
     a.prependTo("div.post");     
     /* nakopírujem poslední čtyři nahoru */
     z = $("div.SliderList > div.OfferItem:last").clone();
     z.prependTo("div.pre");
     y = $("div.SliderList > div.OfferItem:last").prev("div.OfferItem").clone();
     y.prependTo("div.pre");
     x = $("div.SliderList > div.OfferItem:last").prev("div.OfferItem").prev("div.OfferItem").clone();
     x.prependTo("div.pre");
     q = $("div.SliderList > div.OfferItem:last").prev("div.OfferItem").prev("div.OfferItem").prev("div.OfferItem").clone();
     q.prependTo("div.pre");
     }
     if (productItems==3){
     /* nakopírujem první tři dolů*/
     c = $("div.SliderList > div.OfferItem:first").next("div.OfferItem").next("div.OfferItem").clone();
     c.prependTo("div.post");
     b = $("div.SliderList > div.OfferItem:first").next("div.OfferItem").clone();
     b.prependTo("div.post");
     a = $("div.SliderList > div.OfferItem:first").clone();
     a.prependTo("div.post");     
     /* nakopírujem poslední tři nahoru */
     z = $("div.SliderList > div.OfferItem:last").clone();
     z.prependTo("div.pre");
     y = $("div.SliderList > div.OfferItem:last").prev("div.OfferItem").clone();
     y.prependTo("div.pre");
     x = $("div.SliderList > div.OfferItem:last").prev("div.OfferItem").prev("div.OfferItem").clone();
     x.prependTo("div.pre");
     }
     if (productItems==2){
     /* nakopírujem první dva dolů*/
     b = $("div.SliderList > div.OfferItem:first").next("div.OfferItem").clone();
     b.prependTo("div.post");
     a = $("div.SliderList > div.OfferItem:first").clone();
     a.prependTo("div.post");     
     /* nakopírujem poslední dva nahoru */
     z = $("div.SliderList > div.OfferItem:last").clone();
     z.prependTo("div.pre");
     y = $("div.SliderList > div.OfferItem:last").prev("div.OfferItem").clone();
     y.prependTo("div.pre");
     }
     if (productItems==1){
     /* nakopírujem první dolů*/
     b = $("div.SliderList > div.OfferItem:first").next("div.OfferItem").clone();
     b.prependTo("div.post");    
     /* nakopírujem poslední nahoru */
     z = $("div.SliderList > div.OfferItem:last").clone();
     z.prependTo("div.pre");
     } 
	
     /* klikání na buttony */
	$("div.Down").click(function(){
	    if (spust==1){
          down();
         }
     });
	$("div.Top").click(function(){
	    if (spust==1){
          up();
         }
     });	
     
     /* funkce */
     function down(){
          spust = 0;
	     $("div.Top").removeClass("TopOff");
          var slidetop = parseInt($("div.SliderList").css("top"));          
          var slideLength = ($("div.SliderList > div.OfferItem").length); /* počet prvků na začátku */
          var slideLength = slideLength + (2*items); /* počet prvků po naklonování prvních */
          //alert(slideLength);
          var slidebottom = ((slideLength*vyskaBoxuMin)+(slideLength-1));
          slideCondition = odsazeni-(slidebottom - ((items * vyskaBoxuMin)+(items * 1)));    
          //alert('condition '+slideCondition+' top '+slidetop);          
          if ( slidetop == slideCondition ) {
               //
          }else{
               x = parseInt(($("div.SliderList").css("top")));
               x = x-vyskaBoxu;
               x = x+'px';               
               $("div.SliderList").animate({"top": x}, "fast", function(){
                    if (slidetop == (slideCondition+vyskaBoxuMin)){
                         var where = odsazeni-(items*vyskaBoxu);
                         $("div.SliderList").css("top",where+"px");                         
                    }
                    spust = 1;
               });             
          }
     }

     function up(){
          spust = 0;
	    $("div.Down").removeClass("DownOff");
         var slidetop = parseInt($("div.SliderList").css("top"));          
         var slideLength = ($("div.SliderList > div.OfferItem").length); /* počet prvků na začátku */
         var slideLength = slideLength + (2*items); /* počet prvků po naklonování prvních */
         var slidebottom = ((slideLength*vyskaBoxu)+(slideLength-1));
         //alert(slidebottom);
	    /* pokud je horní okraj přesně nahoře */
	    if ( slidetop==odsazeni){
               //
         }else{
          	//alert(slidetop);
               x = parseInt(($("div.SliderList").css("top")));
               x = x+vyskaBoxu;
               x = x+'px';
               $("div.SliderList").animate({"top": x}, "fast", function(){
                    if (slidetop == odsazeni-vyskaBoxu){
                         var where = odsazeni-((items*2)*vyskaBoxu);
                         $("div.SliderList").css("top",where+"px");                         
                    }
                    spust = 1;
               });
         }
     }
     
     


/* tooltipy */    
     /* init */
     $("a.ttip").click(function(){
          return false;
     });
     $("a.ttip").each(function(){
     
          var text = $(this).html();
          //var OldLink = "http://www.itesco.sk/cmsapi.php?method=article&amp;id=187";    
          var OldLink = $(this).attr("href"); 
          var link = OldLink; 
          
          //alert (link);
          
          $(this).append('<div class="ttipDiv">');

          var anticache = Math.round(Math.random()*1000);          
          $(this).children("div").load(link+"&anticache="+anticache, function(){
               var x = $(this).html();
               //alert (x);
               $(this).html(x+'<span class="arrow"></span>');
          }          
          );    
          
     });

     $("a.ttip").mouseover(function(e){
          var relativeX = e.pageX - 115;
          var relativeY = e.pageY + 35;
                    
          $(this).children("div").css("left", relativeX+"px");
          $(this).children("div").css("top", relativeY+"px");
          $(this).children("div").show();
     });
     $("a.ttip").mousemove(function(e){
          var relativeX = e.pageX - 115;
          var relativeY = e.pageY + 35;
                    
          $(this).children("div").css("left", relativeX+"px");
          $(this).children("div").css("top", relativeY+"px");
          $(this).children("div").show();
     });
     $("a.ttip").mouseout(function(){
          $(this).children("div").hide();
     });

/* faq */
     $("div.faq .a").hide();   
     $("div.faq p strong").css("cursor", "pointer");  
     $("div.faq p.q").click(function(){     
          $("div.a").slideUp(500);

          if ( $(this).parent("div.faqItem").children("div.a").is(":visible") ){
               $(this).parent("div.faqItem").children("div.a").slideUp(500);  
          }else{
               $(this).parent("div.faqItem").children("div.a").slideDown(500);   
          }


     });


});



