function ZIG_Popup()
{
	if($('ZIG_Popup_layer'))
	{
		new ZIG_PopupClass();
    }
}

var ZIG_PopupClass = Class.create();
ZIG_PopupClass.prototype = 
{   	 
	initialize : function(element) 
   	{
		new Draggable('ZIG_Popup_popup');
		

		this.id = $('ZIG_Popup_popup').className; 
		this.buttons = $A($('ZIG_Popup_popup').getElementsByClassName('ZIG_Popup_popup_button'));
        this.buttons.each(this.setupButton.bind(this));
        
        $('ZIG_Popup_popup').style.visibility = 'visible'; 
        $('ZIG_Popup_layer').style.visibility = 'visible';  
        
        $('ZIG_Popup_popup').style.marginTop = (($('ZIG_Popup_popup').getHeight() / 2) * -1) + 'px';
 	},
 	initCloseButton :  function(ev) 
    {
 		var elm = Event.element(ev);
 		this.checkSetCookie(elm);
 		
  		this.close(elm,true);
  		return false;
    },
  	initLinkButton :  function(ev) 
  	{
    	var elm = Event.element(ev);
    	this.checkSetCookie(elm);
    	this.close(elm,false);
    	return false;
    },
    close : function(elm,effect)
    {
    	if(effect)
    	{
    		new Effect.Fade('ZIG_Popup_layer',{ duration: 0.5, from: 0.8, to: 0 });
    		new Effect.Fade('ZIG_Popup_popup',{ duration: 0.5, from: 0.8, to: 0 });
    	}
    	else
    	{
    		$('ZIG_Popup_layer').hide();
    		$('ZIG_Popup_popup').hide();
    	}
    },
    setupButton : function(elm) 
    {        
    	if(elm.href != document.URL)
		{
			Event.observe(elm,'click',this.initLinkButton.bindAsEventListener(this),false);
		}
		else
		{
			Event.observe(elm,'click',this.initCloseButton.bindAsEventListener(this),false);
			elm.onclick = function() {return false;};
		}
  	},
  	checkSetCookie : function(elm)
  	{
  		var classname = elm.className;
  		
  		if(classname.indexOf("ZIG_Popup_NotAgain")!=-1)
  		{
  			cookiejar.bake('ZIG_Popup', this.id, 365, "/");
  		}
  	}	
}

var cookiejar =
{
	/* set a cookie */
	bake: function(cookieName,cookieValue,days,path)
	{
		var expires='';
		if (days)
		{
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			expires = "; expires="+date.toGMTString();
		}
		var thePath = '; path=/';
		if (path) thePath = '; path='+path;
		document.cookie = cookieName+'='+escape(cookieValue)+expires+thePath;
	},
	/* get a cookie value */
	fetch: function(cookieName)
	{
		var nameEQ = cookieName + '=';
		var ca = document.cookie.split(';');
		for (var i=0; i<ca.length; i++)
		{
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ)==0) return unescape(c.substring(nameEQ.length,c.length));
		}
		return null;
	},
	/* delete a cookie */
	crumble: function(cookieName)
	{
		cookiejar.bake(cookieName,'',-1);
	}
};

