var curHash;
var crossInterval;

Array.prototype.find = function(element)
{
	for(var key in this)
	{
		if(this[key] == element)
		{
			return key;
			break;
		}
	}
	return -1;
}

function catalogInit()
{
	curHash = window.location.hash.replace('#', '');
	if(window.location.hash == '')
	{
		var list = location.href.split('/');
		curHash = list[list.length-1];
	}
	setInterval("checkNavigationHashChange()", 500);
	startCrossSelling();
	if(document.getElementById('menu'))
	{
		document.getElementById('menu').onclick = function(e)
		{
			if(!e)
			{
				var e = window.event;
			}
			var target = e.target || e.srcElement;
			if(target.tagName == 'A')
			{
				target = target.parentNode;
			}
			if(target.tagName == 'P' && target.id != 'navCart' && target.id.indexOf('shops') == -1)
			{
				id = target.id.substr(8);
				if(target.className.indexOf('treeHeader') != -1)
				{
	/* CHANGE NAVIGATION HEADER */
					var list = document.getElementById('menu').getElementsByTagName('DIV');
					for(var i = 0 ; i < list.length ; i++)
					{
						if(list[i].className.length == 12 && list[i].className.substring(0, 11) == 'headerGroup')
						{
							list[i].className = list[i].className.replace('1', '0');
							var listItems = list[i].getElementsByTagName('P');
							for(j = 0 ; j < listItems.length ; j++)
							{
								listItems[j].className = listItems[j].className.replace('Active', '');
							}
							var listLists = list[i].getElementsByTagName('UL');
							for(j = 0 ; j < listLists.length ; j++)
							{
								listLists[j].style.display = 'none';
							}
						}
					}
					target.parentNode.className = target.parentNode.className.replace('0', '1');
					if(document.getElementById('menuList'+id))
					{
						target.className = target.className+'Active';
						document.getElementById('menuList'+id).style.display = 'block';
					}
					else
					{
						loadFile('post', '/includes/navigation.php', 'menuId='+id+'&level=1&name='+target.getElementsByTagName('A')[0].innerHTML, 'text', 'menuContainer'+id);
					}
				}
				else if(target.id.indexOf('menuItem') != -1)
				{
	/* CHANGE NAVIGATION LINK */
					var list = document.getElementById('menu').getElementsByTagName('UL');
					for(var i = 0 ; i < list.length ; i++)
					{
						list[i].style.display = 'none';
					}
					list = document.getElementById('menu').getElementsByTagName('P');
					for(var i = 0 ; i < list.length ; i++)
					{
						list[i].className = list[i].className.replace('Active', '');
					}
					if(document.getElementById('menuList'+id))
					{
						target.className = target.className+'Active';
						document.getElementById('menuList'+id).style.display = 'block';
					}
					else
					{
						var level = parseInt(target.className.substring(5, 6)) + 1;
						loadFile('post', '/includes/navigation.php', 'menuId='+id+'&level='+level+'&name='+target.getElementsByTagName('A')[0].innerHTML, 'text', 'menuContainer'+id);
					}
					var parent = document.getElementById('menuContainer'+id).parentNode.parentNode;
					while(parent.id.substring(0, 13) == 'menuContainer')
					{
						parent.getElementsByTagName('P')[0].className = parent.getElementsByTagName('P')[0].className+'Active';
						parent.getElementsByTagName('UL')[0].style.display = 'block';
						parent = parent.parentNode.parentNode;
					}
				}
				if(id != 'Shops')
				{
					changeCatalogPage(id);
				}
			}
			return false;
		}
	}
}

function checkNavigationHashChange()
{
	var ignoreHash = new Array('0', '0.5', '1', '1.5', '2', '2.5', '3', '3.5', '4', '4.5', '5');
	if(ignoreHash.find(window.location.hash.replace('#', '')) == -1)
	{
		var newHash = window.location.hash.replace('#', '');
		if(newHash == '')
		{
			var list = location.href.split('/');
			newHash = list[list.length-1];
		}
		if(curHash != newHash)
		{
			curHash = window.location.hash.replace('#', '');
			if(curHash == '')
			{
				var list = location.href.split('/');
				curHash = list[list.length-1];
			}
			changeNavigation(curHash);
		}
	}
}

function changeNavigation(id)
{
	changeCatalogPage(id);
	var list = document.getElementById('menu').getElementsByTagName('DIV');
	for(var i = 0 ; i < list.length ; i++)
	{
		if(list[i].className.length == 12 && list[i].className.substring(0, 11) == 'headerGroup')
		{
			list[i].className = list[i].className.replace('1', '0');
			var listItems = list[i].getElementsByTagName('P');
			for(j = 0 ; j < listItems.length ; j++)
			{
				listItems[j].className = listItems[j].className.replace('Active', '');
			}
			var listLists = list[i].getElementsByTagName('UL');
			for(j = 0 ; j < listLists.length ; j++)
			{
				listLists[j].style.display = 'none';
			}
		}
	}
	var list = document.getElementById('menu').getElementsByTagName('UL');
	for(var i = 0 ; i < list.length ; i++)
	{
		list[i].style.display = 'none';
	}
	list = document.getElementById('menu').getElementsByTagName('P');
	for(var i = 0 ; i < list.length ; i++)
	{
		list[i].className = list[i].className.replace('Active', '');
	}
	loadFile('post', '/includes/navigation.php', 'menuId='+id, 'json', catalogMenuResponse);
}

function changeCatalogPage(id)
{
	activePageId = id;
	window.location.hash = activePageId;
	curHash = activePageId;
	setCatalogLoader();
	loadFile('post', '/includes/catalogHandler.php', 'id='+id, 'text', catalogPageResponse);
}

function catalogMenuResponse(data)
{
	if(document.getElementById('menuContainer'+data.sourceId))
	{
		document.getElementById('menuContainer'+data.sourceId).innerHTML = data.html;
		document.getElementById('menuContainer'+data.sourceId).className = document.getElementById('menuContainer'+data.sourceId).className.replace('0', '1');
	}
	clearRequest();
}

function catalogPageResponse(data)
{
	if(req.getResponseHeader('CatalogPageId') == activePageId)
	{
		document.getElementById('right').innerHTML = data;
		document.title = req.getResponseHeader('CatalogPageTitle');
		prepareTextBlock();
	}
	clearRequest();
}

function setCatalogLoader()
{
	var preLoader = '<div style="text-align:center;margin-top:50px"><img src="/images/general/layout/preloader.gif" alt="loading" /><br /><br />Loading...<\/div>';
	if(document.getElementById('right').innerHTML != preLoader)
	{
		document.getElementById('right').innerHTML = preLoader;
	}
}

function offerInfo_addqty(line, qty)
{
	if(line = document.getElementById('qte'+line))
	{
		line.value =  parseInt(line.value) + parseInt(qty);
		if(line.value <= 0)
		{
			line.value = Math.abs(qty);
		}
	}
	return false;
}

function offerInfo_checkqty(line, qty)
{
	if(line = document.getElementById('qte'+line))
	{
		if(isNaN(line.value) || parseInt(line.value) < parseInt(qty))
		{
			line.value = qty;
		}
		else if(line.value%qty != 0)
		{
			line.value = line.value - (line.value%qty);
			startAlert(7, {'minAmount':qty, 'newAmount':line.value});
		}
	}
	return false;
}

function showMoreOfferInfo(self, line)
{
	if(document.getElementById('offerInfoLine'+line))
	{
		if(document.getElementById('offerInfoLine'+line).style.display == 'none')
		{
			document.getElementById('offerInfoLine'+line).style.display = 'table-row';
			self.src = '/images/general/layout/navigationSubArrowActive.png';
		}
		else
		{
			document.getElementById('offerInfoLine'+line).style.display = 'none';
			self.src = '/images/general/layout/navigationSubArrow.png';
		}
	}
}

function filterOfferlist(self)
{
	if(self.tagName == 'LI')
	{
		self.firstChild.checked = !self.firstChild.checked;
		var filterList = document.getElementById('offerListFilters').getElementsByTagName('DIV');
		var filterString = 'filter=true&id='+document.getElementById('subID').value;
		for(i=0;i<filterList.length;i++)
		{
			if(filterList[i].className == 'offerListFilterList')
			{
				filterString += '&'+filterList[i].id.substr(7)+'=';
				elementList = filterList[i].getElementsByTagName('LI');
				for(j=0;j<elementList.length;j++)
				{
					if(elementList[j].firstChild.checked == true)
					{
						filterString += "'"+elementList[j].firstChild.value+"',";
					}
				}
				filterString = filterString.substr(0, filterString.length-1);
			}
		}
		loadFile('post', '/includes/offerlistItems.php', filterString, 'text', 'offerList');
	}
}

function startCrossSelling()
{
	//crossInterval = setInterval('document.getElementById(\'crossNext\').onclick()', 5000);
}

function crossSelling(id, showID)
{
	clearInterval(crossInterval);
	loadFile('post', '/includes/offer.php', 'cross=true&offerID='+id+'&showOffer='+showID, 'text', 'offerCross');
	startCrossSelling();
}

function prepareTextBlock() {
	if($('#description > p').length)
	{
		var textbox = $('#description > p').eq(0);
		var button = $('#description .more').eq(0);
		var height = $(textbox).css('height');
		var width = $(button).css('width');
		$(textbox).attr('smallheight', $(textbox).css('height')).css('height', 'auto').attr('fullheight', $(textbox).height()+'px').css('height', height);
		$(button).attr('bigwidth', $(button).css('width')).css('width', 'auto').attr('smallwidth', $(button).width()+'px').css('width', width);
	}
}

(function ($) {
	window.$ = window.jQuery;
	$(document).ready(function () {
/* SEO textblock */
		prepareTextBlock();
		$('#description .more').live('click', function () {
			var textbox = $('#description > p').eq(0);
			var button = $('#description .more').eq(0);
			if($(textbox).css('height') == $(textbox).attr('smallheight')) {
				$(textbox).animate({height: $(textbox).attr('fullheight')}, 'slow');
				$(button).animate({width: $(button).attr('smallwidth')}, 'slow', function () {
					$(button).find('span').hide();
					$(button).find('img').show();
				});
			}
			else {
				$(textbox).animate({height: $(textbox).attr('smallheight')}, 'slow');
				$(button).animate({width: $(button).attr('bigwidth')}, 'slow', function () {
					$(button).find('span').show();
					$(button).find('img').hide();
				});
			}
		});
/* R&R and A&A tabs */
		$('#offerInfoMenu > div.offerTab').live('click', function () {
			var shade = $('#offerInfoMenu div.offerTabActive > div').clone();
			var tab = this.title;
			$('#offerInfoMenu div.offerTabActive > div').remove();
			$('#offerInfoMenu > div').attr('class', 'offerTab');
			$(this).attr('class', 'offerTabActive').append(shade);
			$('#offerInfoBlock > div').hide();
			$('#offerInfoBlock > div.'+tab).show();
		});
	});
})(window.jQuery)




/** Bazaar Voice **/
function bvShowTab(application, displayCode, subjectId, deepLinkId) {
	if (application == 'PRR') {
		$('#offerInfoMenu div[title="reviews"]').click();
	}
	if (application == 'QA') {
		$('#offerInfoMenu div[title="questions"]').click();
	}
}

function ratingsDisplayed(totalReviewsCount, avgRating, ratingsOnlyReviewCount, recommendPercentage, productID) {
	if (totalReviewsCount == 0) {
		var bvRevCntr = document.getElementById("BVReviewsContainer");
		var bvSVPLink = document.getElementById("BVSVPLinkContainer");
		if (bvRevCntr) { bvRevCntr.style.display = "none"; }
		if (bvSVPLink) { bvSVPLink.style.display = "none"; }
	}
}

function BVQADisplayed(questionCount, answerCount) {
	if (questionCount > 0) {
		var bvALPLink = document.getElementById("BVALPLinkContainer");
		if (bvALPLink) { bvALPLink.style.display = "block"; }
	}
}