var submenu = false;
var activemenu = false;
var menutoclose = false;
var timer = false;

function showMenu(parent, target) {
	if ( document.getElementById('menu-'+target) ) {
		
		if ( ( navigator.userAgent.toLowerCase().indexOf( 'msie 6' ) != -1 ) || ( navigator.userAgent.toLowerCase().indexOf( 'msie 7' ) != -1 ) ) {
			document.getElementById('menu-'+target).style.top = findPosTop(parent)+ parent.offsetTop + 10 + 'px';
		} else if ( navigator.userAgent.toLowerCase().indexOf( 'msie 8' ) != -1 ) {
			document.getElementById('menu-'+target).style.top = findPosTop(parent)+ parent.offsetTop - 9 + 'px';
		} else {
			document.getElementById('menu-'+target).style.top = findPosTop(parent)+ parent.offsetTop - 9 + 'px';
		}
		document.getElementById('menu-'+target).style.left = findPosLeft(parent) -16 +  'px';
		document.getElementById('menu-'+target).style.display= 'block';
	  activemenu = document.getElementById('menu-'+target);
	}
}
  
function closeMenu() {
	if ( activemenu )
 		activemenu.style.display = 'none';
}
  	
function hideMenu() {
	closeMenu();
}
  
function findPosTop(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		do {
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
		return curtop;
	}
}
  	
function findPosLeft(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
		} while (obj = obj.offsetParent);
		return curleft;
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
	do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
	} while (obj = obj.offsetParent);
	return [curleft,curtop];
	}
}
  	
function showSubmenu(target) {
	menutoclose = false;
	document.getElementById('menu-'+target).style.display = 'block';
}


function requestObj () { // získa objekt XMLHttpRequest
  var req = false;;
  try { req = new XMLHttpRequest();} // IE 7, Firefox, Opera 8...
  catch(e) {
    try { req = new ActiveXObject('Microsoft.XMLHTTP'); } // IE 5.x, IE 6
    catch(e) {}
  }
  return req; // vráti vytvorený objekt
}




// spustí sa automaticky po prijatí správy zo servera
function handleResponse1() {
  if (request.readyState == 4) {
    if (request.status == 200) {
      if ( ajaxOutputType == 'textfield' )
      	document.getElementById(ajaxOutputTarget).value = request.responseText;
      if ( ajaxOutputType == 'innerHTML' ) {
      	document.getElementById(ajaxOutputTarget).innerHTML = request.responseText;
      	if ( ajaxOutputTarget == 'contact-form2' ) {
      		document.getElementById(ajaxOutputTarget).style.display = "";
      	}
      }
      if ( ajaxOutputType == 'innerHTMLadd' )
      	document.getElementById(ajaxOutputTarget).innerHTML += request.responseText;
      if ( ajaxOutputType == 'kosicek' )
      	BasketInfo();
    }
  } else {
  }
  return;
}

var request = requestObj();
var ajaxOutputType = null;
var ajaxOutputTarget = null;


function fixedEl(id){
//	if(document.all){
//		document.all[id].style.pixelTop = document.body.scrollTop + 90;
		document.getElementById(id).style.pixelTop = document.body.scrollTop + 90;
//		alert(document.getElementById(id).style.pixelTop);
		// pixelTop je něco jako top v CSS
		// document.body.scrollTop říká, jak moc je to odrolované dolů
		// ta devadesátka je ta samá devadesátka jako ve stylu
//	}
}


function showContactForm() {
	if ( document.getElementById( 'contact-form2' ) != null ) {
		document.body.removeChild( document.getElementById( 'contact-form2' ) );
		return false;
	}
	
	el = document.createElement( 'DIV' );
	el.id = 'contact-form2';
	el.innerHTML = "";
	el.style.display = "none";
	
	viewport = getViewPort();
	var scroll = getScrollXY();
	
	el.style.left = ( Math.round( viewport[0] / 2 ) - 276 ) + "px";
	el.style.top = Math.round( viewport[1] / 2 ) + scroll[1] - 200 + "px";

	document.body.appendChild( el );
	
	if ( request.readyState == 4 || request.readyState == 0 ) {
		ajaxOutputType = 'innerHTML';
		ajaxOutputTarget = 'contact-form2';
		request.open('GET', '/cs/kontaktni-formular.html', true);
		request.setRequestHeader('Content-Type','text/plain;charset=utf-8');
		request.send();
		request.onreadystatechange = handleResponse1;
	}
}

function sendContactForm( form ) {
	var poststr = '';
	
	if ( form.jmeno.value == '' || form.email.value == '' ) {
		alert( 'Některé z povinných polí není vyplněné!' );
		return false;
	}
	
	poststr =	"jmeno=" + encodeURIComponent( form.jmeno.value ) + 
						"&email=" + encodeURIComponent( form.email.value ) + 
						"&telefon=" + encodeURIComponent( form.telefon.value ) + 
						"&mesto=" + encodeURIComponent( form.mesto.value ) + 
						"&poptavka=" + encodeURIComponent( form.poptavka.value );
	
	request.open('POST', '/cs/odeslat-kontaktni-formular.html', true);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request.setRequestHeader("Content-length", poststr.length);
	request.setRequestHeader("Connection", "close");
	request.onreadystatechange = confirmSendForm;
	request.send(poststr);
}


function showPolohovani() {
	if ( document.getElementById( 'contact-form2' ) != null ) {
		document.body.removeChild( document.getElementById( 'contact-form2' ) );
		return false;
	}
	
	el = document.createElement( 'DIV' );
	el.id = 'contact-form2';
	el.innerHTML = "";
	el.style.display = "none";
	
	viewport = getViewPort();
	var scroll = getScrollXY();
	
	el.style.left = ( Math.round( viewport[0] / 2 ) - 276 ) + "px";
	el.style.top = Math.round( viewport[1] / 2 ) + scroll[1] - 200 + "px";
	el.style.border = "1px solid red";
	
	var pol = document.getElementById("polohovacka");
	pol.style.display = "";
	el.appendChild( pol );
//	el.innerHTML = pol.innerHTML;
//	alert(pol.innerHTML);
	
	
/*
	el.innerHTML = eval(
		"AC_FL_RunContent(" +
		"'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0'," +
		"'width', '785',	'height', '350'," +
		"'src', '/t/p/flash/pullman_bed_animation-2'," +
		"'quality', 'high', 'pluginspage', 'http://www.adobe.com/go/getflashplayer'," +
		"'align', 'middle', 'play', 'true', 'loop', 'true'," +
		"'scale', 'showall', 'wmode', 'transparent'," +
		"'devicefont', 'false', " +
		"'id', 'pullman_bed_animation-2'," +
		"'bgcolor', '#ffffff'," +
		"'name', 'pullman_bed_animation-2'," +
		"'menu', 'true',	'allowFullScreen', 'false',	'allowScriptAccess','sameDomain'," +
		"'movie', '/t/p/flash/pullman_bed_animation-2'," +
		"'salign', '');");
*/
/*
	el.innerHTML = '      <object id="myId" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="780" height="420"> ' +

        '<param name="movie" value="/t/p/flash/pullman_bed_animation-2.swf" />' +
        '<!--[if !IE]>-->' +
        '<object type="application/x-shockwave-flash" data="/t/p/flash/pullman_bed_animation-2.swf" width="780" height="420">' +
        '<!--<![endif]-->' +
          '<p>Alternative content</p>' +
        '<!--[if !IE]>-->' +
        '</object>' +
        '<!--<![endif]-->' +
      '</object>';
*/
	
document.body.appendChild( el );
	
	
return false;

	if ( request.readyState == 4 || request.readyState == 0 ) {
		ajaxOutputType = 'innerHTML';
		ajaxOutputTarget = 'contact-form2';
		request.open('GET', '/cs/flash-polohovani.html', true);
		request.setRequestHeader('Content-Type','text/plain;charset=utf-8');
		request.send();
		request.onreadystatechange = handleResponse1;
		
		alert(document.getElementById('polohovacka'));
	}
}



function confirmSendForm() {
  if (request.readyState == 4) {
    if (request.status == 200) {
			document.getElementById( 'contact-form2' ).innerHTML = request.responseText;
    }
  }
}


function addBasket( item ) {
	if ( request.readyState == 4 || request.readyState == 0 ) {

		ajaxOutputTarget = 'kosik';
		ajaxOutputType = 'kosicek';
		
		qty = document.getElementById( 'f01' ).value;
		
    request.open('GET', '/kosik/vlozit-do-kosiku.html?item='+item+'&qty='+qty, true);
    request.setRequestHeader('Content-Type','text/plain;charset=utf-8');
    request.send( 'item='+item+'&qty='+qty );
    request.onreadystatechange = handleResponse1;
    
	} else {
		if (timeout) clearTimeout(timeout);
		timeout = setTimeout('addBasket()',500);
	}
}



var actualBedoverItem = [0,0,0,0];

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

function callOnLoad() {
	for ( i=0; i<document.images.length; i++ ) {
		if ( document.images[i].className.indexOf( "bedover" ) != -1 ) {
			var im = document.images[i];
			im.onmouseover = function() {
				if ( document.getElementById( "overs_"+this.getAttribute( "rel" ) ) == null ) {
					var pos = findPos( this );
					var ima = document.createElement( 'img' );
					var anch = document.createElement( 'a' );
					anch.href = this.parentNode.href;
					this.actualBedoverItem = [];
					ima.src = '/t/p/i/overs_'+this.getAttribute( "rel" )+'.png';
					ima.style.position = "absolute";
					ima.style.top = ( pos[1] + Math.round(this.clientHeight / 2 ) - 30 ) + "px";
					ima.style.left = ( pos[0]+Math.round(this.clientWidth / 2 ) - 30 ) + "px";
					actualBedoverItem = [parseInt(pos[0]), parseInt(pos[1]), parseInt(this.clientWidth+pos[0]), parseInt(this.clientHeight+pos[1])];
					this.actualBedoverItem = [parseInt(pos[0]), parseInt(pos[1]), parseInt(this.clientWidth+pos[0]), parseInt(this.clientHeight+pos[1])];
					anch.id = "overs_"+this.getAttribute( "rel" );
					anch.appendChild( ima );
					document.body.appendChild( anch );
				}
			}
		
			im.onmouseout = function( event ) {
				
				if ( !event ) {
					var event = window.event;
				}
				
				if ( typeof(event.clientX) != "undefined" ) {
					var x = event.clientX;
					var y = event.clientY;
				} else {
					var x = event.pageX;
					var y = event.pageY;
				}
				var scroll = getScrollXY();
				
				if ( x <= this.actualBedoverItem[0] || y <= ( this.actualBedoverItem[1] - scroll[1] ) || x >= this.actualBedoverItem[2] || y >= ( this.actualBedoverItem[3] - scroll[1] ) ) {
					document.body.removeChild( document.getElementById( "overs_"+this.getAttribute( "rel" ) ) );
					this.actualBedoverItem = [0,0,0,0];
				}
			}
		
		}
	}
	
	anchs = document.getElementsByTagName( 'a' );
	for ( i=0; i<anchs.length; i++ ) {
		if ( anchs[i].className.indexOf("smallbedover") > - 1 ) {
			var an = anchs[i];
			
			an.onmouseover = function() {
				var box = document.getElementById("other-beds-img");
				if ( box != null ) {
					box.innerHTML = '';
					var img = document.createElement( 'IMG' );
					img.src = '/images/?item='+this.getAttribute( "rel" )+'&sirka=120';
					box.appendChild( img );
				}
			};
			
			an.onmouseout = function() {
				var box = document.getElementById("other-beds-img");
				if ( box != null ) {
					box.innerHTML = '&nbsp;';
				}
			}
			
		}
	}
	
/*
	// test flashe
	var divs = document.getElementsByTagName( 'div' );
	for ( i=0; i<divs.length; i++ ) {
		if ( divs[i].className.indexOf("addflash") > -1 ) {
			
		}
	}
*/


	for ( i=0; i<document.images.length; i++ ) {
		if ( document.images[i].className.indexOf( "textilover" ) != -1 ) {
			var im = document.images[i];
			im.onmouseover = function() {
//				if ( document.getElementById( "textilover_"+this.getAttribute( "rel" ) ) == null ) {
				if ( document.getElementById( "textilover" ) != null ) {
					document.body.removeChild( document.getElementById( "textilover" ) );
				}

				if ( document.getElementById( "textilover") == null ) {
					var pos = findPos( this );
					var ima = document.createElement( 'img' );
					this.actualBedoverItem = [];
					ima.src = '/t/p/i/latky/'+this.getAttribute( "rel" )+'-1.png';
					ima.style.position = "absolute";
					ima.style.top = ( pos[1] + Math.round(this.clientHeight / 2 ) - 80 ) + "px";
					ima.style.left = ( pos[0]+Math.round(this.clientWidth / 2 ) - 80 ) + "px";
//					actualBedoverItem = [parseInt(pos[0]), parseInt(pos[1]), parseInt(this.clientWidth+pos[0]), parseInt(this.clientHeight+pos[1])];
//					this.actualBedoverItem = [parseInt(pos[0]), parseInt(pos[1]), parseInt(this.clientWidth+pos[0]), parseInt(this.clientHeight+pos[1])];
					ima.onmouseout = function() {
						document.body.removeChild( this );
					}
					
//					ima.id = "textilover_"+this.getAttribute( "rel" );
					ima.id = "textilover";
					document.body.appendChild( ima );
				}
			}
		
/*
			im.onmouseout = function( event ) {
				
				var x = event.clientX;
				var y = event.clientY;
				var scroll = getScrollXY();
				
				if ( x <= this.actualBedoverItem[0] || y <= ( this.actualBedoverItem[1] - scroll[1] ) || x >= this.actualBedoverItem[2] || y >= ( this.actualBedoverItem[3] - scroll[1] ) ) {
					document.body.removeChild( document.getElementById( "overs_"+this.getAttribute( "rel" ) ) );
					this.actualBedoverItem = [0,0,0,0];
				}
			}
*/
		
		}
	}
	
}


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


function getViewPort() {
	var viewportwidth;
	var viewportheight;
 
 	if ( typeof window.innerWidth != 'undefined' ) {
		viewportwidth = window.innerWidth,
		viewportheight = window.innerHeight
	}
 
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0 ) {
		viewportwidth = document.documentElement.clientWidth,
		viewportheight = document.documentElement.clientHeight
	}
 
	else {
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}

	return [viewportwidth,viewportheight];
}

function showHiddenProductInfo() {
	var items = document.getElementsByTagName( 'tr' );
	var butttext = document.getElementById( 'showonclick-button' ).innerHTML;
	
	if ( butttext.indexOf('zobrazit detail' ) > -1  ) {
		for ( i=0; i<items.length; i++ ) {
			if ( items[i].className == 'showonclick' ) {
				items[i].style.visibility = 'visible';
			}
		}
		document.getElementById( 'showonclick-button' ).innerHTML = 'skrýt detail';
	} else {
		for ( i=0; i<items.length; i++ ) {
			if ( items[i].className == 'showonclick' ) {
				items[i].style.visibility = 'hidden';
			}
		}
		document.getElementById( 'showonclick-button' ).innerHTML = 'zobrazit detail';
	}
	
	
}
