//var MyCesarToolbar = function() {
//need to make global so functions can be called from code behind ScriptEngine manager calls

 MyCesarToolbar = function() {
	var collapsed = false;
	var opened = false;
	var hideTipOffset = 34;
	var colorPull;
	var notificationBGColor;
	
	// Set background color based on a.stbar.chicklet color
	
  if($("a.stbar.chicklet").get(0)) {
      colorPull = "a.stbar.chicklet";
  }
  else {
      colorPull = ".ui-dialog-titlebar a";
  }
  //if not homepage
  if (!$('#cesarHome').get(0)) {
      $(".toolbarNotificationMessage").css("background-color",$(colorPull).css("color"));
  }
  $("#Toolbar_Notifications li.unread a").css("background-color",$(colorPull).css("color"));
	$("#Toolbar_Notifications li.unread a").hover(
		function() {
			$("#Toolbar_Notifications li.unread a").css("background-color",$(colorPull).css("color"));
		},
		function() {
			notificationBGColor = $("#Toolbar_Notifications li.unread a").css("background-color");
			$("#Toolbar_Notifications li.unread a").css("background-color",notificationBGColor);
		}
	);

  //$("#Toolbar_Notifications li a:hover").css("background-color",$("a.stbar.chicklet").css("color"));
	
	// Init (mouse events)
	var init = function() {
	    $('#Toolbar_Trigger').live("click",toggleToolbar);
	    $('#Toolbar_Trigger').live("mouseenter",showHideTip);
	    $('#Toolbar_Trigger').live("mouseleave",hideHideTip);
			
	    $('.toolbarNotificationMessage').live("click", toggleNotifications);
	    $('#Notification_Modal').live("click", toggleNotifications);
		if($('#Toolbar_Notifications li').size()<=0){
		    $('.toolbarNotificationMessage').addClass('toolbarNoHover');
		}else{
		    $('.toolbarNotificationMessage').removeClass('toolbarNoHover');
		}
	}
	
	// Toggle toolbar (collapse/expand)
	var toggleToolbar = function(evt) {
		hideHideTip();
		if (collapsed) {
			$('#Toolbar_Trigger').hide()
			$('#Toolbar_Trigger').attr('src','/images/mycesar_toolbar/trigger_closed.png');
			$('#MyCesar_Toolbar').slideDown('fast',function(){
				$('#Toolbar_Trigger').fadeIn('fast');
			});
			collapsed = false;
		} else {
			$('#Toolbar_Trigger').attr('src','/images/mycesar_toolbar/trigger_open.png');
			if (opened) {
				$('#Toolbar_Notifications').slideUp('fast',function(){
					$('#MyCesar_Toolbar').slideUp('fast');
				});
				opened = false;
				toggleModal();
			} else {
				$('#MyCesar_Toolbar').slideUp('fast');
			}
			collapsed = true;
		}
	}
	
	// Toggle notifications (open/close)
	var toggleNotifications = function() {
		if (opened) {
		    // added buy mvb to prevent opening when alerts are all hidden
		   
		    
			    $('#Toolbar_Notifications').slideUp('fast',function(){
				$('.toolbarNotificationMessage').removeClass("notificationsOpen");
			    });
			    opened = false;
			
		} else {
		     if($('#Toolbar_Notifications li').size()>0){
			    $('#Toolbar_Notifications').slideDown('slow');
			    $('.toolbarNotificationMessage').addClass("notificationsOpen");
			    opened = true;
			}
		}
		toggleModal();
	}
	
	var toggleModal = function() {
	 
		if (opened) {
			$('#Notification_Modal').show();
		} else {
			$('#Notification_Modal').hide();
		}
	}
	
		var showHideTip = function() {
				var tipID = collapsed ? "Toolbar_Show_Tip" : "Toolbar_Hide_Tip";
				
				$('#'+ tipID).css({'bottom':hideTipOffset,'display':'inline','opacity':0});
				$('#'+ tipID).animate({
					opacity: 1,
					bottom: "+="+5
				}, 250);
		}

		var hideHideTip = function() {
				var tipID = collapsed ? "Toolbar_Show_Tip" : "Toolbar_Hide_Tip";
				
				$('#'+tipID).animate({
					opacity: 0,
					bottom: hideTipOffset
				}, 250,function(){
						$('#'+tipID).css('display','none');
				});
		}
	
	init();
	
}

$(InitMyCesarToolbar);

function DisplayGrowl() {
    $().ready(function() {
        $('#growl').delay(2000).fadeIn(5000, function() {
            $('#growl').delay(5000).fadeOut(5000);
        });
    });
}

function InitMyCesarToolbar() {
    if ($('#MyCesar_Toolbar').get(0)) {
        MyCesarToolbar();
    }
}

function ForceNotificationsOpen() {  
     if($('#Toolbar_Notifications li').size()>0){
			 $('#Toolbar_Notifications').addClass("notificationsOpen");
			 $('#Toolbar_Notifications').show();
     }
     
}

function HideGrowl() {
    $('#growl').fadeOut(1000);
}

$().ready( function() {
    $('#growl .notificationClose').live('click', function() {
        HideGrowl();
    }); 
});
