/***
* GeoModal - used to show the GeoIP modal for Polix.com.au
* Author: Kinnect Digital
* Dependencies: 
* - jquery.cookie.js (used to read and write cookies)
* - jquery.fancybox.js
*/
var GeoModal = function()
{
	var self = this; 	// Store a reference to the object
	var cookieName = "politixGeoWarning";	// The name to use to store the cookie.
	
	/***
	* Shows the geo modal window
	*/
	this.showGeoModal = function()
	{
		jQuery.fancybox({
			"type" : "inline",
			"href" : "#geoModal",
			"padding" : 0
		});	
	}
	
	/***
	* Hides the geo modal window
	*/
	this.closeGeoModal = function()
	{
		jQuery.fancybox.close();
	}
	
	/***
	* Checks to see if the writing of the cookie in the users browser was successful or not.
	* If not, a request is made to the server to store the fact that the user has seen the popup and has clicked on
	* an option.
	*/
	this.checkCookie = function()
	{
		var cookieWriteFailure = false;
		
		// Attempt to read the cookie
		var cookieVal = jQuery.cookie(cookieName);
		if((cookieVal == null) || (cookieVal == ""))
		{
			// No cookie was found - the cookie was not written.
			cookieWriteFailure = true;	
		}		
		
		if(cookieWriteFailure)
		{
			// The geo cookie could NOT be stored in the users cookies.
			// Store it in Magentos session instead.	
			jQuery.post("http://www.politix.com.au/politixajax/geo/setsession", {}, function(data) {}, "");
		}
	}
	
	/***
	* This is the main entry point.  This method shows the GeoIP modal if the user is NOT in Australia has not already seen 
	* it before and made a selection.
	*/
	this.checkShowModal = function()
	{
		// Find out which country the visitor is in
                //try{
		var country_code = geoip_country_code();	// Loaded via app/code/local/Politix/Ajax/Controllers/GeoController geojsAction
               // }
               // catch(err){
                    
               // }
		// Delete this next line later - it nukes any previous cookies.
		//jQuery.cookie(cookieName, '', { expires: -365, path: '/', domain: 'politix.com.au'});
		//country_code = "ZZ";  // Delete this later.
		
		// Find out if the user has already been shown the geomodal popup.
		var cookieVal = jQuery.cookie(cookieName);
		
		if(cookieVal == null)
		{
			// The user has NOT seen the cookie.
			
			// If the visitor is NOT in Australia, show the warning popup.
			if(country_code != "AU")
			{
				// Make a call to the server to see if the user has already seen this cookie or not.
				jQuery.post("http://www.politix.com.au/politixajax/geo/checksession", {}, function(hasSeen) 
				{
					if(hasSeen == "NO")
					{
						self.showGeoModal();
						
						// Trap the event when the user taps on the "I AM IN AUSTRALIA" button
						jQuery("#geoInAustralia").click(function(e)
						{
							e.preventDefault();
							
							// Set the cookie so the warning will not be shown on subsequent page loads
							// This cookie is set for a year so the setting is remembered.
							jQuery.cookie(cookieName, 'INAUSTRALIA', { expires: 365, path: '/', domain: 'politix.com.au'});
							
							self.checkCookie();
							
							// Close the modal
							self.closeGeoModal();
						});
						
						// Trap the event when the user taps on the "CONTINUE" button
						jQuery("#geoContinue, .geoContinue").click(function(e)
						{
							e.preventDefault();
							
							// Set the cookie so the warning will not be shown on subsequent page loads
							// but WILL show if the browser is shut and reopened (this is a session cookie)
							jQuery.cookie(cookieName, 'CONTINUE', { expires: null, path: '/', domain: 'politix.com.au'});
							
							self.checkCookie();
							
							// Close the modal
							self.closeGeoModal();
						});		
					}			
					
				}, "");				
			}   
		}
	}		
}
