var options = { path: '/', expires: 10 };

$(document).ready(function() {
    // silencer
    $('#words').keyup(function() {
      if ($(this).val().length >= 2){
        $('#silencer').css('display','inline');
        $('#silencer').html("<img src='/img/loader.gif' alt='' />");
        //var sHtml = "<ul class='silencer'>";
        $.get('/jquery/silencer.php?srch='+$(this).val(), function(data){
          if (data > ''){ 
            $('#silencer').html(data);          
          }else{
            $('#silencer').css('display','none');
          }
        });

      }
    });


    // horozntal hover efekt
    $('.horizontal').find('li').each(function(){
      $(this).hover(
        function () {

          $(this).addClass('sel');
          obj = $(this);
          obj.find('.catPopUp').css('display','inline');
          
        }, 
        function () {
          $(this).removeClass('sel');
          obj = $(this);
          obj.parent().find('.catPopUp').css('display','none');
        }
      );
    });

    $('.horizontal').find('li:first').each(function(){
      $(this).hover(
        function () {
          $(this).addClass('sel-first');
          $(this).find('a:first').addClass('sel');
        }, 
        function () {
          $(this).removeClass('sel-first');
          $(this).find('a:first').removeClass('sel');
        }
      );
    });
    $('.horizontal').find('li.last').each(function(){
      $(this).hover(
        function () {
          $(this).addClass('sel-last');
          $(this).find('a:first').addClass('selected');
        }, 
        function () {
          $(this).removeClass('sel-last');
          $(this).find('a:first').removeClass('selected');
        }
      );
    });
    

    $('.category').find('li.level0').each(function(){
      $(this).hover(
        function () {
          $(this).addClass('sele');
        }, 
        function () {
          $(this).removeClass('sele');
        }
      );
    });

    $('.category').find('li.level1').each(function(){
      $(this).hover(
        function () {
          $(this).addClass('select');
        }, 
        function () {
          $(this).removeClass('select');
        }
      );
    });

    $('.category').find('li.level2').each(function(){
      $(this).hover(
        function () {
          $(this).addClass('select');
        }, 
        function () {
          $(this).removeClass('select');
        }
      );
    });


    /*
    / zalozky v detailu zbozi
    */
    $('.bookmarks li').find('a').each(function(){
      if ($(this).attr('href').indexOf('gallery') <= 0){
        $(this).click(function(){
           $('.bookmarks li').each(function(){
              $(this).removeClass('bm-active');
           });
           $(this).parent().attr('class','bm-active');
           $.get('/detail.php'+$(this).attr('href')+'&j=1', function(data){
                $('#param-content').html(data);
           });
           return false;
        });
      }
    });
    
    /*
    / odeslani emailu a dalsi formulare v detailu zbozi, provazani se zalozkama
    */
    
    $('.buttons').find('a').each(function(){
      
      $(this).click(function(){
         $('.bookmarks li').each(function(){
            $(this).removeClass('bm-active');
         });
         $.get('/detail.php'+$(this).attr('href')+'&j=1', function(data){
              $('#param-content').html(data);
         });
         return false;
      });
    });
    
    
    /*
    * pridani focus a blur na novinky emailem
    */    
    $("#mailnewsinput").focus(function () {
      if ($(this).val() == 'váš@e-mail'){
        $(this).val('');
      }
    });
    $("#mailnewsinput").blur(function () {
      if ($(this).val() == ''){
        $(this).val('váš@e-mail');
      }
    });

    /*
    * pridani focus a blur na pole hledani
    */    
    $("#words").focus(function () {
      if ($(this).val() == 'Hledat...'){
        $(this).val('');
      }
    });
    $("#words").blur(function () {
      if ($(this).val() == ''){
        $(this).val('Hledat...');
      }
    });


    /*
    * pridani focus a blur na logovaci formular
    */    
    $("#logininput").focus(function () {
      if ($(this).val() == 'Přihlašovací jméno'){
        $(this).val('');
      }
    });
    $("#logininput").blur(function () {
      if ($(this).val() == ''){
        $(this).val('Přihlašovací jméno');
      }
    });
    

    /*
    * pridani focus a blur na logovaci formular
    */    
    $("#passwordinput").focus(function () {
      if ($(this).val() == 'heslo'){
        $(this).val('');
      }
    });
    $("#passwordinput").blur(function () {
      if ($(this).val() == ''){
        $(this).val('heslo');
      }
    });


    $("#mesto").blur(function () {
      if (($(this).val() > '')&&($('#psc').val() == '')){
        $.get('/jquery/psc.php?mesto='+$(this).val(), function(data){
           $('#psc').val(data);
        });        
      }
    });
    $("#psc").blur(function () {
      if (($(this).val() > '')&&($('#mesto').val() == '')){
        $.get('/jquery/psc.php?psc='+$(this).val(), function(data){
           //alert(data);
           $('#mesto').val(data);
        });        
      }
    });


    $('.advrt-control').find('li').each(function(){
      if ($(this).attr('class') == 'counter'){
        $(this).click(function(){ 
        //alert(mainTimeoutmainimg);
          clearTimeout(mainTimeoutmainimg);
          counter = (parseInt($(this).attr('id').replace('dot-','')) );
          //alert(XMLID);
          //$.cookie('advert-last'+XMLID, counter, options);
          XMLBan(XMLFILE,XMLID,counter,XMLTIME,'1','mainTimeoutmainimg');
        });
      }
    });

   
});

/*
* parsovani XML s bannerama a stridani banneru po sekundach
*/
var counter = 0;
var name = "";
var url = "";
var img = "";
var blank = "";
var text = "";
var params = "";
var items = "";

var XMLFILE = "";
var XMLID = "";
var XMLCOUNTER = "";
var XMLTIME = "";


function XMLBan(file,id,counter,time, bannerControlBool,timeoutName){
  
  clearTimeout(timeoutName);
          
  var i = 1;
  if (bannerControlBool == '1'){
    XMLFILE = file;
    XMLID = id;
    XMLCOUNTER = counter;
    XMLTIME = time;
    XMLCONTROL = bannerControlBool;
  }
  $.get(file, function(data){
    $(data).find('banner').each(function(){
        name = $(this).find('name').text();
        url = $(this).find('url').text();
        img = $(this).find('img').text();
        blank = $(this).find('blank').text();
        text = $(this).find('text').text();
        params = $(this).find('params').text();
        items = $(this).find('count').text();
        if ((parseInt(counter)) > items){
          $.cookie('advert-last'+id, 1, options);
        }else{
          $.cookie('advert-last'+id, (parseInt(counter)), options);
        }
        if ((parseInt($.cookie('advert-last'+id))) == i){
          if (bannerControlBool == '1'){
            SelectDot((parseInt($.cookie('advert-last'+id))));
          }
          ShowBanner(id,img,(parseInt($.cookie('advert-last'+id))),file,items,time,name,url,bannerControlBool,timeoutName);
          return false;
        }
        i += 1; 
    });


  }); 


}

function ShowBanner(id,img,counter,file,items,time,name,url,bannerControlBool,timeoutName){
  if (img.indexOf('.swf') > 0){
    $('#'+id).hide();
    $("object").remove();
    $("<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' width='1180' height='359' id='blade' align='middle'><param name='allowScriptAccess' value='sameDomain' /><param name='allowFullScreen' value='false' /><param name='movie' value='"+img+"' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' /><embed src='"+img+"' quality='high' bgcolor='#ffffff' width='1180' height='359' name='blade' align='middle' allowScriptAccess='sameDomain' allowFullScreen='false' type='application/x-shockwave-flash' pluginspage='http://www.adobe.com/go/getflashplayer' /></object>").insertBefore($('#'+id));
  }else{
    $("object").remove();
    $('#'+id).show();
    $('#'+id).attr('src',img);
    $('#'+id).attr('alt',name);
    $('#'+id).attr('title',name);
    $('#'+id).parent().attr('href',url);
    $('#'+id).parent().attr('alt',name);
    $('#'+id).parent().attr('title',name);
  }
  Show(document.getElementById(id), 0);
  if ((parseInt(counter)) > items){
    counter = 1;
  }else{
    counter = parseInt(counter) + 1;
  }
  if (bannerControlBool){
    mainTimeoutmainimg = setTimeout(function () {XMLBan(file,id,counter,time,bannerControlBool,'mainTimeoutmainimg')},time);
  }else{
    timeoutName = setTimeout(function () {XMLBan(file,id,counter,time,bannerControlBool,timeoutName)},time);
  }
}

function SelectDot(counter){
  //alert(counter);
  $('.advrt-control').find('li').each(function(){
    if ($(this).attr('class') == 'selected'){
      $(this).attr('class','counter');
      //return false;
    }
  });
  $('#dot-'+counter).attr('class','selected');
}

function setOpacity (obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	obj.style.filter = "alpha(opacity:"+opacity+")"; 
	obj.style.KHTMLOpacity = opacity/100;  
	obj.style.MozOpacity = opacity/101; 
	obj.style.opacity = opacity/100; 
}

function Show(obj, opacity) {
	if (opacity <= 100 && obj) {
		setOpacity(obj, opacity);
		opacity += 10;
		setTimeout(function () { Show(obj, opacity) }, 30); // rekurze na tuto funkci, k provedení dalšího stupně změny průhlednosti
	}
}

function Hide(obj, opacity, img) {
	if (opacity >= 0 && obj) {
		setOpacity(obj, opacity);
		opacity -= 10;
		setTimeout(function () { Hide(obj, opacity, img) }, 30); // rekurze na tuto funkci, k provedení dalšího stupně změny průhlednosti
  	
	}//else{
	// obj.src = img;
	// Show(obj, 0);
	//}
}


/*
* kontrola formularu
*/
function ControlForm(form){
  var errorMessage = '';
  $('#'+form).find('input').each(function(){
    if ($(this).attr('mandatory') > ''){
      if (($(this).attr('type') == 'text')||($(this).attr('type') == 'password')){
        if ($(this).val() == ''){
          if ($(this).attr('name').search('mail') != -1){
            if(!EmailControl($(this).val())){
              errorMessage = errorMessage + $(this).attr('mandatory') + '\n';
            }
          }else{
              errorMessage = errorMessage + $(this).attr('mandatory') + '\n';
          }
        } 
      }else if ($(this).attr('type') == 'checkbox'){
        if (!$(this).is(':checked')){
          errorMessage = errorMessage + $(this).attr('mandatory') + '\n';
        } 
      }  
    }
  });
  
  if (errorMessage > ''){
    alert(errorMessage);
    return false;
  }else{
    return true;
  }
}


function EmailControl(f){
    if ((f=='' || f=='vas@email.cz') || f.indexOf('..') != -1 || f.indexOf('.@') != -1 || f.indexOf('@.') != -1 || (f.indexOf('@') < 1 || f.indexOf('@') != f.lastIndexOf('@') || f.lastIndexOf('.') < f.lastIndexOf('@')+2  || f.lastIndexOf('.') > (f.length-3) || f.lastIndexOf('.') < (f.length-4))){
       return false;
      }
    if ((f.indexOf('ř') > -1) || (f.indexOf('ě') > -1) || (f.indexOf('š') > -1) || (f.indexOf('č') > -1) || (f.indexOf('ž') > -1) || (f.indexOf('ý') > -1) || (f.indexOf('á') > -1) || (f.indexOf('í') > -1) || (f.indexOf('é') > -1) || (f.indexOf('ů') > -1) || (f.indexOf('ú') > -1)){
       return false;
    }
    return true;
}

function DelCountBasket(ID){
  document[ID].count.value = 0;
  document[ID].submit();
}

function printDetail(url){
  window.open(url);
  //alert(url);
}

function ControlNumber(){
     if ((event.keyCode <48) || (event.keyCode >57)) event.returnValue = false;
}

function OpenWindow(sUrl,sParam){
  window.open(sUrl,"_blank",sParam);
}	
