/**
 * Wrapper function for wz Tip()
 * @param myTitle Title of the tooltip
 * @param myText Text of the tooltip
 * @param myWidth Width of the tooltip window
 * @param myHeight Height of the tooltip window (0 = automatic)
 * @param sticky True, if sticky
 * @returns {void}
 */
function callToolTip(myTitle, myText, myWidth, myHeight, sticky) {
	var width = 300;
	var height = 300;
	var title = '';

	if(myWidth === undefined) {
		myWidth = width;
	}
	if(myHeight === undefined) {
		myHeight = 0;
	}
	if(sticky === true) {
		Tip(myText, CLOSEBTN, true, STICKY, true, WIDTH, myWidth, HEIGHT, myHeight, TITLE, myTitle, FONTSIZE, '12px', PADDING, 10, TITLEPADDING, 5, SHADOW, true, BORDERWIDTH, 4); 
	}
	else {
		Tip(myText, WIDTH, myWidth, HEIGHT, myHeight, TITLE, myTitle, FONTSIZE, '12px', PADDING, 10, TITLEPADDING, 5, SHADOW, true, BORDERWIDTH, 4); 
	}
}


/**
 * Toggles the div layer
 * @param query ID of element
 * @returns {void}
 */
$.toggle = function(query) {
	$(query).toggle("medium");
};


/**
 * Shows the div layer
 * @param query ID of element
 * @returns {void}
 */
$.show = function(query) {
	$(query).show("slow");
};


/**
 * Helper variable for function windowPopDown() and windowPopUp()
 */
var pop = null;

/**
 * Close the window opened before
 * 
 * @returns {void}
 */
function windowPopDown() {
	if (pop && !pop.closed) {
		pop.close();
	}
}

/**
 * Open a popup window
 * 
 * @param obj Tag where window were opened
 * @param w Width of window
 * @param h Height of window
 * @returns {Boolean} False, if window created, otherwise true (that means, window can get closed)
 */
function windowPopUp(obj, w, h) {
	var url = (obj.getAttribute) ? obj.getAttribute('href') : obj.href;
	if (!url) {
		return true;
	}
	w = (w) ? w += 20 : 150; // 150px*150px is the default size
	h = (h) ? h += 25 : 150;
	var args = 'width=' + w + ',height=' + h
			+ ',resizable=yes,scrollbars=yes,locationbar=no';
	windowPopDown();
	pop = window.open(url, '', args);
	return (pop) ? false : true;
}

/**
 * Set default behavior of popup windows
 */
window.onunload = windowPopDown;
window.onfocus = windowPopDown;



/**
 * Show a div layer
 * 
 * @param name Id of the layer
 * @returns {void}
 */
function showDiv(name) {
	document.getElementById(name).style.visibility = "visible";
	document.getElementById(name).style.display = "block";
}
/**
 * Hide a div layer
 * 
 * @param name Id of the layer
 * @returns {void}
 */
function hideDiv(name) {
	document.getElementById(name).style.visibility = "hidden";
	document.getElementById(name).style.display = "none";
}


/**
 * Setup PrettyPhoto
 */
$(document).ready(function(){
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animation_speed: 'normal', /* fast/slow/normal */
		show_title: true
	});
});

/*
Sets the width and length attribute of an <img> element according to
a maximum value.
*/
function scaleImage(img, lengthLimit) {
// Protect against incompletely loaded images
// with wrong image sizes
/*
if(!img.complete) {
function checkAgain() {
// alert(img.src + " " + img.height + " " + img.width);
scaleImage(img, lengthLimit);
}
window.setTimeout(checkAgain, 100);
return;
}
*/
var h = img.height;
var w = img.width;
if (h && w
&& (w > lengthLimit || h > lengthLimit)) {
var r = 1;
if (w > h) {
r = lengthLimit/w;
} else {
r = lengthLimit/h;
}
img.width = w*r;
img.height = h*r;
img.title = img.alt + " rescaled by r=" + r + " from w=" + w + " h=" + h;
} else {
img.title = img.alt;
}
}
/*
Sets the height attribute of an <img> element according to
a maximum value.
*/
function scaleImageHeight(img, heightLimit) {
var h = img.height;
if (h && h > heightLimit) {
img.width = img.width * (heightLimit/h);
img.height = heightLimit;
img.title = img.alt + " rescaled by r=" + (heightLimit/h) + " from h=" + h;
} else {
img.title = img.alt;
}
}
function scaleThumb(img, lengthLimit) {
// Protect against thumbnails that appear to
// have strange dimensions. This is a heuristic
// approach for lack of anything better.
var h = img.height;
var w = img.width;
if(w > 100 && h < 50) {
img.title = img.alt + " rescaling supressed"
+ " because of unplausible dimensions w=" + w + " h=" + h;
return;
} else {
scaleImage(img, lengthLimit);
}
} 
