// global variable for the currently displayed feature list
var currentFeatureList;
var currentBubble = "";

function getFlashMovie(movieName) {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    return (isIE) ? window.document.forms[0][movieName] : document.forms[0][movieName];
}

// Finds all the plan activation buttons, and assigns event handlers for rollover
function findPlanButtons() {
	// set the current feature list marker to the fcc
	currentFeatureList = "plans_compare";
	
	/*
	// make a container array for the menu anchor tags
	var allMenuAnchors = document.getElementById("plans_menu").getElementsByTagName("a");
	
	// loop through the menu anchor tags, and assign the rollover effect to each
	for (i=0; i<allMenuAnchors.length; i++) {
		var thisId = allMenuAnchors[i].id;
		var thisLink = document.getElementById(thisId);
		
		// assign the event handler
		thisLink.onmouseover = menuRollover;
	}*/

    // Are we working with three tabs or 2?	
	if (document.getElementById("tab_smart"))
	{
        // find the tabs, and assign event handlers to them
	    var tabSmart = document.getElementById("tab_smart");
	    var tabSmart2 = document.getElementById("tab_smart2");
	    var tabFCC = document.getElementById("tab_fcc");
	    var tabFCC2 = document.getElementById("tab_fcc2");
	    var tabFAM = document.getElementById("tab_fam");
	    var tabFAM2 = document.getElementById("tab_fam2");
    	
    	
	    tabSmart.onclick = clickTab;
	    tabSmart2.onclick = clickTab;
	    tabFCC.onclick = clickTab;
	    tabFCC2.onclick = clickTab;
	    tabFAM.onclick = clickTab;
	    tabFAM2.onclick = clickTab;
	    
	}
	else
	{
	    var tabFCC = document.getElementById("tab2_fcc");
	    var tabFAM = document.getElementById("tab2_fam");
	
	    tabFCC.onclick = clickTab;
	    tabFAM.onclick = clickTab;
	}
	
	// Check querystring and open the correct tab.
    var tabValue = getQueryString("tab");
    if (tabValue != "")
    {
        openTab(tabValue);
    }
}

// Get the value of the var inside the querystring
function getQueryString(varValue) {
    var returnValue = "";
    loc = window.location.search.split("?");
    if(loc.length > 1){
        qsArray = loc[1].split("&");
        for (i=0;i<qsArray.length;i++) {
            varArray = qsArray[i].split("=");
            if (varArray[0] == varValue) {
                returnValue = varArray[1];
            }
        }
    }
    return returnValue;
}

// Open the provided tab
function openTab(thisTab)
{
    switch (thisTab)
    {
        case "family":
            if (document.getElementById("tab_smart"))
            {
	            document.getElementById("tab_fam").parentNode.style.display = "none";
                document.getElementById(currentFeatureList).style.display = "none";
                var tabFCC = document.getElementById("tab_fcc");
            }
            else
            {
	            document.getElementById("tab2_fam").parentNode.style.display = "none";
                document.getElementById(currentFeatureList).style.display = "none";
                var tabFCC = document.getElementById("tab2_fcc");
            }
            tabFCC.parentNode.style.display = "block";
            document.getElementById("plans_family").style.display = "block";
            currentFeatureList = "plans_family";
            break;
        case "fcc":
            if (document.getElementById("tab_smart"))
            {
	            document.getElementById("tab_fam").parentNode.style.display = "none";
                document.getElementById(currentFeatureList).style.display = "none";	
                var tabFAM = document.getElementById("tab_fam");
            }
            else
            {
	            document.getElementById("tab2_fam").parentNode.style.display = "none";
                document.getElementById(currentFeatureList).style.display = "none";
                var tabFAM = document.getElementById("tab2_fam");
            }
            tabFAM.parentNode.style.display = "block";
	        document.getElementById("plans_compare").style.display = "block";
	        currentFeatureList = "plans_compare";
            break;
        case "smartphones":
            if (document.getElementById("tab_smart"))
            {
	            document.getElementById("tab_fam").parentNode.style.display = "none";
                document.getElementById(currentFeatureList).style.display = "none";	
                
                var tabSmart = document.getElementById("tab_fam2");
                tabSmart.parentNode.style.display = "block";
	            document.getElementById("plans_smartphones").style.display = "block";
	            currentFeatureList = "plans_smartphones";
            }
            break;
        default:
            break;
    }
}

// Event handler for clicking the FCC tab
function clickTab(e) {
	// Get the event object
	if (!e) var e = window.event;
	
	// which link is this?
	var thisId = (e.target) ? e.target.id : e.srcElement.id;
    var thisLink = (e.target) ? e.target : e.srcElement;

    if (currentBubble != "") {
        currentBubble.parent().children(".bubble").hide();
        currentBubble = "";
    }

    // Hide the link we just clicked, and show the other one
    thisLink.parentNode.style.display = "none";
    
    // hide the current feature list
	document.getElementById(currentFeatureList).style.display = "none";	
    
    if (thisId.indexOf("tab_fam") != -1) {
        openTab("family");
    } else if (thisId.indexOf("tab_smart") != -1) {
        openTab("smartphones");
    } else if (thisId.indexOf("tab_fcc") != -1) {
        openTab("fcc");
    } else if (thisId.indexOf("tab2_fcc") != -1) {
        openTab("fcc");
    } else if (thisId.indexOf("tab2_fam") != -1) {
        openTab("family");
    }
    return false;
}

/*
// The rollover effect for each menu feature list
function menuRollover(e) {

    // Don't do anything if the feature compare chart is up.
	if (currentFeatureList != "plans_compare") {

	    // Get the event object
	    if (!e) var e = window.event;
    	
	    // what's the id of this link?
	    var thisElement = (e.target) ? e.target : e.srcElement;
    	
	    // Did we just touch the text on this "button"?
	    if (thisElement.nodeName == "SPAN") {
	        if (thisElement.className == "non_superdollar") {
	            var thisId = (e.target) ? e.target.parentNode.parentNode.id : e.srcElement.parentNode.parentNode.id;
	        } else {
	            var thisId = (e.target) ? e.target.parentNode.id : e.srcElement.parentNode.id;
            }
	    } else {
	        var thisId = (e.target) ? e.target.id : e.srcElement.id;
        }
    		
	    // which plan did we just roll over?  The first element in this array will tell us...
	    var idSplit = thisId.split("_");
    	
	    // No need to do anything if we're already on it...
        if (currentFeatureList != ("plans_" + idSplit[1])) {
	        // hide the current feature list
	        document.getElementById(currentFeatureList).style.display = "none";	
        	
	        // show the plan by changing the 'display' from 'none' to 'block'
	        document.getElementById("plans_" + idSplit[1]).style.display = "block";
	        currentFeatureList = "plans_" + idSplit[1];
        }
        
    }
}

// The rollover effect for each menu - called from flash
function menuRolloverFlash(thisFeatureDiv) {
    // No need to do anything if we're already on it...
    if (currentFeatureList != ("plans_" + thisFeatureDiv)) {

        if (currentBubble != "") {
	       currentBubble.parent().children(".bubble").hide();
           currentBubble = "";
        }
       
	    // hide the current feature list
	    document.getElementById(currentFeatureList).style.display = "none";	
    	
	    // show the plan by changing the 'display' from 'none' to 'block'
	    document.getElementById("plans_" + thisFeatureDiv).style.display = "block";
	    currentFeatureList = "plans_" + thisFeatureDiv;

	    var thisBubbleSet = "#" + currentFeatureList + " div.bubble_wrap a.bubble_pop";
	    var thisBubbleCloseSet = "#" + currentFeatureList + " div.bubble_wrap a.bubble_close";

	    $(thisBubbleSet).unbind("click");
	    $(thisBubbleSet)
	        .click(function(){
		        if (currentBubble != "") {
			        currentBubble.filter(":visible").fadeOut(250);
			        currentBubble = $(this).parents("p").siblings(".bubble");
		        } else {
			        currentBubble = $(this).parents("p").siblings(".bubble");
		        }
		        $(this).parents("p").siblings(".bubble").filter(":hidden").fadeIn(250);
		        return false;
        });
        $(thisBubbleCloseSet).unbind("click");
        $(thisBubbleCloseSet)
	        .click(function(){
		        $(this).parents(".bubble").filter(":visible").fadeOut(250);
		        return false;
        });

	}
}
*/
if (window.addEventListener) //DOM method for binding an event
window.addEventListener("load", findPlanButtons, false);
else if (window.attachEvent) //IE exclusive method for binding an event
window.attachEvent("onload", findPlanButtons);
else if (document.getElementById) //support older modern browsers
window.onload= findPlanButtons;