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();
  
  //snow();
}


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() {

  if(window.screen.width > 1024 && ge('footerNew')) {
    ge('footerNew').setAttribute('id', 'footerNew2');
    ge('stinobal').setAttribute('id', 'stinobal2');
    
    FlashStart();
  }else {
  	ge('rightFlash').style.display = 'none';
    //ge('rightFlash').childNodes[0].style.position = 'absolute';
  }
  
  var ex = window.location.host.split('.');
  ex.shift();
  ex.unshift('www');
  var link = 'http://'+ex.join('.');
  
  if(document.getElementsByTagName('h1')[0])
    document.getElementsByTagName('h1')[0].getElementsByTagName('a')[0].href = link;
  
  // flashe pro ie
  if(is_ie) {
    objects = document.getElementsByTagName("object");
    for (var i = 0; i < objects.length; i++)  {
      objects[i].outerHTML = objects[i].outerHTML;
    }
  } 
}


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 y = getScrollXY()[1];
    
    if(y > 0 && is_ie && (!IE7 && !IE8))
      div.style.top = y + 0 - defaultTop + 'px';
    else if(y > 0)
      div.style.top = Math.max(0, defaultTop - y) + 'px';
    else
      div.style.top = '';
      
    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;
}


/**
 *  COOKIE
 */  
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}


function serialize( inp ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Arpad Ray (mailto:arpad@php.net)
    // *     example 1: serialize(['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'
 
    var getType = function( inp ) {
        var type = typeof inp, match;
        if(type == 'object' && !inp)
        {
            return 'null';
        }
        if (type == "object") {
            if(!inp.constructor)
            {
                return 'object';
            }
            var cons = inp.constructor.toString();
            if (match = cons.match(/(\w+)\(/)) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
 
    var type = getType(inp);
    var val;
    switch (type) {
        case "undefined":
            val = "N";
            break;
        case "boolean":
            val = "b:" + (inp ? "1" : "0");
            break;
        case "number":
            val = (Math.round(inp) == inp ? "i" : "d") + ":" + inp;
            break;
        case "string":
            val = "s:" + inp.length + ":\"" + inp + "\"";
            break;
        case "array":
            val = "a";
        case "object":
            if (type == "object") {
                var objname = inp.constructor.toString().match(/(\w+)\(\)/);
                if (objname == undefined) {
                    return;
                }
                objname[1] = serialize(objname[1]);
                val = "O" + objname[1].substring(1, objname[1].length - 1);
            }
            var count = 0;
            var vals = "";
            var okey;
            for (key in inp) {
                okey = (key.match(/^[0-9]+$/) ? parseInt(key) : key);
                vals += serialize(okey) +
                        serialize(inp[key]);
                count++;
            }
            val += ":" + count + ":{" + vals + "}";
            break;
    }
    if (type != "object" && type != "array") val += ";";
    return val;
}




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;
}