/* init ondomready
----------------------------------- */
onDomReady(init);

function init() {
  myPrint.init();
  imgSwitch.init();
  mapa.init();
  
  toggle.init();
  stripe.tableClass('list');
  msieFix.init();
};





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;
};





/* stripe tables
----------------------------------- */
var stripe = {
  table: function (t) {
    var odd = true;
  	for (var i = 0, count = t.rows.length; i<count; i++) {
  		t.rows[i].className += odd ? ' odd' : ' even';
  		odd = !odd;
  	}
  },

  tableAll: function () {
  	var t = document.getElementsByTagName('table');
  	for (var i = 0, count = t.length; i<count; i++) {
      this.table(t[i]);
    }
  },

  tableId: function (id) {
  	var t = getObj(id);
  	if (t) this.table(t);
  },

  tableClass: function (name) {
    var patern = new RegExp(name);
  	var t = document.getElementsByTagName('table'), table = [], j = 0;
  	for (var i = 0, count = t.length; i<count; i++) {
      if(patern.test(t[i].className)) {
        table[j] = t[i];
        j++;
      }
    }
  	for (var i = 0; i<table.length; i++) {
      this.table(table[i]);
    }
  },
  
  list: function (ul) {
    var odd = true, li = ul.getElementsByTagName('li');
  	for (var i = 0, count = li.length; i<count; i++) {
  		li[i].className += odd ? ' odd' : ' even';
  		odd = !odd;
  	}
  },
  
  listClass: function (name) {
    var patern = new RegExp(name);
  	var l = document.getElementsByTagName('ul'), ul = [], j = 0;
  	for (var i = 0, count = l.length; i<count; i++) {
      if(patern.test(l[i].className)) {
        ul[j] = l[i];
        j++;
      }
    }
  	for (var i = 0, count = ul.length; i<count; i++) {
      this.list(ul[i]);
    }
  }
};



/* toggle
----------------------------------- */
var toggle =  {  
  init: function () {  
    
    var page = false;     
    if(location.hash) {
      page = location.hash.match(/#(.+)/)[1]; 
    }            
  
    // poslat e-mailem
    if(document.body.className != 'produkt') 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(/email/.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 () { 
    this.productPage();
  },
  
  productPage: function () {
    if(!getObj('control')) return;
    
    var li = document.createElement('li');
    li.className = 'print';
    
    var a = document.createElement('a');
    a.href = '#';
    a.innerHTML = 'Tisknout'; 
    a.onclick = function () {
      window.print();
      return false;
    };    
      
    var firstLi = getObj('control').getElementsByTagName('li')[0];
    
    // umisteni
    li.appendChild(a);
    firstLi.parentNode.insertBefore(li, firstLi);
  }
}



/* 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;
    this.photo.parentNode.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;
    this.photo.alt = tmp.alt;
    this.photo.title = tmp.title;
  },
    
  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';
  }
}



/* msie: 
----------------------------------- */
var msieFix = { 
    
  init: function () {
    if(/msie 6/i.test(navigator.userAgent)) {
      msieFix.product();
    }
  },
  
  // detail produktu h3:first-child
  product: function () {  
    if(document.body.className != 'produkt' || !getObj('text-box')) return false;
    
    var h3 = getObj('text-box').getElementsByTagName('h3')[0];
    if(h3) {
      h3.style.marginTop = '-4px';
    }
  }
}









/* 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);
}



/* viewer
------------------------------------------------------------------ */

jQuery(function($) {
  // fancy box
  $("a[rel='viewer']").fancybox({
    'centerOnScroll': false,
    //'imageScale': true, 
    //'zoomOpacity': true,
		'overlayShow': false
		//'zoomSpeedIn': 500,
		//'zoomSpeedOut': 500 
  });
});

