/*
 * 
 * 
 * Generates href for language select box (select box appears if there are more than two languages in site_configuration) 
 * 
 * 
 */

this.changeLanguage = function() {

	$('#select_language').bind("change", function() {
		// get current URI
		var requestUri = location.href;
		// get language option box
		var languageOptions = document.getElementById('select_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();

		window.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");
			});

};

/*
 * @author rur@bauer.de @2010-06-08
 * 
 * IMAGE Galleries with Colorbox
 * 
 * 
 */
this.imageGallery = function() {

	$('a img.imagesingle').colorbox({
		href : function() {
			var url = $(this).parent("a").attr("href");
			return url;
		}
	});

	$('a img.imagegroup').colorbox({
		href : function() {
			var url = $(this).parent("a").attr("href");
			return url;
		},
		rel : "imagegroup",
		current : "Bild {current} von {total}"
	});

};

/*
 * @author rur@bauer.de @2011-03-18
 * 
 * Etracker Event Tracker Search Usage
 * 
 * 
 */
this.etSearch = function() {

	/*
	 * onmousedown="ET_Event.eventStart('Interne%20Suche', 'Suchstring',
	 * 'Suchen', '')"
	 */

	
	$("#topnav form").bind("change submit blur",function(event)
	
		{
		
		var searchString = $('input[name=search]').val();
		searchString = escape(searchString);
		ET_Event.eventStart('Interne%20Suche', searchString, 'Suchen', '');
	
		
		
		});

};


this.cboxajaxload = function(){
	/*
	 * Loading content using jquery's load function into a cbox
	 * Only floatbox div is picked
	 * It's ajax so it only works with documents from within the same domain
	 * 
	 */
	
	
	$('a.cboxcontent').colorbox({
		href : function() {
			var url = $(this).attr("href");
			return url + ' div.floatbox';
		},
		innerWidth: '610px',
		onComplete:function(){
			$("#colorbox .floatbox").css({'background-color': '#eee', 'text-align': 'left'});
		},
		
	});
};


this.cboxiframe = function(){
	/*
	 * Loading iframe into a cbox
	 *
	 *	innerWidth:620, 
	 *	innerHeight:390
	 * 
	 */
	
	
	$('a.cboxiframe').colorbox({
		href : function() {
			var url = $(this).attr("href");
			return url;
		},
		iframe: true,
		innerWidth:620,
		innerHeight:390
	});
};

/*
 * Here the scripts are called when the document is ready
 * 
 * 
 * 
 */

$(document).ready(function() {

	imagePreview();
	imageGallery();
	// privacy();
	printversion();
	changeLanguage();
	etSearch();
	cboxajaxload();
	cboxiframe();
});

