

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/* show, hide */
function show(id,data){
  var obj  = $(id);
  if(obj){
    obj.style.display = "block";
    if(data) obj.innerHTML = data;
  }
}


function hide(id){
  var obj  = $(id);
  if(obj){
    obj.style.display = "none";
  }
}

// JavaScript Document
/* NAPOVEDA */
var napoveda = {
  ukaz:function(e) {
    
    var nBox = document.getElementById('napovedaBox');
    e = napoveda.getEvent(e)
    nBox.innerHTML = this.title;
    this.title = '';
    nBox = nBox.style;
    
    nBox.top = (this.getPosition().y +15) + 'px';
    nBox.left = (this.getPosition().x +15) + 'px';
    nBox.display = 'block';
    this.onmouseover = '';
  },

  skryj:function(e) {
    var nBox = document.getElementById('napovedaBox');
    this.title=nBox.innerHTML;
    nBox.innerHTML='';
    nBox = nBox.style;
    nBox.display='none';
    nBox.top = '';
    nBox.left = '';
    this.onmouseover = napoveda.ukaz;
  },

  getEvent : function (e) {
    if (!e) return window.event;
    return e;
  },



  init:function() {
    // alert('init');
    // try {
    
      if(!$('napovedaBox')) {
        napBox = new Element('div', {'id':'napovedaBox'}).inject($(document.body));
      }
      
      var boxy = $$('.napovedaPop');

      // alert(boxy);

      for(var i=0; i<boxy.length; i++) {
       // alert('a');
        boxy[i].onmouseover = napoveda.ukaz;
        boxy[i].onmouseout = napoveda.skryj;
      }
    // } catch(e) { alert(e);}
  }

}

var libi = {
	init : function(){
	  
		var libis = $$('.libi')
		for(var i=0;i<libis.length;i++){
		    libis[i].addEvent('click',function(e){ 
              libi.hlasuj(e);
              return false;
        
        });
		}
	},
	hlasuj : function(e){
	  
     
	  
	  
		 var obj = e.target;
		 e.stop();
		
	 	obj.setStyle('cursor', 'progress');
		
		var id = obj.id.replace(/libi/,'').replace(/\-.*/,'');

		var a = new Request(
		  {
		    url : '/php/libi.php',
  			method:'post',
  			data : {sekce: url, id: id},
  			
        onSuccess: function(transport){    
          obj.setStyle('cursor', 'default');
          libi.zobraz(id,transport);
        },
        
  			onFailure: function(){
          obj.setStyle('cursor', 'default');
          alert('Głosowanie nie powiodło się.') 
        }
      
		  });
    a.send();		  
		
		try {
      var foxterform = document.getElementById('foxterform');
      
      if(foxterform == null || foxterform == '') {
        foxterform = obj.parentNode.getElementsByTagName('form')[0];
      }
      
      if(foxterform) {
        
        if(confirm('Líbí? Tak to přidej na Foxter.cz!')) {
          foxterform.submit();
        }
      }
    } catch(e) { }
    
    return false;
    
	},
	zobraz : function(id,resp){
		if(resp == 'false')
		{
			alert('Możesz głosować tylko raz!')
		}
		else
		{
			try
			{
				$('libi'+id.toString()).getElementsByTagName('SPAN')[0].innerHTML = '('+resp.toString()+')'
        //$('libi'+id).descendants[1].update('('+resp+')')
			}
			catch(e){
			}
			try
			{
        $('libi'+id.toString()+'-2').getElementsByTagName('SPAN')[0].innerHTML = '('+resp.toString()+')'
				//$('libi'+id+'-2').descendants[1].update('('+resp+')')
			}catch(e){
      }

		}
	}
}
// JavaScript Document

function more(co, vice, mene) {
  var prvek = document.getElementById('srchSkryte').style;
  try {
    if(prvek.display=='none' || prvek.display=='') {
      prvek.display = 'block';
      co.innerHTML = mene;
    } else if(prvek.display == 'block') {
      prvek.display = 'none';
      co.innerHTML = vice;
    }

  } catch(e) {}

}

function maxWH(obj, sirka, vyska) {
  obj.onload = function() {

    var w = this.width;
    var h = this.height;

    var pomerW = w/sirka;
    var pomerH = h/vyska;

    // alert (w + ' ' + h);
    // alert(w + ' ' + h + ' ' + sirka + ' ' + vyska + ' ' pomerW + ' ' + pomerH);
    if((pomerW >= pomerH) && (w > sirka)) {
      this.style.width = sirka + 'px';
      this.style.height = 'auto'

    } else if((pomerW < pomerH) && (h > vyska)) {
      this.style.height = vyska + 'px';
      this.style.width = 'auto';
    }
  }
}


function resetAkcniForm(idcko) {
  $('akcninabidka_'+ idcko +'_barvatext').style.backgroundColor = '#000000';
  $('akcninabidka_'+ idcko +'_barvatext').style.color = '#FFFFFF';
  $('akcninabidka_'+ idcko +'_barvatext').value= '000000';
  
  $('akcninabidka_'+ idcko +'_barvaback').style.backgroundColor = '#FFFFFF';
  $('akcninabidka_'+ idcko +'_barvaback').style.color = '#000000';
  $('akcninabidka_'+ idcko +'_barvaback').value = 'FFFFFF';
  
  $('akcninabidka_'+ idcko +'_barvaframe').style.backgroundColor = '#B2B2B2';
  $('akcninabidka_'+ idcko +'_barvaframe').style.color = '#000000';
  $('akcninabidka_'+ idcko +'_barvaframe').value = 'B2B2B2';
  
  $('akcninabidka_'+ idcko +'_barvalink').style.backgroundColor = '#E19002';
  $('akcninabidka_'+ idcko +'_barvalink').style.color = '#000000';
  $('akcninabidka_'+ idcko +'_barvalink').value = 'E19002';
  
  $('defaultColorsButt').blur();
  
  return false;
}





var dokosiku = {
		
	timeout: null,
  linkSeznam : 'seznam', 
  kosikyInner : false,
	
	init: function(){ 
	  
	  
	  if(typeof(lnkSeznam) != 'undefined') { this.linkSeznam = lnkSeznam;  }
	  
	  var a = new Request({ 
          url: '/'+ this.linkSeznam +'/a/kosiky/',
	        method:'post',
	        data: {'href': location.href},
	        onSuccess: function(transport){
	             dokosiku.kosiky(transport)
	        },
	        onFailure: function(){
                  var elms = $$('a.dokosiku');		
	                $each(elms, function(val) {
                    val.set('href', '/'+ dokosiku.linkSeznam +'/');
                  });
              	 
          }
	      });
    a.send();	
	},
	
	kosiky: function(inner){
	 var elms = $$('a.dokosiku');		
	
   if(inner.indexOf('not_logged') >= 0){
      	
			$each(elms, function(val) {
        val.set('href', '/'+ dokosiku.linkSeznam +'/');
      });
			return false;
		} 
		
		
		
		var kosiky = new Element('div' , {
      id: 'kosiky',
      styles: {'background': 'none',
               'display':'none',
               'position':'absolute' 
              }  
    });
    
    this.kosikyInner = new Element('div', {
      id: 'kosikyInner',
      styles: {'background': 'white',
               'position':'absolute',
               'border':'1px solid #B2B2B2',
               'top': 19,
               'left': 0,
               'white-space': 'nowrap',
               'padding': 3,
               'text-align': 'left' 
              },
      html : inner
    }).inject(kosiky);
    
    var kosikyTop = new Element('div', {
      id: 'kosikyTop',
      styles: {'background': 'white',
               'top': 0,
               'left': 0,
               'position':'absolute',
               'border-left':'1px solid #B2B2B2',
               'border-right':'1px solid #B2B2B2',
               'border-top':'1px solid #B2B2B2',
               'height':20,
               'line-height':20,
               'width':'auto',
               'white-space': 'nowrap',
               'padding': '0 5px',
               'color': '#E08F02',
               'font-weight': 'bold'  
              }
    }).inject(kosiky);
    
   
    
    kosiky.inject(document.body);
    
    
    $('kosiksubmit').addEvent('click',function(ev){dokosiku.insert(ev)});
		$('kosikyInner').addEvent('mouseout',function(ev){dokosiku.hide(ev)});
		$('kosikyInner').addEvent('mouseover',function(ev){clearTimeout(dokosiku.timeout)});
		$('kosikyTop').addEvent('mouseout',function(ev){dokosiku.hide(ev)});
		$('kosikyTop').addEvent('mouseover',function(ev){clearTimeout(dokosiku.timeout)});

		
		$each(elms , function(val){        
                  val.addEvent('click', function(ev){
                  dokosiku.kosik(ev);
        });         
    });
	},
	
	kosik: function(ev){
	
    var el = ev.target;
		ev.stop();
    $('kosiky-vyrobek').value = el.href.substr( el.href.indexOf('#') ).replace(/[^0-9]/g, ''); // el.href.replace(/[^0-9]/g, '');
		        
		$('kosiky').setStyle('top' , el.getPosition().y);
		$('kosiky').setStyle('left', el.getPosition().x);
    $('kosiky').style.width = 'auto';
		$('kosiky').style.height = 'auto';
		$('kosikyInner').style.width = 'auto';
		$('kosikyInner').style.height = 'auto';
		$('kosikyTop').set('html', el.get('html'))  ;
		el.blur();
		$('kosiky').show();
	},
	
	hide: function(ev){
		var el = ev.target;
		dokosiku.timeout = setTimeout(function(){$('kosiky').hide()},1000)
	},
	
	insert: function(ev){
		ev.stop();
		var a = new Request({
              url:'/'+ this.linkSeznam +'/',
			        method:'post',
			        data: $('kosikform'),
			        onRequest: function(){
			           $('kosiky').setStyle('display','none');
			           infoOverlay.loaderShow();
			        },
			        onSuccess: function(transport){
			           dokosiku.reset();
			           infoOverlay.infoBoxShow(transport,1);
			           infoOverlay.hideDelay(2000);
			        },
			        onFailure: function(){
			           dokosiku.reset();
                 infoOverlay.infoBoxShow('FAIL',1);
			           infoOverlay.hideDelay(2000);
              }
			      });	
     a.send();	
	}, 
	
	reset : function() {
      var a = new Request({ 
          url: '/'+ this.linkSeznam +'/a/kosiky/',
	        onSuccess: function(transport){
	             dokosiku.kosikyInner.set('html', transport);
	             $('kosiksubmit').addEvent('click',function(ev){dokosiku.insert(ev)});
	        }
	      });
      a.send();	
      
  }
	

	
	
	
	
		
}







var showHideLabel = function(from, dest, showLabel, hideLabel) {
  var fromEl = $(from);
  var destEl = $(dest); 
 
  if(destEl.getStyle('display')== 'none') {
      fromEl.set('html', hideLabel);
      destEl.setStyle('display', 'block');
  } else {
      fromEl.set('html', showLabel);
      destEl.setStyle('display', 'none');
  } 
}



window.addEvent('domready', function(){
 try{krajForm('v');}catch(e){}
 try{krajForm('l');}catch(e){}
 try{krajForm('k');}catch(e){}
 try{krajForm('p');}catch(e){}
 
 try{libi.init();}catch(e){}
 try{makeFocus();} catch(e){}
 
}); 

window.addEvent('domready', function(){
  // alert('s')
  try{
    napoveda.init();
  } catch(e){}
});


window.addEvent('domready', function(){
  if($$('a.dokosiku') != '' && $$('a.dokosiku') != []) {
    dokosiku.init(); 
  }
});





var infoOverlay = {
  
  ready : false,
  staticServer : '',
  overlay : false,
  infoBox : false,
  loader : false,
  closeBut : false,
  timer : false,

  init : function() {
    if(typeof(srvStatic) != 'undefined') { this.staticServer = srvStatic; }
    
    if(!this.ready) {
        this.overlay = new Element('div' , {
          id: 'infoOverlay',
          styles: {'width': '100%',
                   'height':'100%',
                   'display':'none',
                   'position' : 'absolute',
                   'left':0,
                   'top':0,
                   'background' : 'none' 
                  }
        }).inject(document.body);
        
        this.infoBox = new Element('div' , {
          id: 'infoOverlayInner',
          styles: {'width': 'auto',
                   'height':'auto',
                   'display':'none',
                    'background': 'white',
                    'color': '#E08F02',
                    'font-weight': 'bold',  
                    'border':'15px solid #B2B2B2',
                    'position' : 'absolute',
                    'padding': '15px 45px'
                    
                  }
        }).inject(this.overlay);
        
        this.loader = new Element('img', {
          id : 'infoOverLoader',
          src : this.staticServer + '/images/redesign/loader.gif',
          styles : {'border' : 0,
                    'padding' : 15
                  }
        });
        
        this.closeBut = new Element('img', {
          'id' : 'infoOverLoader',
          'src' : this.staticServer + '/images/redesign/close2.gif',
          'styles': {'border' : 0,
                    'position' : 'absolute',
                    'top' : 0,
                    'right': 0
                  },
          'events' : { 'click' : function() { infoOverlay.hide(); } 
                  }
        });
        
        this.ready = true;   
    }     
  },
  
  infoBoxShow : function(cont,close) {
      if(!this.ready) { this.init(); }
      $clear(this.timer);
      this.overlayFull();      
      this.badElHide();      
      this.overlay.setStyle('display', 'block');
      if(cont != '') {
        this.infoBox.set('html', cont);
      }
      this.infoBox.setStyle('display', 'block');
      this.infoBoxCenter(); 
      if(close == 1 ){
        this.closeBut.inject(this.infoBox);
      } 
      this.scrollResizeStart();
           
      
  },
  
  infoBoxChange : function(cont, close) {
      if(!this.ready) { this.init(); }
      $clear(this.timer);
      if(this.infoBox.getStyle('display') != 'block') {
        this.infoBox.setStyle('display', 'block');        
      }            
      if(cont != '') {
        this.infoBox.set('html', cont);
      }
      this.infoBoxCenter();  
      if(close == 1 ){
        this.closeBut.inject(this.infoBox);
      } 
  },
  
  hide : function() {
      if(!this.ready) { this.init(); }
      this.infoBox.setStyle('display', 'none');
      this.badElShow();
      this.overlay.setStyle('display', 'none');
      this.scrollResizeStop();
      this.timer = false;
  },
  
  hideDelay : function(delay) {
    this.timer = (function() { infoOverlay.hide(); }).delay(delay);    
  },
  
  loaderShow : function(close) {
    if(!this.ready) { this.init(); }
    $clear(this.timer);
    this.infoBox.empty();
    
    this.loader.inject(this.infoBox);
    this.infoBoxShow('',close);
    
    this.infoBoxCenter();
     
  },

  infoBoxCenter : function() {
      if(!this.ready) { this.init(); }
      var dim = this.infoBox.getSize();    
      this.infoBox.setStyles({
        'left' : ( getScroll().x +  (getSize().x / 2) - (dim.x / 2) ) ,
        'top' : ( getScroll().y +  (getSize().y / 2) - (dim.y / 2) )
      });
  },
  
  overlayFull : function() {
      if(!this.ready) { this.init(); }
      this.overlay.setStyles({
          'width': getScrollSize().x ,
          'height': getScrollSize().y 
        });
  },
  
  badElHide : function() {
    $each($$('object'), function(el){
      el.style.visibility = 'hidden';
    });
    $each($$('embed'), function(el){
      el.style.visibility = 'hidden';
    });
   
  },
  
  badElShow : function() {
    $each($$('object'), function(el){
      el.style.visibility = 'visible';
    });
    $each($$('embed'), function(el){
      el.style.visibility = 'visible';
    });
  },
  
  scrollResizeStart : function() {
    window.addEvent('resize', function() { infoOverlay.infoBoxCenter() });
    window.addEvent('scroll', function() { infoOverlay.infoBoxCenter() });    
  },
  
  scrollResizeStop : function() {
    window.removeEvents('resize');
    window.removeEvents('scroll');    
  }
  
    
  
  
}
