$(document).ready(function() {
    /*
    / zalozky v detailu zbozi
    */
    $('.tab').find('a').each(function(){
      if ($(this).attr('href').indexOf('gallery') <= 0){
        $(this).click(function(){
           var obj = $(this).find('img');
           $.get('/detail.php'+$(this).attr('href')+'&j=1', function(data){
                $('#param-content').html(data);
           });
           $('.tab').find('img').each(function(){
            $(this).attr('src',$(this).attr('src').replace('-up.gif','.gif'));
           });
           obj.attr('src',obj.attr('src').replace('.gif','-up.gif'));
           return false;
        });
      };
    });
    $('.links').find('a').each(function(){
      $(this).click(function(){
         
         $('.tab').find('img').each(function(){
          $(this).attr('src',$(this).attr('src').replace('-up.gif','.gif'));
         });
         $.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');
          }
        });
    });

    

    
});



/*
* 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 windowOpenFunction(url,params){
  alert(url);
  alert(params);
  window.open(url,params);
}

function ConfiguratorChange(OBJ,nValue,ImageToShow){
  
  var Hodnota = nValue.split('##');
  if ((Hodnota[1] == null)||(Hodnota[1] == undefined)){
  }else{
    getE(OBJ).value = Hodnota[1];
    //getE('ids').value = getE('ids').value +','+ Hodnota[0];
    
    getE('ids').value = 0;
    
    $('#konfigurator').find('select').each(function(){
      var hodnotaSelectu = $(this).val();
      var kod = hodnotaSelectu.split('##');
      if (kod[0] > ''){
        getE('ids').value = getE('ids').value +','+ kod[0];
      }
    });
    
    if ((Hodnota[2] != null)||(Hodnota[2] != undefined)){
      //var ImageObject = document.createElement('img');
      //var file = "/foto1/"+Hodnota[2]+".gif";
      //ImageObject.src = file;
      //getE(ImageToShow).innerHTML = '';
      //getE(ImageToShow).appendChild(ImageObject);
    }
    if ((Hodnota[6] != null)||(Hodnota[6] != undefined)){
      getE(OBJ+'Text').title = Hodnota[6];
    }
  }  

  var CelkovaHodnota = 0;
  var div = getE('konfigurator');
  var siblings = div.getElementsByTagName('select');
  for (var i = 0; i < siblings.length; i++) {
			var element = siblings[i].value;
			var Hodnota = element.split('##');
			if (parseInt(Hodnota[1]) != '0'){
        CelkovaHodnota = CelkovaHodnota + parseInt(Hodnota[1]);
      }
  }
  getE('celkem').value = parseInt(CelkovaHodnota);

}
function ShowImageKonfigurator(sObj){
  var element = document.getElementById(sObj);
  var elementArray = element.getElementsByTagName('img');
  //alert(elementArray.length);
  if (elementArray.length > 0){
    getE(sObj).style.display = 'inline';
    for (var i = 0;i < elementArray.length;i++){
      if (elementArray[i].width <= 28){
        getE(sObj).style.display = 'none';
      }else{
      }  
    }
  }  
}
function HideImageKonfigurator(sObj){
  getE(sObj).style.display = 'none';
}

function getE(objectID) {
	return document.getElementById(objectID);
}

function OrderKonfigurator() {

  var global = getE('konfigurator');
  
  var Childs = global.getElementsByTagName('select');
  
  var Code = '';
  var PriceWithDph = 0;
  var CommodityID = 0;
  var Name = '';
  var PriceWithoutDph = 0;
  var PriceDph = 0;
  for (var i = 0;i < Childs.length;i++){
    var element = Childs[i];
    var elementValue = Childs[i].value;
    var Hodnota = elementValue.split('##');
    if (Hodnota[1] > 0){
      Code = Code + Hodnota[0] + '$$'; 
      PriceWithDph = PriceWithDph + parseFloat(Hodnota[1]) + '$$'; 
      CommodityID = CommodityID + Hodnota[3] + '$$'; 
      Name = Name + Hodnota[4] + '$$'; 
      PriceWithoutDph = PriceWithoutDph + parseFloat(Hodnota[5]) + '$$';
      PriceDph = PriceDph + (parseFloat(Hodnota[1]) - parseFloat(Hodnota[5])) + '$$';
    }
  }

  getE('konf').value = 1;
  getE('konf-code').value = Code;
  getE('konf-commodity').value = CommodityID;
  getE('konf-name').value = Name;
  getE('konf-price_without_dph').value = PriceWithoutDph;
  getE('konf-price_with_dph').value = PriceWithDph;
  getE('konf-price_dph').value = PriceDph;
}

function SetLayoutCookie(sCookieName){
  document.cookie = "layout="+sCookieName;
  window.location.reload();
}
function ControlNumber(){
     if ((event.keyCode <48) || (event.keyCode >57)) event.returnValue = false;
}

/*
* google map
*/
//49.841158,18.28169
function initialize() {
      if (GBrowserIsCompatible()) {
        if(document.getElementById("map_canvas") != null){
          var map = new GMap2(document.getElementById("map_canvas"));
          map.setCenter(new GLatLng(49.841158,18.28169), 15);
          map.setUIToDefault();
        }
      }
    }

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