/*!
 * Modal Window PopUP
 * http://www.artemisoftnian.com
 *
 * Copyright 2011, Artemisoftnian
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.opensource.org/licenses/gpl-2.0.php
 *
 */

(function($){
	$.fn.modalwindow = function(addOptions){
		var defaults = { 
			active: false,
			cookie: false, //Use Cookie
			expiration:"YYYY/MM/DD",
			cookieName:null,
			fade:true,
			fadeOpacity:0.8,
			fadeSpeed:"slow", // "slow" | "fast"
			addHTML:"",
			fadeTime:2
		};
		
		var addOptions = $.extend(defaults, addOptions);  
		
		$(this).append('<div id="modal" class="window"><div class="anuncio"></div></div>');

		var oid = $('#modal', this);
		
		$(this).append("<div id='mask' style='position:absolute; top:0; left:0; z-index:9000; height:100%; background-color:#000; display:none'></div>");
     	var oMask = $('#mask', this);	
		
		var cookieName = addOptions.cookieName;
		var fadeTime = addOptions.fadeTime * 1000;	
		
			
		$( '.anuncio', this ).load(addOptions.addHTML);
		$( oid, this ).append("<div class='close' style='position:absolute; z-index:9999; top:0; right:0; cursor:pointer;'></div>");		
		var oCloseBtn = $('.close', oid);
		
		return this.each(function(){
			
			
		if(addOptions.active == true){	
		
			if(addOptions.cookie == true)
			{
				var visit = GetCookie(addOptions.cookieName);
   				if (visit == null) {
        			ShowModal();
        			SetCookie();        	
	   			}
			}
			else
				ShowModal();
				
		}
				
		});
		
		function ShowModal(){
			    
		    //Set height and width to mask to fill up the whole screen       
			$(oMask).css({'width':$(window).width(),'height':$(document).height()}); 
		    //Get the window height and width       
		    var winH = $(window).height();  var winW = $(window).width();    
		                       
		    //Set the popup window to center        
		    $(oid).css('top',  winH/2-$(oid).height()/2);        
		    $(oid).css('left', winW/2-$(oid).width()/2);  		              
		    
			if(!addOptions.fade){
    
			    //transition effect        
			    $(oid).show();			    
				//if close button is clicked    
			    $(oCloseBtn).click(function (e) {       
			    	//Cancel the link behavior       
			     	e.preventDefault();        
			    	$(oid).hide();  
			    	$(oMask).hide();
			  	}); 	
			}
			else{		    
                
			    //transition effect             
			    $(oMask).fadeTo(fadeTime,addOptions.fadeOpacity);	    
			    
			    //transition effect        
			    $(oid).fadeIn(fadeTime);
			    
				//if close button is clicked    
			    $(oCloseBtn).click(function (e) {       
			    	//Cancel the link behavior       
			     	e.preventDefault();        
			    	$(oid).fadeOut(fadeTime);   
			    	$(oMask).fadeOut(fadeTime);
			  	}); 	  	
			  	
				//if mask is clicked    
				$(oMask).click(function () {        
					$(oid).fadeOut(fadeTime); 
					$(this).fadeOut(fadeTime);   
				});  
			}
		}
		
		function SetCookie()
		{
			var expire = new Date();  		
		    expire.setDate(expire.getDate() + 1);
            document.cookie = addOptions.cookieName + "=here; expires=" + expire;
		}
		
		function GetCookie(name) {
			var arg = name + "=";
			var alen = arg.length;
			var clen = document.cookie.length;
			var i = 0;
			while (i < clen) {
				var j = i + alen;
			    if (document.cookie.substring(i, j) == arg)
			    	return "here";
				    i = document.cookie.indexOf(" ", i) + 1;
			        if (i == 0) break;
			    }
			return null;
		}
	};
})(jQuery);




