$(document).ready(function() {
    $('.prodejci').find('img').each(function(){
      $(this).hover(
        function () {
          $(this).attr('src',$(this).attr('rel2'));
        }, 
        function () {
          
          $(this).attr('src',$(this).attr('rel'));
          
        }
      );
      
    });

    /*
    * sipky na detailu
    */    
    $('#related').click(function(){
      if ($('.related').css('display') == 'none'){
        $(this).removeClass('dp-arrow-right').addClass('dp-arrow-down');
        $('.related').css('display','inline'); 
      }else{
        $(this).removeClass('dp-arrow-down').addClass('dp-arrow-right');
        $('.related').css('display','none'); 
      }
    });
    $('#files').click(function(){
      if ($('.soubory').css('display') == 'none'){
        $(this).removeClass('dp-arrow-right').addClass('dp-arrow-down');
        $('.soubory').css('display','inline'); 
      }else{
        $(this).removeClass('dp-arrow-down').addClass('dp-arrow-right');
        $('.soubory').css('display','none'); 
      }
    });
    $('#links').click(function(){
      if ($('.odkazy').css('display') == 'none'){
        $(this).removeClass('dp-arrow-right').addClass('dp-arrow-down');
        $('.odkazy').css('display','inline'); 
      }else{
        $(this).removeClass('dp-arrow-down').addClass('dp-arrow-right');
        $('.odkazy').css('display','none'); 
      }
    });
    $('#faq').click(function(){
      if ($('.faq-h').css('display') == 'none'){
        $(this).removeClass('dp-arrow-right').addClass('dp-arrow-down');
        $('.faq-h').css('display','inline'); 
      }else{
        $(this).removeClass('dp-arrow-down').addClass('dp-arrow-right');
        $('.faq-h').css('display','none'); 
      }
    });
    $('#gallery').click(function(){
      if ($('.galerie').css('display') == 'none'){
        $(this).removeClass('dp-arrow-right').addClass('dp-arrow-down');
        $('.galerie').css('display','inline'); 
      }else{
        $(this).removeClass('dp-arrow-down').addClass('dp-arrow-right');
        $('.galerie').css('display','none'); 
      }
    });



    /*
    * menu
    */    
    $('.menu').find('li').each(function(){
      $(this).hover(
        function () {
          $(this).find('img').attr('src',$(this).find('img').attr('src').replace('.png','-a.png'));
          $(this).find('ul').css('display','inline');
          
        }, 
        function () {
          $(this).find('img').attr('src',$(this).find('img').attr('src').replace('-a.png','.png'));

          $(this).find('ul').css('display','none');
        }
      );
    });

    /*
    / zalozky v detailu zbozi
    */
    $('.bookmarks li').find('a').each(function(){
      $(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 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() == 'jméno'){
        $(this).val('');
      }
    });
    $("#logininput").blur(function () {
      if ($(this).val() == ''){
        $(this).val('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');
      }
    });


    $('.banner-control').find('li').each(function(){
      if ($(this).attr('class') == 'counter'){
        $(this).click(function(){ 
          clearTimeout(mainTimeout);
          XMLCOUNTER = $(this).attr('id').replace('dot-','');
          XMLBan(XMLFILE,XMLID,XMLCOUNTER,XMLTIME);
        });
      }
    });

    $('.banner-control').find('img').each(function(){
      if ($(this).attr('id') == 'play'){
        $(this).click(function(){ 
          if ($(this).attr('src') == '/img/pause.png'){ // zastaveni
            $(this).attr('src','/img/play.png');
            clearTimeout(mainTimeout);
          }else{ // spusteni
            $(this).attr('src','/img/pause.png');
            XMLBan(XMLFILE,XMLID,XMLCOUNTER,XMLTIME);
          };
        });
      };
    });

    
        
});


/*
* 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 mainTimeout = "";

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


function XMLBan(file,id,counter,time){
 var i = 0;
 XMLFILE = file;
 XMLID = id;
 XMLCOUNTER = counter;
 XMLTIME = time;
 $.ajax({
      type: "GET",
      url: file,
      dataType: "xml",
      success: function(xml) {
          $(xml).find('banner').each(function(){
              name = $(this).find('name').text();
              url = $(this).find('url').text();
              img = $(this).find('img').text();
              //alert(img);
              blank = $(this).find('blank').text();
              text = $(this).find('text').text();
              params = $(this).find('params').text();
              items = $(this).find('count').text();
              if (counter == i){
                SelectDot(counter);
                ShowBanner(id,img,counter,file,items,time,name,url);
                return false;
              }
              i += 1; 
          });
      }
  });


}

function ShowBanner(id,img,counter,file,items,time,name,url){
  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);
  }
  //document.getElementById(id).src = img;
	Show(document.getElementById(id), 0);
  //Hide(document.getElementById(id), 100, img);
  if ((parseInt(XMLCOUNTER) + 1) >= items){
    XMLCOUNTER = 0;
  }else{
    XMLCOUNTER = parseInt(XMLCOUNTER) + 1;
  }
  mainTimeout = setTimeout(function () {XMLBan(file,id,XMLCOUNTER,time)},time);
}

function SelectDot(counter){
  $('.banner-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');
            }
          }else{
              errorMessage = errorMessage + $(this).attr('mandatory');
          }
        } 
      }else if ($(this).attr('type') == 'checkbox'){
        if (!$(this).is(':checked')){
          errorMessage = errorMessage + $(this).attr('mandatory');
        } 
      }  
    }
  });
  
  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;
}
