$(document).ready(
	function() {
		
		//light box gallery
		$("a.img_gallery").lightBox();
		
		//
		$('#login').css({
			width: '0'
		});
		//
		$('#search a').click(
			function() {
				if ($('#search a span').html() == '-') {
					$('#login').animate({
						width: '0'
					});
					$('#search a span').html('+');
				}
				else {
					$('#login').animate({
						width: '275px'
					});
					$('#search a span').html('-');
				}
			}
		);
		
		hashTab = document.location.hash.substr(1, document.location.hash.length);
		if ( hashTab ) {
			$("#activeTab").attr("id", "");
			$('[id2='+hashTab+']').attr("id", "activeTab");
		}
		
		//tab coloring temp fix
		$("li a[rel='active_FIT_facts']").attr("id", "orange_tab");
		$("li a[rel='active_FIT_facts']").parent().addClass("inactiveRightOrange");

		$("li a[rel='body_glo-how-to']").attr("id", "pink_tab");
		$("li a[rel='body_glo-how-to']").parent().addClass("inactiveRightPink");
		
		
		
		// Clear input text
		$('#header input').focus(clearHeaderFields);
		$('#header input').blur(clearHeaderFields);
		// Set selects on side nav
		setSideNav();
		// Check if tabs exist
		if ($('#activeTab').find('a').attr('rel') != undefined) {
			// Order tabs
			orderTabs();
			//orderEnrollmentTabs();
			// Load content
			loadTabContent();
		}
		// Start setting up product tabs
		$('#tabs li').click (
			function() {
				
				
				//old method
				$('#tabs li').attr('id', '');
				// Set active tab
				$(this).attr('id', 'activeTab');

				
				// Deactivate last active tab
				$('#tabs li').attr('class', 'inactive');
				//if first tab
				$('#tabs li:first-child').attr('class', 'inactive1');
				
				
				// Set active tab
				$(this).attr('class', 'activeTab');
				//if first tab
				if($(this).is('#tabs li:first-child')) {
					$('#tabs li:first-child').attr('class', 'activeTab1');
				}
				
				// Set tabs in correct order
					
				orderTabs($(this));
				// Load content
				loadTabContent($(this));
			}
		);

		
		$('div#slideshow').cycle({ 
		    fx:     'fade', 
		    speed:  1500,
		    timeout: 6000
		});		

	}
);
//
function loadTabContent() {
	// Hide pages
	$('#productDesc').hide();
	$('#product-image').show();
	//
	if ($('#activeTab').find('a').attr('rel') == 'body_how-to') {
		//
		$('#product-image').hide();
	}
	// Load first link of content in products
	var oldURL = $('#activeTab').find('a').attr('rel').split('_');
	var newURL = baseURL + '/products';
	// Loop through and make URL
	var c = 0;
	for (var i = 0; i < oldURL.length; i ++) {
		if (oldURL[i] != '') {
			newURL = newURL + '/' + oldURL[i];
			c = i;
		}
	}
	newURL = newURL+'?'+oldURL[c]+'=1&ajax=1';
	//alert(newURL);
	// Load the content
	$.ajax(
		{
			method: 'get',
			url: newURL,
			beforeSend: function() {
				$('#loading').show(1);
			},
			success: function(html) {
				$('#loading').hide(1,
					function() {
						$('#productDesc').html(html).show(1,
							function() {
								
								sIFRactivate();
							}
						);
					}
				);
			}
		}
	);
}
//
function orderTabs(index) {
	// Set variables
	var index = $('#tabs li').index($('#activeTab'))
	var length = $('#tabs li').length;
	// Remove styles
	$('#tabs a').removeClass('activeLeft activeRight activeFirst activeLast inactiveLeft inactiveRight inactiveFirst inactiveLast shadowLeft shadowRight');
	$('#tabs li').removeClass('activeLeft activeRight activeFirst activeLast inactiveLeft inactiveRight inactiveFirst inactiveLast shadowLeft shadowRight');
	// Set tabs in correct order
	$('#tabs').find('li').each(
		function(i) {
			// Less than active
			if (i < index) {
				// If first tab
				if (i == 0) {
					$(this).addClass('inactiveFirst');
					$(this).find('a').addClass('shadowRight');
				}
				else {
					$(this).addClass('inactiveLeft');
					$(this).find('a').addClass('shadowRight');
				}
			}
			// Greater than active
			else if (i > index) {
				// If last tab
				if (i == (length - 1)) {
					$(this).addClass('inactiveLast');
					$(this).find('a').addClass('shadowLeft');
				}
				else {
					$(this).addClass('inactiveRight');
					$(this).find('a').addClass('shadowLeft');
				}
			}
			// Equal to active
			else {
				// If first tab
				if (i == 0) {
					$(this).addClass('activeRight');
					$(this).find('a').addClass('activeFirst');
				}
				// If last tab
				else if (i == (length - 1)) {
					$(this).addClass('activeLast');
					$(this).find('a').addClass('activeLeft');
				}
				else {
					$(this).addClass('activeRight');
					$(this).find('a').addClass('activeLeft');
				}
			}
		}
	);
}
function orderEnrollmentTabs(index) {
	alert("here");
	// Set variables
	var index = $('#enrollment_nav li').index($('#activeTab'))
	var length = $('#enrollment_nav li').length;
	// Remove styles
	$('#enrollment_nav a').removeClass('activeLeft activeRight activeFirst activeLast inactiveLeft inactiveRight inactiveFirst inactiveLast shadowLeft shadowRight');
	$('#enrollment_nav li').removeClass('activeLeft activeRight activeFirst activeLast inactiveLeft inactiveRight inactiveFirst inactiveLast shadowLeft shadowRight');
	// Set tabs in correct order
	$('#enrollment_nav').find('li').each(
		function(i) {
			// Less than active
			if (i < index) {
				// If first tab
				if (i == 0) {
					$(this).addClass('inactiveFirst');
					$(this).find('a').addClass('shadowRight');
				}
				else {
					$(this).addClass('inactiveLeft');
					$(this).find('a').addClass('shadowRight');
				}
			}
			// Greater than active
			else if (i > index) {
				// If last tab
				if (i == (length - 1)) {
					$(this).addClass('inactiveLast');
					$(this).find('a').addClass('shadowLeft');
				}
				else {
					$(this).addClass('inactiveRight');
					$(this).find('a').addClass('shadowLeft');
				}
			}
			// Equal to active
			else {
				// If first tab
				if (i == 0) {
					$(this).addClass('activeRight');
					$(this).find('a').addClass('activeFirst');
				}
				// If last tab
				else if (i == (length - 1)) {
					$(this).addClass('activeLast');
					$(this).find('a').addClass('activeLeft');
				}
				else {
					$(this).addClass('activeRight');
					$(this).find('a').addClass('activeLeft');
				}
			}
		}
	);
}
//
function clearHeaderFields() {
	//
	var element = $(this);
	var default_value = '';
	//
	switch(element.attr('name')) {
		//
		case 'loginUsername':
			default_value = 'username';
		break;
		//
		case 'searchInput':
			default_value = 'search';
		break;
		//
		case 'loginPassword':
			return true;
		break;
	}
	//
	if (element.attr('value') == default_value) {
		element.attr('value', '');
	}
	//
	else if (element.attr('value') == '') {
		element.attr('value', default_value);
	}
}
//
function sIFRactivate() {
    if (typeof(sIFR) == "undefined") {
        return;
    }
	//
	
	sIFR.replace(scalapro_bold, {
		selector: '#middle .title h1',
		css: '.sIFR-root { color: #ffffff; font-weight: bold; }',
		wmode: 'transparent',
		offsetTop: 18,
		tuneHeight: 18
	});
	//
	sIFR.replace(scalapro_bold, {
		selector: '#middle .left-bar h5, .product-list h4',
		css: '.sIFR-root { color: #45525a; text-transform: uppercase; font-size: 10px; }',
		wmode: 'transparent'
	});
	//
	sIFR.replace(scalapro_bold, {
		selector: '#middle h2',
		css: '.sIFR-root { color: #384f5a; }',
		wmode: 'transparent'
	});
	
}

//
function setSideNav() {
	// Hide sub-nav
	$('.left-bar ul ul').hide();
	// Set up vars
	var compareURL = location.pathname;
	
	var tiers = compareURL.split('/');
	
	var compareTieredURL = new String('/' + tiers.slice(1, (tiers.length - 1))).replace(/,/g, '/');
	
	
	if(tiers.length > 4 ) {
		
		compareTieredURL = new String( '/' + tiers.slice(1,2) + '/' +tiers.slice(2,3) );
		
	}
	
	// Find selected links
	$('.left-bar a').each(
		function() {
			// Add class and show siblings 
			
			if ($(this).attr('href') == compareURL || $(this).attr('href') == compareTieredURL) {
				
				$(this).addClass('select');
				$(this).siblings().show();
				
			}
			if( $(this).attr('href') == compareURL ) {
				
				$(this).parents("[id^='p_menu']").children("a").addClass('select');
				$(this).parents("[id^='p_menu']").children("a").siblings().show();
				
			}
		}
	);
	// added by rashid
	$("span.parent_add_child").hide();
	$("span.child_edit_url").hide();
	$("span.child_edit_avail").hide();
}


