/**
* Tab Control Library
* Requires jQuery 1.2.3 or higher
**/

//Temporary
webvisible = {};
webvisible.common = {};
webvisible.common.ui = {};
webvisible.common.ui.tabs = {};
//=================================

webvisible.common.ui.tabs = function () {
		var addTab = function (tab_id, tab_content_id, default_display, callbacks) {
			if (typeof default_display == "undefined") {
				default_display = false;
			}			
			this.tabs[tab_id] = new Tab(tab_id, tab_content_id, callbacks);
			this.bindClick(tab_id);
			if (default_display) {
				this.default_display 	= tab_id;
				this.active_tab			= tab_id;
			}
		};
		
		var bindClick = function (tab_id) {
			ref = this;
			$("#"+tab_id).click(function () {	
				ref.show(tab_id);
				
				return false;
			});
		};
		
		var show = function (tab_id) {
			if ($("#"+tab_id).length) {
				target_tab = this.tabs[tab_id];
				activeTab = this.tabs[this.active_tab];
				
				this.hideContent(activeTab.tab_content_id);
				this.hideTab(activeTab.tab_id);
			
				this.showContent(target_tab.tab_content_id);
				this.showTab(target_tab.tab_id);
				
				this.active_tab = tab_id;
				
				//fire callback events attached to this tab
				for (var i = 0; i < target_tab.callbacks.length; i++) {					
					target_tab.callbacks[i].call(this, tab_id);
				}
			}				
		};
		
		var showContent = function (tab_content_id) {
			$("#"+tab_content_id).show();			
		};
		
		var hideContent = function (tab_content_id) {
			$("#"+tab_content_id).hide();
		};
		
		var showTab = function (tab_id) {
			$("#"+tab_id).removeClass(this.inactive_class);
			$("#"+tab_id).addClass(this.active_class);
		};
		
		var hideTab = function (tab_id) {
			$("#"+tab_id).removeClass(this.active_class);
			$("#"+tab_id).addClass(this.inactive_class);
		};
		
		//Tab object
		var Tab = function (tab_id, tab_content_id, callbacks) {
			this.tab_id 		= tab_id;
			this.tab_content_id = tab_content_id;
			this.callbacks		= callbacks;
		};
		
		var TabSet = function (active_class, inactive_class) {
			if (typeof active_class == "undefined") {
				active_class = '';
			}
			if (typeof inactive_class == "undefined") {
				inactive_class = '';
			}
			
			//Members
			this.tabs = new Object();
			
			this.active_tab 		= '';
			this.default_display 	= '';
			this.active_class		= active_class;
			this.inactive_class		= inactive_class;
						
			//Methods
			this.addTab 		= addTab;
			this.bindClick		= bindClick;
			this.show			= show;
			this.showContent 	= showContent;
			this.hideContent	= hideContent;
			this.showTab		= showTab;
			this.hideTab		= hideTab;
		};
		
	return {
		TabSet: TabSet,
		show:show
	};
}();


//Browser Action Functionality
webvisible.common.browser = {};
webvisible.common.browser = function () {
	var print = function () {
		window.print();
	};
	
	var bookmark = function (url, title) {
		if (typeof url == "undefined") {
			url = location.href;
		}
		if (typeof title == "undefined") {
			title = document.title;
		}
		
		//Bookmark
		if (window.sidebar && window.sidebar.addPanel) {
		    window.sidebar.addPanel(title, url, "");
		} else if (window.opera && window.print) {
		    var element = document.createElement('a');
		    element.setAttribute('href',url);
		    element.setAttribute('title',title);
		    element.setAttribute('rel','sidebar');
		    element.click();
		} else if (document.all) {
	   		window.external.AddFavorite(url, title);
	   	}
	};
	
	return {
		print: print,
		bookmark: bookmark
	}; 
}();