/* SOME CONSTANTS */
var colHLInfo = '#FAD15B';  //Highlight colour for informational messages
var colHLError = '#E3352D'; //Highlight colour for error messages

/* Force removal of depreciated player/history cookies */
delCookie("playerdata");
delCookie("history");

function addLoadEvent(func) {
	$(func);
}

function getXHR() {
	var xmlhttp = false;
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
			    $.log.error(e, 'Unable to create XML HTTP Request');
				xmlhttp = false;
			}
		}
	}
	return xmlhttp;
}

function formData(form) {
	var query = "";
	for (var i=0; i<form.elements.length; i++) {
		query += form.elements[i].name;
		query += "=";
		query += encodeURIComponent(form.elements[i].value);
		query += "&";	
	}
	return query;
}
				
function setTextSize(size) {
	if(size != null) {
		document.body.style.fontSize = size + "%";
		setCookie('textSize',size,364)
		$(window).trigger('resize');
	}
}

function showTab(names) {
	// Shows namegiven tab(s) (e.g. "Basket")
	arrNames = names.split(',');
	var theTab = document.getElementById('tab' + arrNames[0])
	if(theTab != null) {
		var theTabs = theTab.parentNode.getElementsByTagName('li')
		for(i = 0;i < theTabs.length;i++) {
			if(theTabs[i].className != 'tabTitle') {
				theTabs[i].className = '';
			}
		}
				
		var thePanel = document.getElementById('pnl' + arrNames[0])
		var thePanels = theTab.parentNode.parentNode.parentNode.getElementsByTagName('div')
		for(i = 0;i < thePanels.length;i++) {
			if(thePanels[i].className.indexOf('panel') != -1) {
				thePanels[i].style.display = 'none';
			}
		}
		
		theTab.className = 'tabActive';
		theTab.getElementsByTagName('a')[0].blur();
		
		for(i = 0;i < arrNames.length;i++) {
			thePanel = document.getElementById('pnl' + arrNames[i])
			if(thePanel != null) {
				thePanel.style.display = 'block';
			}
		}
	}
	return false;
}

function setFormatGroup(groupID, newPrice) {
	//This is now also used by the widget format selectors on product.aspx

	//reset format selector highlighting
	var fmtAllSelectors = getElementsByClassName('div','formatSelector');
	for(i = 0;i < fmtAllSelectors.length;i++){
		remClassName(fmtAllSelectors[i],'selected');
	}
	
	//check the formatgroup selector
	var fmtRadio = document.getElementById('fmtRadio_' + groupID);
	fmtRadio.checked = true; 
	
	//highlight current formatgroup selector
	var fmtSel = document.getElementById('fmtSel_' + groupID);
	addClassName(fmtSel,'selected');
	
	//hide all formatgroup prices
	var fmtAllGroups = getElementsByClassName('form','fmtGrp');
	for(i = 0;i < fmtAllGroups.length;i++){
		fmtAllGroups[i].style.display = 'none';
	}
	
	//only display prices and formats for the selected group
	var fmtGroups = getElementsByClassName('form','fmt_' + groupID);
	for(i = 0;i < fmtGroups.length;i++){
		fmtGroups[i].style.display = 'inline';
	}
		
	var packShot = document.getElementById('packShot');
	var oldPrice = $(packShot).attr('class').match(/[0-9]+/);
	remClassName(packShot, "p" + oldPrice);
	addClassName(packShot, "p" + newPrice);
}

var showToolTip = function(tooltip, location, left, top, pos, forceShow) {
	$('.toolTip').hide().data('openedBy', null);
	var noCookie = getCookie(tooltip) != 0;
	if(typeof(location) == "object") {
		var location = $(location);
	} else {
		var location = $('#' + location);
	}
	var tooltip = $('#' + tooltip);
	
	// Displays the specified tooltip at the specified object's location (adjusted with left & top)
	if ((forceShow || noCookie) && location.length > 0 && tooltip.length > 0) {
		var leftPos = left + location.offset().left + ((pos.indexOf('L') != -1) ?  -tooltip.outerWidth()  : location.outerWidth());
		var topPos  = top  + location.offset().top  + ((pos.indexOf('B') != -1) ?  location.outerHeight() : -tooltip.outerHeight());
		
		tooltip.css({
			left: leftPos + 'px',
			top:  topPos + 'px'
		}).fadeIn().data('openedBy', location.attr('id'));
		
		// If either the location or the tooltip are out of scroll view, then scroll to them
		if ($('html').scrollTop() > Math.min(location.offset().top, tooltip.offset().top)) {
			$('html').animate({ scrollTop: Math.min(location.offset().top, tooltip.offset().top) - 20});
		} else if ($('html').scrollTop() + $(window).height() < Math.max(location.offset().top + location.outerHeight(), tooltip.offset().top + tooltip.outerHeight())) {
			$('html').animate({ scrollTop: (Math.max(location.offset().top + location.outerHeight(), tooltip.offset().top + tooltip.outerHeight()) - $(window).height()) + 20});
		}
	}	
}

var closeTooltip = function(tooltip) {
	$('#' + tooltip).fadeOut().data('openedBy', null);
	setCookie(tooltip, 0, 365);
}

function setCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function delCookie(name) {
	setCookie(name,"",-1);
}

function getElementsByClassName(strTagName, strClassName){
	return $(strTagName + '.' + strClassName + ', ' + strTagName + ' .' + strClassName).get();
}

function hasClassName(objElement, strClass) {
	return $(objElement).hasClass(strClass);
}

function addClassName(objElement, strClass, blnMayAlreadyExist) {
	$(objElement).addClass(strClass);
}

function remClassName(objElement,strClass) {
	$(objElement).removeClass(strClass);
}


(function() {
	//tips javascript
	var objFocus = 'undefined';
	var bitTip = false;

	$(function() {
		$('.tool').hover(function() {
				if(objFocus == 'undefined' &! bitTip) {
					showTip($(this));
				}
			}, function() {
				if(objFocus == 'undefined' && bitTip) {
					hideTip();
				}
			}).focus(function() {
				objFocus = this;
				if(!bitTip) {
					showTip(this);
				}
			}).blur(function() {
				objFocus = 'undefined';
				if(bitTip) {
					hideTip();
				}
			});
	});

	function showTip(objTool) {
		if (!bitTip) {
			objTool = $(objTool);
			$('#divToolTip')
				.text(objTool.attr('title'))
				.css({
					left: objTool.offset().left + 20 + 'px',
					top: objTool.offset().top - 1 + objTool.outerHeight() + 'px'
				})
				.data('for', objTool)
				.show();
				
			objTool.attr('title', '');
		}
		bitTip = true;
	}

	function hideTip() {
		$('#divToolTip').hide().data('for').attr('title', $('#divToolTip').text());
		bitTip = false;
	}
})();



// Used by CMS pages (so don't show up in a code search)
// Should see if these can be tidied up
function setOpacity(obj,opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	if (typeof(obj) == 'object' && obj.style) {
		if (obj.style.MozOpacity!=null) {  
			obj.style.MozOpacity = (opacity/100) - .001;
		} 
		else if (obj.style.opacity!=null) {
			obj.style.opacity = (opacity/100) - .001;
		} 
		else if (obj.style.filter!=null) {
			obj.style.filter = "alpha(opacity="+opacity+")";
		}
	}
}
	
function getOpacity(obj) {
	var opactiy = 100;
	if (typeof(obj) == 'object' && obj.style) {
		if (obj.style.MozOpacity!=null) {  
			opactiy = parseInt((obj.style.MozOpacity*100) + .001);
		} 
		else if (obj.style.opacity!=null) {
			opactiy = parseInt((obj.style.opacity*100) + .001);
		} 
		else if (obj.style.filter!=null) {
			opactiy = obj.filters.item("alpha").opacity;
		}
	}
	return opactiy;
}

//Placed here to avoid Google Chrome bug
$(document).ready(function() {
	$('.carousel').carousel();
});