function addClassName(obj, name) {
	if (!obj)
		return;
	if (-1 < obj.className.indexOf(name))
		return;
	if (obj.className.length)
		obj.className += " "+name;
	else
		obj.className = name;
}

function removeClassName(obj, name) {
	if (!obj)
		return;
	obj.className = obj.className.rplace(name, "").trim();
}

String.prototype.trim = function() {
	var str = ""+this;
	while (str.substr(0,1)==" " || str.substr(0,1)=="\n" || str.substr(0,1)=="\r" || str.substr(0,1)=="\t")
		str = str.substr(1);
	while (str.substr(str.length-1,1)==" " || str.substr(str.length-1,1)=="\n" || str.substr(str.length-1,1)=="\r" || str.substr(str.length-1,1)=="\t")
		str = str.substr(0,str.length-1);
	return str;
}

String.prototype.rplace = function(from, to) {
	var res = ""+this;
	var idx = 0;
	if (typeof from=="object") {
		var a;
		for (a in from) {
			while (-1 < (p = res.indexOf(a)))
				res = res.substr(0, p)+from[a]+res.substr(p+a.length);
		}
	} else {
		while (-1 < (p = res.indexOf(from)))
			res = res.substr(0, p)+to+res.substr(p+from.length);
	}
	return res;
}

String.prototype.explode = function(d,cnt) {
	if (!this.length)
		return [ ];
	var x = ""+this;
	var res = [];
	while (-1 < (p = x.indexOf(d))) {
		res.push(x.substr(0, p));
		x = x.substr(p+d.length);
		if (cnt && res.length==cnt-1)
			break;
	}
	res.push(x);
	return res;
}


function showandcenter(obj,rel) {
	obj.style.position = "absolute";
	obj.style.display = "block";

	width = parseInt(obj.clientWidth);
	height = parseInt(obj.clientHeight);
		
	var w, ycorrection;
	var h = document.documentElement.clientHeight;
	if (rel) {
		w = rel.clientWidth;
		ycorrection = rel.clientTop;
		if (!ycorrection)
			ycorrection = rel.offsetTop;
		if (!ycorrection)
			ycorrection = 0;
	} else {
		rel = document.documentElement;
		w = document.documentElement.clientWidth;
		ycorrection = 0;
	}
	obj.style.left = (parseInt(w)/2 - parseInt(width)/2 + parseInt(document.documentElement.scrollLeft))+"px";
	obj.style.top = (parseInt(h)/2 - parseInt(height)/2 + parseInt(document.documentElement.scrollTop) - ycorrection)+"px";
}

var Slideshow = {
	images: [ ],
	index: 0,
	title: "",
	show: function(url) {
		for (var i=0; i<this.images.length; i++) {
			if (this.images[i]==url) {
				this.index = i;
				break;
			}
		}
		if (!$("tools-image")) {
			var b = document.createElement("iframe");
			b.frameBorder = "0";
			b.border = "0";
			b.id = "tools-image-under";

			var bg = document.createElement("div");
			bg.id = "tools-image-bg";

			var a = document.createElement("div");
			a.id = "tools-image";

			a.innerHTML = "<h1 id=\"closeup-title\"></h1><div id=\"tools-image-controls\">"+
				"<a href=\"javascript:void(0)\" onclick=\"Slideshow.close();return false;\"><img src=\"/images/closeup-close.gif\" border=\"0\"/></a></div>";

			$("root").appendChild(b);
			$("root").appendChild(bg);
			$("root").appendChild(a);
		}
		showandcenter($("tools-image"));
		$("tools-image").style.background = "url("+url+") center 45px no-repeat white";
		
		$("tools-image-under").style.left = (parseInt($("tools-image").style.left))+"px";
		$("tools-image-under").style.top = (parseInt($("tools-image").style.top))+"px";
		$("tools-image-under").style.display = "block";

		$("tools-image-bg").style.left = (parseInt($("tools-image").style.left)-10)+"px";
		$("tools-image-bg").style.top = (parseInt($("tools-image").style.top)-9)+"px";
		$("tools-image-bg").style.display = "block";
		
		$("closeup-title").innerHTML = this.title;
		
		var close = function() {
			Slideshow.close();
			document.onmousedown = function() { }
			$("tools-image").onmousedown = function() { }
			document.onkeypress = function() { }
		}

		document.onmousedown = close;
		$("tools-image").onmousedown = close;
		document.onkeypress = function(e) {
			if (!e)
				e = window.event;
			if (e.keyCode==27)
				close();
		}
	},
	next: function() {
		if (this.index<this.images.length-1)	
			this.index ++;
		else
			this.index = 0;
		this.show(this.images[this.index]);
	},
	previous: function() {
		if (this.index>0)
			this.index --;
		else
			this.index = this.images.length-1;
		this.show(this.images[this.index]);
	},
	close: function() {
		$("tools-image-under").style.display = "none";
		$("tools-image-bg").style.display = "none";
		$("tools-image").style.display = "none";
	},
	add: function(img) {
		this.images[this.images.length] = img;
	},
	settitle: function(t) {
		this.title = t;
	}
}

var Hint = {
	show: function(element, title, text) {
		addClassName(element, "hint-container");
		if (!$("hint")) {
			var d = document.createElement("div");
			d.id = "hint";
			element.appendChild(d);
		} else {
			var d = $("hint");
			$("hint").parentNode.removeChild($("hint"));
			element.appendChild(d);
		}
		$("hint").innerHTML = "<p class=\"hinttitle\">"+title+"</p><p class=\"hinttext\">"+text+"</p>";
		addClassName($("hint"), "iefix");
		if (BrowserDetect.browser=="Explorer" && BrowserDetect.version>=8)
			$("hint").style.top = "6px";
		else
			$("hint").style.top = "20px";
		element.onmouseout = function() {
			removeClassName(this, "hint-container");
			removeClassName($("hint"), "iefix");
			this.onmouseout = function() { }
		}
		$("hint").onmouseout = function() {
			removeClassName(element, "hint-container");
			removeClassName($("hint"), "iefix");
			this.onmouseout = function() { }
		}
	}
}

function qsLoadDiameters(params) {
	var widthprofile = params.widthprofile.explode("-");
	var diameter = params.diameter;
	var width = widthprofile[0];
	var profile = widthprofile[1];
	new Ajax.Updater(
		'quicksearch-diameter-container',
		'/home/quicksearchdiameters',
		{ asynchronous: true, evalScripts: false, parameters: { width_id: width, profile_id: profile, diameter_id: diameter } }
	);
}

function qsLoadWidthsProfiles(params) {
	var widthprofile = params.widthprofile.explode("-");
	var diameter = params.diameter;
	var width = widthprofile[0];
	var profile = widthprofile[1];
	new Ajax.Updater(
		'quicksearch-width-profile-container',
		'/home/quicksearchwidthsprofiles',
		{ asynchronous: true, evalScripts: false, parameters: { width_id: width, profile_id: profile, diameter_id: diameter } }
	);
}

function fillselect(sel, values, value) {
	while (sel.options.length>0)
		sel.options[0] = null;
	for (var i=0; i<values.length; i++) {
		var o = new Option();
		o.value = values[i][0];
		o.text = values[i][1];
		if (value==values[i][0])
			o.selected = "selected";
		sel.options[sel.options.length] = o;
	}
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

var request = {
	json: function(url, callback) {
		new Ajax.Request(url, {
			method: 'get',
			onSuccess: function(transport) {
				var result = eval("("+transport.responseText+")");
				callback(result);
			}
		});
	}
}
