﻿// WCP Global JS File!
var SectionTitle = "not Loaded";
var MainTitle = "not Loaded";


/*** UTILITIES ***/
function capitalize(string)
{
    if(string.indexOf("FAQs") < 0)//this is a huge HACK... ugh...
	{
		return string.replace(/\w+/g, function(a){return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();});
	}
	else return string;
}

function replaceSeparator(string, findThis, replaceWith)
{
	var finalString = "";
	if(string.indexOf(findThis) > -1)
	{
		//parse it!
		var splitString = string.split(findThis);
		if(splitString.length == 1) return;
		for(i=0;i<splitString.length;i++)
		{
			if(i != splitString.length - 1)
			{//not the last word in string
				finalString += splitString[i].toString() + replaceWith.toString();
			}
			else
			{//last word of string
				finalString += splitString[i].toString();
			}
		}
	}
	else
	{ 
		finalString = string;
	}
	return finalString;
}

/*** Global getters/setters ***/
function get(updateThis, url)
	{
		new Ajax(url,
            {
                method: 'get',
                update: $(updateThis)
            }
        ).request();
	}

function getPageInfo()
{
    //filePath is defined in ucNavigation.ascx [Request.FilePath in C#]
    var splitPath = filePath.split("/");
	var x = 2;
	if(filePath.indexOf("localhost") > -1)
	{
		x = 3;
	}
    //get the last string in the path [the file-name.aspx]
	var localhost = splitPath[x];
    var fileName = splitPath[splitPath.length - 1];
    //remove .aspx and anything after it
    var splitFileName = fileName.split(".aspx");
    //keep the file-name, trash the .aspx
    var parsedName = splitFileName[0];
    //if name has dashes cut out them out, and lets make some variables that we can toss around:
    var thePageName = "";
    var categoryName = "";
    var name = "";//to build the page name string with
    
    if(parsedName.indexOf("-") > -1)
    {
        //since there is dashes, lets split it
        var splitIt = parsedName.split("-");
	    for(i=0;i<splitIt.length;i++)
	    {
	        if(i>0)//get the page Name
	        {
	            if(i != splitIt.length - 1) //not the last word
	            {
	                name += splitIt[i] + " ";
	            }
	            else //last word
	            {
	                 var lastWord = splitIt[i];
	                 if(lastWord.indexOf("_") > -1)//if we are in a teritary page - ie: Tools-Wireless-Bluebook_Faq.aspx
	                 {//we need to parse out the _Faq and use that to set the tTnav
	                    var finalSplit = lastWord.split("_");
	                    var lastPageWord = finalSplit[0];
	                    var tSelected = finalSplit[finalSplit.length - 1];
	                    name += lastPageWord;
	                 }
	                 else
	                 {
	                    name += splitIt[i];
	                 }
	            }
	        }
	    }
	    thePageName = name;
	    categoryName = splitIt[0];
	}
	else
	{
	     //there isn't any "-"s
	     thePageName = categoryName = parsedName;
	}
	//out of parsed info get pageName and category name and possibly tNav selected
	this.host = localhost;
	this.pageName = capitalize(thePageName);
	this.category = capitalize(categoryName);
	if(tSelected != null)
	{
	    this.tNavSelected = replaceSeparator(tSelected, "=", " ");
	}
	this.parseCategory = function()
    {
        var category = this.category;
        var fullCategory;
        if(category == "Default") fullCategory = "Home";
        if(category == "Services") fullCategory = "Our Services";
        if(category == "Tools") fullCategory = "Tools &amp; Resources";
        if(category == "Customers") fullCategory = "Customer Successes";
        if(category == "Press") fullCategory = "Press Center";
        if(category == "About") fullCategory = "About WCP";
        return fullCategory;
    }
}

function setSelected()
{
    var li = document.getElementsByTagName("li");
    for(i=0; i<li.length; i++)
    {
        var LI = li[i];
        if(LI.className == pageInfo.category)
        {
            LI.className += " selected";
        }
        if(pageInfo.category != pageInfo.pageName)
        {
            if(LI.title == pageInfo.pageName)
            {
                var catchThis = LI;
            }
        }
    }
    if(catchThis != null)
    {
        setSelectedSnav(catchThis);
    }
    //set body class as the category and the id as the page name
    document.getElementsByTagName("body")[0].className = (pageInfo.pageName == "") ? "Home" : pageInfo.category;
	document.getElementsByTagName("body")[0].id = replaceSeparator(pageInfo.pageName.toString(), " ", "-") + "-Body";
	
    //display subnav
    document.getElementById(pageInfo.category).style.display = "block";
}

function setContentTitle()//this is the content title [in div class="mainContent"]
{
    var titleText = "";
    var title = document.getElementById('contentTitle');
    if(title.innerHTML == "" || title.innerHTML == '<!-- InstanceBeginEditable name="contentTitle" -->Title<!-- InstanceEndEditable -->')
    {
        if(pageInfo.pageName == pageInfo.category)
        {
            //since this title does not exsist on category landing pages...
            title.style.display = "none";
        }
        else
        {
            if(pageInfo.tNavSelected != null)
            {
                titleText = pageInfo.tNavSelected;
            }
            else
            {
                titleText = pageInfo.pageName;
            }
        }
        title.innerHTML = titleText.toString();
		SectionTitle = "loaded";
    }
}

function setTitle()//this is the header title
{
    if(document.getElementById(pageInfo.category + "Title") != null)
    {
		MainTitle = "loaded";
        //document.getElementById(pageInfo.category + "Title").style.display = "block";
    }
    else
    {
        setTimeout(setTitle, 100);
    }
}

function setSelectedSnav(el)
{
    //build the link... todo: try this another cleaner way
    var link = pageInfo.category + "-";
    var pageNameParsed = pageInfo.pageName.split(" ");
    for(i=0;i<pageNameParsed.length;i++)
    {
        if(i != pageNameParsed.length - 1)
        {
            link += pageNameParsed[i] + "-";
        }
        else
        {
            link += pageNameParsed[i];
        }
    }
    el.innerHTML = '<img src="Common/Images/bgTabLeft.gif" /><div><a href="' + link + '.aspx">' + pageInfo.pageName + '</a></div><img src="Common/Images/bgTabRight.gif" />';
    el.className = "selected";
}

function setTnav()
{
    if(pageInfo.pageName != pageInfo.category)
    {
        new Ajax("Common/AJAX-Controls/tNav " + pageInfo.pageName + ".html",//request the titles from sectionTitles.html [so contribute: editable]
            {
                method: 'get',
                update: $('tNav')
            }
        ).request();
    }
    function populateTnav()
    {
		function displayTnav()
		{
			if($("tNav").innerHTML != "")
			{
				$("tNav").style.display = "block";
				var li = document.getElementsByTagName("li");
				for(i=0; i<li.length; i++)
				{
					if(li[i].title == pageInfo.tNavSelected)
					{
						li[i].className = "selected";
					}
				}
			}
			else
			{
				setTimeout(displayTnav, 100);
			}
		}
		displayTnav();
    }
    populateTnav();
}

/* warning the following is a hack, someday it wont be */

if(navigator.userAgent.indexOf('Safari') > -1)
{
	//in safari the dynamic selected snav that is generated generates correctly but either doesn't take the style after it's generated or somehow otherwise breaks (the right image falls off on random titles)... this little hack fixes it... since nothing else is dependent on this function... we can leave it like this until we have time to fix.
	setTimeout(1000, setSelected);//re-call the function setSelected() after everything is loaded.
	//to fix: look at when setSelected is originally called and what Safari has loaded at that time... i bet something is off due to how quickly Safari is loading... or i have no clue.
}

function linksNewWindow()
{
	var allLinks = document.getElementsByTagName("a");
	for(i=0;i<allLinks.length;i++)
	{
		if(allLinks[i].href.indexOf("javascript") == -1)
		{
			if(allLinks[i].href.indexOf(pageInfo.host) == -1)
			{
				allLinks[i].target = "_blank";
			}
		}
	}
}

function bodyLoaded()//todo: make this a little better (this is to keep initial page loads crisp anod not let everything jump around...
{
	linksNewWindow();
	//hide it! (if empty)
	for(i=0;i<document.getElementsByTagName("h5").length;i++)
	{
		if(document.getElementsByTagName("h5")[i].innerHTML == '<!-- InstanceBeginEditable name="rightColumnHdr" --><!-- InstanceEndEditable -->')
		{
			document.getElementsByTagName("h5")[i].style.display = "none";
		}
	}
	document.getElementsByTagName("body")[0].style.display = "block";
}