// JavaScript Document



function showuserrating(iRating, iProduct){
	if((iRating==0) && (document.getElementById("ratingstate_" + iProduct).innerHTML != "")){
				iRating = parseInt(document.getElementById("ratingstate_" + iProduct).innerHTML);
		}
		for(var i=1;i<=iRating;i++){
			document.getElementById("prodstar_" + iProduct + "_" + i).src = "http://www.curanovum.se/images/star1.png";
		}
		for(var i=iRating+1;i<=10;i++){
			document.getElementById("prodstar_" + iProduct + "_" + i).src = "http://www.curanovum.se/images/star0.png";
		}
	}



function clickuserrating(iRating, iProduct){
	 document.getElementById("ratingstate_" + iProduct).innerHTML = iRating; 
	 document.getElementById("betyg_" + iProduct).value = iRating; 
} 

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}




var MOUSE_OFFSET_X = 40;
var OVERFLOW_OFFSET_Y = -40;
var SHADOW_OFFSET_X = -25;
var SHADOW_OFFSET_Y = -25;

var tooltipCacheKeys = new Array();
var tooltipCacheValues = new Array();

function getCacheIndex(key) {
	var index = -1;

	for (i = 0; i < tooltipCacheKeys.length && index < 0; i++) {
		if (tooltipCacheKeys[i] == key) {
			index = i;
		}
	}
	
	return index;
}

function getCachedContent(key) {
	var index = getCacheIndex(key);
	
	if (index >= 0) {
		return tooltipCacheValues[index];
	} else {
		return null;
	}
}

function cacheContent(key, value) {
	var index = getCacheIndex(key);
	if (index < 0) {
		index = tooltipCacheKeys.length;
		tooltipCacheKeys[index] = key;
	}
	tooltipCacheValues[index] = value;
}

function TT(event, obj, action, args, offsetX, offsetY) {
	if (obj != tooltip.trigger) {
		tooltip.create(obj, action, args);
		if (offsetX != undefined) {
			tooltip.offsetX = offsetX;
			tooltip.offsetY = offsetY;
		}
	}
	
	if (tooltip.status == 0) {
		tooltip.startShowing();
	} else {
		tooltip.stopTimer();
	}
	
	return true;
}

var tooltip = {
	trigger:null, 
	args:null,
	timer:-1,
	status:0,
	layer:null,
	layerStyle:null,
	action:null,
	offsetX:0,
	offsetY:0,
	ajax:null,
	cacheKey:null,
	
	initialize:function() {
		this.layer = getObject("tooltip");
		this.layerStyle = getObjectStyle(this.layer);
		try {
			this.ajax = new JSONRpcClient("/RPC");
		} catch(e) {
		}
	},
	
	create:function(trigger, action, args) {
		if (!this.layer) {
			this.initialize();
		}
		this.hide();

		this.trigger = trigger;
		this.action = action;
		this.args = args.split(",");
		this.timer = -1;

		this.cacheKey = action + "_" + args;

		this.trigger.onmousemove = updateMouseCoordinates;
		this.trigger.onmouseout = function() { tooltip.stopTimer(); return true; };
	},
	
	show:function() {
		var x = 0;
		var y = 0;
		if (this.trigger.tagName == "A") {
			x = (mouseX + MOUSE_OFFSET_X);
			y = mouseY;
			/*
			this.layer.onmouseover = stopTimer();
			this.layer.onmouseout = startHiding();
			*/
		} else {	
			//x = (getOffsetX(this.trigger) + this.trigger.width + (SHADOW_OFFSET_X * -1));
			if (this.offsetX >= 0) {
				x = (getOffsetX(this.trigger) + this.trigger.width + this.offsetX);
			} else {
				x = (getOffsetX(this.trigger) - stripUnits(this.layer.offsetWidth) + this.offsetX);
			}
			y = getOffsetY(this.trigger) + this.offsetY;
			
			this.layer.onmouseout = function() { return true };
			this.layer.onmouseover = function() { return true };
		}
	
		var layerHeight = stripUnits(this.layer.offsetHeight);
		var layerBottom = y + layerHeight;
		var wH = getWindowHeight();
	
		if (layerBottom > wH + document.body.scrollTop) {
			y = wH - layerHeight - OVERFLOW_OFFSET_Y;
			if (document.body.scrollTop > 0) {
				y += document.body.scrollTop;
			}
		}
		
		//x += SHADOW_OFFSET_X;
		//y += SHADOW_OFFSET_Y;
	
		if (y < document.body.scrollTop) {
			y = document.body.scrollTop;
		}
	
		this.layerStyle.left = x;
		this.layerStyle.top = y;
								
		var titleObject = getObject("tooltipTitle");
		var bodyObject = getObject("tooltipContent");

		var content = getCachedContent(this.cacheKey);		
		if (content == null) {
			var content = eval("this.ajax." + this.action + ".execute(this.args)");
			cacheContent(this.cacheKey, content);
		}

		titleObject.innerHTML = "<p>" + content.title + "</p>";
		bodyObject.innerHTML = content.body;
		
		this.trigger.onmouseout = function() { tooltip.startHiding(); return true; };
	
		window.setTimeout("tooltip.layerStyle.visibility = 'visible';", 10);
		this.status = 1;
	},

	hide:function() {
		this.layerStyle.visibility = "hidden";
		this.status = 0;
	},

	startShowing:function() {
		this.timer = window.setTimeout("tooltip.show();", 10);
	},

	startHiding:function() {
		this.timer = window.setTimeout("tooltip.hide();", 10);
	},
	
	stopTimer:function() {
		if (this.timer >= 0) {
			window.clearTimeout(this.timer);
			this.timer = -1;
		}
	}
};


function getObject(objId) {
	var obj = null;
	if (document.all) obj = document.all[objId];
	else if (document.getElementById) obj = document.getElementById(objId);
	else if (document.layers) obj = document.layers[objId];
	return obj;
}

function objectExists(objId) {
	return (getObject(objId) != null);
}

function getObjectStyle(objId) {
	var obj = (typeof objId == "string") ? getObject(objId) : objId;
	var styleObj = null;
	if (document.all) styleObj = obj.style;
	else if (document.getElementById) styleObj = obj.style;
	else if (document.layers) styleObj = obj;
	return styleObj;
}

function isLayerHidden(objId) {
	var hidden = false;
	var objStyle = getObjectStyle(objId);
	if (document.all || document.getElementById) {
		hidden = (objStyle.visibility == 'hidden');
	} else {
		hidden = (objStyle.visibility == 'hide');
	}
	return hidden;
}

function showLayer(objId) {
	var objStyle = getObjectStyle(objId);
	if (objStyle) {
		if (document.all || document.getElementById) {
			objStyle.visibility = 'visible';
		} else {
			objStyle.visibility = 'show';
		}
	}
}
  
function hideLayer(objId) {
	var objStyle = getObjectStyle(objId);
	if (objStyle) {
		if (document.all || document.getElementById) {
			objStyle.visibility = 'hidden';
		} else {
			objStyle.visibility = 'hide';
		}
	}
}

function toggleLayer(objId) {
	if (isLayerHidden(objId)) {
		showLayer(objId);
	} else {
		hideLayer(objId);
	}
}

function displayInline(objId) {
	var style = getObjectStyle(objId);
	style.display = "inline";
}

function displayNone(objId) {
	var style = getObjectStyle(objId);
	style.display = "none";
}

function stripUnits(valueWithUnits) {
	return parseInt(valueWithUnits);
}

function getOffsetX(obj){
	var x = 0;
	if (obj.offsetLeft != null) {
		x += obj.offsetLeft;
		while (obj.offsetParent) {
			x += obj.offsetParent.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	return x;
}

function getOffsetY(obj){
	var y = 0;
	if (obj.offsetTop != null) {
		y += obj.offsetTop;
		while (obj.offsetParent) {
			y += obj.offsetParent.offsetTop;
			obj = obj.offsetParent;
		}
	}
	return y;
}

function getMouseCoordinates(e){
	var _x = 0;
	var _y = 0;

	if (!e) {
		e = window.event;
	}

	if (e.pageX || e.pageY) {
		_x = e.pageX;
		_y = e.pageY;
	} else if (e.clientX || e.clientY) {
		_x = (e.clientX + document.body.scrollLeft);
		_y = (e.clientY + document.body.scrollTop);
	}

	return {x:_x,y:_y};
}

function getWindowHeight() {
	var h = 0;

	if (typeof (window.innerWidth) == "number") {
		h = window.innerHeight;
	} else {
		if (document.documentElement && document.documentElement.clientHeight) {
			h = document.documentElement.clientHeight;
		} else if (document.body && document.body.clientHeight) {
			h = document.body.clientHeight;
		}
	}

	return h;
}

var mouseX = 0;
var mouseY = 0;
function updateMouseCoordinates(event) {
	var coords = getMouseCoordinates(event);
	mouseX = coords.x;
	mouseY = coords.y;
	
	return true;
}
