//js for displaying unseen popup messages and handling floating toolbar for viewing user's all list of active unexpired messages for current website
//it also handles the toolbar slide to place and hide actions
//initially the toolbar is hidden and only the switch is shown (after backend status reply), clicking the switch will slide open the toolbar
if (window==window.top) {//not in an iframe, do not apply toolbar and popup messages when in an embedded iframe
jQuery(document).ready(function() {
	//create toolbar and message list container html, hide toolbar, get toolbar and message container reference, load any unseen popup, align toolbar, set resize listener,toolbar container click listener, message list button toggler, body click and toolbar close button handler
	//it also creates the toolbar open switch and hides it, and adds a click listener to slide open the toolbar
	
	//create toolbar and message list container html
	var tbh_html = '<div id="tbh_list_container"><div id="tbh_list_inner">No messages available</div></div><div id="tbh_toolbar_holder"><div class="tb"><ul><li id="tbh_close"><a href="#" title="Hide Message Toolbar">&lt;</a></li><li id="tbh_list_button"><a href="#" >Messages<span id="tbh_list_total">0</span></a></li></ul></div></div>';
	//append to document body
	jQuery("body").append(tbh_html);
	jQuery("#tbh_toolbar_holder").hide();//initially toolbar only to be displayed by toolbar switch clcik
	
	//create toolbar open switch
	tbh_html = '<div id="tbh_toolbar_switch"><div class="tb"><ul><li id="tbh_open"><a href="#" title="Show Message Toolbar">&gt;</a></li></ul></div></div>';
	//append to document body
	jQuery("body").append(tbh_html);
	jQuery("#tbh_toolbar_switch").hide();//initially hide open switch status of user login discovered
	
	// Get a reference to the container.
	var tbh_list_container = jQuery( "#tbh_list_container" );
	
	tbh_first_load = true;//first load variable set to true (stored so that on status received, the toolbar switch will be made visible)
	popupAnnouncement(null);//display initially any pending unseen announcement onload
	setInterval(function(){popupAnnouncement(null);},60000);//rerun every minute
	
	//toolbar holder
	var tbh = jQuery("#tbh_toolbar_holder");
	//toolbar open switch
	var tbh_switch = jQuery("#tbh_toolbar_switch");
	//var msie6 = jQuery.browser == 'msie' && jQuery.browser.version < 7;
	
	tbh_align();//align toolbar
	/*
	if(jQuery.browser.msie){
		jQuery(window).scroll(function(){
			tbh_align();
		});
	}
	*/
	jQuery(window).resize(function() {//on window resize, realign toolbar
		tbh_align();
	});
	
	jQuery("#tbh_close").click(function( event ){//on toolbar close button click, slide toolbar to left and show toolbar open switch while hiding the toolbar
		tbh_list_container.hide();//hide messages list container
		
		// Prevent the default event.
		event.preventDefault();
		event.stopPropagation();
		//slide to the left, at end of animation show open switch and hide toolbar
		tbh.animate({
			'left': -(jQuery(window).width() - tbh.width())/2
		}, 500, function() {
			// Animation complete.
			tbh_switch.show();
			tbh.hide();
		});
	});
	
	jQuery("#tbh_open").click(function( event ){//on toolbar open button click, hide toolbar switch and slide toolbar to the right and align in center while hiding the open switch
		tbh_switch.hide();//hide open switch
		tbh.show();//show toolbar
		//set toolbar on left edge
		var cssObj = {
			'margin-left':0,
			'left':0
		}
		tbh.css(cssObj);
		// Prevent the default event.
		event.preventDefault();
		event.stopPropagation();
		
		//slide to the right center of screen, at end of animation re-align toolbar
		tbh.animate({
			'left': (jQuery(window).width() - tbh.width())/2
		}, 500, function() {
			// Animation complete.
			tbh_align();
		});
	});
	
	function tbh_align(){
		//align the toolbar and its message container at the center bottom of the screen
		/*
		var top,bottom,left,cssObj;
		if(jQuery.browser.msie){
			top = jQuery(window).scrollTop() + jQuery(window).height() - tbh.height() -2;
			cssObj = {
				'position' : 'absolute',
				'top' :top ,
				'margin-left':(jQuery(window).width() - tbh.width())/2
			}
			
			tbh.css(cssObj);
			
			left = (jQuery(window).width() - tbh_list_container.width())/2 + tbh.width()/2;
			bottom = tbh.height()-jQuery(window).scrollTop();
			cssObj = {
				'position':'absolute',
				'bottom':bottom,
				'margin-left':left
			}
			
			tbh_list_container.css(cssObj);
		}else{
		
			tbh.css({'position':'fixed','bottom':0,'margin-left':(jQuery(window).width() - tbh.width())/2});
			tbh_list_container.css({'position':'fixed','bottom':tbh.height(),'margin-left':(jQuery(window).width() - tbh_list_container.width())/2 + tbh.width()/2});
		}
		*/
		//align toolbar, used fixed position css to fix to bottom of visible screen
		tbh.css({'position':'fixed','bottom':0,'margin-left':(jQuery(window).width() - tbh.width())/2,'left':0});
		//align message list container
		tbh_list_container.css({'position':'fixed','bottom':tbh.height(),'margin-left':(jQuery(window).width() - tbh_list_container.width())/2 + tbh.width()/2});
	}
	 
	tbh_list_container.click(function(event){
		//on message list container click, stop click event bubbling so that body click listener would not close the container
		event.preventDefault();
		event.stopPropagation();
	});
	
	
	jQuery( "#tbh_list_button a" ).click(function( event ){// Bind the link to toggle the list container slide, slide. shows or hides the message list container on messages button click
		// Prevent the default event.
		event.preventDefault();
		event.stopPropagation();
		// Toggle the slide based on its current
		// visibility.
		if (tbh_list_container.is( ":visible" )){
			hide_tbh_list_container();//hide container
		} else {
			show_tbh_list_container();//show container
		}
	});
	
	jQuery("html").click(function( event ){//on document body click hides the message list container (in case user clicks outside of the container)
		if (tbh_list_container.is( ":visible" )){
			hide_tbh_list_container();
		}
	});
	
});
}

function hide_tbh_list_container(){
	// Hide message list container - slide up.
	var tbh_list_container = jQuery( "#tbh_list_container" );//get container jquery object
	//tbh_list_container.slideUp( 600 );
	//using multiple animation (opacity and height)
	tbh_list_container.animate({
		opacity: 0,
		height: 'toggle'
	}, 600, function() {
		// Animation complete.
	});
}
	
function show_tbh_list_container(){
	// Show message list container- slide down.
	var tbh_list_container = jQuery( "#tbh_list_container" );//get container jquery object
	//tbh_list_container.slideDown( 600 );
	//using multiple animation (opacity and height)
	tbh_list_container.animate({
		opacity: 100,
		height: 'toggle'
	}, 600, function() {
		// Animation complete.
	});
}
	
function popupAnnouncement(id){
	//popup announcement, when id null, pending unseen message is returned, when id is specified (message click in message list container), that message is displayed
	//when popup script received, get message list json and parse and build the content for the toolbar message list, add click event handlers for message titles to popup corresponding popups
	var website = tbh_website;//set to corresponding site name
	var proceed = true;
	var url;
	//do not proceed if popup still open
	if(!(typeof(popupDHTMLAd) == "undefined")){
		if(typeof popupDHTMLAd.isShowing == 'function'){
			proceed = !popupDHTMLAd.isShowing();
		}
	}
		
	if(proceed){//proceed
		
		popupDHTMLAd = {};//clear popup js
		jQuery("#popupAdDiv").remove();//remove popup html
		if(id!=null){//get specific id (on message click in list container)
			url = '/shared/libraries/dhtml_popup/popupjs.php?id='+id.toString()+'&website='+website;
		}else{//id is null, get pending unseen message
			url = '/shared/libraries/dhtml_popup/popupjs.php?website='+website;
		}
		
		jQuery.getScript(url, function(data, textStatus){//load announcement script (will popup requested popup message)
			if(!(typeof(popupDHTMLAd) == "undefined")){//if popupDHTMLAd namespace exists
				if(!(typeof(popupDHTMLAd.list) == "undefined")){//if list of messages returned with script
					if(tbh_first_load){//on first load, show toolbar
						jQuery("#tbh_toolbar_switch").show();
						tbh_first_load = false;//reset first load flag
					}
					//remove previous message click listerners in message container
					jQuery(".tbh_item").unbind("click");
					temp ="";//html for message list container
					if(!(typeof(popupDHTMLAd.list.length) == "undefined")){//if list is array
						for(i=0;i<popupDHTMLAd.list.length;i++){//for each item
							//get title, truncate if too long
							title = popupDHTMLAd.list[i]["title"];
							if(title.length>40){
								title = title.substr(0,40)+"...";
							}
							//get id
							id = popupDHTMLAd.list[i]["id"];
							//construct message link, add id in class name, append to message list container
							temp += "<a href='#' class='tbh_item'><span class='tbh_item_"+id.toString()+"'>"+title+"</span></a><br/>";
						}
					}
					//update number of messages display
					jQuery("#tbh_list_total").html(popupDHTMLAd.list.length);
					//populate message container
					if(temp==""){
						jQuery("#tbh_list_inner").html("No messages available");//when no messages
					}else{
						jQuery("#tbh_list_inner").html(temp);//messages available update list container with it
					}
					jQuery(".tbh_item").click(function( event ){//on message item click handler
						var className = jQuery(jQuery(this).children()[0]).attr('class');//get internal child class name (contains the id)
						var id = parseInt(className.replace("tbh_item_",""),10);//parse id from class name
						if(!isNaN(id)){//if id is valid integer
							//close any open message
							if(!(typeof(popupDHTMLAd.closeAd) == "undefined")){
								popupDHTMLAd.closeAd();
							}
							//popup clicked message
							popupAnnouncement(id);
						}
						//hide open message list container
						hide_tbh_list_container();
						event.preventDefault();
					});
				}
			}
			
		});
	}
}
