/*
 * 
 * 
 * Generates href for language select box (select box appears if there are more than two languages in site_configuration) 
 * 
 * 
 */

function changeLanguage() {
	// get current URI
	var requestUri = location.href;
	// get language option box
	var languageOptions = document.getElementById('language');
	// get current language
	var currentLanguage;
	for ( var i = 0; i < languageOptions.options.length; i++) {
		if (languageOptions.options[i].defaultSelected == true) {
			currentLanguage = languageOptions.options[i].value;
		}
	}
	currentLanguage = "/" + currentLanguage + "/";
	// get new language
	var newLanguage = "/" + languageOptions.value + "/";
	// change language
	var newUri = requestUri.replace(currentLanguage, newLanguage);
	location.href = newUri;
}

/*
 * @author rur@bauer.de @2009-09-19
 * 
 * Print Version Window Binds a click listener to an id "printversion" Displays
 * a new window 
 * 
 */
this.printversion = function() {

	$('#printversion').bind(
			"click",
			function(event) {

				event.preventDefault();

				var a = window.location.href + "?print=1";

				var printwindow = window.open(a, "Print",
						"scrollbars=1, resizable=1, width=600, height=500");

				printwindow.print();

			});

};

/*
 * @author rur@bauer.de @2009-08-31
 * 
 * PRIVACY WINDOW FOR FORMS Binds a click listener to an id "privacy_window"
 * Displays a new window loading privacy.html - File must be present in system
 * folder
 * 
 */
this.privacy = function() {

	$('#privacy_window').bind(
			"click",
			function(event) {

				event.preventDefault();

				var a = "http://" + window.location.host
						+ "/system/galleries/htmlgalleries/privacy.html";

				window.open(a, "Privacy",
						"scrollbars=1, resizable=1, width=450, height=650");
			});

};

/*
 * @author rur@bauer.de @2009-08-31
 * 
 * IMAGE PREVIEW SCRIPT
 * 
 * 
 */
this.imagePreview = function() {
	/* CONFIG */

	xOffset = 200;
	yOffset = 10;

	// these 2 variable determine popup's distance from the cursor
	// you might want to adjust to get the right result
	// only for preview_center - preview class has fixed offset

	/* END CONFIG */

	$("img.preview").hover(function(e) {

		this.t = $(this).attr("alt");
		this.title = "";

		// var bildquelle = $(this).attr("src").split("?");

			var online = $(this).attr("src").match(/\?/);

			// UNterscheidung ob Bild exportiert oder nicht.

			if (online) {
				var bildquelle = $(this).attr("src").split("?");

			} else {
				var bildquelle = $(this).attr("src").split(".jpg_");
				bildquelle[0] = bildquelle[0] + ".jpg";
			}

			var c = (this.t != "" && this.t != null) ? "<br/>" + this.t : " ";
			$("body").append(
					"<p id='preview'><img src='" + bildquelle[0] + "' alt='"
							+ c + "' />" + c + "</p>");
			$("#preview").css("top", (e.pageY - 10) + "px").css("left",
					(e.pageX + 10) + "px").fadeIn("fast");

		}, function() {
			this.title = this.t;
			$("#preview").remove();

		});

	$("img.preview_center").hover(function(e) {

		this.t = $(this).attr("alt");
		this.title = "";

		// var bildquelle = $(this).attr("src").split("?");

			var online = $(this).attr("src").match(/\?/);

			// UNterscheidung ob Bild exportiert oder nicht.

			if (online) {
				var bildquelle = $(this).attr("src").split("?");

			} else {
				var bildquelle = $(this).attr("src").split(".jpg_");
				bildquelle[0] = bildquelle[0] + ".jpg";
			}

			var c = (this.t != "" && this.t != null) ? "<br/>" + this.t : " ";
			$("body").append(
					"<p id='preview'><img src='" + bildquelle[0] + "' alt='"
							+ c + "' />" + c + "</p>");
			$("#preview").css("top", (e.pageY - xOffset) + "px").css("left",
					(e.pageX + yOffset) + "px").fadeIn("fast");

		}, function() {
			this.title = this.t;
			$("#preview").remove();

		});

	$("img.preview").mousemove(
			function(e) {
				$("#preview").css("top", (e.pageY - 10) + "px").css("left",
						(e.pageX + 10) + "px");
			});

	$("img.preview_center").mousemove(
			function(e) {
				$("#preview").css("top", (e.pageY - xOffset) + "px").css(
						"left", (e.pageX + yOffset) + "px");
			});

};

/*
 * Here the scripts are called when the document is ready
 * 
 * 
 * 
 */

$(document).ready(function(){



	imagePreview();
	privacy();
	printversion();
});
