//===================================================================
//  Javascript dynamic content module
//  by Bert vt Veer
//===================================================================

// Globals
var lastclick = 0;
var activeImg;
	
	
//---------------------------------------------------------
// Dynamic page loading
//---------------------------------------------------------

function getpage(target,url,form)
{
	var httpRequest;
	meth = (form==undefined) ? 'GET' : 'POST';
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) {
			httpRequest.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject) { // IE
		try { httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) {
			try { httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (e) {}
		}
	}
	if (!httpRequest) {
		return false;
	}
	
	httpRequest.onreadystatechange = function() { alertContents(target,httpRequest); };
	httpRequest.open(meth, url, true);
	if (meth=='POST') {
		parms = '';
		form = document.forms[form];
		for (i=0; i<form.length; i++) {
			parms += '&' + form.elements[i].name + '=' + form.elements[i].value;
		}
		httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		//httpRequest.setRequestHeader("Content-length", parms.length);
		//httpRequest.setRequestHeader("Connection", "close");
		httpRequest.send(parms);
	} else 
	    httpRequest.send('');
	    
	popup_hide(); // Close any popups    
}
	

function alertContents(target, httpRequest)
{
	if (httpRequest.readyState == 4) {
		document.getElementById(target).innerHTML = httpRequest.responseText;
	}
}


//---------------------------------------------------------
// General
//---------------------------------------------------------

function toggle(elem)
{
	disp = document.getElementById(elem).style.display=="block" ? "none" : "block";
	document.getElementById(elem).style.display = disp;
}


function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}


function setTheme(index)
{
	switch(index) {
		case 1: document.body.style.background = "rgb(240,240,240) url('graphics/bg_body1.gif') repeat-x"; break;
		case 2: document.body.style.background = "rgb(15,15,15) url('graphics/bg_body2.gif') repeat-x"; break;
		case 3: document.body.style.background = "rgb(188,188,188) url('graphics/bg_body3.gif') repeat-x"; break;
	}
	//setCookie("theme",1,365);
}


//---------------------------------------------------------
// Menu system
//---------------------------------------------------------	

function link_click(elem)
{
	document.getElementById("menuopt"+lastclick).className = "menuopt";
	lastclick = elem;
	document.getElementById("menuopt"+elem).className = "menuopt_hi";
}


function img_click(elem,page)
{
	if (activeImg) document.getElementById(activeImg).className = "thumbs_img";
	document.getElementById(elem).className = "thumbs_img_hi";
	activeImg = elem;
	getpage('content2', page);
}


function menu_click(menuopt, page)
{
	document.getElementById("content1").innerHTML = "";
	document.getElementById("content2").innerHTML = "";
	getpage("content1", page);
	if (menuopt==1) {
		menuopt = 5;
	}
	link_click(menuopt);	
	activeImg = "but1";
	
	if (menuopt<5) {
		document.getElementById("submenu1").style.display = "none";
		document.getElementById("menuopt1").className = "menuopt"; // products
		document.getElementById("content2").style.display = "none";
	}
	else {
		document.getElementById("submenu1").style.display = "block";
		document.getElementById("menuopt1").className = "menuopt_open"; // products
		getpage("content2","detail.php?cat="+(menuopt-4));
		document.getElementById("content2").style.display = "block";
	}
}


//---------------------------------------------------------
// Popup window
//---------------------------------------------------------

function getDocHeight() {
	var D = document;
	return Math.max(
		Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
		Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
		Math.max(D.body.clientHeight, D.documentElement.clientHeight)
	);
}


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 { x:scrOfX, y:scrOfY };
}


function getClientSize()
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	    myHeight = document.body.clientHeight;
	}
	return { w:myWidth, h:myHeight };
}


function popup_show(page,ptitle,form)
{
	// Load content
	getpage("mainwin_client", page, form);
	var title = (ptitle==undefined) ? '&nbsp;' : ptitle;
	//document.getElementById("mainwin_titlebar").innerHTML = title;
  
	var cSize = getClientSize();
	//var cScroll = getScrollXY();
	var midx = cSize.w/2;
	var midy = cSize.h*0.42;
	var w = 670;
	var h = 380;
	var x = Math.floor(midx-(w/2));
	var y = Math.floor(midy-(h/2));// + cScroll.y;
	     
	// background shadow
	var bg = document.getElementById("background_shadow").style;
	bg.width = "100%";
	bg.height = getDocHeight()+"px";
	bg.visibility = "visible";
	
	// window
	var win = document.getElementById("mainwin").style;
	var cl = document.getElementById("mainwin_client").style;
	cl.width = w+"px";
	cl.height = h+"px";
	win.left = x+"px";
	win.top = y+"px";
	win.visibility = "visible";
	   
	// shadow
	var sh = document.getElementById("mainwin_shadow").style;
	sh.width = (w+66)+"px";//"100%";
	sh.height = (h+58)+"px";
	sh.left = (x+6)+"px";
	sh.top = (y+8)+"px";
	sh.visibility = "visible";
}

function popup_hide()
{
	document.getElementById("mainwin_shadow").style.visibility="hidden"; 
	document.getElementById("mainwin").style.visibility="hidden";
	document.getElementById("background_shadow").style.visibility="hidden";
	document.getElementById("mainwin_client").innerHTML = "";
}


//---------------------------------------------------------
// Image fader
//---------------------------------------------------------

var imageList;
var fadeSpeed = 25;
var fadeInterval = 10000;
var timer1;
var imgIndex = 0;

	
function setAlpha(elem, val)
{
	if (document.getElementById(elem)) {
		document.getElementById(elem).style.opacity = val/100.0;
		document.getElementById(elem).style.MozOpacity = val/100.0;
		document.getElementById(elem).style.filter = "alpha(opacity=" + (val) + ")";
	}
}

function getNextImage() {
	len = imageList.length;
	if (--imgIndex<0) imgIndex = len-1;
	return "url(graphics/"+imageList[imgIndex]+")"
}


function stepAlpha(elem, val, step)
{
	setAlpha(elem, val);
	if (val+step>0 && val+step<100) timer1=setTimeout("stepAlpha('"+elem+"',"+(val+step)+","+step+")", fadeSpeed);
	else if (document.getElementById("mainpic1") && document.getElementById("mainpic2")) {
		document.getElementById("mainpic1").style.background = document.getElementById("mainpic2").style.background;
		setAlpha("mainpic1", 100);
		document.getElementById("mainpic2").style.background = getNextImage();
	}
}


function fadeIn(elem, step) {
	clearTimeout(timer1);		
	stepAlpha(elem, 0, step);
}


function fadeOut(elem, step) {
	clearTimeout(timer1);
	stepAlpha(elem, 100, -Math.abs(step));
}


function startFader() {
	imageList = new Array("pic_1.jpg", "pic_2.jpg", "pic_3.jpg", "pic_4.jpg");
	if (document.getElementById("mainpic1")) {
		fadeOut("mainpic1", 2);
	}
	//else alert("mainpic1 not ready");
	setTimeout("startFader()", fadeInterval);
}


