function activateMainNav(self)
{
	if(!self.length)
		return;
		
	if(!self.hasClass('first'))
		self.prev(0).children(0).attr('src', '/images/to_active.jpg');
	
	var image = self.children(0).children(0);
	setActiveImage(image);
	
	if(!self.hasClass('last'))
		self.next(0).children(0).attr('src', '/images/from_active.jpg');
}

function activateToolsMenu(self)
{
	if(!self.length) 
		return;

	var image = self.children(0).children(0);
	setActiveImage(image);
}

function setActiveImage(image)
{
	if(!image.length)
		return;
		
	var current_src = image.attr('src');
	var current_name = current_src.substring(0, current_src.length - 4);
	var new_name = current_name + '_active' + current_src.substring(current_src.length - 4);
	image.attr('src', new_name);
}

function setInactiveImage(image)
{
	if(!image.length)
		return;

	current_src = image.attr('src');
	current_name = current_src.substring(0,current_src.length - 11);
	new_name = current_name + current_src.substring(current_src.length - 4);
	image.attr('src', new_name);
}

$(document).ready(function() {
    $('#header .nav').find('.active').prev().addClass('noBg');

    $('#container .menu').find('.active').prev().addClass('noBg');

    Cufon.replace('#header .nav .container', {
        fontFamily: 'VAG Rounded Std Light'
    });

    Cufon.replace('h2,h3,h4,#container h5,h6', { fontFamily: 'VAG Rounded Std', hover: true });
    Cufon.replace('#container h1', { fontFamily: 'VAG Rounded Std', hover: true });
    Cufon.replace('.green, .cufon, .cufon ', { fontFamily: 'VAG Rounded Std Light', hover: true });

    activateMainNav($('#header .mainNav li.active'));

    /* member section styles */

    Cufon.replace('#tblGraph tr th', { fontFamily: 'VAG Rounded Std' });
    Cufon.replace('#tblData tr th', { fontFamily: 'VAG Rounded Std' });
    Cufon.replace('#tblDataSteps tr th', { fontFamily: 'VAG Rounded Std' });
    Cufon.replace('div.frmAddNew label', { fontFamily: 'VAG Rounded Std' });

    /* vert tabs */

    var tabToShow = getParameterByName('activeTab');
    if (tabToShow.length > 0) {
        showTab('tab-' + tabToShow);
    }

    $('.vertical-tabs li').click(function() {
        var id = $(this).attr('id');
        if ($(this).hasClass('active') || !id.length)
            return false;
        var tab_id = id.substring(1, id.length);
        showTab(tab_id);
        return false;
    });

    $.each($('.vertical-tabs2 li, .vertical-tabs3 .totab'), function() {
        var id = $(this).attr('id');
        var tab_id = id.substring(1, id.length);
        $(this).children("a").attr("href", "javascript:showTab('" + tab_id + "')")
    });

    /* horizontal tabs on bmi page */

    $('ul.topMenu li').click(function() {
        var id = $(this).attr('id');
        if ($(this).hasClass('active') || !id.length)
            return false;

        var tab_id = id.substring(1, id.length);

        $('.vtab').each(function() {
            $(this).removeClass('tab-active');
        });

        $('#' + tab_id).addClass('tab-active');

        var top = $('#BMICalc').children(0); // bigBoxTops
        if (!top.length)
            return false;

        if (top.hasClass('bigBoxTopImperial')) {
            top.removeClass('bigBoxTopImperial');
            top.addClass('bigBoxTopMetric');
        }
        else {
            top.addClass('bigBoxTopImperial');
            top.removeClass('bigBoxTopMetric');
        }


        return false;
    });

    $("#BMICalc input.small").focus(
		function() {
		    var parent = $(this).parent(0).parent(0).parent(0);
		    if (parent.hasClass('error')) {
		        parent.removeClass('error');
		        parent.children('.formError').hide();
		    }
		    parent.addClass('focused');
		    if ($(this).attr('id') == 'waist') {
		        $('#BMIRightBox').css('background-image', 'url(/images/blue_box_bg_yellow.gif)');
		        $('#BMIRightBox .blueBoxTop').css('background-image', 'url(/images/blue_box_top_yellow.gif)');
		    }
		}
	).blur(
		function() {
		    var parent = $(this).parent(0).parent(0).parent(0);
		    parent.removeClass('focused');
		    if ($(this).attr('id') == 'waist') {
		        $('#BMIRightBox').css('background-image', 'url(/images/blue_box_bg.gif)');
		        $('#BMIRightBox .blueBoxTop').css('background-image', 'url(/images/blue_box_top.gif)');
		    }
		}
	);

    $("#BMICalc select").focus(
		function() {
		    var parent = $(this).parent(0).parent(0);
		    if (parent.hasClass('error')) {
		        parent.removeClass('error');
		        parent.children('.formError').hide();
		    }
		    parent.addClass('focused');
		}
	).blur(
		function() {
		    var parent = $(this).parent(0).parent(0);
		    parent.removeClass('focused');
		}
	);

    /* hover effect on subnavigation */
    $('#header .nav .text').hover(
		function() {
		    if ($(this).hasClass('active'))
		        return;
		    $(this).addClass('hover');
		    if (!$(this).prev(0).hasClass('active'))
		        $(this).prev(0).addClass('noBg');
		},
		function() {
		    if ($(this).hasClass('active'))
		        return;
		    $(this).removeClass('hover');
		    if (!$(this).prev(0).hasClass('active'))
		        $(this).prev(0).removeClass('noBg');
		}
	);

    /* hover effects on main navigation */
    $('#header .mainNav li.item').hover(
		function() {
		    if ($(this).hasClass('active'))
		        return;
		    if (!$(this).hasClass('first')) {
		        if ($(this).prev(0).prev(0).hasClass('active'))
		            new_file = 'keep_active';
		        else
		            new_file = 'to_active';


		        $(this).prev(0).children(0).attr('src', '/images/' + new_file + '.jpg');
		    }
		    if (!$(this).hasClass('last')) {
		        if ($(this).next(0).next(0).hasClass('active'))
		            new_file = 'keep_active';
		        else
		            new_file = 'from_active';

		        $(this).next(0).children(0).attr('src', '/images/' + new_file + '.jpg');
		    }

		    myself = $(this).children(0).children(0);
		    setActiveImage(myself);
		}, function() {
		    if ($(this).hasClass('active'))
		        return;

		    if (!$(this).hasClass('first')) {
		        if ($(this).prev(0).prev(0).hasClass('active'))
		            new_file = 'from_active';
		        else
		            new_file = 'no_active';

		        $(this).prev(0).children(0).attr('src', '/images/' + new_file + '.jpg');
		    }
		    if (!$(this).hasClass('last')) {
		        if ($(this).next(0).next(0).hasClass('active'))
		            new_file = 'to_active';
		        else
		            new_file = 'no_active';

		        $(this).next(0).children(0).attr('src', '/images/' + new_file + '.jpg');
		    }

		    myself = $(this).children(0).children(0);
		    setInactiveImage(myself);
		}
	);

    $('#showLabel, .howTo').hover(
		function() {
		    $('#showLabel').attr('src', '/images/show_hover.gif');
		},
		function() {
		    $('#showLabel').attr('src', '/images/show.gif');
		}
	);

    $('#hideLabel, .howTo').hover(
		function() {
		    $('#hideLabel').attr('src', '/images/hide_hover.gif');
		},
		function() {
		    $('#hideLabel').attr('src', '/images/hide.gif');
		}
	);

    $('li.login a').hover(
		function() {
		    if ($(this).children(0).attr('src').indexOf('images/login.jpg') > -1)
		        $(this).children(0).attr('src', '/images/login_hover.gif');

		    if ($(this).children(0).attr('src').indexOf('images/edit_details.png') > -1)
		        $(this).children(0).attr('src', '/images/edit_details_hover.png');

		    if ($(this).children(0).attr('src').indexOf('images/register.jpg') > -1)
		        $(this).children(0).attr('src', '/images/register_hover.gif');

		    if ($(this).children(0).attr('src').indexOf('images/logout.png') > -1)
		        $(this).children(0).attr('src', '/images/logout_hover.png');
		},
		function() {
		    if ($(this).children(0).attr('src').indexOf('images/login_hover.gif') > -1)
		        $(this).children(0).attr('src', '/images/login.jpg');

		    if ($(this).children(0).attr('src').indexOf('images/edit_details_hover.png') > -1)
		        $(this).children(0).attr('src', '/images/edit_details.png');

		    if ($(this).children(0).attr('src').indexOf('images/register_hover.gif') > -1)
		        $(this).children(0).attr('src', '/images/register.jpg');

		    if ($(this).children(0).attr('src').indexOf('images/logout_hover.png') > -1)
		        $(this).children(0).attr('src', '/images/logout.png');
		}
	);

    $("#weightTrackerBottom li a").hover(
		function() {
		    if ($(this).parent("li").hasClass("button-activity")) {
		        $(this).parent("li").removeClass("button-activity").addClass("button-activity-hover");
		    }
		    if ($(this).parent("li").hasClass("button-bmi")) {
		        $(this).parent("li").removeClass("button-bmi").addClass("button-bmi-hover");
		    }
		    if ($(this).parent("li").hasClass("button-food")) {
		        $(this).parent("li").removeClass("button-food").addClass("button-food-hover");
		    }
		    if ($(this).parent("li").hasClass("button-goals")) {
		        $(this).parent("li").removeClass("button-goals").addClass("button-goals-hover");
		    }
		    if ($(this).parent("li").hasClass("button-weight")) {
		        $(this).parent("li").removeClass("button-weight").addClass("button-weight-hover");
		    }
		}, function() {
		    if ($(this).parent("li").hasClass("button-activity-hover")) {
		        $(this).parent("li").removeClass("button-activity-hover").addClass("button-activity");
		    }
		    if ($(this).parent("li").hasClass("button-bmi-hover")) {
		        $(this).parent("li").removeClass("button-bmi-hover").addClass("button-bmi");
		    }
		    if ($(this).parent("li").hasClass("button-food-hover")) {
		        $(this).parent("li").removeClass("button-food-hover").addClass("button-food");
		    }
		    if ($(this).parent("li").hasClass("button-goals-hover")) {
		        $(this).parent("li").removeClass("button-goals-hover").addClass("button-goals");
		    }
		    if ($(this).parent("li").hasClass("button-weight-hover")) {
		        $(this).parent("li").removeClass("button-weight-hover").addClass("button-weight");
		    }

		}
	);



    /* ie 6 :hover effects on non-a elements emulator */
    if ($.browser.msie && $.browser.version == '6.0') {
        /* oddly - the margin's will change after adding loginHover class? */
        $('input.login, button.login').hover(
			function() { $(this).css('background-image', 'url(/images/form_login_hover.gif)'); },
			function() { $(this).css('background-image', 'url(/images/form_login.gif)'); }
		);

        $('input.register, button.register').hover(
			function() { $(this).css('background-image', 'url(/images/form_register_hover.gif)'); },
			function() { $(this).css('background-image', 'url(/images/form_register.gif)'); }
		);

        $('input.send, button.send').hover(
			function() { $(this).css('background-image', 'url(/images/form_send_hover.png)'); },
			function() { $(this).css('background-image', 'url(/images/form_send.png)'); }
		);

        $('input.saveChanges, button.saveChanges').hover(
			function() { $(this).css('background-image', 'url(/images/form_save_changes_hover.png)'); },
			function() { $(this).css('background-image', 'url(/images/form_save_changes.png)'); }
		);

        $('.calculateBmi').hover(
			function() { $(this).addClass('calculateBmiHover'); },
			function() { $(this).removeClass('calculateBmiHover'); }
		);

        $('.saveYourWeight').hover(
			function() { $(this).addClass('saveYourWeightHover'); },
			function() { $(this).removeClass('saveYourWeightHover'); }
		);

        $('.addAWeight').hover(
			function() { $(this).addClass('addAWeightHover'); },
			function() { $(this).removeClass('addAWeightHover'); }
		);

        $('.saveAndRegsiter').hover(
			function() { $(this).addClass('saveAndRegsiterHover'); },
			function() { $(this).removeClass('saveAndRegsiterHover'); }
		);

        $('#registerNow').hover(
			function() { $(this).addClass('registerNowHover'); },
			function() { $(this).removeClass('registerNowHover'); }
		);

        $('#toolsSave').hover(
			function() { $(this).addClass('toolsSaveHover'); },
			function() { $(this).removeClass('toolsSaveHover'); }
		);

        $('#toolsSaveAndUpdate').hover(
			function() { $(this).addClass('toolsSaveAndUpdateHover'); },
			function() { $(this).removeClass('toolsSaveAndUpdateHover'); }
		);

        $('#saveStartAndTargetWeights').hover(
			function() { $(this).addClass('saveStartAndTargetWeightsHover'); },
			function() { $(this).removeClass('saveStartAndTargetWeightsHover'); }
		);

        $('.menu li').hover(
			function() { $(this).addClass('hover'); },
			function() { $(this).removeClass('hover'); }
		);
    }

    $('#start_tracking_weight').hover(
		function() { $(this).attr('src', '/images/button_start_tracking_weight_hover.jpg'); },
		function() { $(this).attr('src', '/images/button_start_tracking_weight.jpg'); }
	);

    $('#start_food_diary').hover(
		function() { $(this).attr('src', '/images/button_start_food_diary_hover.jpg'); },
		function() { $(this).attr('src', '/images/button_start_food_diary.jpg'); }
	);

    observeButtonRollovers();

});

function hideWeight() {
	if ($('#weightTrackerClose').is(':hidden')) {
		$('#weightTrackerClose').show();
		$('#weightTrackerOpen').hide();
	}
	else {
		$('#weightTrackerClose').hide();
		$('#weightTrackerOpen').show();
	}
	return false;
} 

function observeButtonRollovers() {
	// preload any rollover images
	jo = 'img.button_hover';
	$.each ($(jo), function(i, n) {
		i_src = $(n).attr('src');
		r_src = i_src.substring(0,(i_src.length -4)) + '_hover' + i_src.substring((i_src.length -4));
		preload_img = new Image();
		preload_img.src = r_src;
	});
	
	
	// observe the images/inputs
	$(jo).bind('mouseover', function(){
		i_src = $(this).attr('src');
		i_src = i_src.replace(/_hover/, '');
		r_src = i_src.substring(0,(i_src.length -4)) + '_hover' + i_src.substring((i_src.length -4));
		$(this).attr({src: r_src});
	});
	
	$(jo).bind('mouseout', function(){
		i_src = $(this).attr('src');
		r_src = i_src.replace(/_hover/, '');
		$(this).attr({src: r_src});
	});
}
         
function showTab(i) {
 	$('.tab').removeClass('tab-active');
   	$('#'+i).addClass('tab-active');
    $('.vertical-tabs li').removeClass('active');
    $('#a' + i).addClass('active'); 
}

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}


if (!($.browser.msie && $.browser.version=="6.0"))
	document.write('<link rel="stylesheet" type="text/css" href="/styles/fonts.css" />');

