/*
COMPONENTS REQUIRED:
Add this to html/view or layout

<script src="http://static.hazecash.com/shared/scripts/nats_referrer.js" type="text/javascript"></script>
*/
jQuery('document').ready(function() {
	nrefLoadPlugins();
});

function nrefLoadPlugins() {
    if(typeof jQuery.cookie == 'undefined' || typeof jQuery.url == 'undefined')
	{
        jQuery.getScript('http://static.hazecash.com/shared/scripts/jquery/plugins/cookie/jquery.cookie.js?1', function() {
            jQuery.getScript('http://static.hazecash.com/shared/scripts/jquery/plugins/url/2.0/jquery.url.js?1', function() {
                createNatsReferrer();
            });
        });
	}
    else createNatsReferrer();
}

function createNatsReferrer() {
	// get the domain
	var host = jQuery.url().attr("host");
	var hostParts = host.split(".");
	
	var numHostParts = hostParts.length;
	
	if (numHostParts < 2) {
		return;
	}
	var domain = hostParts[numHostParts-2]+'.'+hostParts[numHostParts-1];
	
	// set our referring URL cookie if it doesn't exist and is from a different domain
	
	if (document.referrer && document.referrer.indexOf('http://' + host) == -1 && typeof jQuery.cookie('nats_http_referrer') != 'string')
	{
		jQuery.cookie('nats_http_referrer', document.referrer,{ domain: '.'+domain, path: '/' });
	}
		
	// get redirect path from the NATS redirect cookie
	var cookies = jQuery.cookie();
	var natsRedirectCookie = 'nats_redirect';
	var cookieFound = false;

	for(var name in cookies) {
		if(name == natsRedirectCookie){
			cookieFound = true;
			var natsRedirectPath = jQuery.cookie(name);
			break; //exit loop
		}
	}
	
	if (!cookieFound) {
		return;
	}
}
