// html5shiv MIT @rem remysharp.com/html5-enabling-script
// iepp v1.6.2 MIT @jon_neal iecss.com/print-protector
/*@cc_on(function(m,c){var z="abbr|article|aside|audio|canvas|details|figcaption|figure|footer|header|hgroup|mark|meter|nav|output|progress|section|summary|time|video";function n(d){for(var a=-1;++a<o;)d.createElement(i[a])}function p(d,a){for(var e=-1,b=d.length,j,q=[];++e<b;){j=d[e];if((a=j.media||a)!="screen")q.push(p(j.imports,a),j.cssText)}return q.join("")}var g=c.createElement("div");g.innerHTML="<z>i</z>";if(g.childNodes.length!==1){var i=z.split("|"),o=i.length,s=RegExp("(^|\\s)("+z+")",
"gi"),t=RegExp("<(/*)("+z+")","gi"),u=RegExp("(^|[^\\n]*?\\s)("+z+")([^\\n]*)({[\\n\\w\\W]*?})","gi"),r=c.createDocumentFragment(),k=c.documentElement;g=k.firstChild;var h=c.createElement("body"),l=c.createElement("style"),f;n(c);n(r);g.insertBefore(l,
g.firstChild);l.media="print";m.attachEvent("onbeforeprint",function(){var d=-1,a=p(c.styleSheets,"all"),e=[],b;for(f=f||c.body;(b=u.exec(a))!=null;)e.push((b[1]+b[2]+b[3]).replace(s,"$1.iepp_$2")+b[4]);for(l.styleSheet.cssText=e.join("\n");++d<o;){a=c.getElementsByTagName(i[d]);e=a.length;for(b=-1;++b<e;)if(a[b].className.indexOf("iepp_")<0)a[b].className+=" iepp_"+i[d]}r.appendChild(f);k.appendChild(h);h.className=f.className;h.innerHTML=f.innerHTML.replace(t,"<$1font")});m.attachEvent("onafterprint",
function(){h.innerHTML="";k.removeChild(h);k.appendChild(f);l.styleSheet.cssText=""})}})(this,document);@*/

function log(value){
	if(typeof console == 'undefined'){
		return false;
	}
	console.log(value)	
}


function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString())+
	";path=/";	
}

function getCookie(c_name){
	
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return false;
}


function setUpSlides(objectToCycle){
	// set up the pointer 
	$(objectToCycle).find('.slide').each(function(){
		if($(this).find('a').length > 0){
			$(this).css('cursor', 'pointer');
		}
		else {
			$(this).css('cursor', 'default');
		}
	});
	// set up the background images
	$(objectToCycle).find('.slide > img').each(function(){
		//$(this).load(function(){
			var bg = 'url(' + $(this).attr('src') + ') top left no-repeat';		
			$(this).parent('.slide').css('background', bg);					
			if($(this).closest('.slide').find('p').length == 1){
				$(this).closest('.slide').find('p').css('margin-top', '50px');
			}
			
			$(this).remove();		
		//});
	});	
}


function tidyUpProductSpecTable(){
	var header = '<tr class="headings"><th>Type</th><th colspan="2">Detail</th></tr>';
	cols = $('.productSpec > table tr th').length;
	// determine the type of imported table
	if($('.productSpec > table td.mainclass').length > 0){
		var header = '<tr class="headings"><th>Type</th><th colspan="2">Detail</th></tr>';
		$('.productSpec > table').addClass('tableCmn01');
		// find the mainclass rows and remove them to get rid of the hard return
		$('.productSpec > table td.mainclass').parent('tr').remove();	
		$('.productSpec > table td h3:first').css('margin-top', '0');	
			
		var count =1;
		
		$('.productSpec table td h3').each(function(){
			
			$('td').each(function(){
				if($(this).attr('rowspan') > 1){
					$(this).attr('rowspan', '');
				}
			});
			if($(this).closest('tr').next().find('ul').length > 0){				
				optionList = $(this).closest('tr').next().detach().find('ul');
				optionHeading = $(this).closest('tr').detach();
				optionHeading = '<h2 class="optionHeading"><span class="bgHeading">' + (optionHeading.unwrap().text()) + '</span></h2>';			
				$('.productSpec').last().after('<div class="section indented productSpec" id="optionList' + count + '"></div>');	
				$('#optionList' + count).before(optionHeading);
				$('#optionList' + count).append(optionList);
				count++;				
			}
			else {				
				$(this).parent('td').attr('colspan', '3').closest('tr').after(header);			
			}		
		});	
	}
	else {
		/*
		if($('.productSpec table table').length > 0){		
			
			$('.productSpec h3').not(':first').css('margin-top', '20px');				
			$('.productSpec table table td').css('padding', '3px');
			$('.productSpec table table table table').each(function(){
				log('found a nested table');
				var tableContent = new Array();
				$(this).find('td').each(function(){
					tableContent.push($(this).html());					
				});
				parentContainer = $(this).parent('td');
				$(this).remove();
				jQuery.each(tableContent, function(index, val) {
					$(parentContainer).css('border', '1px solid #f00').html(val).after('<br />');
				});
								
			});			
		}
		*/
	}	
}


function setUniformHeights(identifier) {	
	
	if($(identifier).length > 0){
		var heights = [];
		if($(identifier).length > 0){
			$(identifier).each( function(){			
				heights.push($(this).outerHeight(true));			
			});	
			heights.sort(function(a,b){return b - a});	
			$(identifier).each( function(){		
				$(this).height(heights[0]);
				
			});	
			return heights[0];
		}		
	}
}

function setHomepageContentHeights(){
	
	maxLeftHeight = setUniformHeights('#indexTabMenu .tabBox');
	
	// if the height of the right side box is bigger than 240 pixels and 
	// is bigger than the height of the biggest left hand element plus the tabnav above it 		
	if($('#secondaryPromo').outerHeight(true) > 240 && $('#secondaryPromo').outerHeight(true) > (maxLeftHeight +  $('.tabBoxNav li').outerHeight(true)) ){	
		
		$('#indexTabMenu .tabBox').each( function(){
			$(this).height($('#secondaryPromo').outerHeight(true) - $('.tabBoxNav li').outerHeight(true) - 12);			
		});
	}
	// if a left side content height plus the tabnav above it
	// is bigger than the secondaryPromo box.
	else{			
		$('#secondaryPromo').height(maxLeftHeight  + $('.tabBoxNav li').outerHeight(true));
		$('#secondaryPromo .wrap').height(maxLeftHeight + $('.tabBoxNav li').outerHeight(true) - 20); // minus the 10px of top margin that the wrap has
	
	}
}	

function setComparatorCookie(){
	var listValues = new Array();
	var items = 0;
	$('#comparator .productList li:not(.placeholder)').each(function(){
		listValues[items] = new Array();		
		listValues[items][0] = $(this).attr('id').split('comp')[0];
		listValues[items][1] = $(this).find('img').attr('src');
		listValues[items][2] = $(this).find('h6').html();
		items++;
	});
	values = listValues.join("|~|");
	setCookie("comparator_products",values, 2);
}

function getComparisonUrl(useCurrentPath){
	
	if(useCurrentPath){
		var compareUrl = window.location['pathname'] + '?';
	}
	else {
		var compareUrl = $('#comparatorContainer .bigButton a').attr('href') + '?';
	}
	var items = 0;
	$('#comparator .productList li:not(.placeholder)').each(function(){
		items++
		compareUrl += 'product' + items + '=' + ($(this).attr('id').split('comp')[0]) + '&';
	});
	compareUrl = compareUrl.substring(0, compareUrl.length - 1);
	return compareUrl;

}

function bindComparatorButtons(){

	$('.remove').unbind('click').bind( 'click', function (event){	
		event.preventDefault();
		removeComparatorProduct($(this).parents('li').attr('id'));
		updateCountContainer();
		setComparatorCookie();	
		cleanUpComparatorPage();
		if($('table.comparison').length > 0){
			
			window.location = getComparisonUrl(true);
		}			
	});
	
	$('#comparatorContainer .bigButton').unbind('click').bind( 'click', function (event){	
		event.preventDefault();
		window.location = getComparisonUrl();
	});
}

function cleanUpComparatorPage(){
	maxProducts = 3;		
	placeholders = $('#comparator .productList li.placeholder').length ? $('#comparator .productList li.placeholder').length : 0;
	products = $('#comparator .productList li:not(.placeholder)').length ? $('#comparator .productList li:not(.placeholder)').length : 0;
		
	if(products == 0){				
		toggleComparatorContainer(true);
	}	
	
	if(products < maxProducts){
		
		$('#comparator .productList li.placeholder ').each(function(){
			$(this).remove();
		});
		$('.compareButton.disabled').each(function(){		
			$(this).removeClass('disabled').find('a span').text(dictionary.add_to_compare);
		});	
		
		for(x = products+1; x <= maxProducts; x++){		
			$('#comparator .productList').append('<li class="placeholder"><span>' + (x) + '</span></li>');
		}
		
		
	}		
	
	else if(products == maxProducts){
		$('.compareButton').each(function(){
			
			if(!$(this).hasClass('added')){
				$(this).addClass('disabled').find('a span').text(dictionary.compare_list_full);
			}
		});	
	}
	
	
}


function addComparatorProduct(productId, title, thumbnail){
	if(productId.length > 0){
		var compareListId = productId + 'comp';
		if($('#' + compareListId).length == 0){
			// if the comparatorContainer is minimised
			if(!$('#comparatorContainer').hasClass('expanded')){					
				toggleComparatorContainer();					
			}			
			$('#' + productId + ' .compareButton').addClass('added').find('a span').text(dictionary.remove_from_compare);	
			$('#comparator .productList').find('li.placeholder').remove().end().append('<li id="' + compareListId + '" class="hidden"><img src="' + thumbnail + '" /><h6>' + title + '</h6><p class="button remove">' + dictionary.remove + '</p></li>');	
			$('#' + compareListId).removeClass("hidden").hide().fadeIn();	
		}	
	}
}	



function removeComparatorProduct(compareListId){
	if($('#' + compareListId).length > 0){	
		$('#' + compareListId).fadeOut(50, function(){	
			var productId = compareListId.split('comp')[0];			
			$('#' + productId + ' .compareButton').removeClass('added').find('a span').text(dictionary.add_to_compare);		
		}).remove();		
	}		
}


function removeAllComparatorProducts(){
	
	if(	$('#comparator .productList').children().length > 0){
		$('#comparator .productList li:not(.placeholder)').each(function(){
			removeComparatorProduct($(this).attr('id'))
		});		
	}
	
	$('.disabled').each(function(){		
		$(this).removeClass('disabled').find('a span').text(dictionary.add_to_compare);
	});
	cleanUpComparatorPage();
	toggleComparatorContainer(true);
	
}


function hideComparatorProducts(){
	
	$('#comparator .productList').fadeOut(100, function(){
		$(this).addClass('hidden');
	});
}


function showComparatorProducts(){	
	if($('#comparator .productList').length > 0){	
		$('#comparator .productList').fadeIn('fast', function(){
			$(this).removeClass('hidden');
		});		
	}
}

function updateCountContainer(){
	$('#comparator .productList li:not(.placeholder)')
	if($('#comparator .productList li:not(.placeholder)').length == 0){
		$('#countContainer').text(dictionary.comparator_no_items);
	}
	else if ($('#comparator .productList li:not(.placeholder)').length == 1){
		$('#countContainer').text(dictionary.comparator_one_item);
	}
	else {
		$('#countContainer').text(dictionary.message_intro + ' ' + $('#comparator .productList li:not(.placeholder)').length + ' ' + dictionary.comparator_some_items);
	}	
}

function toggleComparatorContainer(close){	
	// are we closing it?
	if(close){	
		hideComparatorProducts();
		var anim = {height: 0};
		$('#comparatorContainer').animate( anim, 300, function(){
			$(this).removeClass('expanded').removeClass('collapsed');
		});
		return;
	}
	$('#emptier').fadeOut(100);
	$('#toggler').fadeOut(100);
	
	// then I guess we're collapsing it
	if($('#comparatorContainer').hasClass('expanded')){	
				
		hideComparatorProducts();
			
		var anim = {height: 50};				
		$('#comparatorContainer').animate( anim, 300, function(){
			$(this).removeClass('expanded').addClass('collapsed');				
			updateCountContainer();
			$('#countContainer').fadeIn(100, function(){
				$('#toggler').text(dictionary.expand).fadeIn(100);
			}).removeClass('hidden');					
			
		});
		
			
		
	}	
	// No? Then we must be expanding it
	else {		
		$('#countContainer').hide().addClass('hidden');
		$('#comparatorContainer').animate( {height: 230}, 300, function(){					
			$('#toggler').text(dictionary.collapse).fadeIn('fast');
			$('#emptier').fadeIn('fast');				
			showComparatorProducts();
			$('#comparatorContainer').addClass('expanded').removeClass('collapsed');
		});	
		
	}
}


function shareToLinkedIn(title, link, referrer) {
	if(!title && !link && !referrer){
		title = encodeURIComponent($('h1').text());
		link = encodeURIComponent(window.location['href']);
		referrer = encodeURIComponent(document.title);
	}
	var targetUrl = "http://www.linkedin.com/shareArticle?mini=true&url=" + link + "&title=" + title + "&source=" + referrer;	
	window.open(targetUrl, 'ShareWindow', 'width=570,height=400');
	return false;
}

function shareToTwitter(title, link){
	if(!title && !link){
		title = encodeURIComponent($('h1').text());
		link = encodeURIComponent(window.location['href']);
	}
	var targetUrl = "http://twitter.com/home?status=" + title + " " + link;
	window.open(targetUrl, 'ShareWindow');
	return false;
}

function shareToFacebook(title, link){
	if(!title && !link){
		title = encodeURIComponent($('h1').text());
		link = encodeURIComponent(window.location['href']);
	}
	
	
	var targetUrl = "http://www.facebook.com/sharer.php?u=" + encodeURIComponent(link) + "&t=" + encodeURIComponent(title);
	window.open(targetUrl, 'ShareWindow', 'width=570,height=400');
	return false;
}

function shareToDelicious(title, link) {
	if(!title && !link){
		title = encodeURIComponent($('h1').text());
		link = encodeURIComponent(window.location['href']);
	}
	var targetUrl = "http://www.delicious.com/save?v=5&noui&jump=close&url=" + encodeURIComponent(link) +"&title=" + encodeURIComponent(title);
	window.open(targetUrl, 'ShareWindow', 'width=550,height=550');
	return false;
}
