$(document).ready(function() {
    
    $("#sf_products dl").hide();
    var r = "#"+$("#dietary_needs input:checked").val();
    $(r).show();
    
    
    $("#dietary_needs input").click(function() {
        var e = "#"+$(this).val();
        $("#sf_products dl").hide();
        $(e).show();
    });



    // member home JS
    var mh = (document.getElementById("member_home") || document.getElementById("pallette"));
    if (mh) {
	    $("dl#profile, dl#pal_profile").accordion( {
	        header: "dt",
	        autoheight: false
        });
        
        $("#profile dt, #pal_profile dt").click(function() {
        
            if ($(this).attr("id") == "tab_fitness") {
                $("#profile, #pal_profile").addClass("end");
            } else {
                $("#profile, #pal_profile").removeClass("end");
            }
            
        });
        
        
        $("#profile dt a, #pal_profile dt a").click(function() {
            var u = $(this).attr("href");
            window.location.href = u;
        });
        $("#body_container").addClass("member_home");
    }   
    
    
     // edit profile tabs
     $("#user_profile dd").hide();
     $("#user_profile dd").eq(0).show();
     $("#user_profile dt").eq(0).addClass("current_tab");
     placeButtons();
     
     $("#user_profile dt").each(function(i) {
        var x = Number($("#user_profile dt").css("left").substr(0, 1));
        var l = (i * 150);
        $(this).css("left",l);
        $(this).click(function() {
            var dd = $(this).next("dd");
            $("#user_profile dd").hide();
            $(dd).show();
            $("#user_profile dt").removeClass("current_tab");
            $(this).addClass("current_tab");

            placeButtons();

        });
     });
     
     $("#user_profile a[href='#account_info']").click(function() {
        $("#user_profile dd").hide();
        $("#user_profile dd").eq(1).show();
        $("#user_profile dt").removeClass("current_tab");
        $("#user_profile dt").eq(1).addClass("current_tab");

        placeButtons();

        return false;
     });
     
     
     var err = document.getElementById("ctl00_bcr_FormError");
     if (err) {
        if ($("#ctl00_bcr_FormError").val() != "") { 
            $("#user_profile dd").hide();
            $("#user_profile dd").eq(1).show();
            $("#user_profile dt").removeClass("current_tab");
            $("#user_profile dt").eq(1).addClass("current_tab");
            
            placeButtons();
        }
     }
     
     
     
});

function placeButtons() {

    if ($(".ie6").css("display") == "block") {
        $(".submit_buttons").hide();
        $(".submit_buttons").css("bottom","7px");
        $(".submit_buttons").show();
    }

}

function toggleMenu(l, e, state) {
	var obj = document.getElementById(l)
	document.getElementById(e).style.visibility = state;
	if (state == "visible") {obj.className = "hover";} else { obj.className = ""; }
}

function changeNavImage(e, img) {
	if (img != "") {
		img = "http://www.slim-fast.com/images/"+img;
		console.log(img);
		document.getElementById(e).style.backgroundImage = img;
	}
}



/* Forum Overlay */

function doLinkPopUp(arg){
	document.getElementById('overfade').style.display = "block";
	document.getElementById(arg).style.display = "block";
	centerElement(document.getElementById(arg));
	$("#body_container").attr({ disabled: "disbled" });
}


function doLinkPopUpCancel(arg){
    document.getElementById('overfade').style.display = "none";
	document.getElementById(arg).style.display = "none";
	$("#body_container").attr({ disabled: "" });
}

function centerElement(elem) {
    var viewport = getViewportDimensions();
    var left = (viewport.width == 0) ? 50 : parseInt((viewport.width - elem.offsetWidth) / 2, 10);
    var top = (viewport.height == 0) ? 50 : parseInt((viewport.height - elem.offsetHeight) / 2, 10);

    elem.style.left = left + 'px';
    elem.style.top = top + 'px';

    viewport, left, top, elem = null;    
}
function getViewportDimensions() {
    var intH = 0, intW = 0;
    
    if(self.innerHeight) {
       intH = window.innerHeight;
       intW = window.innerWidth;
    } 
    else {
        if(document.documentElement && document.documentElement.clientHeight) {
            intH = document.documentElement.clientHeight;
            intW = document.documentElement.clientWidth;
        }
        else {
            if(document.body) {
                intH = document.body.clientHeight;
                intW = document.body.clientWidth;
            }
        }
    }

    return {
        height: parseInt(intH, 10),
        width: parseInt(intW, 10)
    };
}

function avatarPopup(){
    window.open("UpdateAvatar.aspx" , "UploadAvatar" , "top=300px, left=350px, width=526px, height=300px, scrollbar=no, toolbar=no, directories=no, status=no, menubar=no, location=no, resizable=no");
}

function linkMealPlan(){
    window.location = "/User/mealPlan.aspx";
}

function linkFitnessPlan(){
    window.location = "/User/fitnessPlan.aspx";
}

function overlayTrack(){
    //Google Analytics
    pageTrack('/profile/configure');
    
    //DoubleClick Spotlight Tag
    var axel = Math.random()+"";
    var a = axel * 10000000000000;
    document.write('<IFRAME SRC="https://fls.doubleclick.net/activityi;src=2078215;type=slimf198;cat=sfcon680;ord='+ a + '?" WIDTH=1 HEIGHT=1 FRAMEBORDER=0></IFRAME>');
    
    //RealMedia B3 Tag
    document.write ('<IFRAME SRC="http://ad.doubleclick.net/adi/N3352.b3247/B3365710;sz=160x600;click0=https://oasn04.247realmedia.com/RealMedia/ads/click_lx.ads/Slim-Fast/ConfirmPageNew/L9/1166148334/x01/USNetwork/BCN9010322_01_Slimquick_CC_160/Slimquick_CC_160.html/782b554262306c7271664141426f7a6c?;ord=1166148334?" WIDTH=160 HEIGHT=600 MARGINWIDTH=0 MARGINHEIGHT=0 HSPACE=0 VSPACE=0 FRAMEBORDER=0 SCROLLING=no BORDERCOLOR=');
    document.write ("'");
    document.write ('#000000');
    document.write ("'");
    document.write ('>\n');
    document.write ('<SCRIPT language=');
    document.write ("'");
    document.write ('JavaScript1.1');
    document.write ("'");
    document.write (' SRC="http://ad.doubleclick.net/adj/N3352.b3247/B3365710;abr=!ie;sz=160x600;click0=https://oasn04.247realmedia.com/RealMedia/ads/click_lx.ads/Slim-Fast/ConfirmPageNew/L9/1166148334/x01/USNetwork/BCN9010322_01_Slimquick_CC_160/Slimquick_CC_160.html/782b554262306c7271664141426f7a6c?;ord=1166148334?">\n');
    document.write ('</SCRIPT>\n');
    document.write ('<NOSCRIPT>\n');
    document.write ('<A HREF="https://oasn04.247realmedia.com/RealMedia/ads/click_lx.ads/Slim-Fast/ConfirmPageNew/L9/1166148334/x01/USNetwork/BCN9010322_01_Slimquick_CC_160/Slimquick_CC_160.html/782b554262306c7271664141426f7a6c?http://ad.doubleclick.net/jump/N3352.b3247/B3365710;abr=!ie4;abr=!ie5;sz=160x600;ord=1166148334?">\n');
    document.write ('<IMG SRC="http://ad.doubleclick.net/ad/N3352.b3247/B3365710;abr=!ie4;abr=!ie5;sz=160x600;ord=1166148334?" BORDER=0 WIDTH=160 HEIGHT=600 ALT="Click Here"></A>\n');
    document.write ('</NOSCRIPT>\n');
    document.write ('</IFRAME>');
}