/* init ondomready
----------------------------------- */
onDomReady(init);

function init() {  
  myPrint.init();
  imgSwitch.init();
  mapa.init();
  toggle.init();
  stripeAllTables();
};





function onDomReady(f){
  var a = onDomReady, b = navigator.userAgent, d = document, w = window, c="onDomReady", e = "addEventListener", o = "opera", r = "readyState", s = "<scr".concat("ipt defer src='//:' on",r,"change='if(this.",r," == \"complete\"){this.parentNode.removeChild(this);",c,".",c,"()}'></scr","ipt>");
  a[c] = (function(o){return function(){a[c] = function(){}; for(a=arguments.callee;!a.done; a.done = 1) f(o ? o() : o)}}) (a[c]);
  if(d[e])d[e]("DOMContentLoaded",a[c],false);
  if(/WebKit|Khtml/i.test(b)||(w[o]&&parseInt(w[o].version())<9)) (function(){/loaded|complete/.test(d[r])?a[c]() : setTimeout(arguments.callee,1)})();
  else if(/MSIE/i.test(b))d.write(s);
};


function getObj(id) {
  var obj = document.getElementById ? document.getElementById(id) : document.all[id];  
  return obj;
};



/* toggle
----------------------------------- */
var toggle =  {  
  init: function () {  
    
    var page = false;     
    if(location.hash) {
      page = location.hash.match(/#(.+)/)[1]; 
    }            
  
    // poslat e-mailem
    if(document.body.className != 'subpage') return;    
    if(!getObj('submit')) return;
    
    var page = false;     
    if(location.hash) {
      page = location.hash.match(/#(.+)/)[1]; 
    } 
    var box = getObj('submit');
    if (!page) {
      toggle.hide(box);             
    } else {
      toggle.show(box);  
    }
    
    var kotva;
    var li = getObj('control').getElementsByTagName('li');    
    for(var i = 0, count = li.length; i<count; i++) {
      if(/mail/.test(li[i].className)) {
        kotva = li[i].getElementsByTagName('a')[0];
        kotva.onclick = function () {
          toggle.set(box);
          return false;   
        };
      }       
    }
        
  },
   
  set: function (obj) {
    if(obj.style.display == 'block') {
      toggle.hide(obj);
    } else {
      toggle.show(obj);
    }    
  }, 
  
  show: function (obj) {
    obj.style.display = 'block'; 
  },
    
  hide: function (obj) {
    //getObj('footer').style.position = 'relative';
    obj.style.display = 'none';    
  }
};



/* produkt: tisk
----------------------------------- */
var myPrint = {  
  init: function () { 

    if(!getObj('control')) return;
    
    var li = document.createElement('li');
    li.className = 'tisk';
    
    var a = document.createElement('a');
    a.href = '#';
    a.innerHTML = 'Tisknout'; 
    a.onclick = function () {
      window.print();
      return false;
    };    
      
    var item = getObj('control').getElementsByTagName('li');
    for(var i=0, count=item.length; i<count; i++) {
      if (/koupit/i.test(item[i].className)) {        
        var targ = item[i]; 
      }
    }
    
    // umisteni
    li.appendChild(a);
    targ.parentNode.insertBefore(li, targ);
  }
}



/* produkt detail: zmena nahledu
----------------------------------- */
var imgSwitch = {
  link: [],
  
  init: function () {
    var box = getObj('img-set');
    if(!box) return;
    
    this.link = box.getElementsByTagName('a');    
    this.photo = this.link[0].getElementsByTagName('img')[0];
    
    // prvni nahled jako aktivni
    if(box.getElementsByTagName('a')[1])
      box.getElementsByTagName('a')[1].className = 'set';
            
    var i = 1, count = this.link.length;
    for(i; i<count; i++) {    
      this.link[i].onclick = function() {
        // zvyrazni aktivni nahled
        imgSwitch.cleanup();
        this.className = 'set';
        // nahrad obrazek    
        imgSwitch.load(this);
        return false;
      };
    }
  },
    
  load: function (obj) {     
    // docasny obrazek
    var tmp = new Image(); 
    
    /* adresare: 116x83 | 238x149 | fullsize */
    tmp.src = obj.getElementsByTagName('img')[0].src.replace(/116x83/, '238x149');
    tmp.title = obj.getElementsByTagName('img')[0].title;
    tmp.alt = obj.getElementsByTagName('img')[0].alt;        
    tmp.href = obj.getElementsByTagName('img')[0].src.replace(/116x83/, 'FULL');
		
    // po nacteni muzeme zobrazit
    tmp.onload = function () {
      imgSwitch.show(this);        
    };   
    if(tmp.complete) {
      imgSwitch.show(tmp);   
    }     
  },
    
  show: function (tmp) {
    this.photo.src = tmp.src.replace(/https?:\/\/[0-9a-zA-Z-\.]*/, '');
    this.photo.alt = tmp.alt;
    this.photo.title = tmp.title;
		this.photo.parentNode.href = tmp.href.replace(/https?:\/\/[0-9a-zA-Z-\.]*/, '');
  },
    
  cleanup: function () {
    var i = 1, count = this.link.length;
    for(i; i<count; i++) {
      this.link[i].className = '';  
    }
  }   
}



/* prodejny: mapa
----------------------------------- */
var mapa = { 
  region: [],
    
  init: function () {
    if(!getObj('mapa')) return; 
    
    this.region[1] = 'Praha';
    this.region[2] = 'Středočeský kraj';
    this.region[3] = 'Ústecký kraj';
    this.region[4] = 'Karlovarský kraj';
    this.region[5] = 'Plzeňský kraj';
    this.region[6] = 'Jihočeský kraj';
    this.region[7] = 'Liberecký kraj';
    this.region[8] = 'Královehradecký kraj';
    this.region[9] = 'Pardubický kraj';
    this.region[10] = 'Kraj Vysočina';
    this.region[11] = 'Jihomoravský kraj';
    this.region[12] = 'Olomoucký kraj';
    this.region[13] = 'Moravskoslezský kraj';
    this.region[14] = 'Zlínský kraj';    

    this.box = getObj('mapa');
    this.box.onmouseout = this.reset;
    
    var area = this.box.getElementsByTagName('area');        
    var i = 0, count = area.length;     
    for(i; i<count; i++) {
      area[i].onmouseover = this.setup;
    }
    
    this.width = this.box.clientWidth;
    this.height = this.box.clientHeight;    
  },
  
  setup: function () {  
    var i = 0, count = mapa.region.length, k = 0; 
    for(i; i<count; i++) {
      if(this.title == mapa.region[i]) {
        k = i;
        //alert(k);
      }    
    }
             
    mapa.box.style.backgroundPosition = '0px -' + (k * mapa.height) + 'px';
  },
  
  reset: function() { 
    this.style.backgroundPosition = '0px 0px';
  }
}



/* filip
------------------------------------------------------------------ */
function var_dump(obj) {
	var result = '';
	for (var i in obj) {
		result += obj['nodeName'] + '.' + i + ' = ' + obj[i] + '<br />';
	}
	result += '<hr />';
	return result;
}

function cWin(url, name, w, h, resizable, scrollbars, menubar, toolbar) {
	var menub = (menubar == true) ? "yes" : "no";
	var scrollb = (scrollbars == true) ? "yes" : "no";
	var toolb = (toolbar == true) ? "yes" : "no"; 
	var resiz = (resizable == true) ? "yes" : "no";
	
	retVal = window.open(url,name,'status=no,width='+w+',height='+h+',resizable='+resiz+',menubar='+menub+',scrollbars='+scrollb+',toolbar='+toolb);
}

/* stripe table */
function stripeTable(t) {
	var i, odd = true;
	for (i=0; i<t.rows.length; i++) {
		t.rows[i].className += odd ? ' odd' : ' even';
		odd = !odd;
		}
	}
function stripeTableById(id) {
	var t = document.getElementById(id);
	if (t) stripeTable(t);
	}

function stripeAllTables() {
	var t = document.getElementsByTagName('TABLE');
	for (var i=0; i<t.length; i++) stripeTable(t[i])
	}

