// override the localstorage by using the parameter m, to be used in vanities
var modemState = $.getUrlVar('m');  

if(modemState && modemState === 's') {
	localStorage.setItem('modemSelected', 'steve');
} else if(modemState && modemState === 'v') {
	localStorage.setItem('modemSelected', 'valerie');
} else if(modemState && modemState === 'vincent') {
	localStorage.setItem('modemSelected', 'vincent');
} 

$(document).ready(function() {
	var modemSelectedStorage = localStorage.getItem('modemSelected');

	// display beginning modem choices if not in localstorage, otherwise display menu/content
	if(modemSelectedStorage === null ) {
		$("#modemChoice").fadeIn("fast");
	} else {

		$("#menuAndContent").show();
		menuPersistence();

		if (modemSelectedStorage==="valerie") {
			$("body").addClass("valerie");
		} else if (modemSelectedStorage==="vincent") {
			$("body").addClass("vincent");
		} else {
			$("body").addClass("steve");
		}
	}
	
	$('.modemStart').click(function(e){
	 	var $this = $(this);
	    var modem = $this.data('modem');
	 	// set memory for modemSelected
	   	localStorage['modemSelected']=modem;
	   	if (modem==="valerie") {
			$("body").addClass("valerie");
		} else if (modem==="vincent") {
			$("body").addClass("vincent");
		} else {
			$("body").addClass("steve");
		}
	   	// show/hide appropriate content, set up links
	   	$("#menuAndContent").show();
		$("#modemChoice").hide();

		menuPersistence();

	   	e.preventDefault();
	   	
	});
	
	$('.modemChange').click(function(e){
		localStorage.removeItem('modemSelected');
		$("#modemChoice").fadeIn("fast");
		$("#menuAndContent").hide();
		$("body").removeClass("steve valerie vincent");
		e.preventDefault();
	});
	
	
});

// called on index page
function menuPersistence() {
    var anchor = $.getUrlVar('topic');   // used on the main support pages, in the bread crumbs
    var tabMemory;
    var lang = getLang();
    var prov=getProvince().toLowerCase();
    var internetTabChoice = localStorage.getItem('internetTabSelected');
    // depending on if user comes from header links, set the tabMemory, either to default, or to whatever it is
    if(typeof anchor === "undefined"){
        if(internetTabChoice === null){
			localStorage.setItem('internetTabSelected', 'getting_started');
			tabMemory = localStorage.getItem('internetTabSelected');
        }else if(internetTabChoice !== null){
			tabMemory = localStorage.getItem('internetTabSelected');
        }
    }else if(typeof anchor !== "undefined"){
        tabMemory = anchor;
    }


    // hide all right side content to start
    $("#faq_wrapper .sector").hide();

    // load content based on either the memory, or the header links
    jQuery("#" + tabMemory).show();

	jQuery("#" + tabMemory).show();
    $("#faq_menu div[role=\"tab\"]").removeClass("active");
    jQuery("#faq_menu div[role=\"tab\"] span[rel='" + tabMemory + "']").parent().addClass("active");
	
	var isOpen;
    //faq menu click function
    $("#faq_menu div[role=\"tab\"]").click(function() {

        //add and remove the 'active' class
        $("#faq_menu div[role=\"tab\"]").removeClass("active");
        $(this).addClass("active");

        //hide(); all content tabs
        $(".sector").hide();
        //go inot the faq menu and look for the title attribute
        var content_show = $(this).find("span").attr("rel");

        // find the id with the same name as title and show();
        $("#"+content_show).show();

        //.load show regionalized items
        jQuery(".prov-"+prov).show()

        // omniture tracking
        // var content_text = $(this).find("a").text().substring(0,74).replace(/[^a-z0-9\s]/gi, '');
        s_track('customlink','Support:LeftNav:'+content_show);

        //Local storage for selected tabs for memory
        localStorage['internetTabSelected']= content_show;

	
	    if ($(window).width() < 800) {
			if (isOpen == 'true') { 
				$("#faq_menu div[role=\"tab\"]:not(.active)").slideUp();
				isOpen = '';
			} else {
				$("#faq_menu div[role=\"tab\"]:not(.active)").slideDown();
				isOpen = 'true';
			}
		} // end if width

        return false;

    });
	
 

    // omniture tracking for all (non-accordion) links in the right side content
    $("#faq_wrapper a.omnitureTracking").click(function() {

        var content_id = $(this).closest(".sector").attr("id");
        var tab_link_title = $("#faq_menu span[rel='"+content_id+"']").attr("rel").replace(/[^a-z0-9\s]/gi, '');
        var content_text = $(this).text();

        s_track('customlink','Support:TV:'+tab_link_title+':'+content_text);

    });
    
}
