/** Effect containers **/
var effects = {show:null,hide:null,drag:null};
var ajaxReq = null;
var activeMenuID = null;
var menuAnimationEnabled = false;
var menuAnimationSpeed = ( menuAnimationEnabled ? 0.2 : 0 );
/** Smart input fields **/
var inputTexts = [];
var saveTexts = [];
/** Other globals **/
var IE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;
var IE7 = navigator.userAgent.toLowerCase().indexOf('msie 7') != -1;
var IE = IE6 || IE7;

/** Functions **/
function windowHeight() {
	var h = 600; 
	if (self.innerHeight) {
		h = self.innerHeight; 
	} else if (document.documentElement && document.documentElement.clientHeight) {
		h = document.documentElement.clientHeight; 
	} else if (document.body) {
		h = document.body.clientHeight;
	}
	return h;
}

function windowWidth() {
	var w = 800; 
	if (self.innerWidth) {
		w = self.innerWidth; 
	} else if (document.documentElement && document.documentElement.clientWidth) {
		w = document.documentElement.clientWidth; 
	} else if (document.body) {
		w = document.body.clientWidth;
	}
	return w;
}

function activateForm() {
	/*$($$('#loginbox input[name="username"]')[0]).focus();*/
}

function showLogin() {
	effects.show = new Effect.Appear('loginbox',{duration:0.5});
	setTimeout('activateForm()',500);
}

function hideLogin() {
	effects.hide = new Effect.Fade('loginbox',{duration:0.5});
}

function showList() {
	effects.show = new Effect.Appear('table-menu',{duration:0.5});
}

function hideList() {
	effects.hide = new Effect.Fade('table-menu',{duration:0.5});
}


function showLoginbox(event) {
	$('loginbox_show').onclick = closeLoginbox;
	hideList();
	setTimeout('showLogin()',500);
	$('loginbox_cancel').onclick = closeLoginbox;
	$('loginbox_login').onclick = submitLoginbox;
	$($$('#loginbox input[name="username"]')[0]).onkeyup = function(e) {
		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		else return true;
		if (keycode == 13) $($$('#loginbox input[name="password"]')[0]).focus();
		return true;
	};
	$($$('#loginbox input[name="password"]')[0]).onkeyup = function(e) {
		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		else return true;
		if (keycode == 13) $('loginbox_form').submit();
		return true;
	};
	smartInputs(false);
}

function submitLoginbox() {
	$('loginbox_form').submit();
	closeLoginbox();
}

function closeLoginbox() {
	$('loginbox_show').onclick = showLoginbox;
	hideLogin();
	setTimeout('showList()',500);
}

function showMenu(ID) {
	if( menuAnimationEnabled ) {
		effects.show = new Effect.Appear($(ID+'_menu'),{duration:menuAnimationSpeed});
	} else {
		$(ID+'_menu').style.display="block";
	}
	changeMenuImage(ID,true);
	return false;
}

function checkMenus(event) {
	var x = Event.pointerX(event);
	var y = Event.pointerY(event);
	if(activeMenuID !== null) {
		var link = $(activeMenuID);
		var menu = $(activeMenuID+'_menu');
		link.cumulativeOffset();
		menu.cumulativeOffset();
		link.getDimensions();
		menu.getDimensions();
		if(!(Position.within(link,x,y) || Position.within(menu,x,y))) {
			if( menuAnimationEnabled ) {
				effects.hide = new Effect.Fade($(activeMenuID+'_menu'),{duration:menuAnimationSpeed});
			} else {
				$(activeMenuID+'_menu').style.display="none";
			}
			changeMenuImage(activeMenuID,false);
			$(activeMenuID).removeClassName('active');
			activeMenuID = null;
		}	
	}
}

function clearMenuImages() {
	var imgs = $$('#topmenu a img');
	for(i=0;i<4;i++) {
		var is_passive = ($(imgs[i].parentNode.id+'_menu').style.display == "none");
		if( IE6 && is_passive && imgs[i].filters.item(0).src.toString().indexOf('_hover') >= 0 ) {
			imgs[i].filters.item(0).src = imgs[i].filters.item(0).src.toString().replace( '_hover.png', '.png' );
		}
		if( !IE6 && is_passive && imgs[i].src.toString().indexOf('_hover') >= 0 ) {
			imgs[i].src = imgs[i].src.toString().replace( '_hover.png', '.png' );
		}
	}
}

function changeMenuImage(ID,on) {
	var img = $($$('#'+ID+' img')[0]);
	if( IE6 ) {
		if( on ) {
			if( img.filters.item(0).src.toString().indexOf('_hover') < 0 ) {
				img.filters.item(0).src = img.filters.item(0).src.toString().replace( '.png', '_hover.png' );
			}
		} else {	
			if( img.filters.item(0).src.toString().indexOf('_hover') >= 0 ) {
				img.filters.item(0).src = img.filters.item(0).src.toString().replace( '_hover.png', '.png' );
			}
		}
	} else {
		if( on ) {
			if( img.src.toString().indexOf('_hover') < 0 ) {
				img.src = img.src.toString().replace( '.png', '_hover.png' );
			}
		} else {	
			if( img.src.toString().indexOf('_hover') >= 0 ) {
				img.src = img.src.toString().replace( '_hover.png', '.png' );
			}
		}
	}
	setTimeout( 'clearMenuImages()', menuAnimationSpeed*2000+50);
}

function changeMenu(event) {
	var element = this;
	if(element.nodeName.toLowerCase().match('a')==null) element = element.parentNode;
	var ID = element.id;
	var pos = element.cumulativeOffset();
	var menu = $(ID+'_menu');
	var startwidth = 0;
	var reverse = ( ID == 'tm4' );
	if( $('en') && ID == 'tm4' ) { startwidth = 194; } 
	else if( ID == 'tm4' ) { startwidth = 180; }
	changeMenuImage(ID,true);
	element.addClassName('active');
	menu.style.position = 'absolute';
	menu.style.top = pos.top + element.getHeight() + "px";
	menu.style.left = pos.left - ( reverse ? ( startwidth - element.getWidth() ) : 0 ) + "px";
	if(activeMenuID === null) {
		if( menuAnimationEnabled ) {
			effects.show = new Effect.Appear(menu,{duration:menuAnimationSpeed});
		} else {
			menu.style.display="block";
		}
		activeMenuID = ID;
	} else {
		if(activeMenuID != ID) {
			if( menuAnimationEnabled ) {
				effects.hide = new Effect.Fade($(activeMenuID+'_menu'),{duration:menuAnimationSpeed});			
			} else {
				$(activeMenuID+'_menu').style.display="none";
			}
			changeMenuImage(activeMenuID,false);
			$(activeMenuID).removeClassName('active'); 
			activeMenuID = ID;
			var func = "showMenu('"+ID+"')";
			setTimeout(func,menuAnimationSpeed*1000);
		}
	}
	return false;
}

function smartInputs(init) {
	var inputs = $$('input.smart');
	var cnt = inputs.length;
	for( i=0 ; i<cnt ; i++ ) {
		input = inputs[i];
		if(init) {
			inputTexts[input.id] = input.value;
		} 
		if( input.id == "newsletter_email" ) {
			input.onclick = input.onfocus = function(e) {
				if( this.value == inputTexts[this.id] ) {
					this.value = '';
				} 
				this.style.width = "206px";
				$($$('.fooldal .left-block')[0]).style.width = "220px";
				$($$('.fooldal .right-block')[0]).style.width = "250px";
				$($$('.fooldal .side-block')[0]).style.width = "220px";
				$($$('.fooldal .side-block b.title')[0]).innerHTML = $($$('.fooldal .side-block b.title')[0]).innerHTML.toString().replace('<br/>',' ');
				$('newsletter_hide').style.display = "block";
				$('newsletter_button').style.display = "none";
			};
		} else {
			input.onclick = input.onfocus = function(e) {
				if( this.value == inputTexts[this.id] ) {
					this.value = '';
				} 
			};
		}
		input.onblur = function(e) {
			if( this.value == "" ) {
				this.value = inputTexts[this.id];
			}
		};
	}
}

/**
 * Initialize page
 */
function init() {
	if($('loginbox_show')) $('loginbox_show').onclick = showLoginbox;
	smartInputs(true);
	for(i=4;i>0;i--) 
		if($('tm'+i)&&$('tm'+i+'_menu')) $('tm'+i).onmouseover = changeMenu;
	Event.observe(document,'mousemove',checkMenus);
}

/**
 * Add window event handlers
 */
window.onload = init;


/** NEWSLETTER FORM SCRIPT **/

var SIDString = '';
function getRandomLetter() { return String.fromCharCode(getRandom(65, 90)); }
function getRandom(lowerBound, upperBound) { return Math.floor((upperBound - lowerBound + 1) * Math.random() + lowerBound); }
function SetSID() {
	if (SIDString.length > 0) { return SIDString; }
	sidc = 0;
	while (sidc < 32) { SIDString = SIDString + getRandomLetter(); sidc++; }
	return SIDString;
}

var captcha_url = "http://marketing.fusiongate.com/admin/resources/form_designs/captcha/index.php?c=" + getRandom(1,1000)+ "&ss=" + SetSID();

function checkNewsletterForm() {

	var f = $('newsletter');
	
	var error = false;
	
	if (f.email.value == "" || f.email.value == inputTexts[f.email.id]) {
		f.email.style.borderColor = "red";
		f.email.focus();
		error = true;
	} else {
		f.email.style.borderColor = "#7f7f7f";
	}
	
	var fname = $('CustomFields_3_1');
	if (fname.value == "" || fname.value == inputTexts[fname.id]) {
		fname.style.borderColor = "red";
		if( !error ) {
			fname.focus();
		}
		error = true;
	} else {
		fname.style.borderColor = "#7f7f7f";
	}
	
	var lname = $('CustomFields_4_1');
	if (lname.value == "" || lname.value == inputTexts[lname.id]) {
		lname.style.borderColor = "red";
		if( !error ) {
			lname.focus();
		}
		error = true;
	} else {
		lname.style.borderColor = "#7f7f7f";
	}
	
	var company = $('CustomFields_10_1');
	if (company.value == "" || company.value == inputTexts[company.id]) {
		company.style.borderColor = "red";
		if( !error ) {
			company.focus();
		}
		error = true;
	} else {
		company.style.borderColor = "#7f7f7f";
	}
	
	if (f.captcha.value == "" || f.captcha.value == inputTexts[f.captcha.id]) {
		f.captcha.style.borderColor = "red";
		if( !error ) {
			f.captcha.focus();
		}
		error = true;
	} else {
		f.captcha.style.borderColor = "#7f7f7f";
	}
	
	if( error ) {
		return false;
	}

	f.submit();
}