function Popup(url, width, height) {
	var w = window.open(url, null, 'width=' + width + ', height=' + height + ', resizable=1');
	w.focus();
}

function PopupAsk(question, url, width, height) {
	if (confirm(question)) {
		Popup(url, width, height);
	}
}

/* Site specific java scripts */

function IsParentOf(test, parent) {
	var result = false;
	while (test) {
		if (test == parent) {
			result = true;
			break;
		}
		test = test.parentNode;
	}
	return result;
}

function ShowCategoryMenu(menuid) {
	var menu = document.getElementById(menuid);
	menu.style.display = "block";
}

function HideCategoryMenu(menuid) {
	var menu = document.getElementById(menuid);
	menu.style.display = "none";
}

function ToggleCategoryMenu(menuid) {
	var menu = document.getElementById(menuid);
	if (menu.style.display == "none") {
		menu.style.display = "block";
	} else {
		menu.style.display = "none";
	}
}

function InitCategoryMenu(menuhdrid, menuid) {
	var menu = document.getElementById(menuid);
	var menuhdr = document.getElementById(menuhdrid);
	
	TimeoutHandler = function () {
		HideCategoryMenu(menuid);
	}
	
	DelayShow = function() {
		ShowCategoryMenu(menuid);
	}
	
	menu.onmouseout = function onmouseout(event) {
		var event = (event) ? event : ((window.event) ? window.event : "");
		var destination = (event.relatedTarget) ? event.relatedTarget : event.toElement;
		if (IsParentOf(destination, menu) == false) {
			HideCategoryMenu(menuid);
			clearTimeout(menuhdr.timeoutid);
			menuhdr.timeoutid = 0;
		}
	}
	menu.onmouseover = function onmouseover(event) {

	}	
	menuhdr.onmouseout = function onmouseout(event) {
		var event = (event) ? event : ((window.event) ? window.event : "");
		var destination = (event.relatedTarget) ? event.relatedTarget : event.toElement;

		if (IsParentOf(destination, menu) == false && IsParentOf(destination, menuhdr) == false) {
			HideCategoryMenu(menuid);
			clearTimeout(menuhdr.timeoutid);
			menuhdr.timeoutid = 0;
		}
	}
	
	menuhdr.onmouseover = function onmouseover(event) {
		if (menuhdr.timeoutid == 0) {
			menuhdr.timeoutid = setTimeout("DelayShow()", 200);
		}
	}
	
	menu.onclick = function onclick(event) {
		var event = (event) ? event : ((window.event) ? window.event : "");
		var target = (event.target) ? event.target : event.srcElement;

		var item = target.firstChild;
		while (item) {
		 	if (item.nodeType == 1) {
				if (item.tagName == 'A') {
					document.location.assign(item.href);
				}
			}
			item = item.nextSibling;
		}

	}
	menuhdr.timeoutid = 0;
	//menu.timeoutid = setTimeout("TimeoutHandler()", 2000);

}

function GetCoords(obj) {
	var coords = Array(0, 0, 0, 0);
	
	coords[2] = obj.offsetWidth;
	coords[3] = obj.offsetHeight;

	do {
		coords[0] += obj.offsetLeft;
		coords[1] += obj.offsetTop;
		obj = obj.offsetParent;
	} while (obj);
	return coords;
}

function GetMousePos(e) {
	var pos = Array(0, 0);

	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		pos[0] = e.pageX;
		pos[1] = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		pos[0] = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		pos[1] = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	return pos;
}

function ishow(img) {
	var PREVIEW_WIDTH = 400;
	var PREVIEW_HEIGHT = 300;
	
	var preview = document.getElementById("image-preview");

	if (window.event) {
		source.event = window.event;
	}
	preview.style.width = PREVIEW_WIDTH + "px";
	preview.style.height = PREVIEW_HEIGHT + "px";
	preview.src = "";
	
	var coords = GetCoords(source.element);

	preview.style.left = coords[0] + (coords[2] / 2) - (PREVIEW_WIDTH / 2) + "px";
	preview.style.top = coords[1] + (coords[3] / 2) - (PREVIEW_HEIGHT / 2) + "px";
	
/*	preview.onmouseover = function(event) {
		setTimeout(function() {preview.style.display = "block"; }, 500);
//		setTimeout(function() {preview.style.opacity = "1"; }, 2000);
	}*/
	
	preview.onmouseout = function(event) {
		preview.style.display = "none";
		preview.style.opacity = "";
	}

	preview.onmousemove=function(event) {
		var pos = GetMousePos(event);

		if (pos[0] < coords[0] || pos[0] > coords[0] + coords[2] || pos[1] < coords[1] || pos[1] > coords[1] + coords[3]) {
			this.style.display = "none";
			this.onmousemove = null;
		}
	}

	preview.onclick = function(event) {
		document.location = preview.src;
	}

	preview.src = img;
	preview.style.display = "block";
	preview.style.cursor = "pointer";
	preview.style.opacity = 1.0;
	
	var i = 0;
	
	/*window.clearInterval(preview.showid);
	preview.showid = window.setInterval(function() {
		i += (3.141593 / 2) / 7;
		if (i > 3.141593 / 2) {
			preview.style.opacity = 1;
			window.clearInterval(preview.showid);
		} else {
			preview.style.opacity = Math.sin(i);
		}
	}, 100);*/
}

function NewTag(product) {
    var content = document.getElementById("tags-" + product).innerHTML;
    var newtag = document.getElementById("new-tag");
    if (newtag == null) {
		content += "<input type='text' id='new-tag' name='new-tag' /> <input type='image' src='images/add.png' onclick='SaveTag(&quot;" + product + "&quot;)' /> <input type='image' src='images/cancel.png' onclick='GetTags(&quot;" + product + "&quot;)' />";
		document.getElementById("tags-" + product).innerHTML = content;
    }
    document.getElementById("new-tag").focus();
}

function SaveTag(product) {
    var tag = document.getElementById("new-tag");
    var opt = {
        method: 'get',
        parameters: {
            'cmd': 'add',
            'pid': product,
            'tag': escape(tag.value)
        },
        onSuccess: function(response) {
            var json = response.responseJSON;
            if (json.Status == 1) {
                var content = "";
                var s = "";

                for (var tag in json.Tags) {
                    content += s + "<a href='" + json.Tags[tag] + "'>" + tag + "</a>";
                    s = ", ";
                }
                content += " <a class='tag-add' href='javascript:NewTag(&quot;" + product + "&quot;)' title='Add new tag'>+</a>";
                var tags = document.getElementById("tags-" + product);
                tags.innerHTML = content;
            }
        }
    }
	
	var Request = new Ajax.Request('/tags.ajax.php', opt);
}

function GetTags(product) {
    var tag = document.getElementById("new-tag");
	var opt = {
		method: 'get',
		parameters: {
			'cmd': 'get',
			'pid': product
		},
		onSuccess: function(transport) {
			var json = transport.responseJSON;
			if (json.Status == 1) {
				var content = "";
				var s = "";

				for(var tag in json.Tags) {
					content += s + "<a href='" + json.Tags[tag] + "'>" + tag + "</a>";
					s = ", ";
				}
				content += " <a class='tag-add' href='javascript:NewTag(&quot;" + product + "&quot;)' title='Add new tag'>+</a>";

				var tags = document.getElementById("tags-" + product);
				tags.innerHTML = content;
			}
		}
	}
	
	var Request = new Ajax.Request('/tags.ajax.php', opt);
}

function AjaxInitializeAnimation(container) {
	var ajax = container.animation;
	if (ajax) {
		AjaxStopAnimation(container);
	}
	var div = new Element('div');
	
	div.absolutize();
	div.clonePosition(container);
	
	div.setStyle({backgroundRepeat: "no-repeat", backgroundPosition: "center", zIndex: 10, backgroundImage: "url(images/ajax/loading-1.png)"});

	div.isAnimationLayer = true;
	div.isAnimationRunning = false;	
	container.animation = div;

	document.documentElement.appendChild(div);
}

function AjaxStartAnimation(container) {
	var ajax = container.animation;
	if (ajax) {
		if (ajax.isAnimationRunning == false) {
			container.setOpacity(0.3);

			ajax.isAnimationRunning = true;
			var index = 1;
			
			ajax.AnimationId = setInterval(function() {
				index++;
				if (index > 8) {
					index = 1;
				}
				var matches = ajax.style.backgroundImage.match(/^url\((.*-)[0-9]+(\..*)\)$/);
				if (matches) {
					ajax.style.backgroundImage = "url(" + matches[1] + index + matches[2] + ")";
				}
			}, 100);
		}
	}
}

function AjaxStopAnimation(container) {
	var ajax = container.animation;
	if (ajax) {
		if (ajax.isAnimationRunning) {
			ajax.isAnimationRunning = false;
			clearInterval(ajax.AnimationId);
			container.setOpacity(1.0);
		}
		container.animation = null;
		document.documentElement.removeChild(ajax);
	}
}

/* Document Loading */

Event.observe(window, "load", function() {
	setTimeout(function() {
		var i = 0;
		var dir = 0;
		setInterval(function() {
			$("header").style.backgroundPosition = i + "px";
			if (dir == 0) {
				i -= 1;
				if (i < -800) {
					dir = 1;
				}
			} else {
				i += 1;
				if (i >= 0) {
					dir = 0;
				}
			}
		}, 50);
	}, 3000);
}
);
