jQuery.fn.outerHTML = function() {
	return $('<div>').append( this.eq(0).clone() ).html();
};

// This is our ready functionality - run on the load of the page.
// --------------------------------------------------------------------------
jQuery(document).ready(function()
{
    //* Set focus to the first input element of type text
    $("input:text:first").focus();
    
     window.setTimeout('InitializePage();', 100);

});
// --------------------------------------------------------------------------

    // JScript File
    function InitializePage()
    {
        //* Get the current page.
        var currentPage = 
            (location.pathname.length > 0)  ? 
                location.pathname.substring(location.pathname.lastIndexOf("/") + 1).toLowerCase()
                :
                "";

        //* Things that happen on all (non-popup) pages.   
        //* ------------------------------------------------------------ 
        switch (currentPage)
        {
            //* List all popup pages here.
            case "videoplayer.aspx":
            case "joinmailinglist.aspx":
                break;
            
            default:
                WriteQuoteList();
                break;
        } 
        //* ------------------------------------------------------------ 
        
//debugger;

        var videoReq = loadXMLDoc("flashplayer/videoplaylist.xml");
        var videoDocs = videoReq.responseXML.getElementsByTagName("track");

        //* Call the appropriate functions for things that happen on specific pages here.
        //* ------------------------------------------------------------ 
        switch(currentPage)
        {
            //case "test.aspx":
            case "videos.aspx":
                WriteVideoList(videoDocs);
                StartSpecificVideo(videoDocs);
                break;
                
            case "videoplayer.aspx":
                StartSpecificVideo(videoDocs);
                ResizePlayerPopup();
                break;

            case "joinmailinglist.aspx":
                resizeOuterTo(515, 498);
                break;

            case "news.aspx":
                WriteNewsList();
                break;

            case "":
            case "default.aspx":
                try
                {
                    //ShowWelcomeVideo();
                    break;
                }
                catch (ex)
                {
                    alert(ex.message);
                }
        }
        //* ------------------------------------------------------------ 


        
    }



    function loadXMLDoc(url) 
    {
	    var req = GetXmlHttpRequest();

        // branch for native XMLHttpRequest object
	    if(req) 
	    {

            req.open("GET", url, false);
            req.send(null);

            if(!req.getResponseHeader("Date"))
            {
                var cached = req;
                req = GetXmlHttpRequest();
                var ifModifiedSince = cached.getResponseHeader("Last-Modified");
                ifModifiedSince = (ifModifiedSince) ? ifModifiedSince : new Date(0); // January 1, 1970
                req.open("GET", url, false);
                req.setRequestHeader("If-Modified-Since", ifModifiedSince);
                req.send("");
                if(req.status == 304)
                {
                  req = cached;
                }
            }

	        return req;
	    }
    	
    }



    function GetXmlHttpRequest()
    {
        var req = false;
        if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	    try {
			    req = new XMLHttpRequest();
            } catch(e) {
			    req = false;
            }
        // branch for IE/Windows ActiveX version
        } else if(window.ActiveXObject) {
       	    try {
        	    req = new ActiveXObject("Msxml2.XMLHTTP");
      	    } catch(e) {
        	    try {
          		    req = new ActiveXObject("Microsoft.XMLHTTP");
        	    } catch(e) {
          		    req = false;
        	    }
		    }
        }

        return req;
    }    
    






// ----------------------------------------------------------------------------------------------------------------

    function WriteNewsList()
    {
        var newsReq = loadXMLDoc("newsdocs/News.xml");
        var newsDocs = newsReq.responseXML.getElementsByTagName("NewsItem");

        //* Generate the menu html
        var newsesHtml = "";
        newsesHtml += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" align=center style=\" margin-left: 20px; border: 0px black solid; width: 200px;\">";
        newsesHtml += "<tr><td class=\"RightPanelTitle\">Click on an article to view</td></tr><tr><td>&nbsp;</td></tr>";

        for (var i=0; i < newsDocs.length; i++)
        {
            newsesHtml += "<tr><td style=\"height: 15px;\">" + 
                "<a class=\"RightPanelLink\" href=\"#\" onclick=\"LoadNewsItem(" + i.toString() + "); return false;\" >" +
                newsDocs[i].getElementsByTagName("Date")[0].childNodes[0].data + "<br />" + 
                newsDocs[i].getElementsByTagName("Headline")[0].childNodes[0].data + " (More...)"
                "</a></td></tr>";

            newsesHtml += "<tr><td><div style=\"height: 20px;\"></div><div style=\"width: 210px; text-align: center; border: 0px yellow solid;\"><img align=\"middle\" src=\"images/bullet.gif\" /></div><div style=\"height: 20px;\"></div></td></tr>";

        }

        newsesHtml += "</table>";

        //* Write the menu html to the screen        
        document.getElementById("newsListDiv").innerHTML = newsesHtml;

        //* Load the first item.
        if (newsDocs.length > 0)
        {
            LoadNewsItem(0);
        }
    }


    function LoadNewsItem(ix)
    {
        var newsReq = loadXMLDoc("newsdocs/News.xml");
        var newsDoc = newsReq.responseXML.getElementsByTagName("NewsItem")[ix];
        
        //* Fill the div elements
        var newsDateDiv = document.getElementById("newsDateDiv");
        var newsHeadlineDiv = document.getElementById("newsHeadlineDiv");
        var newsTextDiv = document.getElementById("newsTextDiv");

        newsDateDiv.innerHTML = newsDoc.getElementsByTagName("Date")[0].childNodes[0].data;
        newsHeadlineDiv.innerHTML = newsDoc.getElementsByTagName("Headline")[0].childNodes[0].data;
        newsTextDiv.innerHTML = newsDoc.getElementsByTagName("Text")[0].childNodes[0].data;
    }
// ----------------------------------------------------------------------------------------------------------------


    function WriteVideoList(videoDocs)
    {
        //debugger;

        //* Generate the menu html
        var videosHtml = "";
        videosHtml += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\" text-align: center; width: 80%; height: 100%'\">";
        videosHtml += "<tr><td>&nbsp;&nbsp;</td><td class=\"RightPanelTitle\">Click on a thumbnail to view.  Scroll down to see all videos.</td></tr><tr><td>&nbsp;</td></tr>";

        for (var i=0; i < videoDocs.length; i++)
        {
            var imgSrc = videoDocs[i].getElementsByTagName("thumbnail")[0].childNodes[0].data;
            //if (imgSrc.indexOf("http") == -1) {imgSrc = "flashplayer/" + imgSrc; }
            
            videosHtml += "<tr><td>&nbsp;&nbsp;</td><td style=\"height: 15px;\">" + 
                "<a class=\"RightPanelLink\" href=\"#\" onclick=\"LoadTrack(" +  i.toString() + "); return false;\" >" +
                "<img style=\"\"src=\"" + imgSrc + 
                    "\" alt=\"" + videoDocs[i].getElementsByTagName("title")[0].childNodes[0].data + "\" border=\"0\" \><br />" + 
                videoDocs[i].getElementsByTagName("title")[0].childNodes[0].data +
                "</a></td></tr>";

            videosHtml += "<tr><td><div style=\"height: 20px;\"></div></td></tr>";

        }

        videosHtml += "</table>";

        //* Write the menu html to the screen
        document.getElementById("videoListDiv").innerHTML = videosHtml;
    }
    
    function StartSpecificVideo(videoDocs)
    {
        if (videoDocs.length > 0)
        {
            var track = 0;
            if (document.location.search.length > 0)
            {
                var search = document.location.search.substring(1);
                search = search.toLowerCase();
                var videoCode = search.split("videocode=")[1].split("&")[0];
                
                for (var i=0; i < videoDocs.length; i++)
                {
                    if (videoDocs[i].getElementsByTagName("linkcode")[0].childNodes[0].data.toLowerCase() == videoCode.toLowerCase())
                    {
                        track = i;
                        break;
                    }
                } 
            }
            else
            {
                //track = GetRandomNumber(videoDocs.length);
                track = 0;
            }
            
            LoadTrack(track);
        }
    }
    
    function LoadTrack(ix)
    {

        try
        {
            var videoReq = loadXMLDoc("flashplayer/videoplaylist.xml");
            var videoDoc = videoReq.responseXML.getElementsByTagName("track")[ix];

            StartVideo(ix, videoDoc);
            LoadtrackCopy(ix, videoDoc);
        }
        catch (ex)
        {
            //alert(ex.message);
        }   
    }
    
    function StartVideo(ix, videoDoc)
    {
//        while(sendEvent == null)
//        {
//            ;
//        }
//        sendEvent('playitem', ix);

        var embedHtml = videoDoc.getElementsByTagName("embed")[0].childNodes[0].data;
        if (videoDoc.getElementsByTagName("embed")[0].childNodes.length > 1)
            embedHtml += videoDoc.getElementsByTagName("embed")[0].childNodes[1].data;
            
        var videoPlayerDiv = document.getElementById("videoPlayerDiv");
        videoPlayerDiv.innerHTML = embedHtml;
        //alert(embedHtml);
    }

    function LoadtrackCopy(ix, videoDoc)
    {
        var videoCopyDiv = document.getElementById("videoCopyDiv");
        videoCopyDiv.innerHTML = videoDoc.getElementsByTagName("copy")[0].childNodes[0].data;
    }
    
    //* Callback function called by flowplayer
    function onClipChanged(clip)
    {
        LoadtrackCopy(clip.id);
    }

// ----------------------------------------------------------------------------------------------------------------

    function WriteQuoteList()
    {
        var quotesReq = loadXMLDoc("QuoteList.xml");
        var quotesDocs = quotesReq.responseXML.getElementsByTagName("Quote");

        var ix = GetRandomNumber(quotesDocs.length);

        //* Get a random number within the range of the list.
        var quote = quotesDocs[ix];

        var quoteTextDiv = document.getElementById("quoteTextDiv");
        var quoteAuthorDiv = document.getElementById("quoteAuthorDiv");
        quoteTextDiv.innerHTML = quote.getElementsByTagName("Text")[0].childNodes[0].data;
        quoteAuthorDiv.innerHTML = quote.getElementsByTagName("Author")[0].childNodes[0].data;
    }
    
// ----------------------------------------------------------------------------------------------------------------
    function GetRandomNumber(maxNum)
    {
        return Math.floor(Math.random() * maxNum);
    }   

// ----------------------------------------------------------------------------------------------------------------
    var intId;
    function ShowWelcomeVideo()
    {
        var pp = document.getElementById("PlayerPanel")
        
        pp.style.visibility = "visible";
        intId = window.setInterval("IncrementWidth(10);", 1);
    }

    function IncrementWidth(interval)
    {
        var pp = document.getElementById("PlayerPanel")

        var width = pp.style.width;
        width = parseInt(width.substring(0, width.length - 2));
        if (width >= 250) 
        {
            window.clearInterval(intId);
            pp.style.width = "260px";
            sendEvent('playitem', 0);
            
            window.setTimeout("StartToWait()", 5000);
        }
        
        pp.style.width = (width + interval).toString() + "px";
    }

    function DecrementWidth(interval)
    {
        var pp = document.getElementById("PlayerPanel")

        var width = pp.style.width;
        width = parseInt(width.substring(0, width.length - 2));
        if (width <= 0)
        {
            window.clearInterval(intId);
            pp.style.width = "0px";
            pp.style.visibility = "hidden";
            return;
        }
        pp.style.width = (width + interval).toString() + "px";

    }
    
    function StartToWait()
    {
        intId = window.setInterval("WaitForEnd()", 500);
    }

    function WaitForEnd()
    {
        var t = document.getElementById("time")
        var sTime = t.innerHTML;
        sTime = parseInt(sTime.split(",")[1]);
        if (sTime == 0)
            intId = window.setInterval("DecrementWidth(-10);", 1);

        //"time: 31, 0"
    }
// ----------------------------------------------------------------------------------------------------------------

    
    function ResizePlayerPopup()
    {
        //debugger;
        var dHeight = $("#videoPlayerTop").innerHeight();
        var dWidth = $("#videoPlayerTop").innerWidth();
        var titleHeight = $("#videoTitleDiv").outerHeight();//document.getElementById("videoTitleDiv").scrollHeight;
        var copyHeight = $("#videoCopyDiv").outerHeight();//document.getElementById("videoCopyDiv").scrollHeight;
        var playerHeight = $("#videoPlayerDiv").outerHeight();//document.getElementById("videoCopyDiv").scrollHeight;
        var basicHeight = 175 + GetBasicHeightModifier();
        
        //alert(navigator.appName);
        //alert(navigator.appCodeName);
        //alert(navigator.userAgent);
        //debugger;

        //* Now resize the item, taking into account the title and copy heights to make the popup window the perfect size
        resizeOuterTo(515, (basicHeight + titleHeight + copyHeight + playerHeight));
    }
    
    function GetBasicHeightModifier()
    {
        var val = 0;
        
        switch (navigator.appName)
        {
            case "Netscape":
                if (navigator.userAgent.indexOf("Chrome") != -1)
                {
                    val += 20;
                    //* Check for dev or prod
                    if (location.hostname == "gchristieconsulting.com")
                    {
                        //val += 30;
                    }
                }
                else
                {
                    val += 50;
                }
            
                break;
                
            case "Microsoft Internet Explorer":
                //* Check for dev or prod
                if (location.hostname == "gchristieconsulting.com")
                {
                    val += 40;
                }
                break;
                
            default:
                break;
        }        
        
        
        
        return val;
        
    }
    
    function ResizeToSize()
    {
        //debugger;
        
        //* figure out how far off we are from where we should be, then resizeBy that amount.
        var widthOff = document.body.scrollWidth- document.body.clientWidth;
        var heightOff = document.body.scrollHeight - document.body.clientHeight;
        window.resizeBy(widthOff, heightOff);

    }
    
    function resizeOuterTo(w,h) 
    {
        if (parseInt(navigator.appVersion) > 3) 
        {
            if (navigator.appName=="Netscape") 
            {
                top.resizeTo(w, h);
                top.outerWidth = w;
                top.outerHeight = h;
            }
            else 
            {
                top.resizeTo(w, h);
            }
        }
    }
// ----------------------------------------------------------------------------------------------------------------



