/**
 * This script allows to pop POG based on several parameters. See inline comments for
 * additional information
 *
 * @version $Id$
 * @author CI
 */


var ZPOGWrap = {
	
	pogDisplayCookie : 'zPogDisplayed',
	pogCounterCookie : 'zPogCounter',
	
	// cookie expiration in hours. Change if needed, 0 for no delay
	pogRepopDelay : 1,
		
	// cookie's visit trim. set it to how many pages a visitor needs
	// to navigate before. To show cookie after 5 page views, set it to 5
	pogViewsBeforeTrigger : 0,
	
	
	// the pog code to pop... replace this for your patner.. make sure you escape the backslashes!!!
	pogCode : '<script type="text/javascript" language="javascript" src="http://prompt.zangocash.com/pog/0229acecb6.js"><\/script><script language="javascript" type="text/javascript">self.focus();<\/script>',
	
	// based on http://techpatterns.com/downloads/javascript_cookies.php code
	setCookie : function (name, value, expiration) {
		
		var today = new Date();
		today.setTime( today.getTime() );
		
		if ( expiration ) {
			expiration = expiration * 1000 * 60 * 60;
		}
		
		var expires_date = new Date( today.getTime() + (expiration) );	
		document.cookie = name + "=" +escape( value ) + ( ( expiration ) ? ";expires=" + expires_date.toGMTString() : "" );	
	},
	
	
	// based on http://techpatterns.com/downloads/javascript_cookies.php code
	getCookie : function (name) {
		
		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		
		if ((!start) && (name != document.cookie.substring(0, name.length))) {
			return null;
		}
		
		if (start == -1) {
			return null;
		}
		
		var end = document.cookie.indexOf( ";", len );
		
		if (end == -1) {
			end = document.cookie.length;
		}
		
		return unescape( document.cookie.substring( len, end ) );		
				
	},
	
	
	// deploys the POG based on cookies
	deploy : function() {
		
		
		var countsRemaning = 0;
		var deployPog = true;
		
		var displayCookie = ZPOGWrap.getCookie(ZPOGWrap.pogDisplayCookie);
		
		if (displayCookie == null || displayCookie != 'delay' || ZPOGWrap.pogRepopDelay == 0) {
			
			
			if (ZPOGWrap.pogViewsBeforeTrigger > 0) {
				
				deployPog = false;
				
				var counterCookie = ZPOGWrap.getCookie(ZPOGWrap.pogCounterCookie)
				
				// if coutner cookie is not set, create it and use it as reference
				if (counterCookie == null || isNaN(counterCookie)) {
					
					ZPOGWrap.setCookie(ZPOGWrap.pogCounterCookie, ZPOGWrap.pogViewsBeforeTrigger, 24);	
					
				} else {
					
					// cookie is present, fetch it
					countsRemaning = parseInt(counterCookie) 
					
					if (--countsRemaning <= 0) {
						deployPog = true;
						
						// kill the cookie
						ZPOGWrap.setCookie(ZPOGWrap.pogCounterCookie, 'something', -10);
						
					} else {
					
						// set the new delay
						ZPOGWrap.setCookie(ZPOGWrap.pogCounterCookie, countsRemaning, 24);
					}
				}
				
			}
						
			// if we have no limit constraint
			if (deployPog) {
				
				// write the POG
				document.write(ZPOGWrap.pogCode);
				
				// if delay is presented, se tthe cookie
				if (ZPOGWrap.pogRepopDelay > 0) {
					ZPOGWrap.setCookie(ZPOGWrap.pogDisplayCookie, "delay", ZPOGWrap.pogRepopDelay);
				}
			}		
		}
	}
	
};

ZPOGWrap.deploy();