/* 
;
; Team 116 Robotics, 2008-2009.
; by Alex L
;
*/

$(document).ready(function()
{    
    // So, the purpose of this is to make updating the site easier. The
    // original plan was to use some server side CMS, but PHP never made it 
    // onto the server. This is the hackety result of that situation... (first
    // draft!!).
     
    // Excuse the JS, I don't know it very well. This is alpha/mockup code!!!
    
    var index = 
    [
        ["Homepage",    "news.html"],
        ["About Us",    "about.html"],
        ["Schedule",    "schedule.html"],
        ["Extras",      "extras.html"],
        ["Contact Us",  "contact.html"]
    ];
    
    $(".noscript").remove();
    
    var $logo = $("div#head a");
        $logo
            .css("opacity", 0.85)
            .hover(
                function() {
                    $logo.animate({opacity: 1.00}, "fast");
                },
                function() {
                    $logo.animate({opacity: 0.85}, "fast");
                }
            );
    
    
    var $tabs = $("div#tabs");
    var $info = $("div#info");
    var $body = $("div#body");
  
    // todo: make these images clicky-expandable.
    var $jcan = $info.find("ul.jCan");
        $jcan.jCan({circular: true, spacing: 3, step: [1, 1]})
    
    var doc = window.location.hash;
        doc = (doc != undefined && doc.length > 2) ? hash(doc) : undefined;
    
    var i = check(doc);
        doc = (i > -1) ? index[i][0] : index[0][0];
    
    var loaded = "";
    
    if (!$.browser.msie) {
        $tabs
            .append(
                "<select id=\"theme\">" +
                    "<option value=\"lite\">Lite</option>" +
                    "<option value=\"dark\">Dark</option>" +
                "</select>"
            );
    
        var select = $("select#theme");
            select
                .change(function() {
                    $(this)
                        .children("option:selected")
                        .each(function() {
                            return switchStyle($(this).attr("value"));
                        });
                });
        
        var style = readCookie("style");
        if (style != null) switchStyle(style);
    }
    
    for (i = 0; i < index.length; i++) {
        var a = b = index[i][0];
            a = a.replace(/ /, "_");
        
        var c = (b == doc) ? "on" : "";
        
        $tabs
            .append("<a href=\"#/"+a+"\" class=\""+c+"\">"+b+"</a>");
    }

    var toggle = function() {
        var $this = $(this),
            html = "";
        
        if ($info.hasClass("off")) {
            $info.removeClass("off");
            html = "[hide &uarr;]";
            
            eraseCookie("info");
        }
        else {
            $info.addClass("off");
            html = "[show &darr;]";
            
            createCookie("info", "off", 365);
        }
        
        $this.html(html);
    }

    var togglebtn = $info.find("div a");
        togglebtn
            .click(toggle, toggle);

    if (readCookie("info") == "off") {
        togglebtn.click();
    }

    $info.find("a[href^='#/About_Us']").click(function(){load(hash($(this).attr("href")));});

    var nodes = $tabs.children("a");
        nodes
            .click(function() {
                $this = $(this);
                
                nodes.removeClass("on");
                
                load(hash($this.attr("href")));
                $this.addClass("on");
            
                // Get rid of the stupid dotted line thingy.
                this.blur();
            })
            .filter(".on")
            .click();

    //
    // Document loading functions
    // ------------------------------------------------------------------------

    function hash(url)
    {
        return url.substr(url.lastIndexOf("#") + 2);
    }

    function check(doc)
    {
        doc = (doc + "").replace(/_/, " ");
        
        for (var i = 0; i < index.length; i++) {
            if (index[i][0] == doc) {
                return i;
            }
        }
        
        return -1;
    }

    function load(doc)
    {  
        var i = check(doc);
            doc = (i > -1) ? index[i][1] : index[0][1];
        
        if (loaded != doc) {
            $.get('docs/'+doc, {}, function(data) {                
                loaded = doc;
                $body.html(data);

                $body
                    .find("a")
                    .not("a[href^='#']")
                    .not("a[href^='http://']")
                    .not("a[href^='www.']")
                    .not("a[href^='mailto:']")
                    .each(function() {
                        $(this).attr("href", "./docs/" + $(this).attr("href"));
                    });
                
                $body
                    .find("img")
                    .not("img[src!='http://']")
                    .not("img[src!='www.']")
                    .each(function() {
                        $(this).attr("src",  "./docs/" + $(this).attr("src"));
                    });
            }); 
        }
    }

    //
    // Style switcher (dark/lite)
    // http://www.kelvinluck.com/assets/jquery/styleswitch/
    // ------------------------------------------------------------------------
    
    function switchStyle(style)
    {
        if (select != undefined) {
            select.children("option")
                  .each(function(i) {
                       if ($(this).attr("value") == style) {
                           $(this).attr("selected", 1);
                       }
                       else {
                           $(this).attr("selected", 0);
                       }
                   });
                   
            $("link[rel*=style][title]").each(function(i) { 
                this.disabled = !($(this).attr("title") == style);
            });
            
            createCookie("style", style, 365);
        }    
    }

    //
    // Cookie manipulation functions
    // http://www.quirksmode.org/js/cookies.html
    //
    // jQuery doesn't have cookie functions??? :o
    // ------------------------------------------------------------------------

    function createCookie(name, value, days)
    {
        if (days) {
            var date = new Date();
                date.setTime(date.getTime() + (days*24*60*60*1000));
                
            var expires = "; expires="+date.toGMTString();
        }
        else {
            var expires = "";
        }
        
        document.cookie = name+"=" + value + expires + "; path=/";
    }
    
    function readCookie(name) 
    {
	    var nameEQ = name + "=";
	    var ca = document.cookie.split(';');
	    
	    for (var i = 0; i < ca.length; i++) {
		    var c = ca[i];
		    
		    while (c.charAt(0) == ' ') {
		        c = c.substring(1, c.length);
            }
		    
		    if (c.indexOf(nameEQ) == 0) {
		        return c.substring(nameEQ.length, c.length);
		    }
	    }
	    
	    return null;
    }
    
    function eraseCookie(name)
    {
	    createCookie(name, "", -1);
    }    
});
