/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};




//OS detect :
$(document).ready(osdet);
function osdet(){if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
/*if (OSName == "UNIX"||OSName=="Linux"){$("#nav li a").css({fontSize:"90%"});$(".reviewsButton").css({fontSize:"90%"});}*/
/*if (OSName =="MacOS"){$("body").css({fontFamily:"arial"});$("#nav li a").css({paddingBottom:"9px"});$("#nav li li a").css({paddingBottom:"3px"});$(".pQty").css({left:"-30px"});$("td .pQty").css({left:"-15px"});}*/
}
//Hides elements that would show if No js is present in browser, eg: js driven tool tip fallback
$(function(){$(".nojs").hide();});
//tool tips
//Search Box control
$(document).ready(sbox);function sbox(){ $("#searchBox").focus( function() { if(this.value=='Product Finder'){this.value='';} } );$("#searchBox").blur( function(){if(this.value==''){this.value='Product Finder'}; } );}
// IE6 dropdown Nav
$(document).ready(iehover);function iehover(){if($.browser.msie){$("#nav li").hover(function(){$(this).addClass("hover");},function(){$(this).removeClass("hover");})};}




//Basket row colors
$(function(){if($.browser.msie){$(".mBasketItem").bind("mouseover", function(){$(this).css({background:"4a4a4a"});});$(".mBasketItem").bind("mouseout", function(){$(this).css({background:"none"});}); }})


//A-Z index Trigger
$(function(){$("#azIndex").click(function(){$("#indexWindow").show();$("#indexWindow").css("position", "absolute");$("#indexWindow").css("z-index", "9999");return false;});});
$(function(){$("#closeIndex").click(function(){$("#indexWindow").hide();return false;});});
//Index Hovers for IE 6 - buggy, dont use yet
//$(function(){$("ul.indexList").hover(function(){$(this).children(".indexTitle").addClass("indexHover");},function(){$(this).children(".indexTitle").removeClass("indexHover");});});
//Category dropdowns on pictoral links
$(function(){if($.browser.msie){$("div.catMenuTrigger").hover(function(){$(this).addClass("iehover");}, function(){$(this).removeClass("iehover");})}});

// pictoral nav
$(function(){$(".pictorals ul").css("-moz-border-radius-bottomleft" ,"6px");$(".pictorals ul").css("-moz-border-radius-bottomright" ,"6px");});
function getY( oElement ){var yReturnValue = 0;while( oElement != null ){yReturnValue += oElement.offsetTop;oElement = oElement.offsetParent;}return yReturnValue;}function getX( oElement )
{var xReturnValue = 0;while( oElement != null ){xReturnValue += oElement.offsetLeft;oElement = oElement.offsetParent;}return xReturnValue;}
$(function(){$(".pictoralHover").click(function(){$(".pictorals").fadeOut("fast");var listoget = this.name;var wheret = getY(this); wherel = getX(this);var listtoshow = document.getElementById(listoget+'_dropdowns');listtoshow.style.display='block';listtoshow.style.position='absolute';listtoshow.style.top=wheret+"px";listtoshow.style.left=(wherel-15)+"px";return false;});});
$(function(){$(".pictorals h4").click(function(){$(".pictorals").fadeOut("fast");return false;});});
$(function(){$(window).resize(function(){$(".pictorals").hide();});});

//Show Inc /Ex Vat Prices

var vatToggleVal = null; //global var for holding current vat toggle setting on page
var vatToggleInProgress = false;

function vatToggle()
{
	var value = $.cookie('EBVATPrice');

	if ( value == 0 ) {
		toggleVATPrices('ex')
	}
	else {
		toggleVATPrices('inc');
	}

	$("#vatToggle").click( function() {
		if( vatToggleVal == 1 ) {
			toggleVATPrices('ex');
		}
		else {
			toggleVATPrices('inc');
		}
	});

} //vatToggle

function toggleVATPrices(option)
{
	if(vatToggleInProgress == true) return false; //stop to from being fired again before it finishes this function

	vatToggleInProgress=true;
	vatToggleVal = (option == 'inc' ? 1 : 0);
	$.cookie('EBVATPrice', vatToggleVal, {expires: +365, path: '/', domain: 'ebuyer.com'});

	if(option == "inc")
	{
		$(".ex").hide();
		$(".inc").show();
		$("#vatToggle").html("Show &nbsp;ex vat prices");
	}
	else {
		$(".inc").hide();
		$(".ex").show();
		$("#vatToggle").html("Show inc vat prices");
	}
	$("#vatToggle").show();
	vatToggleInProgress=false;
	return false;
} //toggleVATPrices

$(document).ready(vatToggle);


//Advanced Nav
$(function(){$('.clickable').click(function(){if(this.id==""){var curon=document.getElementById('on'); curon.id="";this.id="on";$(this).next('div').slideToggle('fast').siblings('div.hideable:visible').slideUp('fast');return false;}else{return false;}});});

/*product Options menu
function shoot(x){$("#"+x).fadeIn("fast");}
$(function(){$('.productMenuLinks').click(function(){$(".productMenuLinks").removeClass("currentOption");var target = this.title;$(this).addClass("currentOption");$("div.hideable").fadeOut("fast");setTimeout('shoot(\''+target+'\')',500);return false;})});*/

//striped tables
$(function(){$("#specsTable tr:even").addClass("alt");});
$(function(){$("#specsTable tr").hover(function(){$(this).addClass("trhov");},function(){$(this).removeClass("trhov");});});

//Fake Pseudo hovers for IE 6
$(function(){$("div.whiteBox ol li").hover(function(){$(this).addClass("sbhov");},function(){$(this).removeClass("sbhov");});});
$(function(){$("#leftCol li").hover(function(){$(this).addClass("pshov");},function(){$(this).removeClass("pshov");});});
$(function(){$("#loginOptions li").hover(function(){$(this).addClass("pshov");},function(){$(this).removeClass("pshov");});});

//Fake Pseudo :Before in IE
$(function(){if($.browser.msie){$(".helpTip").prepend("<img src='yellow_tip_pointer.gif' />");};});
$(function(){if($.browser.msie){$(".tips").prepend("<img src='yellow_tiptop_pointer.gif' />");};});

// Compare Form Limiter - product Page
function checkbox(j){var compareform=document.compareItems;var total=0;for(var i=0;i<compareform.ckb.length;i++){if(compareform.ckb[i].checked){total+=1;}if(total>4){alert("Only 4 items can be compared at any one time.");j.checked=false;return false;}}}
$(function(){$('.compareProduct').click(function(){checkbox(this);});});

/*Thumbnail Rollovers - product page
$(function(){$("a.thickbox img").mouseover(function(){var display = document.getElementById("displayImage");var change = this.longDesc;if (change == display.src){return false;}else{display.src =change;}})});*/


//Product listing filter onchange handler
$(function(){$("form#sortBy select").change(function(){this.form.submit();});});
$(function(){$("form#pageSelect select").change(function(){this.form.submit();});});

// Registration Form
$(function(){$("input").focus(function(){$(this).parent().addClass("lovely");});});
$(function(){$("input").blur(function(){$(this).parent().removeClass("lovely");});});

//Logged in message remover
$(function(){$(".closethis").click(function(){ $("#returningCustomer").fadeOut();})});

//Old functions - Memory config popups

function KingstonConfigurator_UK() {
winpops=window.open("http://www.ec.kingston.com/ecom/config/default.asp?referid=672","KingstonConfigurator","scrollbars=yes,width=750,height=650");
}

