/********************/
/********************/
/* REQUIRES Util.js */
/********************/
/********************/
/********************/
var Script = function(){
    /////////////////////
    /* PRIVATE MEMBERS */
    /////////////////////
    var currentPage = "#";
    /////////////////////////////
    /* LOAD / UNLOAD / HISTORY */
    /////////////////////////////
    function executeOnLoad(){
        var currLocation = window.location.href;
        var location = currLocation + "";
        location = location.replace("www.", "");
        if (location != currLocation){
            //go the the correct url
            Util.navigate(location);
        }else{
        
            //set the content of the page
            setNewsWithResource("news.html");
            setContentWithResource("sections/welcome.html", true);

            //add listeners
            setHandlers();
            
            //register unload event
            window.onbeforeunload = executeOnUnload;
            
            //register pop event
            window.onpopstate = executeOnPopState; 
        }
    }
    function executeOnUnload(){
        //stub
    }
    function executeOnPopState(event){
        if (event && event.state && event.state.resource)
            setContentWithResource(event.state.resource, false);
    }
    //////////
    /* MISC */
    //////////
    function randomClick(){
        //make random ordering of 10 numbers
        var i = 0;
        var numberArray = [];
        for (i = 1; i <= 10; i++)
            numberArray.push(i);
        
        //swap randomly
        var swapVal = -1;
        var swapIndex = -1;
        for (i = 0; i < 10; i++){
            swapVal = numberArray[i];
            swapIndex = Math.floor(Math.random() * 10);
            numberArray[i] = numberArray[swapIndex];
            numberArray[swapIndex] = swapVal;
        }

        //set DOM element
        var element = Util.get("randomHeader");
        if (!element){
            Util.log("could not find randomHeader DOM element");
            return;
        }
        var stringVal = "";
        for (i = 0; i < 10; i++){
            stringVal += numberArray[i].toString();
            if (i < 9)
                stringVal += ", ";
        }
        element.innerHTML = stringVal;
    }
    ///////////////////
    /* FLASH HELPERS */
    ///////////////////
    function fillFlashContainer(){
        //for popup windows
        //set the content div
        var injectedHtml = getInjectedHtml();
        if (injectedHtml){
            setFlash(injectedHtml);
        } else {
            Util.log("could not retrieve injectedHtml");
        }
    }
    function getInjectedHtml(){
        //get url params
        var extraParamString = Util.getUrlParam("extraParams");
        if (!extraParamString || typeof extraParamString != "string" || extraParams == ""){
            Util.log("extraParams could not be found in url");
            return null;
        }
        extraParamString = unescape(extraParamString);
        
        //deserialize json 
        var extraParams = JSON.parse(extraParamString);
        if (!extraParams || typeof extraParams != "object"){
            Util.log("extraParams could not be parsed");
            return null;
        }
        
        //get the injectedHtml
        if (!extraParams.injectedHtml || typeof extraParams.injectedHtml != "string" || extraParams.injectedHtml == ""){
            Util.log("could not find valid injectedHtml in parameter object");
            return null;
        }
        
        return extraParams.injectedHtml;
    }
    function setFlash(flashString){
        if (flashString && typeof flashString == "string"){
            var flashContainer = Util.get("containerDiv");
            if (flashContainer){
                flashContainer.innerHTML = flashString;
            } else {
                Util.log("cant find 'containerDiv' DOM element");
            }
        }else{
            Util.log("bad newsString = " + newsString);
        }
    }
    /////////////////////////////
    /* HANDLERS AND NAVIGATION */
    /////////////////////////////
    function setHandlers(){
        Util.registerClickEvent("aboutLink", aboutHandler);
        Util.registerClickEvent("blogLink", blogHandler);
        Util.registerClickEvent("contactLink", contactHandler);
        Util.registerClickEvent("portfolioLink", portfolioHandler);
        Util.registerClickEvent("resumeLink", resumeHandler);
        Util.registerClickEvent("codeLink", codeHandler);
    }
    function aboutHandler(){
        setContentWithResource("sections/about.html", true);
    }
    function blogHandler(){
        Util.openTab("http://aarongeisler.com");
    }
    function contactHandler(){
        setContentWithResource("sections/contact.html", true);
    }
    function portfolioHandler(){
        setContentWithResource("sections/portfolio.html", true);
    }
    function resumeHandler(){
        var url = Util.getResourcePath("files/resume.pdf");
        Util.navigate(url);
    }
    function codeHandler(){
        Util.openTab("https://github.com/aaron9000");
    }
    //////////
    /* NEWS */
    //////////
    function setNews(newsString){
        if (newsString && typeof newsString == "string"){
            var news = Util.get("news");
            if (news){
                news.innerHTML = newsString;
            } else {
                Util.log("cant find 'news' DOM element");
            }
        }else{
            Util.log("bad newsString = " + newsString);
        }
    }
    function setNewsWithResource(newsResource){
        if (newsResource && typeof newsResource == "string"){
            var path = Util.getResourcePath(newsResource);
            Util.getResource(path, function(response){
                setNews(response);
            });
        }else{
            Util.log("bad newsResource = " + newResource);
        }
    }
    /////////////
    /* CONTENT */
    /////////////
    function setContent(contentString){
        if (contentString && typeof contentString == "string"){
            var content = Util.get("content");
            if (content){
                content.innerHTML = contentString;
            }else {
                Util.log("cant find 'content' DOM element");
            }
        }else{
            Util.log("bad contentString = " + contentString);
        }
    }
    function setContentWithResource(contentResource, makeHistoryEntry){
        if (makeHistoryEntry == null || typeof makeHistoryEntry != "boolean"){
            Util.log("bad makeHistoryEntry = " + makeHistoryEntry);
            return;
        }
        if (contentResource && typeof contentResource == "string"){
            //early return so you dont nav to the same page
            if (currentPage === contentResource && currentPage != "")
                return;
                
            var path = Util.getResourcePath(contentResource);
            Util.getResource(path, function(response){
                //set content div with our result
                setContent(response);
                currentPage = contentResource;
                
                //remember history
                if (makeHistoryEntry === true){
                    var stateObj = {resource: contentResource};
                    history.pushState(stateObj, "slothproductions.org", "#");
                }
            });
        } else {
            Util.log("bad contentResource = " + contentResource);
        }
    }
    ///////////////
    /* PORTFOLIO */
    ///////////////
    function play(itemKey) {
        //play flash games
        if (!itemKey || typeof itemKey != "string" || itemKey == ""){
            Util.log("play: bad itemKey = " + itemKey);
            return;
        }
        
        //get url and determine size
        var flashUrl = Util.getServerUrl() + "flash/" + itemKey + ".swf";
        var width = 100;
        var height = 100;
        switch (itemKey){
            case "zombieprototype":
                width = 800;
                height = 450;
                break;
            case "temple":
                width = 800;
                height = 450;
                break;
            case "kius":
                width = 640;
                height = 480;
                break;
            case "ballprototype":
                width = 320;
                height = 480;
                break;
            case "3000AD":
                width = 768;
                height = 768;
                break;
            case "zombieslayer":
                width = 600;
                height = 300;
                break;
            case "simhell":
                width = 512;
                height = 384;
                break;
            case "missiles":
                width = 512;
                height = 384;
                break;
            case "lander":
                width = 400;
                height = 300;
                break;
            case "spacerocks":
                width = 512;
                height = 384;
                break;
            case "flak":
                width = 400;
                height = 300;
                break;
        }
        
        //build param object and open window
        var injectedHtml = Util.getFlashEmbedHtml(flashUrl, width, height);
        var extraParams = {injectedHtml : injectedHtml};
        Util.openWindow("flashcontainer.html", width + 100, height + 100, extraParams, true);
    }
    function nav (itemKey){
        if (!itemKey || typeof itemKey != "string" || itemKey == ""){
            Util.log("nav: bad itemKey = " + itemKey);
            return;
        }
        var url = "";
        switch (itemKey){
            case "spheresapp":
                url = "http://itunes.apple.com/us/app/spheres!/id467953417?mt=8";
                break;
            case "spheressource":
                url = "https://github.com/aaron9000/iRayTrace";
                break;
            case "scoretrackersource":
                url = "https://github.com/aaron9000/Score-Tracker";
                break;
            case "bubblesnatchapp":
                url = "http://itunes.apple.com/us/app/bubblesnatch/id396812151?mt=8";
                break;
            case "websitesource":
                url = "https://github.com/aaron9000/sloth"
                break;
            case "blog":
                url = "http://aarongeisler.com"
                break;
        }
        if (url != ""){
            Util.openTab(url);
        } else {
            Util.log("no value for itemKey = " + itemKey);
        }
    }
    function info(itemKey){
        //info pages in portfolio
        if (!itemKey || typeof itemKey != "string" || itemKey == ""){
            Util.log("info: bad itemKey = " + itemKey);
            return;
        }
        var resource = "sections/portfolio/" + itemKey + ".html";
        setContentWithResource(resource, true);
    }
    //////////////////////
    /* PUBLICLY EXPOSED */
    //////////////////////
    return {
        randomClick: randomClick,
        executeOnLoad: executeOnLoad,
        fillFlashContainer: fillFlashContainer,
        info: info,
        play: play,
        nav: nav
    };
}();

