$(document).ready(initialise);

function initialise(){
	$('body').addClass('js');
	initProductPreview();
	initSearch();
	initSharing();
	initForms();
}

function initForms(){
	var njs = $('#no-js');
	if(njs.length > 0){
		njs.hide();
	}
}

function initProductPreview(){
	var preview = $('#product-preview');
	$('#side-nav li a').mouseenter(function (){
		preview.find('img').remove();
		var imgSrc = $(this).attr('data-thumb');
		if(imgSrc){
			var newImage = new Image();
			newImage.onload = function (){
				var top = (150 - newImage.height)/2;
				var left = (200 - newImage.width)/2;
				preview.append(newImage);
				preview.children('img').css('top', top + 'px');
				preview.children('img').css('left', left + 'px');
			};
			newImage.src = imgSrc;
			var topPos = ($(this).offset().top - (preview.height() / 2)) + 'px';
			var leftPos = ($(this).width() + 40) + 'px'; 
			preview.css({top: topPos, left: leftPos});
			preview.show();
		}
	});
	
	$('#side-nav li a').mouseout(function (){
		preview.hide();
	});
		
}

function initSearch(){
	var search = $('#txtSearch');
	var defaultVal = 'Enter search terms here';
	var editCol = '#000';
	search.val(defaultVal);
	search.addClass('default');
	search.focus(function (){
		$(this).removeClass('default');
		if(this.value == defaultVal){
			this.value = '';
		} else {
			//select all current text
			this.select();
		}
	});
	
	search.blur(function (){
		if(this.value.trim() == ''){
			this.value = defaultVal;
			$(this).addClass('default');
		}
	});
}

function initSharing(){
	stLight.options({
		publisher:'9ca081ad-ae23-48cf-8538-b96791348236'
	});
}
