//<![CDATA[
hs.registerOverlay({
	html: '<div class="closebutton" onclick="return hs.close(this)" title="Close"></div>',
	position: 'top right',
	fade: 2 // fading the semi-transparent overlay looks bad in IE
});

hs.graphicsDir = '/js/highslide/graphics/';
//	hs.outlineType = 'rounded-white';
hs.wrapperClassName = 'draggable-header';
hs.showCredits = false;

var openedWindows = new Array();
var productWindowsCount = 0;
var lastFocusedDivId  = '';

hs.Expander.prototype.onBeforeClose = function (sender)
{
	openedWindows[this.maincontentId] = false;
}

hs.Expander.prototype.onAfterExpand = function (sender)
{
	nWindowHeight = document.documentElement.clientHeight;
	nElementHeight = document.getElementById(this.maincontentId).style.height.replace("px", "");
	
	if(nElementHeight  > nWindowHeight)
		document.getElementById(this.maincontentId).style.height = nWindowHeight - 100;
	hs.getExpander(this.maincontentId).reflow();
	
	CSBfleXcroll(this.maincontentId);
}

hs.Expander.prototype.onDrag = function (sender, e)
{
	if(e.type == 'resize')
	{
		document.getElementById(this.maincontentId).style.height = e.height + e.dY - 60 + "px";
		CSBfleXcroll(this.maincontentId);
	}
}

hs.Expander.prototype.onFocus = function (sender)
{	
	if(	lastFocusedDivId != 'products_details' && 
		lastFocusedDivId != 'products_full_range' &&
		lastFocusedDivId != 'info_expander')
	lastFocusedDivId = this.maincontentId;
}

hs.Expander.prototype.onBlur = function (sender)
{
	lastFocusedDivId = this.maincontentId;
}

function create_xmlhttp()
{
	var xmlHttp;
	try	{	xmlHttp=new XMLHttpRequest();	}
	catch (e)
	{
		// Internet Explorer
		try	{	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");	}
		catch (e)
		{
			try			{	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");	}
			catch (e)	{	return false;	}
		}
	}
	return xmlHttp;
}
function updateExpander(divId, url)
{
	var xmlHttp = create_xmlhttp();
	// do nothing if wasn't created
	if(!xmlHttp) { alert("Your browser does not support AJAX!"); return FALSE; }
	
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			document.getElementById(divId + "_content").innerHTML = xmlHttp.responseText;
			hs.getExpander(divId).focus();
			
			setTimeout(function() {document.getElementById(divId).scrollUpdate(); }, 1);
			//hs.getExpander(divId).reflow();
			
			// проверить не сменился ли контент на менюху с продукцией
			if(hs.getExpander(divId).myHref.search("main_menu/products") != -1)
				updateProductWindowsOnStripSwap();
		}
		
		if(xmlHttp.readyState==1)
		{
			document.getElementById(divId + "_content").innerHTML = "<img src='/img/loader.gif'> Loading";
			document.getElementById(divId).fleXcroll.setScrollPos(false, 0);
		}
	}
	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return false;
}

function showHideSubMenuExpander(element, heading, divId)
{
	if(divId == 'products_details')		{ width = 465; height = 490; x = 50; y = -520; }
	if(divId == 'products_full_range')	{ width = 525; height = 490; x = 70; y = -500; }
	if(divId == 'info_expander')		{ width = 690; height = 490; x = 50; y = -520; }
	
	if(divId == 'main_menu')			{ width = 250; height = 535; x = -220; y = -540; }
	if(divId == 'search')				{ width = 500; height = 450; x = 50; y = -510; }
	if(divId == 'order_sample')			{ width = 500; height = 650; x = 50; y = -610; }
	
	positionX = 'mi_about ' + x.toString() + 'px';
	positionY = 'mi_about ' + y.toString() + 'px';
	
	if(!openedWindows[divId])
	{
		openedWindows[divId] = true;
		
		hs.htmlExpand(element, {	objectType: 'ajax',
									headingText: heading,
									maincontentId: divId,
									targetX: positionX,
									targetY: positionY,
									width: width,
									height: height,
									preserveContent: false,
									cacheAjax: false} );
									
		setTimeout(function() { hs.getExpander(divId).myHref = element.href; }, 500);
	}
	else
	{
		//проверить совпадают ли 
		if(hs.getExpander(divId).myHref != element.href)
		{
			hs.getExpander(divId).focus();
			hs.getExpander(divId).myHref = element.href;

			updateExpander(divId, element.href + "update/");
			hs.getExpander(divId).heading.innerHTML = heading;
		}
		else
		{
			if(lastFocusedDivId == divId)
				hs.getExpander(divId).close();
			else
				hs.getExpander(divId).focus();
		}
	}
	return false;
}

function updateProductWindowsOnStripSwap()
{
	if(openedWindows["products_details"])
	{
		url = hs.getExpander("products_details").myHref;
		heading = hs.getExpander("products_details").heading.innerHTML;
		
		if(url.search("1") == -1)
		{
			heading = heading.replace('3', '1');
			url = url.replace('3', '1');
		}
		else
		{
			heading = heading.replace('1', '3');
			url = url.replace('1', '3');
		}
		
		hs.getExpander("products_details").heading.innerHTML = heading;
		hs.getExpander("products_details").myHref = url;
		updateExpander("products_details", url + "update/");
	}
	
	setTimeout(function() {
		if(openedWindows["products_full_range"])
		{
			url = hs.getExpander("products_full_range").myHref;
			heading = hs.getExpander("products_full_range").heading.innerHTML;
			
			if(url.search("1") == -1)
			{
				heading = heading.replace('3', '1');
				url = url.replace('3', '1');
			}
			else
			{
				heading = heading.replace('1', '3');
				url = url.replace('1', '3');
			}
			
			hs.getExpander("products_full_range").heading.innerHTML = heading;
			hs.getExpander("products_full_range").myHref = url;
			updateExpander("products_full_range", url + "update/");
		}
	}, 500);
}
