function setCookie(c_name, value, expiredays) {
	try {
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + expiredays);
		parent.document.cookie = c_name
				+ "="
				+ escape(value)
				+ ((expiredays == null) ? "" : ";expires="
						+ exdate.toGMTString());
	} catch (err) {
		return false;
	}
	return true;
}

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

var zoomOptions = {
    zoomWidth: 285,
    zoomHeight: 399,
    xOffset: 10,
    yOffset: 0,
    position: "right",
    lens: false,
    fadeoutSpeed: 'fast',
    showPreload: true,
    preloadText: 'Loading photo',
    title: false
};

var swatches = {
    colour : String,
    cutout : Object,
    list : Object,
    items : Array,

    buttons : function(selector) {
        if (self.document.getElementById(selector)) {
            this.list = self.document.getElementById(selector);
            this.items = this.list.getElementsByTagName("img");

            for ( var i = 0; i < this.items.length; i++) {
                this.colour = this.items[i].getAttribute('id');
                this.items[i].parentNode.removeAttribute("href");

                if (this.items[i].addEventListener) {
                    this.items[i].addEventListener('mouseover',
                            function(event) {
                                swatches.changeColour(event);
                            }, false);
                } else if (this.items[i].attachEvent) {
                    this.items[i].attachEvent('onmouseover', function(event) {
                        swatches.changeColour(event);
                    });
                }
            }
        }
    },

    changeColour : function(e) {
        if (!e)
            var e = window.event;
        var relTarg = e.currentTarget || e.srcElement;
        this.colour = relTarg.id;

        if (self.document.getElementById("cutout")) {
            this.cutout = self.document.getElementById("cutout");
            this.cutout.setAttribute('src',
                    '/lib/img/products/cutouts/' + this.colour + '.jpg');
            this.cutout.parentNode.setAttribute('href',
                    '/lib/img/products/cutouts/large/' + this.colour + '.jpg');
        }
    }
};


try {
    $('document').ready(function() {
        if ($('#banners .rotating').size() > 1) {
            // Trigger banner rotation /lib/packages/
            var rotatorBase = '/lib/packages/rotator';
            try {
                $.getScript(rotatorBase + "/js/custom.wt-rotator.js",
                    function () {
                        $.fn.wtBannerInit(rotatorBase);
                    }
                );
            } catch (err) {
                // Fail silently
            }                    
        }

        $("button, input:submit").button();
        $('.noscript, noscript').remove();

        if (window.location.href.match(/products/)) {
            $('.zoom').jqzoom(zoomOptions);
            $('#lifestyle-thumbnails a').attr('href', window.location.href+'#');
            $('#lifestyle-thumbnails img').click(function() {
                $('#lifestyle-full').attr('src', $(this).attr('src'));
                $('#lifestyle-full').parent().attr('href', $(this).attr('src').replace('products/', 'products/large/'));
            });
            swatches.buttons('swatches');
        }
        
        autoupdate.set();		 
		getCookie('autoupdate') ? autoupdate.addDisable() : autoupdate.addEnable();
    });
} catch (e) {
    // Fail silently
}

