/**
 *
 * @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	
		
		AjaxUpload: new AjaxUpload(),   		//object that makes the upload of a form without refreshin via AJAX
		//StyleSheet: new StyleSheet(),   		//object that changes CSS style for pictures, mouse and focus events
		//MultiLanguage: new MultiLanguage(),   	//object that loads multiple languages in csv format
		Preloader: new Preloader(),  			//the preloader object used for sending data to server through AJAX
		Loader: new Loader(),   				//the loader object for all AJAX events
		//BackToTop: new BackToTop(),			//object that makes the Back to Top Scroll page
		
		
		/*****************************************************************************************/
		/*                                      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");
				//JSInterface.StyleSheet.init(window.document);
				//JSInterface.BackToTop.init(window.document, document.getElementById('back_to_top_container'));
				JSInterface.Loader.init();
				JSInterface.initObjects();
			});	
		},
		
		
		
		/*****************************************************************************************/
		/*                                      INIT INTERFACE OBJECTS                           */
		/*****************************************************************************************/
		/**
		 * initialize the JSInterface objects
		 * method type: LOCAL
		 * params: none
		 */
		initObjects: function(){
			for (var i=0; i<objects_arr.length; i++){
				//alert(objects_arr[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(screenHeight){
			if (screenHeight == null){
				screenHeight = 650;	
			}
				
			//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 x = table.offsetHeight + dif - screenHeight;
				divContainer.style.height = (defaultDivHeight - x)+"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");
			hideSelect.attr("name","TB_HideSelect")
					  .attr("id","TB_HideSelect"); 	
			
			var overlay = $('*[id^="TB_overlay"]').filter(function(){return $(this).data("tb_id") == tb_id;})
			overlay.addClass("TB_overlay");
			overlay.attr("name","TB_overlay")
				   .attr("id","TB_overlay"); 
			
			var tbwindow = $('*[id^="TB_window"]').filter(function(){return $(this).data("tb_id") == tb_id;})
			tbwindow.addClass("TB_window");
			tbwindow.attr("name","TB_window")
					.attr("id","TB_window"); 
			
			var iframeContent = $('*[id^="TB_iframeContent"]').filter(function(){return $(this).data("tb_id") == tb_id;})
			iframeContent.addClass("TB_iframeContent");
			iframeContent.attr("name","TB_iframeContent")
					     .attr("id","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
			$('#TB_HideSelect').attr("name","TB_HideSelect"+"_"+this.thickbox_id)
							   .attr("id","TB_HideSelect"+"_"+this.thickbox_id);
			var iframe = $('#TB_HideSelect'+"_"+this.thickbox_id);
			iframe.data("tb_id",this.thickbox_id);
			iframe.addClass('TB_HideSelect');
			
			
			$('#TB_overlay').attr("name","TB_overlay"+"_"+this.thickbox_id)
							.attr("id","TB_overlay"+"_"+this.thickbox_id);
			var div_overlay = $('#TB_overlay'+"_"+this.thickbox_id);
			div_overlay.data("tb_id",this.thickbox_id);
			div_overlay.addClass('TB_overlay');
			
			
			$('#TB_window').attr("name","TB_window"+"_"+this.thickbox_id)
			               .attr("id","TB_window"+"_"+this.thickbox_id);
			var div_window = $('#TB_window'+"_"+this.thickbox_id);
			div_window.data("tb_id",this.thickbox_id);
			div_window.addClass('TB_window');
			
			
			$('#TB_iframeContent').attr("name","TB_iframeContent"+"_"+this.thickbox_id)
								  .attr("id","TB_iframeContent"+"_"+this.thickbox_id);
			var iframeContent = $('#TB_iframeContent'+"_"+this.thickbox_id);
			iframeContent.data("tb_id",this.thickbox_id);
			iframeContent.addClass('TB_iframeContent');	
			
			
			var top = -5-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");});
			}
			
			
			//move thickboxes elements to other 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)));
				
				var nr1 = 99-(6*(this.thickbox_ids.length-i));
				var nr2 = 100-(6*(this.thickbox_ids.length-i));
				var nr3 = 102-(6*(this.thickbox_ids.length-i));
				//alert(nr1+" "+nr2+" "+nr3)
			}				  
								  
			
		},
		
		
		/*****************************************************************************************/
		/*                                      REMOVE THICKBOX IDENTIFIER                       */
		/*****************************************************************************************/
		/**
		 * remove #TB_window and #TB_iframeContent identifier
		 * method type: LOCAL
		 * params: none
		 */
		removeThickboxID: function(){
			
			//rename all elements of the thickbox
			$('#TB_HideSelect'+"_"+this.thickbox_id).attr("name","TB_HideSelect")
							                        .attr("id","TB_HideSelect");
			var iframe = $('#TB_HideSelect');
			iframe.addClass('TB_HideSelect');
			
			
			$('#TB_overlay'+"_"+this.thickbox_id).attr("name","TB_overlay")
								                 .attr("id","TB_overlay");
			var div_overlay = $('#TB_overlay');
			div_overlay.addClass('TB_overlay');
			
			
			$('#TB_window'+"_"+this.thickbox_id).attr("name","TB_window")
			              					    .attr("id","TB_window");
			var div_window = $('#TB_window');
			div_window.addClass('TB_window');
			
			
			$('#TB_iframeContent'+"_"+this.thickbox_id).attr("name","TB_iframeContent")
								  					   .attr("id","TB_iframeContent");
			var iframeContent = $('#TB_iframeContent');
			iframeContent.addClass('TB_iframeContent');	
			
			
			//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
			// alert(objName+' '+objType);
			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]);
		},
		
		
		/*****************************************************************************************/
		/*                                   SCROLL TO FIT SIZE                                  */
		/*****************************************************************************************/
		/**
		 * scroll the document body so that the object fits his entire height inside the body visible area
		 * method type: LOCAL
		 * params: @obj : jQuery object such as $(div), $(p) ... 
		 */
		scrollToFit: function(obj){
			
			var container = $('html,body');
			var scrollTop = parseInt(container.scrollTop());
			var containerHeight = container.get(0).clientHeight;
			var objTop = parseInt(obj.offset().top);
			var objHeight = obj.height();
			var objBottom = objTop + objHeight;
			
			//alert(objTop+" "+scrollTop+" "+containerHeight)			
			if (objTop < scrollTop){
				$(container).animate({scrollTop: objTop }, 1000);	
			}
			else if (objTop >= scrollTop && objTop < containerHeight+scrollTop){
				if (objBottom > containerHeight+scrollTop){
					$(container).animate({scrollTop: objBottom-containerHeight }, 1000);		
				}
			}
			else if (objTop >= containerHeight+scrollTop){
				$(container).animate({scrollTop: objBottom-containerHeight }, 1000);		
			}
			
			//alert("scrollTop="+scrollTop+" ,containerHeight="+containerHeight+ " ,objTop="+objTop+" ,objHeight="+objHeight);
		}		
	}
}();
