var preloaded = new Array();
function ihover() {
	var i = 0;
  var images = new Array();
	$$('img').each(function(image){
		if(image.src.test('-hp-')) {
			images[i] = image;
			i++;
		}
	});
	$$('input').each(function(image){
		if(image.type=='image' && image.src.test('-hp-')) {
			images[i] = image;
			i++;
		}
	});
	for (var i = 0; i < images.length; i++){
   preloaded[i] = document.createElement('img');
   preloaded[i].setAttribute('src',images[i].src.replace("-hp-","-ha-"));
  };
	images.each(function(image){
		image.onmouseover = function(){ image.src = image.src.replace("-hp-","-ha-"); }
		image.onmouseout  = function(){ image.src = image.src.replace("-ha-","-hp-"); }	
	});
}
function advanced_clases(){
	$$('table').each(function(table){
		if(table.hasClass('velikosti')||table.hasClass('objednavky')){
			count = 0;
			trs = table.getElementsByTagName('tr');
			$$(trs).each(function(tr){
				if(!(count%2)){	tr.addClass('sudy'); }
				else { tr.addClass('lichy');	}
				count++;							
				tr.addEvent('mouseover',function(){ tr.addClass('focus'); });
				tr.addEvent('mouseout',function(){ tr.removeClass('focus'); });
			});
			$$(trs[0]).addClass('prvni');
			$$(trs[--count]).addClass('posledni');
		}
	});
	$$('input').each(function(input){
		input.addClass(input.type);
		if(input.type=='text') {
			input.onfocus = function(){ input.addClass('focus'); }
			input.onblur = function(){ input.removeClass('focus'); }
		}
	});
	$$('textarea').each(function(txta){
		txta.onfocus = function(){ txta.addClass('focus'); }
		txta.onblur = function(){ txta.removeClass('focus'); }
	});
	
}


//rozbalovani nejprodavanejsiho zbozi
var probihapresun=false;
var otevreny = 0; //index otevreneho boxu - ma tridu open
var pocetboxu = 5;
var delka_slide = 300;
var vyska_img = 80;
function rozbal_bnp(){
	$$('div.bnp').each(function(bnp, thisindex){
		bnp.addEvent('mouseover',function(){
			//projed vsecky boxy
			$$('div.bnp').each(function(bnp2, index){
				//nastaveni efektu
				hideSlide = new Fx.Style(bnp2.childNodes[0], 'height', { duration: delka_slide, transition: Fx.Transitions.Quart.easeInOut });
				showSlide = new Fx.Style(bnp.childNodes[0], 'height', { duration: delka_slide, transition: Fx.Transitions.Quart.easeInOut });
				//pokud prave_otevirany<>otevreny, prave_otevirany.open, neprobiha presun, neprekrocime pocet boxu pri citani
				if( thisindex!=index && bnp2.hasClass('open') && !probihapresun && index<pocetboxu) {
					//alert('closing'+index+',opening'+thisindex)
					//zablokuj jine presuny
					probihapresun=true;
					//spust otevreni oteviraneho boxu
					showSlide.start(0,vyska_img).chain(function(){
						bnp.addClass('open');
					});
					//spust zavirani otevreneho boxu
					hideSlide.start(vyska_img,0).chain(function(){
						bnp2.removeClass('open');
						//po dokonceni povol presun
						probihapresun=false;
					});
				}
			});
		});
	});
}

//prepinani prihlaseni
function wannalogin(){
	$('prihl_not').addClass('hidden');
	$('prihl_want').removeClass('hidden');
}
//prihlasovaci formular
function clearform(){
	if($('prihl_login')){
		$('prihl_login').addEvent('focus', function(){ if(this.value==$('pll').innerHTML){ this.value=''; }});
		$('prihl_login').addEvent('blur', function(){ if(this.value==''){ this.value=$('pll').innerHTML; }});
		$('prihl_heslo').addEvent('focus', function(){ if(this.value==$('phl').innerHTML){ this.value=''; this.type='password'; }});
		$('prihl_heslo').addEvent('blur', function(){ if(this.value==''){ this.value=$('phl').innerHTML; this.type='text'; }});
	}
}

// pridat stranku do zaloziek
function AddFavorite(linkObj,addUrl3,addTitle3)
{
     if (document.all && !window.opera){
         window.external.AddFavorite(addUrl3,addTitle3);
         return false;
     }
     else if (window.opera && window.print)     {
         linkObj.title = addTitle3;
         return true;
     }
     else if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function')) {
         window.sidebar.addPanel(addTitle3,addUrl3,'');
         return false;
     }
     window.alert('Po potvrzení stiskněte CTRL-D,\nstránka bude přidána k vašim oblíbeným odkazům.');
     return false;
}




// ----- POPUP START ---------------------------------------------------

// Prehod jQuery do nekonfliktniho rezimu, aby mohl byt spolu s jinymi frameworky
jQuery.noConflict();

/**
 * Ajax popup/splash engine
 */
function popupX (code, cname, expires, setconclick, callback)
{
  var self = this;

  // Requirements check
  if(setconclick && typeof(jQuery.cookie) != 'function')
  {
    if(typeof(console)=='object')
    {
      console.error('popupX: Cannot find jQuery.cookie plugin!');
    }
    return false;
  }

  /**
   * Constructor
   */
  self.init = function(code, cname, expires, setconclick, callback){

    self.expires = expires;
    self.setconclick = setconclick;
    self.cname = cname;
    self.data = jQuery(code);
    // wait for document ready
    jQuery(document).ready(function() {
        //rozbal_bnp();
      // append popup
      jQuery('body').append(self.data);
      // store placeholder
      self.placeholder = jQuery('#popPlaceholder');//self.data;
      // bind actions
      self.placeholder.find('a').click(function(){
        self.hide();
        if(jQuery(this).attr('href')=='#')
        {
          return false;
        }
        else
        {
          return true;
        }
      });
      self.show();
      // call callback
      if(typeof callback == 'function')
      {
        callback();
      }

    });
  };

  /**
   * Set cookie to displayed state
   */
  self.setDisplayedCookie = function()
  {
    if(self.setconclick)
    {
      jQuery.cookie(self.cname, true, {path:'/', expires: self.expires });
    }
  };

  /**
   * Show popup
   */
  self.show = function()
  {
    self.ie6fix();
    //self.placeholder.show();
  };

  /**
   * Hide popup
   */
  self.hide = function()
  {
    self.ie6unfix();
    self.setDisplayedCookie();
    self.placeholder.hide();
  };

  /**
   * Remove popup
   */
  self.remove = function()
  {
    var self = this;
    self.placeholder.remove();
    self.placeholder = false;
  };

  /**
   * Fix for IE6 - hide selects
   */
  self.ie6fix = function()
  {
    /*
    if(! (jQuery.browser.msie && parseInt(jQuery.browser.version) <= 6 )) return false;
    self.fixedSelects = jQuery('select:visible');
    self.fixedSelects.css('visibility', 'hidden');
    */
  };

  /**
   * Unfix for IE6
   */
  self.ie6unfix = function()
  {
    /*
    if(typeof(self.fixedSelects)!=='object') return false;
    self.fixedSelects.css('visibility', 'visible');
    */
  };

  // Initialize
  self.init(code, cname, expires, setconclick, callback);
};

// ----- POPUP END ----------------------------------------------------


window.addEvent('domready', function(){
	ihover();
	advanced_clases();
	rozbal_bnp();
	clearform();
});


