/* called */

bV  = parseInt(navigator.appVersion)
bNS = navigator.appName=="Netscape"
bIE = navigator.appName=="Microsoft Internet Explorer"


function enable_boxes() {
  var arr = getElementsByClassName(null, "*", "menubox");
  for(var i=0; i<arr.length; i++) {
    arr[i].style.visibility = 'visible';
  }
}


function GetDisplaying(objid){
  if (!isObject(objid))
		objid=getId(objid);
  if (objid)
    s=objid.style.display;
  else
    return false;
  //if (s=='none')
	if (s.indexOf('none')>=0)
      return false;
  return true;
}

Array.prototype.find = function(searchStr) {
  var returna = false;
  for (i=0; i<this.length; i++) {
    if (typeof(searchStr) == 'function') {
      if (searchStr.test(this[i])) {
        returna = push;
        break;
      }
    } else {
      if (this[i]===searchStr) {
        returna = i;
        break;
      }
    }
  }
  return returna;
}

var inprogress=false;
var switching=false;
var step=0;
var origw = 0;
var origh = 0;
var firstUpdate = true;

function updateArrows() {
  
  is2=document.getElementById(idfram+'INNER');
  
  if(!IsImageOk(is2)) {
    setTimeout('updateArrows()', 50);
    return;
  }
  
  if(firstUpdate && bNS) {
    setTimeout('updateArrows()', 500);
    firstUpdate = false;
    return;  
  }

  is=document.getElementById('det_bottom');
  is.style.width = is2.width + 'px';
  is.style.top = (120+is2.height)+'px';
  is.style.visibility = "visible";
    
  switching = false;
  firstUpdate = true;
}

// nasty globals
var index = 0;
var idfram = 'detailbigimg';

function ImgForward() {
  if(!switching) {
    index = ((index)==next_imgs.length-1)?0:index+1;
    UpdateImage();
  }
}

function ImgBackward() {
  if(!switching) {
    index = (index==0)?next_imgs.length-1:index-1;
    UpdateImage();
  }
}

function UpdateImage() {
  switching = true;
  if((is2=document.getElementById(idfram+'INNER')) && !inprogress) {
    is2.style.width = "auto";
    is2.style.height = "auto";
    is2.src = next_imgs[index];
    is=document.getElementById('det_bottom');
    is.style.visibility = "hidden";    
    setTimeout('updateArrows()', 50);
  }       
}

function ImgClose() {
  BigImageViewClose('detailbigimg');
}

function BigImageView(idframe,srcframe){
  var objMain;
  var is;
  var nazevobrazku;
  
  index = next_imgs.find(srcframe);

  if(inprogress) {
        
        is2 = document.getElementById(idframe+'INNER');
        if(IsImageOk(is2)) {
          
          if(origw == 0 && step < 8) {
            origw = is2.width;
            origh = is2.height;
            is2.style.width = '1px';
            is2.style.height = '1px';
            is2.style.visibility = 'visible';
          }
          
          step = step+2;
        
          is2.style.width = (origw/(8/step)) + 'px';
          is2.style.height = (origh/(8/step)) + 'px';
          
          is=document.getElementById(idframe);
         
          
          var opacity=(step*10)/2;
                                  
          is.style.opacity = (opacity / 100); 
          is.style.MozOpacity = (opacity / 100); 
          is.style.KhtmlOpacity = (opacity / 100); 
          is.style.filter = "alpha(opacity=" + opacity + ")";
        }
                
        if(step < 8) {                
          setTimeout('BigImageView(\''+idframe+'\', \''+srcframe+'\')', 20);
          return false;
        }    
         
        inprogress = false;
        step=0;
        is2.style.width = origw + 'px';
        is2.style.height = origh + 'px';
        origw=0; origh=0;
        
        // make children (yeah! <> <--O)        
        var objSipL = document.createElement("a");
        objSipL.id = 'det_sipkaL';
        objSipL.innerHTML = '<img src="/img/sipka_image_detail_left.jpg" alt="left" />';
        objSipL.href='javascript:ImgBackward()';    
        //AddEvent(objSipL, 'click', ImgBackward);
        
        var objSipR = document.createElement("a");
        objSipR.id = 'det_sipkaR';
        objSipR.innerHTML = '<img src="/img/sipka_image_detail_right.jpg" alt="right" />';
        objSipR.href='javascript:ImgForward()';    
        //AddEvent(objSipR, 'click', ImgForward);
        
        var objSipC = document.createElement("a");
        objSipC.id = 'det_sipkaC';
        objSipC.innerHTML = '<img src="/img/sipka_image_detail_close.jpg" alt="left" />';
        objSipC.href='javascript:void(0)';    
        AddEvent(objSipC, 'click', ImgClose);
        
        var objText = document.createElement("span");
        objText.innerHTML = '&copy; ' + Vcompany + ' online shop. All Rights Reserved.';
        
        // make parental container
        var objSip = document.createElement("div");
        objSip.id = 'det_bottom'; 
        
        // locate to bottom of page
        objSip.style.left = '210px';
        objSip.style.top = (120+is2.height) + 'px';
        objSip.style.width = is2.width + 'px';
         
        // append child jerks'
        objSip.appendChild(objSipL);
        objSip.appendChild(objSipR);
        objSip.appendChild(objSipC);
        objSip.appendChild(objText);
         
        // bring me to existence!
        document.body.appendChild(objSip);  
        
        // define readystatechange
        is2.onreadystatechange = function() {
            if (this.readyState == "complete") {
                updateArrows(); // call the onload handler
            }
        }
        
                  
    } else {
    
      objMain=document.createElement("div");
      
   	  /* prvni kousek */
      objMain.id = idframe;
      objMain.style.width=GetPageWidth()+'px';
      objMain.style.height=GetPageHeight() + 'px';
      objMain.style.position='absolute';
      objMain.style.left='0';
      objMain.style.top='0';
      objMain.style.zIndex=2000;
      objMain.style.opacity = 0;
      objMain.style.MozOpacity = 0; 
      objMain.style.KhtmlOpacity = 0; 
      objMain.style.filter = "alpha(opacity=0)";
      	
      AddEvent(objMain,'click',BigImageViewEventClose);
      
      
      var objIn = document.createElement("img");
      objIn.id = idframe+'INNER';
      objIn.style.position='absolute';
      objIn.style.left='210px';
      objIn.style.top='120px';
      objIn.style.zIndex=2100;
      objIn.src= srcframe;
      objIn.style.visibility='hidden';
      
      document.body.appendChild(objIn);
      
      AddEvent(objIn, 'click', BigImageViewEventClose);
      
      if(document.all && typeof document.body.style.maxHeight == "undefined"){
      	var selects=document.getElementsByTagName('SELECT');
      	for(var i=0; i<selects.length; i++){
        		SetVisibility(selects[i],false);
      	}
      }
         
      document.body.appendChild(objMain); 
      inprogress = true; 
      setTimeout('BigImageView(\''+idframe+'\', \''+srcframe+'\')', 100);
  }
	
	return false;
}

function BigImageViewEventClose(e){
	var obj=SrcFromEvent(e);	
	if(obj){
		var obj2 = null;
		if(obj.id.indexOf('INNER')>0){
			obj2=getId(obj.id.substring(0,obj.id.indexOf('INNER')));
		}else 
			obj2=getId(obj.id+'INNER');
		obj.parentNode.removeChild(obj);
		if(obj2)
			obj2.parentNode.removeChild(obj2);
			
		var obj3=document.getElementById('det_bottom');
		obj3.parentNode.removeChild(obj3);
	}
	
  // IE6 only	
	if(document.all && typeof document.body.style.maxHeight == "undefined"){
		var selects=document.getElementsByTagName('SELECT');
		for(var i=0; i<selects.length; i++){
      		SetVisibility(selects[i],true);
    	}
	}
}

function BigImageViewClose(idcko){
	var obj=document.getElementById(idcko);	
	if(obj){
		var obj2 = null;
		if(obj.id.indexOf('INNER')>0){
			obj2=getId(obj.id.substring(0,obj.id.indexOf('INNER')));
		}else 
			obj2=getId(obj.id+'INNER');
		obj.parentNode.removeChild(obj);
		if(obj2)
			obj2.parentNode.removeChild(obj2);
			
		var obj3=document.getElementById('det_bottom');
		obj3.parentNode.removeChild(obj3);
	}
	
  // IE6 only	
	if(document.all && typeof document.body.style.maxHeight == "undefined"){
		var selects=document.getElementsByTagName('SELECT');
		for(var i=0; i<selects.length; i++){
      		SetVisibility(selects[i],true);
    	}
	}
	
	return false;
}

function FormCheckInit(form,ignoreHidden){
	if(ignoreHidden)
		setTimeout("FormCheckInitWork('"+form+"',true)",100);
	else
		setTimeout("FormCheckInitWork('"+form+"',false)",100);	
}


function FormCheckTest(form){
	if (isString(form))
		form=getId(form);
	
	//nejdrive podle errorwinu
	var a=getElementsByClassName(form,'*','errorwin');
	for(var i=0; i<a.length; i++)
		if (!parseClassesContain(a[i].className,'hide')){
			return false;
		}
	
	//a nove jeste podle notvalid u inputu
	a=getElementByClassName(form,'*','notvalid');
	if(a){
	 return false;
	}
	
	a=document.getElementById('needcheck');
	if(a != null && !a.checked){
	 return false;
	}
	
	return true;
}

/*  not called */

function FormCheckInitWork(form,ignoreHidden){
	if (isString(form))
		form=getId(form);
	var a=form.getElementsByTagName('INPUT');
	for(var i=0; i<a.length; i++)
		if ((a[i].type=='text')||(a[i].type=='password')){
			FormCheckInput(a[i],false,ignoreHidden ? true : false);
		}
}


function FormCheckInput(obj,objcol,ignoreHidden){
  if (isString(objcol))
		objcol=getId(objcol);
	
	var errors=new Array();
	
	if(!ignoreHidden || GetDisplaying(obj)){
		if (parseClassesContain(obj.className,'cc_atleast4')){
			if (obj.value.length<4) 
				if ((!objcol)||(objcol.value.length<4)) 
					errors.push('err_short');
		}
		if (parseClassesContain(obj.className,'cc_atleast1')){
			if (obj.value.length<1){
				errors.push('err_short');
				errors.push('err_empty');
			}
		}
		if (parseClassesContain(obj.className,'cc_pass')){
			if (!objcol)
				obj.value='';
			else
				if (obj.value!=objcol.value) errors.push('err_notsame');
		}
		if (parseClassesContain(obj.className,'cc_email')){
			r=new RegExp('^\\w(\\w|-|\\.)*@(\\w|-|\\.)+\\.\\w+$');
			if (obj.value.match(r)==null) errors.push('err_notemail');
		}
		if (parseClassesContain(obj.className,'cc_phone')){
			r=new RegExp('^((\\+|00){0,1}\\d{3} {0,1}){0,1}\\d{3} {0,1}\\d{6}$');
			if (obj.value.match(r)==null) errors.push('err_notphone');
		}
		if (parseClassesContain(obj.className,'cc_phone_short')){	//bez predcisli
			r=new RegExp('^\\d{9}$');
			if (obj.value.match(r)==null) errors.push('err_notphone');
		}
		if (parseClassesContain(obj.className,'cc_postal')){
			r=new RegExp('^\\d{3} {0,1}\\d{2,3}$');
			if (obj.value.match(r)==null) errors.push('err_notpostal');
		}
		if (parseClassesContain(obj.className,'cc_login')){
			r=new RegExp('^\\w*$');
			if (obj.value.match(r)==null) errors.push('err_notvalid');
		}
	}
	
	var pcss=parseClasses(obj.className);
	if(errors.length>0){	//nastavime tridu i inputu
		var found=false;
		for(var i in pcss){
			if(pcss[i]=='notvalid'){
				found=true;
				break;
			}
		}
		if(!found){
			obj.className+=' notvalid';
			if(objcol) objcol.className+=' notvalid';			
		}		
	}else{	//odstranime tridu z inputu
		var ncss='';
		for(var i in pcss){
			if(pcss[i]!='notvalid'){
				ncss+=pcss[i]+' ';
			}			
		}	
		obj.className=ncss;
		if(objcol) objcol.className=ncss;	
	}
		
	var index=MyOrderNum(obj);
	var parent=obj.parentNode;
	for(var i=index+1; i<parent.childNodes.length; i++){
		t=parent.childNodes[i];
		if (isUndefined(t.className)) continue;
		if (parseClassesContain(t.className,'errorwin'))
		{
			showwin=false;
			for(var j=0; j<t.childNodes.length; j++){
				s=t.childNodes[j];
				if (isUndefined(s.className)) continue;
				show=false;
				for(var k=0; k<errors.length; k++)
					if (parseClassesContain(s.className,errors[k])){
						show=true;
						showwin=true;
						break;
					}
				SetDisplaying(s, show );
			}
			SetClass(t, showwin ? 'errorwin' : 'errorwin hide');
			return showwin;
		}
	}
	
	return true;
}





function GetPageWidth(){
    return (!document.body.scrollWidth)?document.width:document.body.scrollWidth;
}
function GetPageHeight(){
  var scnHei;
  
  id=document.getElementById('container');
  
	if (id.clientHeight)
	{
		scnHei = id.clientHeight;
	}
	return scnHei;
}


function SetVisibility(objid, boolstate){
  if (boolstate){
    s='visible';
  }else{
    s='hidden';
  }
  if (obj=getId(objid)) obj.style.visibility=s;
}

function parseClassesContain(oClassNames,testedclass){
	var a = parseClasses(oClassNames);
	for(var i=0; i<a.length; i++){
		if (a[i]==testedclass)
			return true;
	}
	return false;
}

function parseClasses(oClassNames){
    return oClassNames.split(' ');
}

function getElementByClassName(oElm, strTagName, oClassNames){
  arr=getElementsByClassName(oElm, strTagName, oClassNames);
  
  return arr.length==0 ? null : arr[0];
}


/*vraci kolikaty children sveho rodice jsem*/
function MyOrderNum(obj){
  par=obj.parentNode;
  for(var i=0; i<par.childNodes.length; i++)
    if (par.childNodes[i]==obj)
      return i;
  return -1;
}

 


function SetDisplaying(objid, boolstate){
  if (boolstate){
    s='';
  }else{
    s='none';
  }
  if (isObject(objid))
	objid.style.display=s;
  else
    if (obj=getId(objid)) obj.style.display=s;
}

function isNull(a) {
    return typeof a == 'object' && !a;
}
function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}

function isUndefined(a) {
    return typeof a == 'undefined';
}

function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}

function isFunction(a) {
    return typeof a == 'function';
}

function isString(a) {
    return typeof a == 'string';
}

function getId(id){
    if (isObject(id)) return id;
    return document.getElementById(id);
}

function SetClass(objid,cl){
	if (isString(objid))
		objid=getId(objid);
  if (objid)
    objid.className=cl;
}


function AddEvent(obj,etype,fn){
	if (!obj) obj=document;
	if(window.opera && (obj == window)) obj = document;
	
	if(document.addEventListener)   
         obj.addEventListener(etype, fn, false);
	else
         obj.attachEvent('on' + etype, fn);
}

function RemoveEvent(obj,etype,fn){
	if (!obj) obj=document;
	if(window.opera && (obj == window)) obj = document;
	
	if(document.removeEventListener)
        obj.removeEventListener(etype, fn, false);
    else
		obj.detachEvent('on' + etype, fn);
}



function SrcFromEvent(e){
	if(!e) return null;
	if (navigator.appName == "Microsoft Internet Explorer" && e.srcElement) {
      return e.srcElement;
  } else if(e.target) {  // is mozilla/netscape
      // need to crawl up the tree to get the first "real" element
      // i.e. a tag, not raw text
      var o = e.target;
      while (!o.tagName) {
          o = o.parentNode;
      }
      return o;
  }
  return e;
}

function getElementsByClassName(oElm, strTagName, oClassNames){
    if (oElm==null) oElm=document.body;

    var arrElements = (strTagName == "*" )? getAllChildren(oElm) : 
					  oElm.getElementsByTagName(strTagName);

    var arrReturnElements = new Array();
    var arrRegExpClassNames = new Array();
    if(typeof oClassNames == "object"){
        for(var i=0; i<oClassNames.length; i++){
            arrRegExpClassNames.push(new RegExp("(^|\\s)" + 
            oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
        }
    }
    else{
        arrRegExpClassNames.push(new RegExp("(^|\\s)" + 
        oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
    }
    var oElement;
    var bMatchesAll;
    for(var j=0; j<arrElements.length; j++){
        oElement = arrElements[j];
        bMatchesAll = true;
        for(var k=0; k<arrRegExpClassNames.length; k++){
            if(!arrRegExpClassNames[k].test(oElement.className)){
                bMatchesAll = false;
                break;                      
            }
        }
        if(bMatchesAll)
            arrReturnElements.push(oElement);
		
    }
    return (arrReturnElements)
}

function getAllChildren(e) {
  // Returns all children of element. Workaround required for IE5/Windows. Ugh.
  return e.all ? e.all : e.getElementsByTagName('*');
} 

function IsImageOk(img) {
  if (!img.complete) {
    return false;
  }

  if (typeof img.naturalWidth != 'undefined' && img.naturalWidth == 0) {
    return false;
  }

  return true;
}

//decode pro RFC1738
function decodeURL(url){
  return unescape(url).replace(/&amp;/g,'&');
}

