/**
 *
 * @author Ardeleanu Ionut
 * @langversion JAVASCRIPT
 *
 * http://www.neokinetics.ro
 * iardeleanu@neokinetics.ro
 *
 */
 /* Browsers tested: IE6+, Firefox 3+, Opera 8+, Chrome, Safari 4 for windows*/
  
var JSInterface =  function(){
	
	var objects_arr = new Array();
	
	
	return{	
		
		localpath: '',				    		//domain path
		
		thickbox_ids: new Array(),      		//array with identifiers for each thickbox opened
		thickbox_id: 0,				   		 	//current thickbox identifier
		
		checkThickBoxHeight: null,      		//a function that gets the maximum available height space for the thickbox
									    		//if it contains more hidden divs	
		MultiLanguage: new MultiLanguage(),   	//object that loads multiple languages in csv format
		
		
		/*****************************************************************************************/
		/*                                      INIT INTERFACE                                   */
		/*****************************************************************************************/
		/**
		 * initialize the JSInterface
		 * method type: LOCAL
		 * params: none
		 */
		init: function(){
			
			//when document is finish loaded, initialize the interface objects (UI_register, UI_users, UI_comments, etc)
			$(document).ready(function(){
				
				JSInterface.MultiLanguage.init(JSInterface.localpath+"includes/csv/Language.csv");
				
			});	
		},
		
		
		
		/*****************************************************************************************/
		/*                                      INIT INTERFACE OBJECTS                           */
		/*****************************************************************************************/
		/**
		 * initialize the JSInterface objects
		 * method type: LOCAL
		 * params: none
		 */
		initObjects: function(){
			for (var i=0; i<objects_arr.length; i++){
				objects_arr[i].init();	
			}
		},
		
		
		/*****************************************************************************************/
		/*                              GENERATE THICKBOX IDENTIFIER                             */
		/*****************************************************************************************/
		/**
		 * generate an unique identifier to the thickbox, and verify if the thickbox have a previous one
		 * method type: LOCAL
		 * params: @id : iframe id.  Ex: TB_iframeContent_98456
		 */
		generateThickboxID: function(id){
			var arr = id.split("_");
			if (!(isNaN(arr[arr.length-1]))){
				var id = Number(arr[arr.length-1]);	
			}
			else{
				var id = Math.floor(Math.random() * 99999);
			}
			return id;
		},
		
		
		/*****************************************************************************************/
		/*                                      INIT THICKBOX                                    */
		/*****************************************************************************************/
		/**
		 * initialize the thickbox according with his content width and height
		 * method type: LOCAL
		 * params: @type : thickbox type. Ex: iframe
		 *         @screenHeight : rezolution height of the browser in pixels
		 */
		initThickbox: function(type, screenHeight){
			if (screenHeight == null){
				screenHeight = 650;	
			}
			
			if (type == "iframe"){
				
				//if we must verify the thickbox content
				if (typeof this.checkThickBoxHeight == "function"){
					var maxHeight = this.checkThickBoxHeight();	
				}
				else{
					var maxHeight = 0;	
				}
				
				
				var table = $('table',$('#TB_iframeContent').get(0).contentWindow.document).get(0);
				var initH = $('#TB_iframeContent').height();
					
				var divContainer = $('#container', $('#TB_iframeContent').get(0).contentWindow.document).get(0);
				
				
				if (divContainer == null || divContainer == "undefined") return;
				
				if (divContainer.offsetHeight > maxHeight){
					var defaultDivHeight = divContainer.offsetHeight;
					var dif = 0;
				}
				else{
					var defaultDivHeight = maxHeight;
					var dif = maxHeight - divContainer.offsetHeight;
				}
				
				//alert("screenHeight:"+screenHeight+" \nmaxHeight:"+maxHeight+" \ndivContainer:"+divContainer.offsetHeight+" \ndefaultDivHeight:"+defaultDivHeight+ " \ndif:"+dif);
				//alert("screenHeight:"+screenHeight+" \ndif:"+dif+ " \ntableHeight:"+table.clientHeight+ " \ndivContainer:"+divContainer.style.height);
				if (table.offsetHeight + dif > screenHeight){
					var top = 0;
					var div = divContainer;
					while (div.offsetParent){
						top += div.offsetTop;
						div = div.offsetParent;
					}
					divContainer.style.height = (screenHeight-top)+"px";
				}
				else{
					divContainer.style.height = defaultDivHeight+"px";
				}
				//alert("screenHeight:"+screenHeight+" \ndif:"+dif+ " \ntableHeight:"+table.offsetHeight+ " \ndivContainer:"+divContainer.style.height);
				
				
				$('#TB_window').height(table.offsetHeight);
				$('#TB_iframeContent').height(table.offsetHeight);
				$('#TB_window').width(table.offsetWidth);
				$('#TB_iframeContent').width(table.offsetWidth);
				//$('#TB_window').animate({marginTop: 0-$('#TB_window').height()/2},1,function(){$('#TB_window').css("visibility","visible");});
			}
			
			this.renameThickbox();
			
		},
		
		
		/*****************************************************************************************/
		/*                                      SET THICKBOX IDENTIFIER                          */
		/*****************************************************************************************/
		/**
		 * add to #TB_window and #TB_iframeContent of the thickbox an identifier
		 * method type: LOCAL
		 * params: @id : random number
		 */
		setThickboxID: function(id){
			
			if (jQuery.inArray(id, this.thickbox_ids) == -1){
				this.thickbox_ids.push(id);
			}
			else{
				this.setDefaultThickbox(jQuery.inArray(id, this.thickbox_ids));
			}
			
			this.thickbox_id = id;
		},
		
		
		/*****************************************************************************************/
		/*                                  SET DEFAULT THICKBOX                                 */
		/*****************************************************************************************/
		/**
		 * if the user refreshes the content of the thickbox (frame format), this function will automatically rename 
		 * the elements that compose the thickbox to default names, to prevent duplication
		 * method type: LOCAL
		 * params: @index :the index in the array of thickbox_ids
		 */
		setDefaultThickbox: function(index){
			var tb_id = this.thickbox_ids[index];
				
			var hideSelect = $('*[id="TB_HideSelect"]').filter(function(){return $(this).data("tb_id") == tb_id;})
			hideSelect.addClass("TB_HideSelect");
			
			var overlay = $('*[id="TB_overlay"]').filter(function(){return $(this).data("tb_id") == tb_id;})
			overlay.addClass("TB_overlay");
			
			var tbwindow = $('*[id="TB_window"]').filter(function(){return $(this).data("tb_id") == tb_id;})
			tbwindow.addClass("TB_window");
			
			var iframeContent = $('*[id="TB_iframeContent"]').filter(function(){return $(this).data("tb_id") == tb_id;})
			iframeContent.addClass("TB_iframeContent");
			
		},
		
		
		/*****************************************************************************************/
		/*                                      RENAME THICKBOX ELEMENTS                         */
		/*****************************************************************************************/
		/**
		 * rename #TB_window and #TB_iframeContent with new names including the thickbox identifier
		 * method type: LOCAL
		 * params: none
		 */
		renameThickbox: function(){
			
			//rename all elements of the thickbox
			var no_hideSelect = $('*[id="TB_HideSelect"]').get().length;
			var iframe = $('*[id="TB_HideSelect"]:eq('+(no_hideSelect-1)+')');
			iframe.data("tb_id",this.thickbox_id);
			iframe.addClass('TB_HideSelect');
			
			
			var no_overlay = $('*[id="TB_overlay"]').get().length;
			var div_overlay = $('*[id="TB_overlay"]:eq('+(no_overlay-1)+')');
			div_overlay.data("tb_id",this.thickbox_id);
			div_overlay.addClass('TB_overlay');
			
			
			var no_window = $('*[id="TB_window"]').get().length;
			var div_window = $('*[id="TB_window"]:eq('+(no_window-1)+')');
			div_window.data("tb_id",this.thickbox_id);
			div_window.addClass('TB_overlay');
			
			var top = 0-div_window.height()/2;
			var left = 0-div_window.width()/2;
			
			//fix for IE 6
			if (jQuery.browser.msie && Math.floor($.browser.version) <= 6) {
				div_window.css("visibility","visible");
			}
			else{
				div_window.animate({marginTop: top, marginLeft: left},1,function(){div_window.css("visibility","visible");});
			}
			
			
			var no_iframeContent = $('*[id="TB_iframeContent"]').get().length;
			var iframeContent = $('*[id="TB_iframeContent"]:eq('+(no_iframeContent-1)+')');
			iframeContent.addClass('TB_iframeContent');		
			
			for (var i=0; i<this.thickbox_ids.length; i++){
				var tb_id = this.thickbox_ids[i];
				
				var hideSelect = $('*[id="TB_HideSelect"]').filter(function(){return $(this).data("tb_id") == tb_id;})
				hideSelect.css("z-index",99-(6*(this.thickbox_ids.length-i)));
				
				var overlay = $('*[id="TB_overlay"]').filter(function(){return $(this).data("tb_id") == tb_id;})
				overlay.css("z-index",100-(6*(this.thickbox_ids.length-i)));
				
				var tbwindow = $('*[id="TB_window"]').filter(function(){return $(this).data("tb_id") == tb_id;})
				tbwindow.css("z-index",102-(6*(this.thickbox_ids.length-i)));
				
			}
			
			
		},
		
		
		/*****************************************************************************************/
		/*                                      REMOVE THICKBOX IDENTIFIER                       */
		/*****************************************************************************************/
		/**
		 * remove #TB_window and #TB_iframeContent identifier
		 * method type: LOCAL
		 * params: none
		 */
		removeThickboxID: function(){
			
			//remove current thickbox id from the array of ids
			var pos = jQuery.inArray(this.thickbox_id, this.thickbox_ids);
			for (var i=0; i<this.thickbox_ids.length; i++){
				
				var id = this.thickbox_ids.shift();
				
				if (i != pos){
					this.thickbox_ids.push(id);	
				}
			}
			
			//move thickboxes elements back to their z-index
			for (var i=0; i<this.thickbox_ids.length; i++){
				var tb_id = this.thickbox_ids[i];
				
				var hideSelect = $('*[id="TB_HideSelect"]').filter(function(){return $(this).data("tb_id") == tb_id;})
				hideSelect.css("z-index",99-(6*(this.thickbox_ids.length-i)));
				
				var overlay = $('*[id="TB_overlay"]').filter(function(){return $(this).data("tb_id") == tb_id;})
				overlay.css("z-index",100-(6*(this.thickbox_ids.length-i)));
				
				var tbwindow = $('*[id="TB_window"]').filter(function(){return $(this).data("tb_id") == tb_id;})
				tbwindow.css("z-index",102-(6*(this.thickbox_ids.length-i)));
				
			}
			
		},
		
		
		/*****************************************************************************************/
		/*                                      ADD TO THICKBOX                                  */
		/*****************************************************************************************/
		/**
		 * add to thickbox system new objects created with AJAX
		 * method type: LOCAL
		 * params: @domChunk : pass where to apply thickbox. Ex: 'a.thickbox, area.thickbox, input.thickbox'
		 */
		addToThickbox: function(domChunk){
			tb_init(domChunk);
		},
		
		
		/*****************************************************************************************/
		/*                                  OPEN THICKBOX FROM FLASH                             */
		/*****************************************************************************************/
		/**
		 * open a thickbox from a flash swf
		 * method type: LOCAL
		 * params: @url : the url from the thickbox
		 */
		openThickboxFromFlash: function(url){
			if (url.indexOf("?") == -1){
				tb_url = url + "?KeepThis=true&TB_iframe=true&height=640&width=200&modal=true";
			}
			else{
				tb_url = url + "&KeepThis=true&TB_iframe=true&height=640&width=200&modal=true";	
			}
			//alert(tb_url);
			$("#openFromFlash").remove();
			$(document.body).append("<a id='openFromFlash' class='thickbox' href="+tb_url+" title=''></a>")
			this.addToThickbox('a.thickbox');
			$('#openFromFlash',document.body).trigger("click");
			
		},
		
		/*****************************************************************************************/
		/*                                      CLOSE THICKBOX                                   */
		/*****************************************************************************************/
		/**
		 * close the thickbox
		 * method type: LOCAL
		 * params: none
		 */
		closeThickbox: function(refresh){
			this.checkThickBoxHeight = null;
			
			tb_remove();
			
			if (jQuery.browser.msie){
	
				if ($("input:text") && $("input:text") != null) $("input:text").get(0).focus();
			}
			
			//setTimeout(function(){window.document.body.focus();}, 1000);
			
			if (refresh && refresh == true){
				window.location.reload();	
			}
		},
		
		
		/*****************************************************************************************/
		/*                                   ADD INTERFACE OBJECT                                */
		/*****************************************************************************************/
		/**
		 * add an object to the JSInterface
		 * method type: LOCAL
		 * params: @objName : the name of the object in the JSInterface
		 *         @objType : object type like: REGISTER, USERS, COMMENTS, etc
		 *         @params  : a JSON with params to pass to the new created object. Ex: {'name':'Johnson','age':24}
		 */
		add: function(objName, objType, params){
			//find similar object and remove it
			for (var i=0; i<objects_arr.length; i++){
				var obj = objects_arr.shift();
				if (obj === this[objName]){
					this[objName] = null;
				}
				else{
					objects_arr.push(obj);
				}
			}
			
			
			//create object
			this[objName] = new window[objType]();
			if (params != null){
				for (var property in params){
					//alert(property+ " "+params[property]);
					this[objName][property] = params[property];
					
				}
			}
			
			objects_arr.push(this[objName]);
		}
		
		
					
	}
}();