
window.addEvent('load', function() {
	//imagePreloader('assets/images/button-free-quote-over.png');


});

window.addEvent('domready', function() {
	loadMenus();

	var lst = $$('.iconlink');
	for(var i=0; i<lst.length; ++i) {
		lst[i].addEvent('mouseover', function() {
			var ilst = this.getElementsByTagName('img');
			for(var n=0; n<ilst.length; ++n) {
				ilst[n].src = ilst[n].src.replace(/\.png/, '-over.png');
			}
		});

		lst[i].addEvent('mouseout', function() {
			var ilst = this.getElementsByTagName('img');
			for(var n=0; n<ilst.length; ++n) {
				ilst[n].src = ilst[n].src.replace(/-over\.png/, '.png');
			}
		});
	}

	var lst = $$('.tooltipicon');
	for(var i=0; i<lst.length; ++i) {
		var ilst = lst[i].getElementsByTagName('img');

		if(ilst[0]) {
			imagePreloader( ilst[0].src.replace(/\.png/, '-over.png') );

			$(ilst[0]).addEvent('mouseover', function() {
				this.src = this.src.replace(/\.png/, '-over.png');
				showtootipicon(this);
			});

			$(ilst[0]).addEvent('mouseout', function() {
				this.src = this.src.replace(/-over\.png/, '.png');
				hidetootipicon(this);
			});
		}
	}


	// select category
	var selectCategory = $('selectCategory');
	if(selectCategory) {
		var k = document.location.href.substr(document.location.href.indexOf('/catalog/'));
		k = k.replace(/\/+$/, '');

		var fnd = false;
		var opts = [];
		for(var i=0; i<selectCategory.options.length; ++i) {
			if(selectCategory.options[i].value == k) {
				selectCategory.selectedIndex = i;
				fnd = true;
				break;
			}
		}
		if(!fnd) {
			// try for products also
			k = k.split('/');
			k.pop();
			k = k.join('/');
			for(var i=0; i<selectCategory.options.length; ++i) {
				if(selectCategory.options[i].value == k) {
					selectCategory.selectedIndex = i;
					fnd = true;
					break;
				}
				
			}
		}
	}

	// select manufacturer
	var selectManufacturer = $('selectManufacturer');
	if(selectManufacturer && document.location.href.indexOf('/catalog/manufacturers/' > -1)) {
		var k = document.location.href.substr(document.location.href.indexOf('/catalog/manufacturers/'));

		for(var i=0; i<selectManufacturer.options.length; ++i) {
			if(selectManufacturer.options[i].value == k) {
				selectManufacturer.selectedIndex = i;
				break;
			}
		}
	}

});


function imagePreloader(imgsrc) {
	if(!window.__imagePreloader) window.__imagePreloader = [];
	var i = window.__imagePreloader.length;
	window.__imagePreloader[i] = new Image();
	window.__imagePreloader[i].src = imgsrc;
}


function showtootipicon(that) {
	that = $(that);
	if(!that.id) that.id = generateObjId();

	var div = $('divtootipicon'+that.id);
	if(!div) {
		with(div = $(document.createElement('div'))) {
			id = 'divtootipicon'+that.id;
			setStyle('display', 'none');
			setStyle('position', 'absolute');
			setStyle('top', 10);
			setStyle('opacity', 0);
		}


		var buf = '';
		buf += '<table cellpadding="0" cellspacing="0">';
	
		buf += '<tr style="background:#75767A;">';
		buf += '<td></td>';
		buf += '<td style="color:white; font-size:11px; padding:2px 4px;">'+that.getAttribute('caption')+'</td>';
		buf += '<td></td>';
		buf += '</tr>';
	
		buf += '<tr>';
		buf += '<td width="7" height="9" valign="top"><img src="assets/images/tooltip-1-sw.png" width="7" height="9" style="display:block;"></td>';
		buf += '<td height="9" style="background:url(assets/images/tooltip-1-s.png) top repeat-x;"></td>';
		buf += '<td width="9" height="9" valign="top"><img src="assets/images/tooltip-1-se.png" width="9" height="9" style="display:block;"></td>';
		buf += '</tr>';
	
		buf += '</table>';
	
	
		div.innerHTML = buf;

		document.body.appendChild(div);
	}
	if(div.getStyle('display') != 'none') return;


	div.setStyle('top', that.getTop() - 32 );
	div.setStyle('left', that.getLeft() + 6);

	div.setStyle('opacity', 0);
	div.setStyle('display', 'block');

	new Fx.Tween(div, { duration: 300 }).start('opacity', 1);

}

function hidetootipicon(that) {
	var div = $('divtootipicon'+that.id);
	if(!div) return;
	new Fx.Tween(div, {
		duration: 50,
		onComplete: function() {
			div.setStyle('display', 'none');
		}
	}).start('opacity', 0);
}

function generateObjId() {
	if(!window.__getIdCnt) window.__getIdCnt = 0;
	return 'obj'+ (++window.__getIdCnt); 
}




function loadMenus() {
	window.menus = [];



	var lst = $$('.menuitem');
	for(var i=0; i<lst.length; ++i) {

		window.menus[lst[i].getAttribute('menuid')] = new Menu(lst[i]);
	}
}

Menu = new Class({
	initialize: function(trigger) {
		this.trigger = trigger;
		this.menuid = this.trigger.getAttribute('menuid');
		this.div = $('submenu'+this.menuid);
		this.timer = null;

		var menuid = this.menuid;
		this.trigger.addEvent('mouseover', function() {
			window.menus[menuid].showSubMenu();
		});

		this.trigger.addEvent('mouseout', function() {
			window.menus[menuid].startHideSubMenu();
		});

		if(this.div) {
			var lst = this.div.getElementsByTagName('a');
			for(var i=0; i<lst.length; ++i) {
				$(lst[i]).addEvent('mouseover', function() {
					this.parentNode.className = 'hover';
				});
				$(lst[i]).addEvent('mouseout', function() {

				});
				$(lst[i]).addEvent('click', function() {
					//return false;
				});

				$(lst[i].parentNode).addEvent('mouseover', function() {
					this.className = 'hover';
					clearTimeout(window.menus[menuid].timer);
				});

				$(lst[i].parentNode).addEvent('mouseout', function() {
					this.className = '';
					window.menus[menuid].startHideSubMenu();
				});

				$(lst[i].parentNode).addEvent('click', function() {
					var lnk = $(this.getElementsByTagName('a')[0]);
					if(lnk.target && lnk.target != '_self') {

					} else document.location = lnk.href;
				});
			}
		}
	},

	showSubMenu: function() {
		clearTimeout(this.timer);

		if(this.div && this.div.getStyle('display') != 'none') return;
		doHideAllSubMenus();
		this.trigger.className = 'menuitem hover';
		if(!this.div) return;


		this.div.setStyle('top', this.trigger.getTop() + this.trigger.offsetHeight);
		this.div.setStyle('left', this.trigger.getLeft());
		this.div.setStyle('opacity', 0);
		this.div.setStyle('display', 'block');
	
		new Fx.Tween(this.div, {
			duration: 200
		}).start('opacity', .95);
	},

	startHideSubMenu: function() {
		clearTimeout(this.timer);

		this.timer = setTimeout('window.menus['+this.menuid+'].hideSubMenu();', 700);
	},
	hideSubMenu: function() {
		clearTimeout(this.timer);
		this.trigger.className = 'menuitem';
		if(!this.div) return;
		this.div.setStyle('display', 'none');

	}

});


function doHideAllSubMenus() {
	for(var i=0; i<window.menus.length; ++i) {
		if(window.menus[i]) window.menus[i].hideSubMenu();
	}
}



function popWin(href, w, h) {
	if(!w) w = 550;
	if(!h) h = 550;

	var x = (screen.availWidth - w) / 2;
	var y = (screen.availHeight-h) / 2;

	var win = window.open(href, 'popwin', 'width='+w+',height='+h+',top='+y+',left='+x+', scrollbars=yes');
	//win.moveTo(x, y);
	win.focus();

	return win;
}


//function popupWindow(href, w, h) {
//	var div = $('popupWindowDiv');
//	if(!div) {
//		with( div = document.createElement('div') ) {
//			id = 'popupWindowDiv';
//			style.position = 'absolute';
//			//style.display = 'none';
//			style.top = '0px';
//			style.left = '0px';
//			style.zIndex = 99;
//			style.overflow = 'auto';
//		}
//		document.body.appendChild(div);
//	}
//	div = $(div);
//	div.setStyle('width', w);
//	div.setStyle('height', h);
//
//
//
//	div.innerHTML = 'adkjflsdk';
//}



//window.addEvent('load', showOverlay);


function validatePopupDesignConsultation(f) {

	var err = false;

	var fld = 'contact_email';
	if(!f[fld].value) {
		$('error_popupDesignConsultation_'+fld).setStyle('display', '');
		err = true;
	}
	else {
		$('error_popupDesignConsultation_'+fld).setStyle('display', 'none');
	}


	var fld = 'contact_phone';
	if(!f[fld].value) {
		$('error_popupDesignConsultation_'+fld).setStyle('display', '');
		err = true;
	}
	else {
		$('error_popupDesignConsultation_'+fld).setStyle('display', 'none');
	}

	var fld = 'contact_subject';
	if(f[fld].selectedIndex < 1) {
		$('error_popupDesignConsultation_'+fld).setStyle('display', '');
		err = true;
	}
	else {
		$('error_popupDesignConsultation_'+fld).setStyle('display', 'none');
	}


	return (err) ? false : true;
}




function popupDiv(div) {
	div = $(div);
	if(!div) return;

	showOverlay();
	popupDivShowDiv(div);
}

function popupDivShowDiv(div) {
	div = $(div);
	if(!div) return;

	var lst = $$('div.popupDiv');
	for(var i=0; i<lst.length; ++i) {
		popupDivHideDiv(lst[i]);
	}

	div.setStyle('opacity', 0);
	div.setStyle('display', 'block');
	div.setStyle('z-index', 99);

	div.setStyle('left', (getPageSize().windowWidth / 2) - (div.offsetWidth / 2)  );
	div.setStyle('top', ( (getPageSize().windowHeight / 2) - (div.offsetHeight / 2) ) * 0.7 );

	new Fx.Tween(div, {
		duration: 500
	}).start('opacity', 1);
}

function popupDivClose(div) {
	div = $(div);
	hideOverlay();
	popupDivHideDiv(div);
}

function popupDivHideDiv(div) {
	div = $(div);
	if(div && div.getStyle('display') == 'block') {
		new Fx.Tween(div, {
			duration: 500,
			onComplete: function() {
				div.setStyle('display', 'none');
			}
		}).start('opacity', 0);
	}
}

function showOverlay() {
	var div = $('divOverlay');
	if(!div) {
		with( div = $(document.createElement('div')) ) {
			id = 'divOverlay';
			setStyle('position', 'absolute');
			setStyle('display', 'none');
			setStyle('z-index', 90);
			setStyle('background', '#4F4F4F');
			setStyle('top', 0);
			setStyle('left', 0);
			setStyle('opacity', 0);
		}
		document.body.appendChild(div);
	}

	if(div.getStyle('display') == 'block') return;

	div.setStyle('opacity', 0);
	div.setStyle('display', 'block');

	div.setStyle('width', getPageSize().windowWidth);
	div.setStyle('height', getPageSize().windowHeight);

	new Fx.Tween(div, {
		duration: 500
	}).start('opacity', 0.4);
}

function hideOverlay() {
	var div = $('divOverlay');
	if(!div || div.getStyle('display') == 'none' ) return;

	new Fx.Tween(div, {
		duration: 500,
		onComplete: function() {
			div.setStyle('display', 'none');
		}
	}).start('opacity', 0);

}
























function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	if(yScroll < windowHeight) pageHeight = windowHeight;
	else pageHeight = yScroll;
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth) pageWidth = windowWidth;
	else pageWidth = xScroll;

	arrayPageSize = {
		'pageWidth':	pageWidth,
		'pageHeight':	pageHeight,
		'windowWidth':	windowWidth,
		'windowHeight':	windowHeight
	}
	return arrayPageSize;
}


