/*js*/
var evt = YAHOO.util.Event,
	dom = YAHOO.util.Dom,
	connect = YAHOO.util.Connect,
	body,html;
	

evt.onDOMReady
(
	function()
	{
		html = document.documentElement;
		body = document.getElementsByTagName('body')[0];
		if(YAHOO.env.ua.ie && (YAHOO.env.ua.ie < 7))
		{
			minWidth();
			evt.on(window, 'resize', minWidth);
			setFixPng(dom.getElementsByClassName('png', 'img'));
		}
		if(dom.get('new')) initSlideDown();
		hoverTopMenu();
		hoverIcon();
		initNewOffer();
	}
);

function minWidth()
{
	var wr = dom.get('wrapper'), footer = dom.get('footer');
	wr.style.width = (html.offsetWidth < 1000) ? '1000px' : '100%';
	footer.style.width = (html.offsetWidth < 1000) ? '1000px' : '100%';
}

function setFixPng(arr)
{
	for(var i=0, l=arr.length; i < l; i++)
	{
		if(/png/.test(arr[i].className)) fixPNG(arr[i]);
	}
}

function fixPNG(el)
{
    var src;
    if (/\.png$/.test(el.src))
    {
        src = el.src;
        el.src = '/i/blank.gif';
    }
    if (src) el.runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + src + '\',sizingMethod=\'scale\')';
}

function hoverTopMenu()
{
	var topMenuItems = dom.getElementsByClassName('hover', 'li', 'top-menu');
	for( var i = 0; i < topMenuItems.length; i++)
	{
		evt.on(topMenuItems[i], 'mouseover', toggleClass, { obj :  topMenuItems[i]}, true);
		evt.on(topMenuItems[i], 'mouseout', toggleClass, { obj :  topMenuItems[i]}, true);
		evt.on(topMenuItems[i], 'click', gotoPage, { obj :  topMenuItems[i]}, true);
	}
}


function gotoPage()
{
	document.location = this.obj.firstChild.firstChild.firstChild.getAttribute('href');
}

function toggleClass()
{
	var classN = this.obj.className;
	classN = (/selected/.test(classN)) ? classN.replace(' selected', '') : classN + ' selected';
	this.obj.className = classN;
}

function hoverIcon()
{
	var icon = dom.getElementsByClassName('icon', 'img', 'columns');
	for( var i = 0; i < icon.length; i++)
	{
		evt.on(icon[i], 'mouseover', toggleSrc, { obj :  icon[i]}, true);
		evt.on(icon[i], 'mouseout', toggleSrc, { obj :  icon[i]}, true);
	}
}

function toggleSrc()
{
	var src = this.obj.src, h1 = dom.get(this.obj.id+'-h'), newSrc,i;
	if(/selected/.test(src))
	{
		src = src.replace('selected-', '');
		h1.style.color = '#000';
	}
	else
	{
		i = src.lastIndexOf('/');
		newSrc = src.substr(0,i+1) + 'selected-' + src.substr(i+1);
		src = newSrc;
		h1.style.color = '#3aaad8';
	}
	this.obj.src = src;
}

function toggleBookmarks(el)
{
	var bm = dom.getElementsByClassName('bkm','div','bkm-frame');
	if(/no-selected/.test(el.className))
	{
		el.className = el.className.replace(' no-selected', ' selected');
		dom.get(el.id+'-content').style.display = '';
		for( var i = 0; i < bm.length; i++)
		{
			if(bm[i].id != el.id)
			{
				bm[i].className = bm[i].className.replace(' selected', ' no-selected');
				dom.get(bm[i].id+'-content').style.display = 'none';
			}
		}
	}
}
var countIcon = 0,
	currentPos = 0,
	authorizedCount = 0,
	box,
	boxFrame,
	boxParent,
	boxMargin = 100,
	currentVisible = 0,
	iconWidth = 140,
	left = 0,
	lButton,
	rButton;

function initNewOffer()
{
	countIcon = dom.getElementsByClassName('n','a','novinki-frame').length;
	box = dom.get('novinki');
	boxFrame = dom.get('novinki-frame');
	boxParent = dom.get('start-news');
	currentVisible = Math.floor((boxParent.offsetWidth-boxMargin)/iconWidth);
	authorizedCount = countIcon - currentVisible;
	lButton = dom.get('left-nav').firstChild;
	rButton = dom.get('right-nav').firstChild;
	hideUnnecessaryIcon();
	evt.on(window, 'resize', hideUnnecessaryIcon);
	evt.on(lButton, 'click', navigateLeft);
	evt.on(lButton, 'mousedown', pushDown, { obj : lButton }, true);
	evt.on(lButton, 'mouseup', pushUp, { obj : lButton }, true);
	evt.on(rButton, 'click', navigateRight);
	evt.on(rButton, 'mousedown', pushDown, { obj : rButton }, true);
	evt.on(rButton, 'mouseup', pushUp, { obj : rButton }, true);
}

function hideUnnecessaryIcon()
{
	//box.style.width = 'auto';
	var c = Math.floor((boxParent.offsetWidth-boxMargin)/iconWidth);
	//box.style.width = c*iconWidth + 'px';
	currentVisible = c;
	authorizedCount = countIcon - currentVisible;
	rButton.src = (authorizedCount > 0) ? '/i/right-arrow-active.jpg' : '/i/right-arrow-passive.jpg';
}

function navigateLeft(e)
{
	evt.stopPropagation(e);
	if(currentPos != 0)
	{
		left += iconWidth;
		new Effect.Morph ( boxFrame, { style:'left:'+left+'px', duration:0.5 } );
		rButton.src = (authorizedCount > 0) ? '/i/right-arrow-active.jpg' : '/i/right-arrow-passive.jpg';
		if(currentPos == 1) lButton.src = '/i/left-arrow-passive.jpg';
		currentPos--;
	}
}
function navigateRight(e)
{
	evt.stopPropagation(e);
	if(currentPos < authorizedCount)
	{
		lButton.src = '/i/left-arrow-active.jpg';
		left -= iconWidth;
		new Effect.Morph ( boxFrame, { style:'left:'+left+'px', duration:0.5 } );
		if(currentPos == authorizedCount-1) rButton.src = '/i/right-arrow-passive.jpg';
		currentPos++;
	}
}

function openBigImage(src,e)
{
	evt.stopPropagation(e);
	evt.preventDefault(e);
	var div = document.createElement('div'), img = new Image();
	img.onload = function()
	{ 
		div.style.border = '1px solid #636363';
		div.style.position = 'absolute';
		div.style.top = Math.round((html.clientHeight - div.offsetHeight)/2) + html.scrollTop + 'px';
		div.style.left = Math.round((html.clientWidth - div.offsetWidth)/2) + 'px';
		div.style.visibility = 'visible';
		evt.on(img, 'click', function(){ body.removeChild(div)})
	}
	body.appendChild(div);
	div.appendChild(img);
	div.style.visibility = 'hidden';
	img.src = src;
	img.style.verticalAlign = 'bottom';
}

function pushDown()
{
	this.obj.src = (/active/.test(this.obj.src)) ? this.obj.src.replace('.jpg','-pushed.jpg') : this.obj.src;
}

function pushUp()
{
	this.obj.src = (/active/.test(this.obj.src)) ? this.obj.src.replace('-pushed','') : this.obj.src;
}
