/**
 * Custom javascript functions
 * 
 * @package		framework
 * @subpackage	javascript
 * @author		Roland Karle <rk@ateliervision.de>
 * @version 	1.0
 * @revision	$Id: functions.js 229 2007-10-29 16:14:20Z rk $
 * @since		25.10.2007
 */
 

var current_image = "";
var last_image = "";
/**
 * function called on image onmouseover
 */
function image_over(id, image, nr, desc, price)
	{
	if (last_image != id)
		{
		image_hide(last_image);
		}
	current_image = id;
	window.setTimeout("image_show(" + id + ",'" + image + "', '" + nr + "', '" + desc + "', '" + price + "')", 20);
	}
/**
 * shows the image layer
 */
function image_show(id, image, nr, desc, price)
	{
	var ele = new e("image.over");
	if (ele.style("visibility") == "hidden")
		{
		if (browser() == "ie")
			{
			ele.position(mouse.PosX - 200,mouse.PosY+ mouse.ScrolledY  + 10);
			}
		else
			{
			ele.position(mouse.PosX - 200,mouse.PosY + 10);
			}
		ele.Element.innerHTML = "<div style=\"color: black; background: white; width: 400px;\">";
		ele.Element.innerHTML += "<img src=\"" + image + "\" style=\"width: 400px;\" hspace=\"5\"><br />";
		ele.Element.innerHTML += "<div style=\"padding-left: 5px;padding-right: 5px;\"><table border=\"0\" style=\"width: 400px;\"><tr><td class=\"main\" style=\"vertical-align: top; white-space: nowrap;\"></td><td class=\"main\" style=\"text-align: center;\"><b>" + desc + "</b></td><td class=\"main\" style=\"text-align: right; vertical-align: top; white-space: nowrap;\"></td></tr></table></div>";	
		ele.Element.innerHTML += "</div>";
		ele.show();
		}
	last_image 		= id;
	current_image 	= id;
	}
/**
 * function called on image onmouseout
 */
function image_out(id, image)
	{
	current_image = "";
	window.setTimeout("image_hide(" + id + ")", 20);
	}
/**
 * hides the image layer
 */
function image_hide(id, image)
	{
	if (current_image == "" && id != "")
		{
		var ele = new e("image.over");
		ele.hide();
		last_image = "";
		}
	}

/**
 * General javascript functions
 * 
 * @package		framework
 * @subpackage	javascript
 * @author		Roland Karle <rk@ateliervision.de>
 * @version 	1.0
 * @revision	$Id: functions.js 229 2007-10-29 16:14:20Z rk $
 * @since		25.10.2007
 */
 
/**
 * Returns the filename of a path
 * 
 * @return 	string
 */
function basename(v)
	{
	if (isset(v))
		{
		var pos = v.lastIndexOf("\\");
		var r = v.substring(pos+1);
		return r;
		}
	else
		{
		return false;
		}
	}
/**
 * Returns the browser type
 * 
 * ie		-> Internet Explorer 
 * opera	-> Opera
 * safari	-> Safari
 * gecko	-> Gecko based browsers (Firefox, Netscape)
 * 
 * @return 	string
 */
function browser()
	{
	switch (true)
		{
		case (window.attachEvent && !window.opera):
			return "ie";
			break;
		case (window.opera):
			return "opera";
			break;
		case (navigator.userAgent.indexOf('AppleWebKit/') > -1):
			return "safari";
			break;
		case (navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1):
			return "gecko";
			break;
		default:
			return false;
			break;
		}
	}
/**
 * Converts a decimal number to hexadecimal 
 * 
 * @param	int		v
 * @return	string
 */
function dec_hex(v)
	{
	return v.toString(16);
	}
/**
 * Converts a hexadecimal string to a decimal number
 * 
 * @param	string	v
 * @return	int
 */
function hex_dec(v) 
	{
	return parseInt(v,16);
	}
/**
 * Returns if a element is defined or undefined
 * 
 * @param	mixed 	v	value
 * @return 	bool
 */
function isset(v)
	{
	return (typeof v != "undefined" && v != null) ? true : false;
	}
/**
 * Returns if a element is null or not
 * 
 * @param	mixed 	v	value
 * @return 	bool
 */
function is_null(v)
	{
	return (v == null);
	}
/**
 * Opens a popup window
 * 
 * Possible options are:
 * - location		Show the location input field 
 * - menubar		Show menu of the browser
 * - resizable		Popup is resizable by the user
 * - scrollbars		Show scrollbars if needed
 * - status			Show the status bar
 * - toolbar		Show the toolbar
 * 
 * Default options are: "resizable,status,scrollbars"
 * 
 * @param	string	name
 * @param	int		width
 * @param	int		height
 * @param	int		left
 * @param	int		top
 * @param	string	options		
 */
function popup(name, url, width, height, left, top, options)
	{
	if (isset(name) && isset(url))
		{
		var option_string = "width=" + width + ",height=" + height;
		if (isset(left) && left != 0) option_string += ",left=" + left; 
		if (isset(top) && top != 0) option_string += ",top=" + top;
		if (!isset(options)) options = "resizable,status,scrollbars";
		if (options.indexOf("location") != -1) option_string += ",location=yes";
		if (options.indexOf("menubar") != -1) option_string += ",menubar=yes";
		if (options.indexOf("resizable") != -1) option_string += ",resizable=yes";
		if (options.indexOf("scrollbars") != -1) option_string += ",scrollbars=yes";
		if (options.indexOf("status") != -1) option_string += ",status=yes";
		if (options.indexOf("toolbar") != -1) option_string += ",toolbar=yes";
		option_string += ",dependent=yes";
		return window.open(url, name, option_string);
		}
	else
		{
		return false;
		}
	}
function rand(min, max)
	{
	var r = Math.round(Math.random() * (max - min)) + min;
	return r;
	}	
/**
 * Redirects the browser
 * 
 * @param	string	v	url to redirect
 * @return 	void
 */
function redirect(v)
	{
	if (isset(v))
		{
		window.location.href = v;			
		}
	}
/**
 * Strip script of a string
 * 
 * @param	string	v	string
 * @return 	string
 */
function strip_scripts(v)
	{
	return v.replace(/<script[^>]*>([\u0001-\uFFFF]*?)<\/script>/g, "");
	}
/**
 * Strip html tags of a string
 * 
 * @param	string	v	string
 * @return 	string
 */
function strip_tags(v)
	{
	return v.replace(/<[^>]*>/g, "");
	}

/* ---------------------------------------------------------------------------------- */

function control_image_clear(id)
	{
	try
		{
		new e.input(id).value(''); 
		new e(id + ".image").Element.src = "";
		}
	catch (e) {}
	}



function insert_image()
	{
	var src		= new e.input("system.media.url").Value;
	var alt 	= new e.input("system.media.image.alt").Value;
	var align 	= new e.select("system.media.image.align").Value;
	var border	= new e.input("system.media.image.border").Value;
	if (border == "") border = 0;
	var hspace	= new e.input("system.media.image.hspace").Value;
	if (hspace == "") border = 0;
	var vspace	= new e.input("system.media.image.vspace").Value;
	if (vspace == "") border = 0;
	var image 	= new e("system.media.image.preview");
	height		= image.Height;
	width		= image.Width;
	tinyMCE.themes['advanced']._insertImage(src, alt, border, hspace, vspace, width, height, align);			
	}



function show(v)
	{
	if (v)
		{
		v.style.visibility = "visible";
		v.style.display = "block";
		}
	}

function hide(v)
	{
	if (v)
		{
		v.style.visibility = "hidden";
		v.style.display = "none";
		}
	}

function get(id)
	{
	if (document.getElementById(id))
		{
		return document.getElementById(id);
		}
	}