var message = '';
var isMac = navigator.userAgent.indexOf('Mac') != -1;
var ie6 = false;
var ie7 = false;
if (jQuery.browser.msie) {
	if ( jQuery.browser.version.substr( 0,1 ) == '6' ) {
		ie6 = true;
	}
	if ( jQuery.browser.version.substr( 0,1 ) == '7' ) {
		ie7 = true;
	}
}

var cufonSelectors = '.bannerImageLinks h2, .bannerImageLinks .bodyText';

Cufon.replace( cufonSelectors, { fontFamily: 'Helvetica-Light' } );

function decorateCollection(sel, rowSize) {
	if (rowSize == null) rowSize = 2;
	$(sel + ':nth-child(' + rowSize + 'n-' + (rowSize - 1) + ')').addClass('first');
	$(sel + ':nth-child(' + rowSize + 'n)').addClass('last');
}

function eqHeights(sel) {
	var hightest = 0;
	
	$(sel).each(function(i, el) {
		if ($(el).height() > hightest) hightest = $(el).height();			
	});
	
	$(sel).height(hightest);
}

function eqHeightsFirstLastPair(selFirsts, lookFor) {
	$(selFirsts).each(function(i, element) {
		var element1 = $(element).find(lookFor);
		var element2 = $(element).next('.last').find(lookFor);
		if ($(element2).length > 0) {
			if ($(element1).height() > $(element2).height()) {
				$(element1).height($(element1).height());
				$(element2).height($(element1).height());
			} else {
				$(element1).height($(element2).height());
				$(element2).height($(element2).height());
			}
		}
	});
}

function syncBg() {
	/* hmmm... appears redundant
	var cH = $('#container').outerHeight(true);
	var wH = $(window).height();
		
	if (wH > cH) {
		nH = wH;
	} else {
		nH = cH;
	}
		
	$('#outer').height(nH);
	*/
}

function testLoginFieldVals() {
	if ($('#ctl00_ctl00_ctl00_Content_MainColumn_UmbracoLoginView_Login1_Login1_Password').val().length > 0) {
		$('#ctl00_ctl00_ctl00_Content_MainColumn_UmbracoLoginView_Login1_Login1_Password').parent().addClass('hide-indicator');
	}
}

function dropdownBasket(){
	$('.productDetailPage .dropBtn').click(function(){
        $(this).children().toggleClass('open');
		$(this).next().slideToggle('fast');
	});	
}

function toolTip(){
	$('.skuListItem .price').hover(function(){
		$(this).children().addClass('alive');
	},function(){
		$(this).children().removeClass('alive');
	});
}

$(window).resize(function() {
	syncBg();
});

$(window).load(function() {
	syncBg();
});

$(document).ready(function() {
	if (isMac && jQuery.browser.mozilla) {
		$('body').addClass('ffMac');
	}
	if (jQuery.browser.safari) {
		$('body').addClass('safari');
	}
	
	if (ie6) {
		$('.moduleOverlay').each(function(i, el) {
			$(el).height($(el).parent().height());
		});
	}
	//forms
	$('.form li:has(.mandatory)').addClass('mandatory-container');
	$('.mandatory-container').each(function(i, el) {
		var elContainer = $(this);
		if ( $(elContainer).find('input, textarea, select').val() != '' ) {
			$(elContainer).addClass('hide-indicator');
		}
		if (!ie6) {
			$(elContainer).find('input, textarea, select').focus(function() {
				$(elContainer).addClass('hide-indicator');
			}).blur(function() {
				if ($(this).val() == '') {
					$(elContainer).removeClass('hide-indicator');
				}
			});
		}
	});
	
	$('.form').each(function(i, el) {
		$(el).append('<span class="corner topLeft"></span><span class="corner topRight"></span><span class="corner bottomLeft"></span><span class="corner bottomRight"></span>');
		$(el).pngFix();
	});
	
	$('.inputTextGroup .inputText').each(function() {
		$(this).find('.error').wrapAll('<span class="validators"></span>');
	});
	
	//special case - login form
	$('#ctl00_ctl00_ctl00_Content_MainColumn_UmbracoLoginView_Login1_Login1_UserName').change(function() {
		testLoginFieldVals();	
	});

	$('#ctl00_ctl00_ctl00_Content_MainColumn_UmbracoLoginView_Login1_Login1_UserName').keyup(function() {	
		testLoginFieldVals();
	});	
	
	//secondary nav
	$('#secondaryNavigation li.level3').has('li.level4').addClass('has-children');
	
	//homepage
	decorateCollection('.modularContent.homePagePods > div', 3);
	decorateCollection('.modularContent.mainContentBlocks > div', 2);
	
	//dashboard
	decorateCollection('.dashboardWidgets > div', 2);
	if (!ie7) { eqHeightsFirstLastPair('.dashboardWidgets > div.first', '.inner'); }
	eqHeightsFirstLastPair('.dashboardPod.first', '.inner');
	
	if ($('#footerContent').length > 0) $('#footer').css('padding-bottom', '5px');
	
	$('#footerContent h3').click(function() {
		$(this).parent().find('.bodyText').slideToggle('normal', function(){
			syncBg();
		});
	});
	
	//buy now buttons
	$('.buyNowLinks ul').each(function(i, el) {
		$(this).find('li:first').addClass('first');
		$(this).find('li:last').addClass('last');
	});
	
	//forms 
	$('.form')
	
	//accordions
	$('.FAQsList, .categorisedPostList').accordion({ 
		header: 'h2',
		autoHeight: false,
		collapsible: true,
		navigation: true
	});

	//success stories modules
	decorateCollection('.StoryPostPageList ul > li', 2);
	eqHeightsFirstLastPair('.StoryPostPageList ul > li', '.inner');
	
	//pick and mix pages
	decorateCollection('.pickAndMixSKUList .skuListItem', 2);
	eqHeightsFirstLastPair('.pickAndMixSKUList .skuListItem', '.inner');

	// Add height to div - IE6 corners fix
	var bannerHeight = $('.bannerImage img').eq(0).height();
	$('.bannerImage .bannerImageOverlay').css('height', bannerHeight);
	
	var beforeImgHeight = $('.beforeImage img').eq(0).height();
	$('.beforeImage').css('height', beforeImgHeight);
	
	// eCommerce 
	dropdownBasket();
	toolTip();
	$('.skuListItem:nth-child(odd)').addClass('odd');
	$('.skuListItem:nth-child(even)').addClass('even');
	
	/****************  please leave this to last i.e. no more code below this line!! *******************/
	message += 'Browser: \n';
	jQuery.each(jQuery.browser, function(i, val) {
		message += i + ': ' + val + '\n';
    });
	// alert(message);
	
});

