/**
 * @package    GeoGroups
 * @author     Daniel Sevcik <sevcik@webdevelopers.cz>
 * @version    $Revision: 1.0 $
 * @copyright  2011 Daniel Sevcik
 * @access     public
 */
if (!location.search.match(/nogeo/)) {
	(function($) {
		var gg=sysCommon.getCookie('gg');
		if (gg) {
			gg=gg.split(':');
			for(var i=0; i < gg.length; i++) gg[i]=gg[i].length ? gg[i].split(' ') : [];
			if (gg.length != 5) {
				//if (console && console.error) console.error("GG (corrupted): %o", gg); 
				gg=null;
			} else {
				//if (console && console.log) console.log("GG (cached): %o", gg);
			}
		}
  
		if (!gg) {
			gg=sys.fireEventShift('gg:membership', 1);
			for(var i=0; i < gg.length; i++) if (!gg[i].length) gg[i]=[]; // empty arrays are objects!
  
			var ggSerialize=[];
			for(var i=0; i < gg.length; i++) ggSerialize.push(gg[i].join(' '));		
			sysCommon.setCookie('gg', ggSerialize.join(':'), 1 / 24);
			//if (console && console.log) console.log("GG (downloaded): %o", gg); 	
		}
  
		// gg{0:all, 1:positive, 2:negative, 3:positiveAll, 4:negativeAll}
		// [["asia004", "jp111", "ca001", 8 more...], ["jp107", "jp111", "asia002"], ["jp524", "asia001"], ["EU", "jp107", "jp111", 4 more...], ["jp524", "asia001", "EU001", "asia003"]]
		$(function() {
				// Negative assertions
				if (gg[2].length) {
					$('.'+gg[2].join(',.')).not('body').remove();
					// Hide Content
					$('body'+('.'+gg[2].join(' .geoInherit,body.'))+' .geoInherit').remove();
				}
				// Warning
				$('body'+(gg[2].length && '.'+gg[2].join(' .geoWarning,body.'))+' .geoWarning').removeClass('geoWarning');
				// Remove all negative that were not applied
				gg[4].length && $('.'+gg[4].join(',.')).removeClass(gg[4].join(' '));
				// Positive assertions
				gg[1].length && $('.'+gg[1].join(',.')).removeClass(gg[1].join(' '));
  			
				// Here should be left only positive groups that you are not a member of
				$('body'+(gg[3].length && '.'+gg[3].join(' .geoWarning,body.'))+' .geoWarning').removeClass('geoWarning');			
  			
				// Notify
				$(document).trigger('geoReady', []);
				//setTimeout(function() {$(document).trigger('geoReady', []);}, 50); // On very fast computers i5 + IE7 we need to settle the changes - taskId.16792329 - 50ms timeout
			})
			})(jQuery);
}
