var mylib = {
	getElementsByClassName : function(className) {
		var els = new Array();

		try {
			if (parent.document.getElementsByClassName) {
				els = parent.document.getElementsByClassName(className);
			} else {
				var docList = parent.document.all || parent.document.getElementsByTagName('*');

				for (var i=0; i<docList.length; i++) {
					if (docList[i].className == className) {
						els.push(docList[i]);
					}
				}
			}
		} catch (err) {
			// fail silently
			els = new Array();
			return els;
		}

		return els;
	}
};

var basket = {
	fields : Array,

	addListeners : function(className) {
		this.fields = mylib.getElementsByClassName(className);

		for (var i=0; i<this.fields.length; i++) {
			this.fields[i].onchange = basket.submit;
		}
	},

	submit : function() {
		if (parent.document.getElementById('basketForm')) {
			var targetForm = parent.document.getElementById('basketForm');
			targetForm.submit();
		}
	}
};

var searchtool = {
	el : String,
	set : Boolean,

	init : function() {
		this.set = false;

		try {
			this.el = parent.document.getElementById("q");
		} catch (err) {
			// Fail silently
			return false;
		}
		this.set = true;
		return true;
	},

	getel : function() {
		return this.el;
	},

	focused : function() {
		if (this.set && (this.el.size != 27)) {
			this.el.size = 27;
		}
	},

	blurred : function() {
		if (this.set && (this.el.size != 20)) {
			this.el.size = 20;
		}
	}
};

var autoupdate = {
	targetEls : Array,

	set : function() {
		try {
			if (!getCookie('autoupdate')) {
				this.disable();
			} else if (getCookie('autoupdate') == 0) {
				throw new Exception("cookie failed");
			}

			this.targetEls = mylib.getElementsByClassName("auto");

			for ( var i = 0; i < this.targetEls.length; i++) {
				switch (this.targetEls[i].nodeName) {
				case 'INPUT':
				case 'SELECT':
					//this.targetEls[i].setAttribute('onchange', 'submit()');
					this.targetEls[i].onchange = basket.submit;
					break;
				}
			}
		} catch (err) {
			return false;
		}

		return true;
	},

	basket : function () {
		if (parent.document.getElementById("basketForm")) {
			var targetForm = parent.document.getElementById("basketForm");
			targetForm.submit();
		}
	},

	disable : function() {
		try {
			setCookie('autoupdate', 0, 30);
			window.location.reload();
		} catch (err) {
			return false;
		}
	},

	enable : function() {
		try {
			setCookie('autoupdate', 1, 30);
			window.location.reload();
		} catch (err) {
			return false;
		}
	},

	addDisable : function() {
		var d = parent.document;
		var spanEl = d.createElement("span");
		var linkEl = d.createElement("a");

		linkEl.setAttribute('href', "javascript:void autoupdate.disable();");
		linkEl.innerHTML = "Disable autoupdate";
		spanEl.appendChild(linkEl);

		if (d.getElementById("options")) {
			d.getElementById("options").appendChild(spanEl);
		}
	},

	addEnable : function() {
		var d = parent.document;
		var spanEl = d.createElement("span");
		var linkEl = d.createElement("a");

		linkEl.setAttribute('href', "javascript:void autoupdate.enable();");
		linkEl.innerHTML = "Enable autoupdate";
		spanEl.appendChild(linkEl);

		if (d.getElementById("options")) {
			d.getElementById("options").appendChild(spanEl);
		}
	}

};

var banners = {
	alt : Array,
	current : Number,
	el : Object,
	filetype : String,
	items : Array,
	placeholder : String,
	prefix : String,
	runningTime : Number,
	total : Number,
	urls : Array,

	load : function(ph) {
		if (document.images && document.getElementById(ph)) {
			var preloaded = new Image();

			this.timer(0);

			this.current = new Number(0);
			this.placeholder = new String(ph);
			this.filetype = new String(".jpg");
			this.prefix = new String("/lib/img/banners/front/");
			this.items = new Array("mothers-day",
					"makeup-masterclass",
					"eyecare"
			);
			
			this.alt = new Array("Mothers day gifts",
					"Dr.Hauschka Make-up Masterclass with Alexandra Bryne",
					"Naturally effective Eye Care. Tone and firm: FREE eye excercise card with any eye product. Find out more."
			);
			
			this.urls = new Array("/products/gifts/mothers-day",
			"/skincare/events",
					"/products/promotions"
			);
			
			for (i = 0; i < this.items.length; i++) {
				this.items[i] = this.prefix
						.concat(this.items[i], this.filetype);
				preloaded.src = this.items[i];
			}

			this.total = this.items.length;

			return true;
		}

		return false;
	},

	run : function() {
		this.el = document.getElementById(this.placeholder);

		if (this.current == this.total) {
			this.current = 0;
		}

		this.el.setAttribute("src", this.items[this.current]);
		this.el.setAttribute("alt", this.alt[this.current]);
		this.el.setAttribute("height", "276");

		this.el.parentNode.setAttribute("href", this.urls[this.current]);

		this.blendimage(1000);
		this.current++;

		setTimeout("banners.run()", 8000);

	},

	blendimage : function(millisec) {
		var speed = Math.round(millisec / 100);
		var timer = 0;

		this.el.style.backgroundImage = "url(" + this.el.src + ")";
		this.opacity(0);
		this.el.src = this.items[this.current];

		for (i = 0; i <= 100; i++) {
			setTimeout("banners.opacity(" + i + ")", (timer * speed));
			timer++;
		}
	},

	opacity : function(level) {
		var styleObj = this.el.style;

		styleObj.opacity = (level / 100);
		styleObj.MozOpacity = (level / 100);
		styleObj.KhtmlOpacity = (level / 100);
		styleObj.filter = "alpha(opacity=" + level + ")";
	},

	timer : function(t) {
		this.runningTime = t;
		t += 1;

		setTimeout("banners.timer(" + t + ")", 1000);
	}
};

/**
 * Strip specified elements from the DOM
 */
var stripper = {
	target : Array,

	go : function(sel, selType) {
		this.target = new Array();

		switch (selType) {
			case 'class':
				if (self.document.getElementsByClassName(sel)) {
					this.target = self.document.getElementsByClassName(sel);
				}
				break;
			case 'id':
				if (self.document.getElementById(sel)) {
					this.target[0] = self.document.getElementById(sel);
				}
				break;
			case 'tag':
				if (self.document.getElementsByTagName(sel)) {
					this.target = self.document.getElementsByTagName(sel);
				}
				break;
			default:
				return false;
		}

		/** @todo Expand function to enable more functionality */
		try {
			for (var i=0; i<this.target.length; i++) {
				var p = this.target[i].parentNode;

				for (var j=0; j<p.childNodes.length; j++) {
					if (p.childNodes[j].nodeName == this.target[i].nodeName) {
						p.removeChild(p.childNodes[j]);
					}
				}
			}
		} catch (err) {
			return false;
		}

		return true;
	}
};
