/**
 *
 * @author Ardeleanu Ionut
 * @langversion JAVASCRIPT
 *
 * http://www.neokinetics.ro
 * iardeleanu@neokinetics.ro
 *
 */
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	
		
		AjaxUpload: new AjaxUpload(),   //object that makes the upload of a form without refreshin via AJAX
		
		
		/*****************************************************************************************/
		/*                                      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(){
				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.clientHeight > maxHeight){
					var defaultDivHeight = divContainer.clientHeight;
					var dif = 0;
				}
				else{
					var defaultDivHeight = maxHeight;
					var dif = maxHeight - divContainer.clientHeight;
				}
				
				//alert("screenHeight:"+screenHeight+" \nmaxHeight:"+maxHeight+" \ndivContainer:"+divContainer.clientHeight+" \ndefaultDivHeight:"+defaultDivHeight+ " \ndif:"+dif);
				//alert("screenHeight:"+screenHeight+" \ndif:"+dif+ " \ntableHeight:"+table.clientHeight+ " \ndivContainer:"+divContainer.style.height);
				if (table.clientHeight + 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.clientHeight+ " \ndivContainer:"+divContainer.style.height);
				
				
				$('#TB_window').height(table.clientHeight);
				$('#TB_iframeContent').height(table.clientHeight);
				$('#TB_window').width(table.clientWidth);
				$('#TB_iframeContent').width(table.clientWidth);
				//$('#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){
			
			//rename all elements of the thickbox
			$('#TB_HideSelect'+'_'+this.thickbox_ids[index]).attr("name","TB_HideSelect")
							   								.attr("id","TB_HideSelect");
			$('#TB_HideSelect').addClass('TB_HideSelect');
			
			
			$('#TB_overlay'+'_'+this.thickbox_ids[index]).attr("name","TB_overlay")
														 .attr("id","TB_overlay");
			$('#TB_overlay').addClass('TB_overlay');
			
			
			$('#TB_window'+'_'+this.thickbox_ids[index]).attr("name","TB_window")
			               								.attr("id","TB_window");
			$('#TB_window').addClass('TB_window');
			
			
			$('#TB_iframeContent'+'_'+this.thickbox_ids[index]).attr("name","TB_iframeContent")
								  							   .attr("id","TB_iframeContent");
			$('#TB_iframeContent').addClass('TB_iframeContent');					  
			
			//alert("setDefaultThickbox");
		},
		
		
		/*****************************************************************************************/
		/*                                      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
			$('#TB_HideSelect').attr("name","TB_HideSelect"+"_"+this.thickbox_id)
							   .attr("id","TB_HideSelect"+"_"+this.thickbox_id);
			$('#TB_HideSelect'+"_"+this.thickbox_id).addClass('TB_HideSelect');
			
			
			$('#TB_overlay').attr("name","TB_overlay"+"_"+this.thickbox_id)
							.attr("id","TB_overlay"+"_"+this.thickbox_id);
			$('#TB_overlay'+"_"+this.thickbox_id).addClass('TB_overlay');
			
			
			$('#TB_window').attr("name","TB_window"+"_"+this.thickbox_id)
			               .attr("id","TB_window"+"_"+this.thickbox_id);
			$('#TB_window'+"_"+this.thickbox_id).addClass('TB_window');
			
			var top = 0-$('#TB_window'+"_"+JSInterface.thickbox_id).height()/2;
			var left = 0-$('#TB_window'+"_"+JSInterface.thickbox_id).width()/2;
			$('#TB_window'+"_"+this.thickbox_id).animate({marginTop: top, marginLeft: left},1,function(){$('#TB_window'+"_"+JSInterface.thickbox_id).css("visibility","visible");});
			
			
			$('#TB_iframeContent').attr("name","TB_iframeContent"+"_"+this.thickbox_id)
								  .attr("id","TB_iframeContent"+"_"+this.thickbox_id);
			$('#TB_iframeContent'+"_"+this.thickbox_id).addClass('TB_iframeContent');					  
								  
			
			//move thickboxes elements to other z-index
			for (var i=0; i<this.thickbox_ids.length; i++){
				
				$('#TB_HideSelect'+"_"+this.thickbox_ids[i]).css("z-index",99-(6*(this.thickbox_ids.length-i)));
				$('#TB_overlay'+"_"+this.thickbox_ids[i]).css("z-index",100-(6*(this.thickbox_ids.length-i)));
				$('#TB_window'+"_"+this.thickbox_ids[i]).css("z-index",102-(6*(this.thickbox_ids.length-i)));
				
			}
			
			//alert("renameThickbox");
		},
		
		
		/*****************************************************************************************/
		/*                                      REMOVE THICKBOX IDENTIFIER                       */
		/*****************************************************************************************/
		/**
		 * remove #TB_window and #TB_iframeContent identifier
		 * method type: LOCAL
		 * params: none
		 */
		removeThickboxID: function(){
			//rename all elements of the thickbox
			$('#TB_window'+"_"+this.thickbox_id).attr("name","TB_window")
			              					    .attr("id","TB_window");
			$('#TB_window').addClass('TB_window');
			
			
			$('#TB_iframeContent'+"_"+this.thickbox_id).attr("name","TB_iframeContent")
								  					   .attr("id","TB_iframeContent");
			$('#TB_iframeContent').addClass('TB_iframeContent');
			
			
			$('#TB_overlay'+"_"+this.thickbox_id).attr("name","TB_overlay")
								                 .attr("id","TB_overlay");
			$('#TB_overlay').addClass('TB_overlay');
			
			
			$('#TB_HideSelect'+"_"+this.thickbox_id).attr("name","TB_HideSelect")
							                        .attr("id","TB_HideSelect");
			$('#TB_HideSelect').addClass('TB_HideSelect');
			
			
			//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++){
				
				$('#TB_HideSelect'+"_"+this.thickbox_ids[i]).css("z-index",99-(6*(this.thickbox_ids.length-i)));
				$('#TB_overlay'+"_"+this.thickbox_ids[i]).css("z-index",100-(6*(this.thickbox_ids.length-i)));
				$('#TB_window'+"_"+this.thickbox_ids[i]).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 ($('input:text')){
				$('input:text').get(0).focus();
			}
			
			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]);
		}
		
		
					
	}
}();