﻿$(document).ready(function() {
	insertBlogMenu();
	$('#TopNavMenu').CSSDropDownMenu();
	$('#TopNavMainMenu').CSSDropDownMenu();
	updateTopNavigationMainMenuClass();
	$('#TopNavigationSearchBox').hint();
	$('#subscribeInput').hint();
	/*setPageHeight();
	$(window).resize(function() {
	  setPageHeight();
	});
	highlightCurrentParentCategory();*/
	removeBrandsFromMenu();
	createDropDownLists();
	replaceFonts();
	displayCartMessage();
});
function updateTopNavigationMainMenuClass() {
	$('#TopNavMainMenu .TopLevelItem:not(:first)').addClass('showBackground');
}
function highlightCurrentParentCategory() {
	var cat = '';
	cat = $.getUrlVar('Category')//.substring(1,4);
	if(cat) {
		cat = cat.substring(1,4);
		var links = $('#TopNavMenu > ul > li > a');
		for(var i = 0; i < links.length; i++) {
			if($(links).eq(i).attr('href').indexOf(cat) > -1) {
				$(links).eq(i).parent().addClass('Selected');
				break;
			}
		}
	}
}

function setPageHeight() {
	var viewportHeight = $(window).height();
	var navHeight = $('#navmain1').height();
	var contentHeight = $('#content').height();
	var footerHeight = $('#footer').height();
	var totalSiteHeight = navHeight + contentHeight + footerHeight;
	if(viewportHeight > totalSiteHeight) {
		var requiredContentHeight = viewportHeight - footerHeight - navHeight;
		$('#content').css({'height':requiredContentHeight + 'px'})
	}
}
function removeBrandsFromMenu() {
	$('.TopLevelSubList > li > a[id$="99"]').parent().remove();
}
function createDropDownLists() {
	var i = '09';
	if($('.TopLevelItemSX' + i).length > 0)
		addList(i);
	for(var i = 10; i <= 11; i++) {
		if($('.TopLevelItemSX' + i).length > 0)
			addList(i);
	}
	var i = 99;
	if($('.TopLevelItemSX' + i).length > 0)
		addList(i);
	var maxWidth = 0;
	$('.TopLevelItemSX99 li').each(function() {
		if($(this).width() > maxWidth)
			maxWidth = $(this).width();
	});
	$('.TopLevelSubListSX99').css({'width':eval(3 * maxWidth + 18) + 'px','margin-left': eval(-1 * (3 * maxWidth + 18)/2) + 'px'});
	$('.TopLevelSubListSX99 li').css({'width':eval(maxWidth) + 'px','float' : 'left'});
	$('.TopLevelSubListSX99 a').css({'width':eval(maxWidth) + 'px'});
}
function addList(i) {
	var obj = $('.TopLevelItemSX' + i);
	$('.TopLevelItemSX' + i +' > a').contents().unwrap().wrap('<span class="TopLevelItemSpan" />');
	$('<div class="DropDownListDivSX' + i + '"><ul class="DropDownList DropDownListSX' + i + '"><ul></div>').appendTo('#SubMenuContainer');
	$('.TopLevelItemSX' + i).clone().appendTo('.DropDownListSX' + i);
	$('.DropDownListDivSX' + i).CSSDropDownMenu();
}
function doSubscribeEnter(e) {
	var KeyID = (window.event) ? e.keyCode : e.which;
	if(KeyID == 13) {
		document.getElementById('subscribeButton').focus();
	}
}
function doSubscribeButton() {
	var searchvalue = document.getElementById('subscribeInput').value;
	window.location.href="/Questionnaire.aspx?Questionnaire=SUBSCRIBE&Email="+escape(searchvalue.replace("&",""))+"";
}
function replaceFonts() {
	Cufon.replace('.CampaignLink');
}
function fixImageError(obj) {
	//alert('hello');
	$(obj).attr('src','/images/controls/bit.gif');
}
function displayCartMessage() {
	if(window.location.href.toLowerCase().indexOf('orders.aspx') > 0) {
		if ($('div.DiscountMsg').length > 0) {
			$('div.DiscountMsg').css({'display':'block','text-align':'center'});
		}
	}
}
function insertBlogMenu() {
	//iGlamourBlogContent
	//http://blog.i-glamour.com
	$('#CategoryMenu .TopLevelItemSX12').after('<li class="TopLevelItem TopLevelItemBlog"><a class="TopLevelItemLink" href="http://blog.i-glamour.com/">Blog</a></li>');
	$.ajax({
		url: '/CustomPage.aspx?CustomPage=iGlamourBlogContent',
		//async: false,
		success: function(data, textStatus, XMLHttpRequest) {
			var menu = $(data).find('#menu-item-126');
			menu.find(' > ul').addClass('TopLevelSubList');
			menu.find(' > ul > li').addClass('SecondLevelItem');
			menu.find(' > ul > li > a').addClass('SecondLevelItemLink');
			$('#CategoryMenu .TopLevelItemBlog').append(menu.find(' > ul'));
		}
	});
}
