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

function getTextNews(){
  $.get('/jquery/textnews.php', function(data){
    $('.news-right').html(data);
  });
}
$(document).ready(function() {

    $('#footerProducts').change(function(){
      window.location.href=$(this).val();
    });

    $('.mainmenu li').mouseover(function(){
        $(this).find('a:first').addClass('sel');
        $(this).find('ul').css('display','inline');
    }).mouseout(function(){
        $(this).find('a:first').removeClass('sel');
        $(this).find('ul').css('display','none');
    })



    setInterval('getTextNews()',4000);


    $("#words").focus(function () {
      if ($(this).val() == 'Napište, co hledáte'){
        $(this).val('');
      }
    });
    $("#words").blur(function () {
      if ($(this).val() == ''){
        $(this).val('Napište, co hledáte');
      }
    });
    $("#mailnews").focus(function () {
      if ($(this).val() == 'Váš email'){
        $(this).val('');
      }
    });
    $("#mailnews").blur(function () {
      if ($(this).val() == ''){
        $(this).val('Váš email');
      }
    });



    $('#lng-ver').change(function(){
      $('#langform').submit();    
    });

    /*
    / hover effect pro menu
    */

    $(".mainmenu li").hover(
      function () {
        
        if ($(this).attr('class') == 'first-li'){
          $(".mainmenu li:first").addClass("first-sel");
        }
        if ($(this).attr('class') == 'last-li'){
          $(".mainmenu li:last").addClass("last-sel");
        }
      },
      function () {
        if ($(this).attr('class') == 'first-li'){
          $(".mainmenu li:first").removeClass("first-sel");
        }
        if ($(this).attr('class') == 'last-li'){
          $(".mainmenu li:last").removeClass("last-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;

      }
    );
    
    
    $('#telefon').keypress(function (e) {
      if(!e) e=window.event;
      key = e.keycode ? e.keycode : e.which;
      if (((key <48) || (key >57)) && (key != 32) && (key != 0) && (key != 43) && (key != 8)) { 
        //e.returnValue=false;
        return false;
      }else{
        return true;
        //e.returnValue=true;
      }
    });    

    
    $('.advrt-control').find('li').each(function(){
      if ($(this).attr('class') == 'counter'){

        $(this).hover(
          function () {
            clearTimeout(mainTimeoutmainimg);
            counter = (parseInt($(this).attr('id').replace('dot-','')) );
            XMLBan(XMLFILE,XMLID,counter,XMLTIME,'1','mainTimeoutmainimg',false);
            //clearTimeout(mainTimeoutmainimg);
          },
          function () {
          }
        );

        /*$(this).click(function(){ 
          clearTimeout(mainTimeoutmainimg);
          counter = (parseInt($(this).attr('id').replace('dot-','')) );
          XMLBan(XMLFILE,XMLID,counter,XMLTIME,'1','mainTimeoutmainimg',true);
        });*/
      }
    });
    
    $('.advrt-control').hover(
      function () {
      },
      function () {
        clearTimeout(mainTimeoutmainimg);
        counter = (parseInt($(this).attr('id').replace('dot-','')) );
        XMLBan(XMLFILE,XMLID,counter,XMLTIME,'1','mainTimeoutmainimg',true);
        //clearTimeout(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,pokracovat){
  
  clearTimeout(timeoutName);
          
  var i = 1;
  if (bannerControlBool == '1'){
    XMLFILE = file;
    XMLID = id;
    XMLCOUNTER = counter;
    XMLTIME = time;
    XMLCONTROL = bannerControlBool;
  }
  $.ajax({
    url: file,
    cache: false,
    success: 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,pokracovat);
            return false;
          }
          i += 1; 
      });
    }
  });
  

}

function ShowBanner(id,img,counter,file,items,time,name,url,bannerControlBool,timeoutName,pokracovat){
  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).html(img);
  }
  Show(document.getElementById(id), 0);
  if ((parseInt(counter)) > items){
    counter = 1;
  }else{
    counter = parseInt(counter) + 1;
  }
  
  if (bannerControlBool){
    if (pokracovat){
      mainTimeoutmainimg = setTimeout(function () {XMLBan(file,id,counter,time,bannerControlBool,'mainTimeoutmainimg',pokracovat)},time);
    }else{
      clearTimeout(mainTimeoutmainimg);
    }
  }else{
    if (pokracovat){
      timeoutName = setTimeout(function () {XMLBan(file,id,counter,time,bannerControlBool,timeoutName,pokracovat)},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')){
          //alert($(this).attr('mandatory'));
          if ($(this).attr('name').search('mail') != -1){
            //alert(EmailControl($(this).val()));
            if(!EmailControl($(this).val())){
              errorMessage = errorMessage + $(this).attr('mandatory') + '\n';
            }
          }else{
            if ($(this).val() == ''){
              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=='@') || 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);
  })
}   



