var browserName=navigator.appName;
window.isIE6 = false;
window.isOpened = false;

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

var fade_popup_mode = getCookie('fade_popup');
//alert(fade_popup_mode);
fade_step = (fade_popup_mode==1) ? 1 : 0.05;
fade_delay = (fade_popup_mode==1) ? 0 : 5;

var fade = { // Namespace
	step	: fade_step, //0.05,
	delay	: fade_delay, // ms
	timer	: null,
	setOpacity : function(elem, nOpacity) {
		if (typeof elem == 'string') elem = document.getElementById(elem);
		var props = ['MozOpacity', 'KhtmlOpacity', 'opacity'];
		for (var i in props) {
			if (typeof elem.style[props[i]] == 'string') {
				elem.style[props[i]] = nOpacity;
				return;
			}
		}
		// IE 6+
		try {
			nOpacity = 100 * parseFloat(nOpacity);
			if ((oAlpha = elem.filters['DXImageTransform.Microsoft.alpha'] || elem.filters.alpha)) oAlpha.opacity = nOpacity;
			else elem.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+nOpacity+");";
		} catch (e) {
			// IE <= 5.5 OR Opera < 9 OR another browser. Do nothing
		}
	},
	_out : function(id, from, to, callback) {
		from -= this.step;
		from = from <= to ? to : from;
		if (this.doit(id, from, to, callback))
			this.timer = setTimeout("fade._out('"+id+"', "+from+", "+to+", '"+(callback ? callback : '')+"')", this.delay);
	},
	_in : function(id, from, to, callback) {
		from += this.step;
		from = from >= to ? to : from;
		if (this.doit(id, from, to, callback))
			this.timer = setTimeout("fade._in('"+id+"', "+from+", "+to+", '"+(callback ? callback : '')+"')", this.delay);
	},
	doit : function(id, from, to, callback) {
		this.setOpacity(id, from);
		clearTimeout(this.timer);
		if (from == to) {
			if (callback) eval(callback+'()');
		} else return true;
	}
}


if (browserName=="Microsoft Internet Explorer")
{
	var agt=navigator.userAgent.toLowerCase();
	var iePos	= agt.indexOf('msie');
	var browserVer = parseFloat(agt.substring(iePos+5,agt.indexOf(';',iePos)));

	if(browserVer < 30)	 window.isIE6=true;
}

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 getPageSize() {

 var xScroll, yScroll;

if (window.innerHeight && window.scrollMaxY) {
	xScroll = window.innerWidth + window.scrollMaxX;
	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
	if(document.documentElement.clientWidth){
		windowWidth = document.documentElement.clientWidth;
	} else {
		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;
}

// for small pages with total height less then height of the viewport
if(yScroll < windowHeight){
	pageHeight = windowHeight;
} else {
	pageHeight = yScroll;
}

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

pageHeight = getDocHeight();


return [pageWidth,pageHeight];
}

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 [ scrOfX, scrOfY ];
}

function getAvailSize() {
	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 [myWidth,myHeight];
}



function popupShow(elemId,prodName)
{
	if (elemId == '' || elemId == null || $(elemId) == null) {
		alert(elemId);
		alert($(elemId));
		return false;

	}
	if(prodName)
	{
		$('prodNameCont').update(unescape(prodName));
	}

	window.openedWndID = elemId;
	var arrayPageSize = this.getPageSize();
	$('popup_overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px', opacity: 0 });

	//if(document.getElementById(elemId+'_holder')==null){
		$('popup_window').appendChild(Builder.node('div',{id:elemId+'_holder'}));
	//}
	$(elemId+'_holder').update($(elemId).innerHTML);
	$(elemId).update("");

	positionPopup();

	if(isIE6)
	{
		$$('select').each(function(sel){
			if(typeof(sel.up('.popup_wnd')) == 'undefined') sel.style.visibility = 'hidden';
		});
	}
	$('popup_overlay').show();

	//fade._in('popup_overlay', 0.5, 0.95 );


	fade._in('popup_overlay', 0, 0.95, 'showWindowPopup');

	if($('flashid')) $('flashid').style.visibility = 'hidden';

	window.isOpened = true;
	internalLink = false;

}

function showWindowPopup() {
	$('popup_window').show();
	fade._in('popup_window', 0, 1);

}

function positionPopup()
{
	if(isIE6)
	{
		$('popup_window').style.marginLeft = '-'+$('popup_window').getWidth()/2 + 'px';

		if(getAvailSize()[1] < $('popup_window').getHeight())
		{
			$('popup_window').style.position = 'absolute';
			$('popup_window').style.top = getScrollXY()[1] + 'px';
			$('popup_window').style.marginTop = '0px';
		}
		else
		{
			$('popup_window').style.position = 'absolute';
			$('popup_window').style.top = getScrollXY()[1] + (getAvailSize()[1]/2)- $('popup_window').getHeight()/2 + 'px';
			$('popup_window').style.marginTop = '0px';
		}

	}
	else
	{
		$('popup_window').style.marginLeft = '-'+$('popup_window').getWidth()/2 + 'px';

		if(getAvailSize()[1] < $('popup_window').getHeight())
		{
			$('popup_window').style.position = 'absolute';
			$('popup_window').style.top = getScrollXY()[1] + 'px';
			$('popup_window').style.marginTop = '0px';
		}
		else
		{
			$('popup_window').style.top = '50%';
			$('popup_window').style.position = 'fixed';
			$('popup_window').style.marginTop = '-'+$('popup_window').getHeight()/2 + 'px';

		}
	}
}
function popupClose()
{
	fade._out('popup_window', 1, 0, 'popupCloseWindow');

	/*$(window.openedWndID).update($(window.openedWndID+'_holder').innerHTML);
	$('popup_window').update('');
	$('popup_window').hide();
	if($('flashid')) $('flashid').style.visibility = 'visible';

	window.isOpened = false;

	if(isIE6)
	{
		$$('select').each(function(sel){
			 if(typeof(sel.up('.popup_wnd')) == 'undefined') sel.style.visibility = 'visible';
		});
	}
	internalLink = false;*/
}

function popupCloseWindow() {
	$(window.openedWndID).update($(window.openedWndID+'_holder').innerHTML);
	$('popup_window').update('');
	$('popup_window').hide();
	window.isOpened = false;
	fade._out('popup_overlay', 0.95, 0, 'popupCloseOverlay');

}

function popupCloseOverlay() {
	$('popup_overlay').hide();
//	$(window.openedWndID).update($(window.openedWndID+'_holder').innerHTML);
	//$('popup_window').update('');
	//$('popup_window').hide();
	if($('flashid')) $('flashid').style.visibility = 'visible';

	window.isOpened = false;

	if(isIE6)
	{
		$$('select').each(function(sel){
			 if(typeof(sel.up('.popup_wnd')) == 'undefined') sel.style.visibility = 'visible';
		});
	}
	internalLink = false;
}


function showTab(tabID, obj)
{
	$$('#tab_nav a').each(function(it){
		it.className = '';
	});

	$$('.tabCont').each(function(it){
		it.style.display = 'none';
	});

	$(obj).className = 'sel';
	$(tabID).style.display = 'block';

	positionPopup();
}


//Event.observe(window, 'load', function () {
/*		new Insertion.Bottom(document.body,'<div id="popup_overlay"></div>');
new Insertion.Bottom(document.body,'<div id="popup_window"></div>');*/
/*document.body.appendChild(Builder.node('div',{id:'popup_overlay'}));
document.body.appendChild(Builder.node('div',{id:'popup_window'}));

$('popup_overlay').setStyle({'position': 'absolute', 'top': '0', 'left': '0', 'z-index': '90', 'width': '100%', 'height': '100%', 'background': '#fff', 'display':'none', 'opacity':'0.8'});

$('popup_window').setStyle({'position':'fixed', 'top':'50%', 'left':'50%', 'z-index':'100', 'display':'none'});

if(isIE6)
{
	$('popup_window').style.position = 'absolute';

	Event.observe(window, 'scroll', function () {
	if(isOpened && getAvailSize()[1] >= $('popup_window').getHeight()) positionPopup();
		});
}

Event.observe(window, 'resize', function () {
		positionPopup();
	});

});*/

