function getLastUrlDir()//Returns the last directory string of url. Null otherwise
{
	var winLoc = window.location.toString().split('/');//Get url string and split into array based on '/'
	for(var i = (winLoc.length - 1); i > 0; i--)//Loop through array in the opposite order and when string return it
	{
		if(jQuery.trim(winLoc[i]).length > 0)return jQuery.trim(winLoc[i].split('.')[0]);//If it is a valid string return it
	}
	return null;//Return null if nothing has been found
}

function getUrlDirArray()//Returns the last directory string of url. Null otherwise
{
	if(arguments.length == 0)
	{
		var winLoc = window.location.toString().split('/');//Get url string and split into array based on '/'
	} else {
		var winLoc = arguments[0].split('/');	
	}
	var aLoc = new Array();
	var end = null;
	for(var i = (winLoc.length - 1); i >= 0; i--)//Loop through array in the opposite order and when string return it
	{
		if(jQuery.trim(winLoc[i]).match(/veganonline./gi) != null)end = true;
		if(!end && jQuery.trim(winLoc[i]).length > 0)
		{
			aLoc.push(winLoc[i]);	
		}
	}
	return aLoc;//Return null if nothing has been found
}

jQuery(document).ready(function() {

	//Set ID of body tag
	var sId = getLastUrlDir();
	if(sId)jQuery('body').attr('id',sId);

	//Set class of first anchor tag of the main nav	
	var sWinLoc = window.location.toString();

	jQuery('#main-nav').children().each(function(){
		if(sWinLoc.indexOf(jQuery.trim(jQuery(this).attr('id'))) != -1)jQuery(this).addClass('current-section');	
	});

	var sWinLoc = getUrlDirArray();
	jQuery('#main-nav .current-section a').each(function(){
		var anchorUrl = getUrlDirArray(jQuery(this).attr('href'));	
		var count = 0;
		for(x = 0; x < sWinLoc.length; x++)
		{
			for(var i = 0; i < anchorUrl.length; i++)
			{
				if(anchorUrl[i] == sWinLoc[x])count = count + 1;	
			}
		}
		if(count == anchorUrl.length)jQuery(this).addClass('current-subsection');
	});

	// Customer Logged In OR Logged Out
	if (loggedIn) {
		jQuery(".logged-in").show();
		jQuery(".logged-out").hide();
		// Show Wholesale Area link
		jQuery('.zoneName').each(function(){
			if(jQuery.trim(jQuery(this).text()) == 'Wholesale Area') {
				jQuery("ul#main-nav li#wholesale").show();
			}
		});		
		// Update all URLS to become SSL if loggedIn
		jQuery("a[href*='veganonline.com.au'], a[href*='veganonline.prettypollution'], a[href^='/']").each(function() {
			if (!jQuery(this).hasClass("logout-link")) {
				this.href = this.href.replace(/^http:\/\/[\w.]*\.com\.*a*u*/, "https://veganonline.worldsecuresystems.com");
			}
		});			
	} else {
		jQuery(".logged-in").hide();
		jQuery(".logged-out").show();
		jQuery("ul#main-nav li#wholesale").hide();
		if(jQuery('body#logout-page').length != 0)
		{
			jQuery("a[href^='/']").each(function() {
				this.href = this.href.replace(/^https*\:\/\/[\w.]*\.com\.*a*u*/, "http://www.veganonline.com.au");
			});
		}
	}

	// Strip Breadcrumbs of Characters
	if(jQuery('#shop-breadcrumbs .cat-cumbs').length > 0){
		jQuery('#shop-breadcrumbs .cat-cumbs').html(jQuery('#shop-breadcrumbs .cat-cumbs').html().replace(/\s*:\s*/gi,''));
	}

	// Home Page Bird Bubble Animation
	setTimeout(function(){
		jQuery('img#bird-bubble-text-fade').fadeIn('slow', function() {
		});
	},2000);

	// Truncate Product Name if needed
	if(typeof jQuery.fn.jTruncate != 'undefined')
	{
		jQuery('ul.productList li h3 a').jTruncate({
			length: 38,
			minTrail: 0,
			moreText: "",
			lessText: "",
			ellipsisText: "..",
			lessAni: 2000,
			moreAni: "fast"
		});
	}
	
	// Truncate Home News Content if needed
	if(typeof jQuery.fn.jTruncate != 'undefined')
	{
		jQuery('div#news-list-small div.announcement-content').jTruncate({
			length: 40,
			minTrail: 0,
			moreText: "",
			lessText: "",
			ellipsisText: "..",
			lessAni: 2000,
			moreAni: "fast"
		});
		
		jQuery('#home-news').css('display','block');//Display news holder with truncated text
	}

	// Set CaptchaV2 input default value
	var NewsletterCaptchaV2 = "Enter code above";
	jQuery("div#newsletter-box input#CaptchaV2").val(NewsletterCaptchaV2).focus(function(){
		if(jQuery.trim(jQuery(this).val()) == 'Enter code above')jQuery(this).val('');
	}).blur(function(){
		if(jQuery(this).val() == '') 
		{
			jQuery(this).val(NewsletterCaptchaV2);
		}	
	});
	
	// Home Page Product Rotator
	if(typeof jQuery.fn.cycle != 'undefined')
	{
		var oPanels = new Array();
		jQuery('#product-rotator a').each(function(){
			var oImg = jQuery(this).find('img').clone().mouseover(function(){
				jQuery(this).css('cursor','pointer');
			}).attr('rel',jQuery(this).attr('href')).click(function(){
				window.location = jQuery(this).attr('rel');
			});
			oPanels.push(oImg);	
		});
		jQuery('#product-rotator a').remove();
		jQuery(oPanels).appendTo('#product-rotator');
		
		jQuery(function() {
			jQuery('#product-rotator').cycle({ 
				fx:    'fade',
				random:  1,
				speed:  2500
			});
		});
		
		jQuery('#product-rotator').css('visibility','visible');
	}
	
	// Remove pipe from pagination links at the bottom
	if(jQuery('.productsearchPagination').length > 0) {
		jQuery('.productsearchPagination').html(jQuery('.productsearchPagination').html().replace('|',''));	
	}
	
	jQuery('ul.productfeaturelist li:last').addClass('feature-product-last')
	jQuery("ul#main-nav li#wholesale").hide();
	
	jQuery('td.invoice-more span.view-more a').text('View More Details');


});
