(function($){    
    $.bulletin = function(el, data, options){
        // To avoid scope issues, use 'base' instead of 'this'
        // to reference this class from internal events and functions.
        var base = this;
        
        // Access to jQuery and DOM versions of element
        base.$el = $(el);
        base.el = el; 
        
        // Add a reverse reference to the DOM object
        base.$el.data("bulletin", base);
        
        base.data = {
        	emState:true,
			cookieID:0,
			news:data.news
        };        
        
        base.init = function(){

            base.options = $.extend({},$.bulletin.defaultOptions, options);
			
            // Put your initialization code here
            if(data.news.length > 0){
            	base.data.cookieID = base.data.news[0].id;
				var cookieVal = base.getCookie("emBulletin");
				/*
				The url below in the box variable used to be data.news[0].url. 
				I had to update it with this if statement below to take into account if the user was logged in with a secure connection or not.
				What was happening was when a user was not logged in and on the portal, the protocol was https, when clicking on the read more
				for the bulletin it was trying to go to https with the news detail page of 204, but it was refreshing itself to be http.
				Due to this refresh the backlink was being changed from the portal page to an empty news detail page.
				I added a check/replace for the news URL to determine if the user was logged in and if they were not
				Update readmore link itself to go to the nonsecure 204 page so it would not refresh and have the proper backlink.
				Updated added 2/14/2011 -KAQ.
				*/
				var url = "";
				// pdGlobal check to see if on a podium page
				if(typeof pdGlobal !== "undefined"){
					(pdGlobal.userLoggedIn) ? url = data.news[0].url : url = data.news[0].url.replace(/https/g,"http");
				}
				else{
					url = data.news[0].url;
				}
				if(parseInt(cookieVal) == parseInt(base.data.cookieID)){
					base.data.emState = false;
				}
				
				//html for bg transparent div
				var bg = "<div id='embulletinbg' class='em-bulletinbg' style='position:absolute; top:0; left:0; display:none;'></div>";
				var box = "<div id='embox' class='em-box' style='position:absolute; display:none;'>"+
							"<div id='outer-border'><div id='inner-border'>"+
							"<div id='close-left'></div><div id='close-sep'></div><div id='close-button'><a href='javascript:void(0);'>x</a></div>"+
							"<div id='close-bottom'></div><div id='logo-top'></div><div id='story-holder'>"+
							"<div id='em-headline'><a href='"+url+"'>"+data.news[0].title+"</a></div>"+
							"<div id='em-brief'>"+data.news[0].brief+"</div>"+
							"<div id='em-readmore'><a href='"+url+"' id='btn-readmore'>read more</a></div>"+
							"</div><div id='logo-bottom'></div></div></div></div>";
				
				var embutton = "";
				
				if(base.options.emButton){
					embutton = "<div id='em-button' style='position:absolute; display:block;'><a href='javascript:void(0);'>emergency notice</a></div>";
					base.$el.append(bg + box);
					base.$el.append(embutton);
				}else{
					//emBar
					embutton = "<div id='em-bar' style='display:none;'><div id='em-bar-text'><span id='text'>"+data.news[0].title+"</span><a href='"+url+"'>read more</a>"+
								"</div><div id='em-bar-bottom'></div></div>";
					base.$el.append(bg + box);
					base.$el.prepend(embutton);
				}
				
				base.initBulletin();
			
            }            
        }
        
        
        base.initBulletin = function(){
			var top = $(window).height()/2;
    		var left = base.$el.width()/2;
    		var $box = $("#embox");
    		$box.css({'top':top-$box.height()/2,'left':left-$box.width()/2});
    		
    		if(base.options.emButton){
    			var $button = $("#em-button");
    			if(base.options.emButtonPosition == "left"){
    				$button.css({'top':0,'left':0-$button.width()});
    			}else{
    				$button.css({'top':0,'left':base.$el.width()-$button.width()});
    			}
    		}
    		base.setBgHeight();
    		
    		if(base.data.emState){
    			base.positionBox();
    	    }else{
    	    	if(base.options.emButton){
    	    		base.animateButtonIn();
    	    	}else{
    	    		base.animateBarIn();
    	    	}
    	    }
    	    base.buildPageActions();
    	}
    	
    	//actions
    	base.buildPageActions = function(){
			var $bg = $("#embulletinbg");
    		var $closebtn = $("#close-button a");
    		$bg.click(function(){
    			base.hideEM();
    		});
    		$closebtn.click(function(){
    			base.hideEM();
    		});
    		
    		var $embutton = $("#em-button");
    		$embutton.click(function(){
    			base.showEM();
    		});
    		$(window).scroll(function() { 
    			if(base.data.emState){
    				base.positionBox();
    				base.setBgHeight();
    			}else{
    				if(base.options.emButton){
    					base.animateButtonIn();
    				}else{
    					//not sure why thats empty...
    				}
    			}	
    		});
    		$(window).resize(function(){					
    			if(base.data.emState){
    				9
    			}else{
    				if(base.options.emButton){
    					base.animateButtonIn();
    				}else{
    					//not sure why thats empty...
    				}
    			}
    		});
    		$("#btn-readmore").click(function(){
    			base.readMoreEM();
    		});
    		$("#em-headline a").click(function(){
    			base.readMoreEM();
    		});
    	}
    	
    	base.readMoreEM = function(){
    		//write cookie
    		base.setCookie("emBulletin",base.data.cookieID,1);
    	}
    	
    	base.hideEM = function(){
    		var $bg = $("#embulletinbg");
    		var $box = $("#embox");
    		$box.css("display","none");
    		$bg.css("display","none");
    		if(base.options.emButton){
    			base.animateButtonIn();
    		}else{
    			base.animateBarIn()		
    		}
    		
    		base.data.emState = false;
    		base.setCookie("emBulletin",base.data.cookieID,1);
    	}
    	base.showEM = function(){
    		var $bg = $("#embulletinbg");
    		var $box = $("#embox");
    		$box.css("display","block");
    		$bg.css("display","block");
    		base.animateButtonOut();
    		base.data.emState = true;
    		base.positionBox();
    		base.setBgHeight();
    	}
    	
    	base.positionBox = function(){
			var $box = $("#embox");
    		var $bg = $("#embulletinbg");
    		var top = $(window).height()/2;	
    		var left = base.$el.width()/2;		
    		var topOffset = (top-$box.height()/2) + $(document).scrollTop();
    		var leftOffset = (left-$box.width()/2) + $(document).scrollLeft();
    		
    		$box.css("display","block");
    		$box.animate({top:topOffset,left:leftOffset},{duration:250,queue:false});
    		if(base.options.emButton){
    			var $button = $("#em-button");
    			if(base.options.emButtonPosition == "left"){
    				$button.css({"top":0-$button.height(),"left":0});
    			}else{
    				$button.css({"top":0-$button.height(),"left":base.$el.width()-$button.width()});
    			}		
    		}
    		$bg.css("display","block");
    		if($.isFunction(base.options.showBoxComplete)){
    			base.options.showBoxComplete();
    		}
    	}
    	
    	base.setBgHeight = function(){
			var $bg = $("#embulletinbg");
			$bg.css({'height':$(document).height(),'width':(base.$el.width()+$(document).scrollLeft())});
		}
        
        //cookie functions
		base.setCookie = function (c_name,value,expiredays){
			var exdate=new Date();
			exdate.setDate(exdate.getDate()+expiredays);
			document.cookie=c_name+ "=" +escape(value)+
				((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+"; path=/";
		}
		
		base.getCookie = function(c_name){
			if (document.cookie.length>0){
				c_start=document.cookie.indexOf(c_name + "=");
				if(c_start!=-1){ 
					c_start=c_start + c_name.length+1; 
					c_end=document.cookie.indexOf(";",c_start);
					if (c_end==-1) c_end=document.cookie.length;
					return unescape(document.cookie.substring(c_start,c_end));
				} 
			}
			return "";
		}
		
		//animate functions
		base.animateBarIn = function(){
			var $bar = $("#em-bar");
		    $bar.slideDown("slow");
		    
		    if($.isFunction(base.options.showBarComplete)){
    			base.options.showBarComplete();
    		}
		}
		
		base.animateButtonIn = function(){
		    var $button = $("#em-button");
		    if(base.$el.width() <= base.options.minPageWidth){
		    	$button.css({"top":"0","left":base.options.minPageWidth});
		    	return;
		    }
		    var topOffset = $(document).scrollTop();
		    if(base.options.emButtonPosition == "left"){
		    	var leftOffset = 0;
		    }else{
		    	var leftOffset = (base.$el.width()-$button.width()) + $(document).scrollLeft();
		    }
		    $button.animate({top:topOffset,left:leftOffset},{duration:500,queue:false});
		}
		base.animateButtonOut = function(){
		    var $button = $("#em-button");
		    var topOffset = $(document).scrollTop()-$button.height();
		    
		    if(base.options.emButtonPosition == "left"){
		    	var leftOffset = 0;
		    }else{
		    	var leftOffset = (base.$el.width()-$button.width()) + $(document).scrollLeft();
		    }
		    $button.animate({top:topOffset,left:leftOffset},{duration:500,queue:false});				
		}
        
        //log function
        base.log = function(msg){
        	if(window.console){
        		console.debug(msg);
        	}
        }
        
        base.init();
    }

	
    $.bulletin.defaultOptions = {
        emButtonPosition: "right",
        emButton: true,
        minPageWidth:1024,
        showBoxComplete:"",
        showBarComplete:""
    }
	
    $.fn.bulletin = function(data, options){
        return this.each(function(){
            (new $.bulletin(this, data, options));
        });
    }
    
    $.fn.bulletin.version = function(){
      	if(window.console){
      		console.debug("Bulletin Plugin, Version: 1.1");
      	}
    }
	
})(jQuery);
