var photoResize;

$(document).ready(function() {

    /*
    / hover effect pro menu
    */

    $("#mainmenu li a").hover(
      function () {
        //if ($(this).attr('class') == ''){
          $(this).addClass("sel");
        //}
      },
      function () {
        //if ($(this).attr('class') == 'sel'){
          $(this).removeClass("sel");
        //}
      }
    );
    
    /*
    / hover effect pro submenu
    */

    $("#submenu li").hover(
      function () {
        //if ($(this).attr('class') == ''){
          $(this).addClass("hover");
        //}
      },
      function () {
        //if ($(this).attr('class') == 'hover'){
          $(this).removeClass("hover");
        //}
      }
    );
    

    /*
    / 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 novinky emailem
    */    

    $("#mailnewsinput").each(function(i){
        $(this).focus(function () {
          if ($(this).val() == 'váš@e-mail'){
            $(this).val('');
          }
        });
        $(this).blur(function () {
          if ($(this).val() == 'váš@e-mail'){
            $(this).val('váš@e-mail');
          }
        });
    });


    /*
    * efekt zvetseni fotky
    */    
    $("#contact .contact-image").hover(
      function () {
        ResizeImageToDefault(100);
        $(this).css( {zIndex: 5000} );
        ResizeImageToBigger($(this),100);
        
      },
      function () {
        //ResizeImageToSmall($(this),250);
        $(this).css( {zIndex: 0} );
        ResizeImageToDefault(100);
        return false;

      }
    );

    $(".reference-image").hover(
      function () {
        ResizeImageToDefault(100);
        $(this).css( {zIndex: 5000} );
        ResizeImageToBigger($(this),100);
        
      },
      function () {
        //ResizeImageToSmall($(this),250);
        $(this).css( {zIndex: 0} );
        ResizeImageToDefault(100);
        return false;

      }
    );
    
});

/******************************************************************************/
/******************************************************************************/
/******************************************************************************/
/*
* parsovani XML s bannerama a stridani banneru po sekundach
*/
var counter = 0;

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

function ShowBanner(id,img,counter,file,items,time){
  Hide(document.getElementById(id), 100, img);
  if ((counter + 1) >= items){
    counter = 0;
  }else{
    counter = counter + 1;
  }
  XMLBanners(file,id,counter,time);
  //Show(document.getElementById(id), 0);
}

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') > '')&&($(this).val() == '')){
      if ($(this).attr('name').search('mail') != -1){
        if(!EmailControl($(this).val())){
          errorMessage = errorMessage + $(this).attr('mandatory');
        }
      }else{
          errorMessage = errorMessage + $(this).attr('mandatory');
      } 
    }
  });
  if (errorMessage > ''){
    alert(errorMessage);
    return false;
  }else{
    return true;
  }
}


function EmailControl(f){
    if ((f=='' || f=='@') || 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 ResizeImageToSmall(obj, size) {
	if (size >= 150 && obj) {
    size -= 10;
		obj.find('img').attr('width',size);
		photoResize = setTimeout(function () { ResizeImageToSmall(obj, size) }, 2); // rekurze na tuto funkci
	}
}
function ResizeImageToBigger(obj, size) {
	if (size <= 250 && obj) {
    size += 10;
		obj.find('img').attr('width',size);
		photoResize = setTimeout(function () { ResizeImageToBigger(obj, size) }, 40); // rekurze na tuto funkci
	}
}
function ResizeImageToDefault(size){
  clearTimeout(photoResize);
  $("#contact .contact-image").each(function(i){
    $(this).find('img').attr('width',size);
  })
}   



