var window_func_style_path = 'design/l1/d1/style/style.css'; 
var window_func_common_style_path = 'design/l1/common/style/style.css'; 

function getEl(id) {
   return document.getElementById(id);
}

function banner(pics) {
   var path = 'data/mod_advert/';
   for (var id in pics) {
      op_reg('b' + id, path, pics[id]);   
   }
}

function show_tab(id) {
   var tab_sec = getEl('tab-sec');
   var divs = tab_sec.getElementsByTagName('DIV');
   for (var i = 0, div; div = divs[i]; i++) {  
      if (div.id == 'tab' + id) {
         div.style.display = 'block';
         var a = getEl('tab-a-' + id);
         a.className = 'car-tab-sel';
      }
      else if (div.id.indexOf('tab') > -1) {   
         var tab_id = div.id.replace(/tab([0-9]+)/, '$1');
         var a = getEl('tab-a-' + tab_id);
         a.className = 'car-tab';
         div.style.display = 'none';
      }
   }
}

/*
 * IEContentLoaded.js
 * Author: Diego Perini (diego.perini at gmail.com) NWBOX S.r.l.
 */
function IEContentLoaded (w, fn) {
   var d = w.document, done = false,
	// only fire once
	init = function () {
		if (!done) {
			done = true;
			fn();
		}
	};
	// polling for no errors
	(function () {
		try {
			// throws errors until after ondocumentready
			d.documentElement.doScroll('left');
		} catch (e) {
			setTimeout(arguments.callee, 50);
			return;
		}
		// no errors, fire
		init();
	})();
	// trying to always fire before onload
	d.onreadystatechange = function() {
		if (d.readyState == 'complete') {
			d.onreadystatechange = null;
			init();
		}
	};
}

//************************************************************//
var op_pics = {};

function op_start() {
   for (var key in b_lang) {
      var el = getEl(key);
      if (el) {
         el.onmouseover = function(){op_set_over(this.id);}
         el.onmouseout = function(){op_set_normal(this.id);}
      }
      op_reg(key, path_lang, b_lang[key]);
   }  
}

function op_reg (id_name, path, normal_src, over_src) {

   if (over_src == null) over_src = normal_src.replace("\.", "_over\.");
   if (!op_pics[id_name]) {
      op_pics[id_name] = {};
      op_pics[id_name]['normal'] = new Image();
      op_pics[id_name]['over'] = new Image();
      op_pics[id_name]['normal'].src = path + normal_src;
      op_pics[id_name]['over'].src = path + over_src;
   }
}

function op_set_normal (id_name) {
   if (op_pics[id_name]) {
      getEl(id_name).src = op_pics[id_name]['normal'].src;
   }
}
function op_set_over(id_name) {
   if (op_pics[id_name]) {
      getEl(id_name).src = op_pics[id_name]['over'].src;
   }
}
/***************************************************/
//test emailu
function validate_email(form) {
   email = /^[-a-zA-Z0-9_.+]+@[-a-zA-Z0-9_.+]+\.[a-zA-Z0-9]+$/;
   if(!email.test(form.email.value)) {
      alert(js_text_mlist["form_bad_email"]); 
      form.email.focus(); 
      return false;
   }
   return true;
}

//test vyhledavaciho formulare	
function validate_search(form) {
   if(form.key_words.value=='' || form.key_words.value.length<2) {
      alert('Nebylo zadáno slovo pro hledání (min. 3 znaky)');
      form.key_words.focus();
      return false;
   }
   return true;
}

//*************************************************************//
//pomucka pro target _blank v xhtml strict
//pouziti: vsude
function newWindow(url)
{
	windowRef = window.open(url,"nove","");
	wasOpen = false;
	if (typeof(windowRef) == "object")
	{
		wasOpen = true;
	}
	return wasOpen;
}	

//****************************************************//
//otevreni okna s obrazkem
//pouziti: veskere pripojene obrazky tzv. male fotogalerie
function otevri_okno(obrazek, popisek)
{
	var win=window.open("","nazev","width=600,height=600,menubar=no,toolbar=no");
	win.document.write("<html><head><title>Detail</title>");
   win.document.write("<script language='javascript'>" +
                       "var arrTemp=self.location.href.split('?');" +
                       "var picUrl = (arrTemp.length>0)?arrTemp[1]:'';" +
                       "function FitPic() {" +
                       "iWidth = document.body.clientWidth;iHeight = document.body.clientHeight;" +
                       "iWidth = document.images[0].width - iWidth;iHeight = document.images[0].height - iHeight;" +
                       "window.resizeBy(iWidth, iHeight);self.focus();};</script>");
   win.document.write('<link href="'+window_func_style_path+'" type="text/css" rel="stylesheet" />');
   win.document.write('<link href="'+window_func_common_style_path +'" type="text/css" rel="stylesheet" /></head>');
	win.document.write("<body class='new-window'><table width='100%' height='100%'><tr><td valign='middle' align='center'><a href='javascript:window.close()'><img src='"+obrazek+"' border='0' alt='"+popisek+"' /></a>");
   if (popisek) {
      win.document.write('<br /><div class="new-window-text">'+popisek+'</div>');      
   }
	win.document.write("</td></tr></table><script>setTimeout(FitPic,300);</script></body></html>");
   win.document.close();
   win.focus();
}


//****************************************************//
//otevreni okna s obrazkem pro fotogalerii
function gallery_window(obrazek, jmeno, popis, i)
{
   win=window.open("","nazev","width=600,height=680,menubar=no,toolbar=no,scrollbars=yes");
	win.document.write("<html><head><title>"+jmeno+"</title>");
   win.document.write('<link href="'+window_func_style_path+'" type="text/css" rel="stylesheet" />');
   win.document.write('<link href="'+window_func_common_style_path +'" type="text/css" rel="stylesheet" /></head>');
	win.document.write('<script type="text/javascript">play_interval = 0; interval = 1;</script>');
	win.document.write('<body class="new-window">');
   win.document.write('<input type="hidden" style="display: none;" id="i" value="'+i+'" />');
   win.document.write("<table width='100%' height='100%'><tr><td valign='middle' align='center'>"); 	
   win.document.write('<div id="gallery_window_name" class="gallery-window-name">'+jmeno+'</div>');
   win.document.write("<a href='javascript:window.close()'><img id='gallery_pic' src='"+obrazek+"' border='0' alt='"+jmeno+"' /></a>");
	win.document.write('<br /><div id="gallery_window_text" class="gallery-window-text">'+popis+"</div>");
   win.document.write("</td></tr></table></body></html>");
   win.document.close();
   win.focus();
}

function gallery_play() {
   /*i = win.document.getElementById('i').value;
   if ((i+1) > js_path.length) {
      clearInterval(win.play_interval); 
      win.play_interval = 0;
      win.document.getElementById('gallery_move').innerHTML = '&gt;';
      return false;
   }*/
   gallery_move('next');
}

function gallery_move(direction) {
   i = win.document.getElementById('i').value;
   if (direction == 'next') {
      i++;
      if ((i+1) > js_path.length) return false;
   }
   if (direction == 'prev') {
      i--;
      if (i < 0) return false;   
   }
   if (direction == 'first') {
      i = 0;
   }
   if (direction == 'last') {
      i = js_path.length - 1;
   }
   win.document.getElementById('gallery_pic').src = js_path[i];
   win.document.getElementById('gallery_window_text').innerHTML = js_text[i];
   win.document.getElementById('gallery_window_name').innerHTML = js_title[i];
   win.document.getElementById('i').value = i;
   if (win.document.getElementById('link_next')) {
      win.document.getElementById('link_next').style.visibility = ((i+1) == js_path.length) ? 'hidden' : 'visible';
   }
   if (win.document.getElementById('link_prev')) {
      win.document.getElementById('link_prev').style.visibility = (i == 0) ? 'hidden' : 'visible';
   }
   if (win.document.getElementById('link_first')) {
      win.document.getElementById('link_first').style.visibility = (i == 0) ? 'hidden' : 'visible';
   }
   if (win.document.getElementById('link_last')) {
      win.document.getElementById('link_last').style.visibility = ((i+1) == js_path.length) ? 'hidden' : 'visible';
   }
   win.document.getElementById('pozice').innerHTML = '('+(i+1)+'/'+js_path.length+')';
   return false;
}

/********************************************************************************/

function DateSelector(form_input, id_name, lang){

   this.mesice = new Array('Leden',
                            'Únor',
                            'Březen',
                            'Duben',
                            'Květen',
                            'Červen',
                            'Červenec',
                            'Srpen',
                            'Září',
                            'Říjen',
                            'Listopad',
                            'Prosinec');
   this.dny = new Array('Po',
                        'Út',
                        'St',
                        'Čt',
                        'Pá',
                        'So',
                        'Ne');

  if (lang == 'en') {
     this.mesice = new Array('January',
                              'February',
                              'March',
                              'April',
                              'May',
                              'June',
                              'July',
                              'August',
                              'September',
                              'October',
                              'November',
                              'December');
     this.dny = new Array('Mo',
                          'Tu',
                          'We',
                          'Th',
                          'Fr',
                          'Sa',
                          'Su');  
  }
  if (lang == 'de') {
     this.mesice = new Array('Januar',
                              'Februar',
                              'März',
                              'April',
                              'Mai',
                              'Juni',
                              'Juli',
                              'August',
                              'September',
                              'Oktober',
                              'November',
                              'Dezember');
     this.dny = new Array('Mo',
                          'Di',
                          'Mi',
                          'Do',
                          'Fr',
                          'Sa',
                          'So');  
  }
  this.lang = lang;
  this.id_name = id_name;
  this.form_input = document.getElementById(form_input);
  this.mesic = 0;
  this.rok = 0;

  // ikonka
  date_sel_pic = document.createElement('img');
  date_sel_pic.src = date_sel_img.src;
  date_sel_pic.className = "date-sel-image";
  date_sel_pic.style.verticalAlign = 'text-bottom';
  if (lang == 'cz') {
    date_sel_pic.alt = 'výběr data z kalendáře';
    date_sel_pic.title = 'výběr data z kalendáře';
  }
  else if (lang == 'en') {
    date_sel_pic.alt = 'select date from calendar';
    date_sel_pic.title = 'select date from calendar';
  }
  else if (lang == 'de') {
    date_sel_pic.alt = 'Kalendar';
    date_sel_pic.title = 'Kalendar';
  }
  date_sel_pic.date_selector = this.id_name;
  date_sel_pic.onclick = this.popupDateSelector;

  // vykreslime ikonku
  this.form_input.parentNode.appendChild(date_sel_pic);  
   
  // div obalujici kalendar
  var dateSelectorDiv = document.createElement('div');
  dateSelectorDiv.className = "date-sel-div";
  dateSelectorDiv.style.display = 'none';
  dateSelectorDiv.id = "date_sel_div"+this.id_name;
  
  // vlozime div do stranky
  obj = document.getElementsByTagName('body')[0];
  obj.insertBefore(dateSelectorDiv, obj.firstChild);
 
  this.date_selector_div = document.getElementById("date_sel_div"+this.id_name);
 
  // vykresli tabulku
  this.updateTable();  
}

DateSelector.prototype.popupDateSelector = function() {
  obj = eval(this.date_selector);
  parent_obj = document.getElementsByTagName('body')[0]; 
  obj.date_selector_div.style.zIndex = '100';
  obj.date_selector_div.style.left = parseInt(get_x_offset(this)-get_x_offset(parent_obj)+21)+'px';
  obj.date_selector_div.style.top = parseInt(get_y_offset(this)+10)+'px';
  // zobrazime
  obj.date_selector_div.style.display = (obj.date_selector_div.style.display == 'block') ? 'none' : 'block'; 
}

DateSelector.prototype.insertIntoForm = function(den){
  this.form_input.value = den + '.' + (parseInt(this.mesic)+1) + '.' + this.rok;
  this.date_selector_div.style.display = 'none';
}

DateSelector.prototype.updateTable = function(rok, mesic){

   // neni zadan mesic nebo rok
   if (typeof(rok) == 'undefined' || typeof(mesic) == 'undefined') {
      // prevezmeme z inputu
      date_regexp = /^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{4})$/;
      if(this.form_input.value != '' && date_regexp.test(this.form_input.value)) {
         parts = date_regexp.exec(this.form_input.value);
         rok = parts[3];
         mesic = parts[2]-1;
      }
      // nebo vyplnime soucasne datum
      else { 
         new_date = new Date();
         rok = new_date.getFullYear();
         mesic = new_date.getMonth();
      }
   } 

   // dnesni datum - pro zvyrazneni bunky s dnesnim datem
   dnes_date = new Date();
   dnes_mesic = dnes_date.getMonth();
   dnes_rok = dnes_date.getFullYear();
   dnes_den = dnes_date.getDate();

   this.mesic = mesic;
   this.rok = rok;
   
   output = '';  
   // krizek pro zavreni
   close_title = 'zavřít';
   if (this.lang == 'de') close_title = 'schliessen';
   if (this.lang == 'en') close_title = 'close';
   output += '<a title="'+close_title+'" class="close-button" onmouseover="this.className=\'close-button-over\'" onmouseout="this.className=\'close-button\'" onclick="'+this.id_name+'.date_selector_div.style.display = \'none\';">×</a>';

   // horni vyber
   output += '<span class="topselect">';
   // vyber mesice
   output += '<select style="display: inline;" size="1" onchange="'+this.id_name+'.updateTable('+this.rok+',parseInt(this.value))">';
   for(i = 0; i < this.mesice.length; i++) {
     output += '<option value="'+i+'"';
     if (i == this.mesic) output += ' selected="selected"';
     output += '>'+this.mesice[i]+'</option>';
   }
   output += '</select>';
   // vyber roku
   output += '<span onmouseover="this.className=\'date-year-over\'" onmouseout="this.className=\'date-year\'" class="date-year" onclick="'+this.id_name+'.updateTable('+(this.rok-1)+','+this.mesic+')">&lt;</span><span class="year-text">&nbsp;'+this.rok+'&nbsp;</span><span class="date-year" onmouseover="this.className=\'date-year-over\'" onmouseout="this.className=\'date-year\'"  onclick="'+this.id_name+'.updateTable('+(parseInt(this.rok)+1)+','+this.mesic+')">&gt;</span>';
   output += '</span>';

   // kalendar - tabulka   
   output += '<table><tr>';
   
   // nazvy dni
   for(i = 0; i < this.dny.length; i++){
   	output += '<th>' + this.dny[i] + '</th>';
   }
   output += '</tr>';
   
   den_offset = new Date(rok, mesic, 1).getDay();
   if (den_offset == 0) den_offset = 7; // nedele
   den_outset = new Date(rok, mesic+1, 0).getDate();
      
   den_tyden = 0;	
   for(i = 1; i < den_offset; i++){
   	output += '<td onmouseover="this.style.cursor=\'default\'">&nbsp;</td>';
   	den_tyden++;
   }
   
   for(i = 1; i <= den_outset; i++){
      if (den_tyden == 7) { // nedele
   		   output += '</tr><tr>';
   			den_tyden = 0;
   	}
   	den_tyden++;
      // dnesni den zvyraznime
   	td_class = (dnes_rok == rok && dnes_mesic == mesic && dnes_den == i) ? 'date-today' : '';
      output += '<td class="'+td_class+'" onmouseover="this.className=\'date-over\'" onmouseout="this.className=\''+td_class+'\'" onclick="'+this.id_name+'.insertIntoForm('+i+')">'+i+'</td>';
   }
   
   for(i = den_tyden; i < 7; i++) {
      output += '<td onmouseover="this.style.cursor=\'default\'">&nbsp;</td>';
   }
   output += '</tr></table>';

   // dole primy vyber dnesniho dne
   output += '<span class="date-direct" onmouseover="this.className=\'date-direct-over\'" onmouseout="this.className=\'date-direct\'"  onclick="'+this.id_name+'.rok = '+dnes_rok+'; '+this.id_name+'.mesic = '+dnes_mesic+'; '+this.id_name+'.updateTable('+dnes_rok+','+dnes_mesic+'); '+this.id_name+'.insertIntoForm('+dnes_den+')">'+dnes_den+'.'+(dnes_mesic+1)+'.'+dnes_rok+'</span>';
   this.date_selector_div.innerHTML = output;  
}

function get_x_offset(obj){
  var x = 0;
  if(obj.offsetParent){
    while(obj.offsetParent){
      x += obj.offsetLeft;
      obj=obj.offsetParent;
    }
  }
  else if(obj.x) x += obj.x;
  return parseInt(x);
}

function get_y_offset(obj){
  var y =0;
  if(obj.offsetParent){
    while(obj.offsetParent){
      y +=obj.offsetTop
      obj=obj.offsetParent;
    }
  }
 else if(obj.y) y += obj.y;
  return parseInt(y);
}
/****************************/
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

var path_common = "design/l1/d1/images/";
var path_lang = "design/l1/d1/cz/images/"
var b_lang = {'button_search': 'button_search.gif', 'button_box_mlist': 'button_ok.gif',
              'cont': 'button_all_contact.gif', 'vice': 'button_vice.gif',
              'full': 'button_full.gif', 'search2': 'btn_search2.gif',
              'zajem': 'btn_form.gif', 'full-ojete': 'button_full.gif',
              'd_akcni': 'detail_akcni.gif', 'd_cenik': 'detail_cenik.gif',
              'd_form': 'detail_form.gif', 'd_katalog': 'detail_katalog.gif'};
//for (key in b_lang) {op_reg(key, path_lang, b_lang[key]);}
for(var i = 1; i < 11; i++) {
   op_reg('btn' + i, path_lang, 'btn_'+i+'.gif');
}
date_sel_img = new Image();
date_sel_img.src = path_common+'date_selector.gif';

/***************************************/
function gmapa() {
   if (GBrowserIsCompatible()) {     
      var map = new GMap2(document.getElementById('gmapa'));
      map.addMapType(G_PHYSICAL_MAP);
      map.setMapType(G_HYBRID_MAP);
      map.setCenter(new GLatLng(50.03583584797169, 14.508047103881836), 17);
      map.enableContinuousZoom();
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.addControl(new GOverviewMapControl());
      var marker = new GMarker(new GLatLng(50.03583584797169, 14.508047103881836));         
      GEvent.addListener(marker, "click", function() {
         marker.openInfoWindowHtml('<strong>Ford Valoušek</strong><br />Ke stáčírně 1167<br >Praha 4');
      });
      map.addOverlay(marker);         
      GEvent.trigger(marker, 'click');
   }
}

function setPageSize ( elem ) {
  elem.style.height = elem.contentWindow.document.body.scrollHeight;
  elem.style.width = elem.contentWindow.document.body.scrollWidth;
}

if (window.attachEvent && typeof document.fileSize != 'undefined') {
   IEContentLoaded (window, op_start);
   window.attachEvent('onload', op_start);
}
else if (window.addEventListener) {
   window.addEventListener('DOMContentLoaded', op_start, false);
   window.addEventListener('load', op_start, false);
}

