$(function () {
	
	var indexLoading = '<div class="indexLoading"><img src="/css/contimg/loading333.gif" alt="loading" /><span>Loading catalogue index<br />Please wait</span></div>';
	$('#index').empty().html(indexLoading);
	$('#index').load(url+'/scripts/getindex.php?url='+loadUrl+'&element=categoryIndex','',indexLinks);

	var mainLoading = '<div class="mainLoading"><img src="/css/contimg/loadingfff.gif" alt="loading" /><span>Loading...<br />Please wait</span></div>';
	$('#main').empty().html(mainLoading);
	
	if (location.pathname.match(/n8,2.html/i)) {  //on a-z index page
		$('#stylesheet').attr('href', '/css/pl-product_az_index.css');
		$('#main').load(url+'/scripts/getindex.php?url='+loadUrl+'/n8,2.html&element=mainContent','',catLinks);
	} else if (location.pathname.match(/catid/i) && location.pathname.match(/subcatid/i)) {  //on a subcategory page
		$('#stylesheet').attr('href', '/css/pl-productindex.css');
		var pathStr = getPath();
		$('#main').load(url+'/scripts/getcontent.php?url='+loadUrl+'/'+pathStr+'&element=productItem','',catLinks);
	} else if (location.pathname.match(/catid/i)) { // on a category main page
		var pathStr = getPath();
		$('#main').load(url+'/scripts/getcategory.php?url='+loadUrl+'/'+pathStr+'&element=mainContent','',catLinks);
	} else {
		$('#stylesheet').attr('href', '/css/pl-product_az_index.css');
		$('#main').load(url+'/scripts/getindex.php?url='+loadUrl+'/n8,2.html&element=mainContent','',catLinks);
	}
	
	function getPath() {
		pathArray = window.location.pathname.split( '/' );
		pathStr = pathArray[pathArray.length-1];
		catpath = pathStr.split(',');
		window.cat_id = catpath[1].replace(/catid=/i,'').replace(/.html/i,'');
		return pathStr;
	}
	
	function indexLinks() {
		$('#index a').each( function () {
			var lnk = $(this).attr('href');
			if ( lnk.match('catid='+window.cat_id)) {
				$(this).addClass('active');
				window.col = $(this).css('background-color');
			}
		});
		$('#categoryIndex ul').prepend('<li id="indexIndex"><a href="/n8,2.html" title="Product A-Z Index">Product A-Z Index</a></li>');
		$('#categoryIndex a').each( function() {
			var oHref = $(this).attr('href').replace(url,'');
			var pHref = oHref.replace('/products.php','');
			$(this).attr('href', '/products.php'+pHref);
		});
		imageLinks();
	}
	
	function catLinks() {
		$('#mainContent a').not('#letterIndex a').each( function() {
			var oHref = $(this).attr('href').replace(url,'');
			var pHref = oHref.replace('/products.php','');
			$(this).attr('href', '/products.php'+pHref);
		});
		$('.indexBackToTop a').each( function() {
			$(this).attr('href', '#');
		});
		imageLinks();
		$('#pageTitle').css('background-color', window.col);
	}
	
	function imageLinks() {
		$('#categoryIndex img, #mainContent img').each( function() {
			var oSrc = $(this).attr('src').replace(url,'');
			$(this).attr('src', loadUrl+'/'+oSrc); 
		});
	}
	
});