/**
 * jQuery-Plugin "language-selection" to replace a language-selection with a list
 * This jQuery plugin was inspired and based on Lightbox 2 by Lokesh Dhakar (http://www.huddletogether.com/projects/lightbox2/)
 * and adapted to me for use like a plugin from jQuery.
 * @name jquery.lang-select.js
 * @author Leiv Bergmann
 * @date March 19, 2009
 * @category jQuery plugin
 * @copyright (c) 2009 Leiv Bergmann
 */
(function($) {
	/**
	 * $ is an alias to jQuery object
	 * @param Array images for the languages
	 */
	$.fn.langReplace = function(LangImg) {
		var ReplaceObj = this; 								//jQuery Object
		var LangShort = []; 								//Abbreviation of language
		var LangLong = [];									//Language
		var LangCount = $(this).children("option").length; 	//Count of languages
		var LangSelectedIndex = 0; 							//Index of selected language
		var LangSelectName = $(this).attr('name');			//Selectname to submit the form
		var arrPageSizes = ___getPageSize();				//Pagesize
		
		/**
		 * Initializing the plugin calling the start function
		 */
		function _initialize() {
			if(LangCount!=0) {
				$(ReplaceObj).children("option").each(function(i) {
					LangShort[i] = $(this).val();
					LangLong[i] = $(this).text();
					if($(this).is(":selected")) LangSelectedIndex = i;
				});
				$(ReplaceObj).replaceWith('<div id="head_lang"><div id="head_lang_selected"></div></div>');
				_start();
			}
		}
		
		/**
		 * Start the jQuery-Plugin "language-selection"
		 */
		function _start() {
			$("#head_lang_selected").append("<div>"+LangLong[LangSelectedIndex]+"</div>");
			$("#head_lang_selected div").css({"background-image":"url(/"+LangImg[LangSelectedIndex]+")","cursor":"pointer"});
			$("#head_lang_selected").click(function() {
				_show_selects();
			});
		}
		
		/**
		 * Show languages with images as a list
		 */
		function _show_selects() {
			$('body').append('<div id="head_lang_overlay"></div>');
			$('#head_lang_overlay').css({width:arrPageSizes[0],height:arrPageSizes[1]});
			$("#head_lang").append("<ul></ul>");
			for(i=0; i<LangCount; i++) {
				$("#head_lang ul").append("<li>"+LangLong[i]+"</li>");
				$("#head_lang li").eq(i).css({"background-image":"url(/"+LangImg[i]+")","cursor":"pointer"});
			}
			$("#head_lang li").eq(LangSelectedIndex).css("background-color","#B2B4BF");
			$("#head_lang li").mouseover(function () {
				$("#head_lang li").css("background-color","#FFF");
				$(this).css("background-color","#B2B4BF");
			});
			$("#head_lang li").click(function() {
				LangClickIndex = 0;
				for(i=0; i<=LangCount; i++) {
					if($(this).text()==LangLong[i]) {
						LangClickIndex = i;	
					}
				}
				$("#head_lang").replaceWith('<input type="hidden" name="'+LangSelectName+'" id="'+LangSelectName+'" value="'+LangShort[$("#head_lang li").index(this)]+'" />');
				$("form:has(#"+LangSelectName+")").submit();
			});
			
			$("#head_lang_selected, #head_lang_overlay").unbind("click").click(function() {
				$("#head_lang ul, #head_lang_overlay").remove();
				$("#head_lang_selected").click(function() {
					_show_selects();
				});
			});
		}
		
		/**
		 * getPageSize() by quirksmode.com
		 *
		 * @return Array Return an array with page width, height and window width, height
		 */
		function ___getPageSize() {
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	
			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight) {
				pageHeight = windowHeight;
			} else { 
				pageHeight = yScroll;
			}
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
			return arrayPageSize;
		};
		
		//Start the replacement
		_initialize();
	}
})(jQuery); // Call and execute the function immediately passing the jQuery object
