


$(document).ready(function(){

    // listen to see if the search within input gains focus and the input value accordingly
    $('#searchWithinKeywords').focus(function () {
        
        document.searchWithinFrm.searchWithinKeywords.value = '';
         
    }); 
    
    // make an array of all page divs
    var pageDivs = $('div');
    
    // set variable to keep count of how many BV divs exist
    var bvDivCount = 0;
    
    // loop over all divs in the array
    for (i=0;i<pageDivs.length;i++){
    
    	// the id of the current array div has an id starting with BV increment the count
    	if( $(pageDivs[i]).attr('id').split('BV').length > 1 ){
    	
    		bvDivCount = bvDivCount + 1;
    		
    		//alert( $(pageDivs[i]).attr('id') );
    	
    	}
    
    }
    
    // if BV divs existed set the neccessary domain
	if( bvDivCount > 0 ){
	
		var bvdocumentDomain = "waterstones.com";
		document.domain = "waterstones.com";
	
	}
         
}); 

// only carry out an actual search within if the text input has characters in it and these are not the prompt
function validateSearchWithin(){

	var searchWithinStringPrompt = 'Enter keywords';
    
    if( (document.searchWithinFrm.searchWithinKeywords.value.length <= 0) || ( $('#searchWithinKeywords').val() == searchWithinStringPrompt ) || ( $('#typeAheadTextInput').val() == searchStringPrompt ) ){
        document.searchWithinFrm.searchWithinKeywords.value = searchWithinStringPrompt;
        return false;
    }
    else{
        return true;
    }
    
}  
    
function createCookie(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 readCookie(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 eraseCookie(name) {
	createCookie(name,"",-1);
}



function clearField(obj) {
	obj.value = '';
}

function hideObject(o) {
  object = document.getElementById(o);
		var str = object.className;
		str = str.replace('hidden','');
		str = str + " hidden";
  object.className = str;
}

function showObject(o) {
  object = document.getElementById(o);
		var str = object.className;
		str = str.replace('hidden','');
  object.className = str;
}

function hideOrShowObject(o) {
  object = document.getElementById(o);
		var str = object.className;
		str = str.replace('hidden','');
  object.className = str;
}

function changeScreenshot(src) {
  object = document.getElementById('uiLargeScreenshot');
		object.src = src;
}

function toggleFaq(o) {
		object = document.getElementById(o);
		if(object.className=='faq-pane') {
			object.className = 'faq-pane active';
		}
		else {
			object.className = 'faq-pane';
		}
}

function verifySelectionMadeAndSubmit(frm, o, unselectedValue) {
	if (o.value != unselectedValue)
		frm.submit();	
}

function verifyRadioSelectionMadeAndSubmit(frm, o) {
	var len = o.length;
	var i = 0;
	while (i < len)
	{
		if(o.item(i).checked)
		{
			frm.submit();	
		}
		i = i+1;
	}	
}

function toggleSet(id,no) {
	objects = document.getElementsByTagName('div')
	for(o in objects) {
		try {
			if(objects[o].id.indexOf(id)>-1)
				if(objects[o].id == id+'-'+no)
					showObject(objects[o].id);
				else
					hideObject(objects[o].id);
		}
		catch(err) { 
			
		}
	}
}
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}


function imageReplace(obj,src) {
	object = document.getElementById(obj);
	object.innerHTML = '<img src="'+src+'" alt="" />';
}

function screenResolution(targetObject,targetClass) { 
	if ( !document.getElementById ) { 
		return false; 
	} 
	if ( !document.getElementById(targetObject) ) { 
		return false; 
	} 
	body = document.getElementById(targetObject); 
	var bodyClass = body.className; var documentWidth = 0; 
	if( typeof( window.innerWidth ) == 'number' ) { 
		documentWidth = window.innerWidth; /* Non-IE */ 
	} else if ( document.documentElement && ( document.documentElement.clientWidth ) ) { 
		documentWidth = document.documentElement.clientWidth; /* IE 6+ in 'standards compliant mode' */ 
	} else if( document.body && ( document.body.clientWidth ) ) { 
		documentWidth = document.body.clientWidth; /* IE 4 compatible */ 
	} if ( documentWidth < "900" ) { 
		bodyClass = bodyClass.replace(' ' + targetClass, '').replace(targetClass, ''); body.className = bodyClass; 
	} else { bodyClass = bodyClass.replace(targetClass, ''); bodyClass = bodyClass + ' ' + targetClass; body.className = bodyClass; 
	} 
}

function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}

function moveElement(elementID,final_x,final_y,interval) {
	var elem = document.getElementById(elementID);
	if (elem.movement) { clearTimeout(elem.movement); }
	if (!elem.style.left) { elem.style.left = "0px"; }
	if (!elem.style.top) { elem.style.top = "0px"; }
	var xpos = parseInt(elem.style.left);
	var ypos = parseInt(elem.style.top);
	if (xpos == final_x && ypos == final_y) { return true; }
	if (xpos < final_x) { var dist = Math.ceil((final_x - xpos)/10); xpos = xpos + dist; }
	if (xpos > final_x) { var dist = Math.ceil((xpos - final_x)/10); xpos = xpos - dist; }
	if (ypos < final_y) { var dist = Math.ceil((final_y - ypos)/10); ypos = ypos + dist; }
	if (ypos > final_y) { var dist = Math.ceil((ypos - final_y)/10); ypos = ypos - dist; }
 	elem.style.left = xpos + "px";
	elem.style.top = ypos + "px";
	var repeat = "moveElement('"+elementID+"',"+final_x+","+final_y+","+interval+")";
	elem.movement = setTimeout(repeat,interval);
}

function prepareOfferSlider(offerslistID,sliderID,productCount) {

	if (!sliderID) { var sliderID = "2"; }

	/* check for productCountID */

	/* make sure the browser understands the DOM methods needed */
    if (!document.getElementById) { return false; }
    if (!document.getElementsByTagName) { return false; }
    if (!document.createElement) { return false; }

	/* make sure the elements needed exist */
	if (!document.getElementById(offerslistID)) { return false; }

	/* asign variables to elements */
	var offerslistID = offerslistID;
	var offerslist = document.getElementById(offerslistID);

	/* setup the slider images division */
	var sliders = document.createElement("div");
	sliders.setAttribute("class","sliders");
	if (document.all) { sliders.setAttribute("className","sliders"); }

	/* setup the left slider anchor and asign it some atributes */
	var slider_left = document.createElement("a");
	slider_left.setAttribute("href","#");
	slider_left.setAttribute("class","slider-left");
	/* check for IE and add atribute className as class does not work */
	if (document.all) { slider_left.setAttribute("className","slider-left"); }

	/* setup the left slider anchor image and asign it some atributes */
	var slider_left_img = document.createElement("img");
	slider_left_img.setAttribute("src","graphics/uk/components/offers_pane/slider_left_"+sliderID+"_grey.gif");
	slider_left_img.setAttribute("alt","scroll left");
	slider_left.appendChild(slider_left_img);
	sliders.appendChild(slider_left);

	/* setup the right slider anchor and asign it some atributes */
	var slider_right = document.createElement("a");
	slider_right.setAttribute("href","#");
	slider_right.setAttribute("class","slider-right");
	/* check for IE and add atribute className as class does not work */
	if (document.all) { slider_right.setAttribute("className","slider-right"); }

	/* setup the right slider anchor image and asign it some atributes */
	var slider_right_img = document.createElement("img");
	slider_right_img.setAttribute("src","graphics/uk/components/offers_pane/slider_right_"+sliderID+".gif");
	slider_right_img.setAttribute("alt","scroll right");
	slider_right.appendChild(slider_right_img);
	sliders.appendChild(slider_right);

	/* insert the slider divsion */
	insertAfter(sliders,offerslist.parentNode);

/*	slider_left.onmouseover = function() { */
	slider_left.onclick = function() {
	
		/* get width of container div */
		var offerparentwidth = offerslist.parentNode.offsetWidth;

		if (offerparentwidth == '410') { var offeritemwidth = 102; }
		if (offerparentwidth == '420') { var offeritemwidth = 106; }

		/* get current x position of offerslist */
		var offerxpos = offerslist.style.left;
		offerxpos = Math.abs(offerxpos.replace('px',''));
		var offerxposcheck = Math.abs(offerxpos);

		var moves = productCount/4;
		var maxmove = (moves*(4*offeritemwidth))-(4*offeritemwidth);
		var tomove = offerxpos - (4*offeritemwidth);
		if (tomove < 1) { tomove = 0; }
		moveElement(offerslistID,-tomove,0,10);

		if ( (offerxposcheck+tomove) <= maxmove) {
			slider_left_img.setAttribute("src","graphics/uk/components/offers_pane/slider_left_"+sliderID+"_grey.gif");
			slider_right_img.setAttribute("src","graphics/uk/components/offers_pane/slider_right_"+sliderID+".gif");
		} else {
			slider_left_img.setAttribute("src","graphics/uk/components/offers_pane/slider_left_"+sliderID+".gif");
			slider_right_img.setAttribute("src","graphics/uk/components/offers_pane/slider_right_"+sliderID+".gif");
		}

		return false;
    }

/*	slider_right.onmouseover = function() { */
	slider_right.onclick = function() {

		/* get width of container div */
		var offerparentwidth = offerslist.parentNode.offsetWidth;

		if (offerparentwidth == '410') { var offeritemwidth = 102; }
		if (offerparentwidth == '420') { var offeritemwidth = 106; }

		/* get current x position of offerslist	*/
		var offerxpos = offerslist.style.left;
		offerxpos = offerxpos.replace('px','');
		var offerxposcheck = Math.abs(offerxpos);

		var moves = productCount/4;
		var maxmove = (moves*(4*offeritemwidth))-(4*offeritemwidth);
		var tomove = Math.abs(offerxpos);

		tomove += (4*offeritemwidth);
		if (tomove > maxmove) { tomove = maxmove; }
		moveElement(offerslistID,-tomove,0,10);

		if ( (offerxposcheck+(4*offeritemwidth)) >= maxmove) {
			slider_left_img.setAttribute("src","graphics/uk/components/offers_pane/slider_left_"+sliderID+".gif");
			slider_right_img.setAttribute("src","graphics/uk/components/offers_pane/slider_right_"+sliderID+"_grey.gif");
		} else {
			slider_left_img.setAttribute("src","graphics/uk/components/offers_pane/slider_left_"+sliderID+".gif");
			slider_right_img.setAttribute("src","graphics/uk/components/offers_pane/slider_right_"+sliderID+".gif");
		}
		
		return false;
    }
 }   
function getWindowWidth() {
	// Default to screen dimensions.
	var winW = window.screen.width;
	// Attempt to retrieve browser window dimensions.
	if (window.innerWidth)
		winW = window.innerWidth;
	else {
		if(document.documentElement.offsetWidth)
			winW = document.documentElement.offsetWidth;
		else if (document.body.offsetWidth)
			winW = document.body.offsetWidth;
	}
	return winW;
}

// jquery based additional functions


$(document).ready(function(){   
    
    // listen to see if mouse triggers category flyout heading
    $('#categoryFlyOut h2').mouseenter(function () {
         $('#categoryFlyOutList').addClass('categoryFlyOutListOver');
    });
    $('#categoryFlyOut h2').mouseleave(function () {
         $('#categoryFlyOutList').removeClass('categoryFlyOutListOver');
    });
    
    // listen to see if mouse triggers category flyout Panel
    $('#categoryFlyOutList').mouseenter(function () {
         $('#categoryFlyOutList').addClass('categoryFlyOutListOver');
    });
    $('#categoryFlyOutList').mouseleave(function () {
         $('#categoryFlyOutList').removeClass('categoryFlyOutListOver');
    });   
    
    
    // listen category flyout link rollovers
    $('#categoryFlyOut li').mouseenter(function () {
         $(this).addClass('selectedCategory');
    });
    $('#categoryFlyOut li').mouseleave(function () {
         $(this).removeClass('selectedCategory');
    });
    
    
    
});
	