
$().ready(function(){  
    checkAnchor();  
});  

var currentAnchor = null;  
var currentPath = null;  
var query = null;

// Check and send anchor to affiliate-handler
function checkAnchor(){  
	// get anchor from URL
	currentAnchor = document.location.hash;
	// get current path from URL
	currentPath = document.location.pathname;
	
	if (currentAnchor)  {
		//Creates the  string callback. This converts the url URL/#DDDDDD-DDDD in URL/?trackidDDDDDD-DDDD
		var splits = currentAnchor.substring(1).split('-');  
		//Get the section  
		var trackid = splits[0];  
		var subid = splits[1]; 
		var aview = splits[2]; 
		
		// RegExp expression
		if (trackid.match(/^[0-9]+$/g)) {
			//Create the params string  
			query = "trackid" + trackid;
		}
		
		if (query && subid) {
			query = query + "&subid=" + subid;
		}
		
		if (query && currentPath) {
			query = query + "&pathname=" + currentPath.substring(1);
		}
		if(query && aview) {
			query = query + "&aview=" + aview;
		}
		
		aview_page_refreshed = $.cookie('aview_page_refreshed');
		
		//Send the petition  
		if (query && !aview_page_refreshed) {
			$.get("/", query, 
				function(data) {
					aview_from_cookie = $.cookie('aview');
					// if cookie is enabled and page need refresh for displaying alternate content
				    if(aview_from_cookie && !aview_page_refreshed) {
				    	aview_page_refreshed = $.cookie('aview_page_refreshed', true);
						location.reload();
				    }
				}
			);
		}
		
	}
}
