// JavaScript Document
var siteURL = "http://" + top.location.host.toString();
var hashLoaded=false;

function viewLog(message){

//$('#errorMessage').html(message+'<br/>'+$('#errorMessage').html());
	
}

function hashizeLinks() {
	//viewLog('haslinks called');
	$("a[href^='"+siteURL+"'], a.leftFeatureLink, #breadCrumbs a, a.cartLink,a.cart_menu, a.storeFeatureLink, #browse_by option, .randomPickAlbum a, a.ajaxMe")
	.not('.gigpress-calendar-links-inner a')
	.not('a.internalLink')
	.not('a.downloadTrackLink')
	.not('.gigpress-related-show li a')
	.not('a.gigpress-rss')
	.not('a.gigpress-ical')
	.not('a.catalogButton').each(function() {
					 
	/* 	
		var queryString=this.href;
		var queryString2=queryString.split("\?");
		
		if(queryString2.length>1){
			var qs='?'+queryString[1];
		}else{
			var qs='';	
		}
		*/
		$el = $(this);
		$el.addClass('internalLink');
		
		// Hack for IE, which seemed to apply the hash tag to the link weird
		
		if ($.browser.msie) {
		   $el.attr("href", "/#/" + this.pathname).attr("rel", "internal");
		} else {
		   $el.attr("href", "#" + this.pathname).attr("rel", "internal");
		}
		
	});
	
}

function startLogo(){
	$('#logo img.title').attr('src','/wp-content/themes/trup/images/logo.loading.gif');
}

function stopLogo(){
	$('#logo img.title').attr('src','/wp-content/themes/trup/images/logo.gif');
	$('body').scrollTop(0);
	$('html').scrollTop(0);
}

var lasturl="";	//here we store the current URL hash

function checkURL(hash){
	
	if(!hash) hash=window.location.hash;
	//if no parameter is provided, use the hash value from the current address
    /*
	if (window.console && window.console.info) {
			console.info(hash);		
	}
	*/
	if(hash != lasturl){	// if the hash value has changed
		lasturl=hash;	//update the current hash
		
		loadPage(hash);	// and load the new page
	}
}

function setupFormStyle(){

	jQuery("select, input:checkbox, input:radio, input:file").uniform();
	
}

function loadPage(url)	//the function that loads pages via AJAX
{
	viewLog('loadPage '+url);
	//alert('load page called'+url);
	url=url.replace('#','');	//strip the #page part of the hash and leave only the page number
	startLogo();
	URL = url + " #inside";
	$("#content").load(URL, function() {
		setupFormStyle();
		$('body').scrollTop(0);
          // $el.addClass("current_link").parent().addClass("current_page_item");
		stopLogo();
        hashizeLinks();   
		storeSetup();
		
		if(url=='/'){
			startTicker();	
		}
		jQuery('#content').show();
    });

}

$(document).ready(function(){	//executed after the page has loaded
	viewLog('document ready');
	//setupFormStyle();
	storeSetup();
	var $mainContent = $("#content"),
        URL = '',
        siteURL = "http://" + top.location.host.toString(),
        $internalLinks = $("a[href^='"+siteURL+"']"),
        hash = window.location.hash,
        $ajaxSpinner = $("#ajax-loader"),
        $el, $allLinks = $("a");
	
	hashizeLinks();
	checkURL();	//check if the URL has a reference to a page and load it
	

	
	$("a.internalLink").click(function (){	//traverse through all our navigation links..
		viewLog('internalLink clicked');
		$(".current_page_item").removeClass("current_page_item");
       	$('a.current_link').removeClass("current_link");
		$(this).addClass("current_link").parent().addClass("current_page_item");
		//checkURL(this.hash);
		//.. and assign them a new onclick event, using their own hash as a parameter (#page1 for example)
		//return false;
	});

	setInterval("checkURL()",250);
	//check for a change in the URL every 250 ms to detect if the history buttons have been used
	
});


