﻿$(document).ready(function() {
	$('#TopNavMainMenu').CSSDropDownMenu();
	$('#PageLeftColumn').CSSDropDownMenu();
	updateMenuWidth();
	updateContentHeight();
	updateLeftColCategories();
	updateButtons();
	moveCartButtons();
	setPreviewCartFunctioanlity();
	if($('#FormEntryPage').length > 0)
		styleForm();
	formValidation();
	showRightNav();
	fixQuirks();
	showSiblings();
	updateProductFavouriteImages();
});
function updateMenuWidth() {
	var menuWidth = 0;
	$('#TopNavMainMenu > ul > li').each(function() {
		menuWidth += $(this).width();
	});
	$('#TopNavMainMenu').width(menuWidth + 10);
}
function updateContentHeight() {
	if($('#PageLeftColumn').height() < $('#PageCenterColumn').height())
		$('#PageLeftColumn').height($('#PageCenterColumn').height());
	if($('#PageRightColumn').height() < $('#PageCenterColumn').height())
		$('#PageRightColumn').height($('#PageCenterColumn').height());
	if($('#PageLeftColumn').height() < $('#PageRightColumn').height())
		$('#PageLeftColumn').height($('#PageRightColumn').height());
	if($('#LeftSideNav').height() < $('#PageCenterColumn').height())
		$('#LeftSideNav').height($('#PageCenterColumn').height());
	if($('#rightNavCell').height() < $('#PageCenterColumn').height())
		$('#rightNavCell').height($('#PageCenterColumn').height());
	if($('#rightNavCell').height() < $('#LeftSideNav').height())
		$('#rightNavCell').height($('#LeftSideNav').height());
	if($('#LeftSideNav').height() < $('#rightNavCell').height())
		$('#LeftSideNav').height($('#rightNavCell').height());
}
function fixErrorImages(obj) {
	$(obj).attr('src','/images/TemplateImages/bit.gif');
}
function updateLeftColCategories() {
	var colCount = 1;
	var defaultColItemCount = 3;
	if($('.LeftColSubList').length > 0) {
		$('.LeftColSubList').each(function() {
			if($(this).attr('colItemCount'))
				colItemCount = $(this).attr('colItemCount');
			else
				colItemCount = defaultColItemCount;
			//if($(this).find('.SecondLevelItem').length > colItemCount) {
				colCount = Math.ceil($(this).find('.SecondLevelItem').length / colItemCount);
				//if(colCount > 1) {
					var newHTML = '<table><tr>';
					for(var i = 0; i < colCount; i++) {
						i == 0 ? newHTML += '<td class="Col1"><ul></ul></td>' : i == (colCount - 1) ? newHTML += '<td class="EndCol"><ul></ul></td>' : newHTML += '<td><ul></ul></td>';
					}
					newHTML += '</table></tr>';
					$(this).append(newHTML);
					var tableRow = $(this).find('table:first tr:first');
					$(this).find('.SecondLevelItem').each(function(i) {
						$(tableRow).find('td:eq(' + Math.floor(i/colItemCount) + ') ul:first').append($(this));
						//$(this).find('.SecondLevelItemDetailYes:first span:first').width($(this).find('.SecondLevelItemLink:first').outerWidth());
						$(this).find('.SecondLevelItemDetailYes:first span:first').width('120');
					});
				//}
			//}
		});
	}
}
function moveCartButtons() {
	var pathname = window.location.pathname.toLowerCase();
	if(pathname.indexOf('orders.aspx') > 0) {
		$('#MainBody').prepend($('input[id^="ordergrid_btn"]'));
		$('input[id^="ordergrid_btn"]').css({'margin-right':'2px','margin-top':'-30px','margin-bottom':'2px','text-transform':'lowercase','width':'130px'});
	}
}
function setPreviewCartFunctioanlity() {
	$('#PreviewCartLink').hover(function(){
		$('#CartPreviewContainer').fadeIn('slow');
		$(this).addClass('CartPreviewVisible');
	}, function () {
		$('#CartPreviewContainer').fadeOut('fast');
		$(this).removeClass('CartPreviewVisible');
	});
}
function updateButtons() {
	$('input[type|="submit"]').addClass('button');
}
function styleForm() {
	$(":input[type='text'][name^='CVFormField_'],:input[type='password'][name^='CVFormField_'],textarea[name^='CVFormField_']").wijtextbox();
	$(":input[type='radio'][name^='CVFormField_']").wijradio();
	$(":input[type='checkbox'][name^='CVFormField_']").wijcheckbox();
	$("select[name^='CVFormField_']").wijdropdown();
	$(".button").button();
}
function formValidation() {
	if($('#FormEntryPage').length > 0)
		$.validator.setDefaults({
			submitHandler: function() {
				$('#Form1 #postmode').val('emailform');
				$(":input[type='hidden'][name^='postmode']").val('emailform');
				if($(":input[type='hidden'][name^='postmode']").length > 1)
					$(":input[type='hidden'][name^='postmode']").eq(1).remove();
				$('#Form1').attr('action',"/post.aspx?mailto=" + $("#mailto").val() + "&RedirectTemplate=" + $("#RedirectTemplate").val() + "&Subject=" + $("#Subject").val() + "&EmailTemplate=" + $("#EmailTemplate").val() + "&ReqMailTo=" + $("#ReqMailTo").value);
				form.submit();
			},
			highlight: function(input) {
				$(input).addClass("ui-state-highlight");
			},
			unhighlight: function(input) {
				$(input).removeClass("ui-state-highlight");
			}
		});
	if($('#FormEntryPage').length > 0) {
		$('#Form1').validate();
		//var formAttributes = new Array('required','minlength','maxlength','rangelength','min','max','range','email','url','date','dateISO','number','digits','equalTo');
		var formAttributes = new Array('required','minlength','maxlength','rangelength','email','url','date','dateISO','number','digits','equalTo');
		$(':input[name^="CVFormField_"],select[name^="CVFormField_"]').each(function() {
			for(attr in formAttributes) {
				if($(this).attr(formAttributes[attr])) {
					var attrVal = formAttributes[attr];
					switch(attrVal)
					{
					case 'required':
						$(this).rules('add', {'required': true});
						break;
					case 'minlength':
						$(this).rules('add', {'minlength': $(this).attr(attrVal)});
						break;
					case 'maxlength':
						$(this).rules('add', {'maxlength': $(this).attr(attrVal)});
						break;
					case 'rangelength':
						$(this).rules('add', {'rangelength': $(this).attr(attrVal)});
						break;
					case 'min':
						$(this).rules('add', {'min': $(this).attr(attrVal)});
						break;
					case 'max':
						$(this).rules('add', {'max': $(this).attr(attrVal)});
						break;
					case 'range':
						$(this).rules('add', {'range': $(this).attr(attrVal)});
						break;
					case 'email':
						$(this).rules('add', {'email': true});
						break;
					case 'url':
						$(this).rules('add', {'url': true});
						break;
					case 'date':
						$(this).rules('add', {'date': true});
						break;
					case 'dateISO':
						$(this).rules('add', {'dateISO': true});
						break;
					case 'number':
						$(this).rules('add', {'number': true});
						break;
					case 'digits':
						$(this).rules('add', {'digits': true});
						break;
					case 'equalTo':
						$(this).rules('add', {'equalTo': true});
						break;
					}
				}
				if($(this).attr(formAttributes[attr] + 'message')) {
					var attrVal = formAttributes[attr];
					switch(attrVal)
					{
					case 'required':
						$(this).rules('add', {messages: {'required': jQuery.format($(this).attr(attrVal + 'message'))}});
						break;
					case 'minlength':
						$(this).rules('add', {messages: {'minlength': jQuery.format($(this).attr(attrVal + 'message'))}});
						break;
					case 'maxlength':
						$(this).rules('add', {messages: {'maxlength': jQuery.format($(this).attr(attrVal + 'message'))}});
						break;
					case 'rangelength':
						$(this).rules('add', {messages: {'rangelength': jQuery.format($(this).attr(attrVal + 'message'))}});
						break;
					case 'min':
						$(this).rules('add', {messages: {'min': jQuery.format($(this).attr(attrVal + 'message'))}});
						break;
					case 'max':
						$(this).rules('add', {messages: {'max': jQuery.format($(this).attr(attrVal + 'message'))}});
						break;
					case 'range':
						$(this).rules('add', {messages: {'range': jQuery.format($(this).attr(attrVal + 'message'))}});
						break;
					case 'email':
						$(this).rules('add', {messages: {'email': jQuery.format($(this).attr(attrVal + 'message'))}});
						break;
					case 'url':
						$(this).rules('add', {messages: {'url': jQuery.format($(this).attr(attrVal + 'message'))}});
						break;
					case 'date':
						$(this).rules('add', {messages: {'date': jQuery.format($(this).attr(attrVal + 'message'))}});
						break;
					case 'dateISO':
						$(this).rules('add', {messages: {'dateISO': jQuery.format($(this).attr(attrVal + 'message'))}});
						break;
					case 'number':
						$(this).rules('add', {messages: {'number': jQuery.format($(this).attr(attrVal + 'message'))}});
						break;
					case 'digits':
						$(this).rules('add', {messages: {'digits': jQuery.format($(this).attr(attrVal + 'message'))}});
						break;
					case 'equalTo':
						$(this).rules('add', {messages: {'equalTo': jQuery.format($(this).attr(attrVal + 'message'))}});
						break;
					}
				}
			}
		});
	}
}
function showRightNav() {
	//showRightNavOnPage('CustomPage.aspx');
	//showRightNavOnPage('Home.aspx');
}
function showRightNavOnPage(page) {
	var pathname = window.location.pathname.toLowerCase();
	if(pathname.indexOf(page.toLowerCase()) > 0) {
		$('#rightNavCell').css({'display':'block'});
		$('#PageRightColumn').css({'display':'block'});
	}	
}
function fixQuirks () {
	if(document.compatMode != 'CSS1Compat') {
		$('#MainMenu').css({'margin-top':'0px'});
		//$('head').append('<link rel="stylesheet" href="/documents/css/quirks.css" type="text/css" media="all" />');
	}
}
function showSiblings() {
	if($('#CategoryNavigation a').length > 3)
		$('#SiblingCategoryContainer').css({'display':'block'});
}
function updateProductFavouriteImages() {
	if($('form').attr('action').toLowerCase().indexOf('prodfavourites.aspx') >= 0) {
		$('.normalRow > td > img,.alternateRow > td > img').each(function() {
			var src = $(this).attr('src');
			src = src.replace(/images/gi, "images/ProductThumbNail");
			$(this).attr('src',src);
			$(this).animate({width: 100, height : 100},0).css({'display':'block'});
			//$(this).css({'display':'none'})
		})
	}
}
