var is_ie = (navigator.userAgent.toLowerCase().indexOf("msie") != -1);
var IE = (document.all && document.getElementById && !window.opera);
var FF = (!document.all && document.getElementById && !window.opera);
var OP = (document.all && document.getElementById && window.opera);
var IE7 = (navigator.userAgent.indexOf('MSIE 7')>-1)?true:false;
var IE8 = (navigator.userAgent.indexOf('MSIE 8')>-1)?true:false;
var IE6 = (navigator.userAgent.indexOf('MSIE 6')>-1)?true:false;


window.onload = function() {
  StartInit();
}


function mp(obj, ext, domain, alias) {
 
  var a = document.createElement('a');
  a.href = 'mailto:'+alias+'@'+domain+'.'+ext;
  a.innerHTML = alias+'@'+domain+'.'+ext;
  
  obj.parentNode.appendChild(a);
  obj.parentNode.removeChild(obj);
}


function priceFormat(x) {
  var x = x + '';
  var ns = '';
  var k=0;
  
  for(var i=x.length-1;i>=0;i--) {
    if(k % 3 == 0 && k > 0) {
     ns += ' ';
   }
   
    ns += x.substr(i,1);
    k++;
  }
  
  var r = '';
  for(var i=ns.length-1;i>=0;i--) {
    r += ns.substr(i,1);
  }
  
  return r;
}

function prVisit(obj, type) {
  return true;
  
  var id = obj.href.match(/^.*zbozi\/(\d+)-.*$/)[1];
  var cookie = {'referer' : escape(window.location),
                'type' : type,
                'pid' : id,
                'old' : 0
              };
          
  //setCookie('visit', serialize(cookie), false, '/', window.domain);
  return true;
}

  
function StartInit() {
  TopMenu();
  
  if(window.screen.width > 1024 && ge('footerNew')) {
    ge('footerNew').setAttribute('id', 'footerNew2');
    ge('stinobal').setAttribute('id', 'stinobal2');

    FlashStart();
  }else if(ge('rightFlash')) {
  	ge('rightFlash').style.display = 'none';
    //ge('rightFlash').childNodes[0].style.position = 'absolute';
  }
  
  
  // flashe pro ie
  if(is_ie) {
    objects = document.getElementsByTagName("object");
    for (var i = 0; i < objects.length; i++)  {
      objects[i].outerHTML = objects[i].outerHTML;
    }
  }
  
  if(typeof(fixActionText) == 'function')
    fixActionText();
}

function TopMenu() {
  var uls = getElementsByClass('sub', ge('Menu'), 'ul');
  
   for(var i=0;i<uls.length;i++) {
    uls[i].parentNode._sm = uls[i]; 
    uls[i].parentNode.onmouseover = function() {
      this._sm.style.display = 'block';
    }
    uls[i].parentNode.onmouseout = function() {
      this._sm.style.display = 'none';
    }
  }
}

function FlashStart() {
  var flash = ge('rightFlash');
  
  if(!flash)
    return false;
  
  var div = ge('rightFlash').childNodes[0];
  var defaultTop = flash.offsetTop;
  
  window.onscroll = function (e) {
    var sc = getScrollXY();
    var y = sc[1];
    var x = sc[0];
    
    
    if(y > defaultTop && (y+600-defaultTop) < document.body.offsetHeight)
      div.style.top = y + 0 - defaultTop + 'px';
    else
      div.style.top = '0px';
      
    return true;
  }
}


function Inserted() {
  ins = ge('Inserted');
  ins.style.display = "block";
  
}
function ge(poss) {
  return document.getElementById(poss);
}

function Hide(poss) {
  ge(poss).style.display = "none";
  return false;
}

function Sort(obj) {
  ge('sortform').action = obj.value;
  ge('sortsubmit').click();
}
function SortFind(obj) {
  ge('sortform').action = ge('sortform').action + 'sort/'+obj.value+'/';
  ge('sortsubmit').click();
}

function Switch(poss) {
  obj = ge(poss);
  if(obj.style.display == 'block') {
    obj.style.display = 'none';
    ge('Filter').className = "short";
  }
  else {
    obj.style.display = 'block';
    ge('Filter').className = "long";
  }
  
  obj.blur();
  return false;
}

// If country is SK then show select with dph
function DPHUser(value) {
  if(value.toLowerCase() == 'sk' || value.toLowerCase() == 'sr') {
    ge('td_vat_1').style.visibility = 'visible';
    ge('td_vat_2').style.visibility = 'visible';
    ge('fact_country').options[1].selected = 'selected';
    ge('userSend_country').options[1].selected = 'selected';
  }
  else {
    if(!ge('fact_country')) return false;
    
    ge('fact_country').options[0].selected = 'selected';
    ge('userSend_country').options[0].selected = 'selected';
    ge('td_vat_1').style.visibility = 'hidden';
    ge('td_vat_2').style.visibility = 'hidden';
  }
}


function DPHUserVO(value) {
  if(value.toLowerCase() == 'sk' || value.toLowerCase() == 'sr') {
    ge('tr_vat').style.visibility = 'visible';
  }
  else ge('tr_vat').style.visibility = 'hidden';
}


function getCenter(w, h) {
  aw = screen.availWidth;
  ah = screen.availHeight;
  t = (aw - w/2) / 12;
  l = (ah - h/2) / 6;
  
  t = 10;

  return new Array(t, l);
}

function MoreImages(id, idimg, lang, domain) {
  w = 1024;
  h = 768;
  pole = getCenter(w, h);

  link = domain+'/'+lang+'/foto/'+idimg+'/productmatch/'+id+'/';
  //alert(link);
  moreimages = window.open(link, "lang", "width="+w+", height="+h+", top="+pole[0]+", left="+pole[1]+", resizable=0");
  moreimages.focus();
  
  return false;
}



function getEvent(e) {
    if (!e) return window.event;
    return e;
}
function getMouseX (e) {
  if (e.pageX) { 
    return e.pageX;
  }else if (e.clientX) {
    return (e.clientX + document.body.scrollLeft);
  }
}
  
function getMouseY(e) {
  if (e.pageY) { 
    return e.pageY;
  }else if (e.clientY) {
    return (e.clientY + document.body.scrollTop);
  }
}


function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (igc = 0, jgc = 0; igc < elsLen; igc++) {
		if ( pattern.test(els[igc].className) ) {
			classElements[jgc] = els[igc];
			jgc++;
		}
	}
	return classElements;
}

function showExtraOpeningTime(obj) {
	var obj= ge('extraOpeningTime')
	var opt = {fps : 30, duration : 500};
	var callBack = function(){return false;};
	
  if(obj.style.visibility == 'visible') {
		callBack = Efect.lib.fadeOut;
	}else {
		obj.style.display = 'block';
		obj.style.visibility = 'visible';
		callBack = Efect.lib.fadeIn;
	}
	
	
	new Efect(obj, callBack, opt).start();
	
	obj && obj.focus && obj.focus()
	
	return false;
}



function Efect(object, step, options) {
  var options = options || {};
  
  this.object = object;
  this.duration = options.duration || 1000;
  this.fps = options.fps || 40;
  this.onStep = step || function() {}
  
  return this;
}

Efect.prototype = {
  start : function() {
    if(this.timer)
      return;
      
    var that = this;
    this.time = +new Date;
    
    this.onStep(0, this.object);
    
    this.timer = window.setInterval(function() {
        that.step()
      },  Math.round(1000/this.fps));
      
    return this;
  },
  
  step : function() {
    var currentTime = +new Date;
     
    if(currentTime < this.time + this.duration) {
      var delta = (currentTime - this.time) / this.duration;
      this.onStep(delta, this.object);
    } else {
      this.timer = clearInterval(this.timer);
      this.onStep(1, this.object);
    }
  }
}

Efect.lib = {
  fadeOut : function(delta, obj) {
		obj.style.filter = 'alpha(opacity = '+(100 - 100 * delta)+')';
    obj.style.opacity = 1 - delta;
    
    if(delta == 1)
      obj.style.visibility = 'hidden';
  },
  
  fadeIn : function(delta, obj) {
    if(delta == 0) {
      obj.style.opacity = 1;
      obj.style.visibility = 'visible';
      obj.style.filter = '';
    }
      
    obj.style.filter = 'alpha(opacity = '+100*delta+')';
		obj.style.opacity = delta;
  }
}
