/**
 * cookieCrumbs
 */

var cookieCrumbs = "cookieCrumbs";
var cookieCrumbsEq = cookieCrumbs + "=";
var cookieDivId = cookieCrumbs;

/**
 * Not providing an expiration date makes the cookie last for only the session.
 * @param value
 */
function createOrUpdateCookieCrumbs(value) {
    document.cookie = cookieCrumbs + "=" + value + "; path=/"

    var cookieValue = readCookieCrumbs();
    if(cookieValue != null) {
        var theUrls = cookieValue.split("~");
        if(theUrls.length > 5) {
            var newUrls = new Array();
            newUrls[0] = theUrls[1];
            newUrls[1] = theUrls[2];
            newUrls[2] = theUrls[3];
            newUrls[3] = theUrls[4];
            newUrls[4] = theUrls[5];
            document.cookie = cookieCrumbs + "=" + newUrls.join("~") + "; path=/";
        }
    }
}

/**
 *
 */
function readCookieCrumbs() {
    var allCookies = document.cookie.split(';');
    
    for(var i=0; i < allCookies.length; i++) {
        var aCookie = allCookies[i];

        while(aCookie.charAt(0) == ' ') {
            aCookie = aCookie.substring(1, aCookie.length);
        }
        
        if(aCookie.indexOf(cookieCrumbsEq) == 0) {
            return aCookie.substring(cookieCrumbsEq.length, aCookie.length);
        }
    }
    
    return null;
}

/**
 * 
 * @param path
 */
function assignLabel(path) {
    if(path == "/") {
        return "Home";
    } else if(path.indexOf("/MyAccount/view") != -1) {
        return "Account";
    } else if(path.indexOf("search") != -1) {
        return "Search";
    } else if(path.indexOf("product/detail") != -1) {
        return path.slice(16,28);
    } else if(path == "/info/etiquette") {
        return "Etiquette";
    } else if(path == "/info/destination/") {
        return "Destination";
    } else if(path == "/info/about") {
        return "About";
    } else if(path == "/info/news") {
        return "News";
    } else if(path == "/info/faq") {
        return "FAQ";
    } else if(path == "/info/jobs") {
        return "Jobs";
    } else if(path == "/info/privacy_policy") {
        return "Privacy Policy";
    } else if(path == "/shipping-info/") {
        return "Shipping";
    } else if(path == "/siteMap") {
        return "Site Map";
    } else {
        // Remove query params...
        if(path.indexOf("?") != -1) {
            path = path.slice(0, path.indexOf("?"));
            return assignLabel(path);
        }
        
        var splitz = path.substring(1, path.length - 1).split("/");

        if(splitz.length == 1) {
            if(splitz[0].indexOf("bar") != -1) {
                return "Bar / Bat Mitzvah";
            }

            return splitz[0].charAt(0).toUpperCase() + splitz[0].slice(1).replace(/-/g, " ").replace(/_/g, " ");

        }else if(splitz.length == 2) {
            if(splitz[1].indexOf("barmitzvah") != -1) {
                return "Bar Mitzvah";
            }else if(splitz[1].indexOf("barmitzvah") != -1){
                return "Bat Mitzvah";
            }

            if(splitz[1].indexOf("?") != -1) {
                return splitz[0].charAt(0).toUpperCase() + splitz[0].slice(1).replace(/-/g, " ").replace(/_/g, " ")
            }
            
            return splitz[1].charAt(0).toUpperCase() + splitz[1].slice(1).replace(/-/g, " ").replace(/_/g, " ");
            
        }else if(splitz.length == 3){
            if(splitz[1].indexOf("save-the-date") != -1) {
                if(splitz[2] == "savethedatemagnets") {
                    return "Save the date Magnets";
                }else if(splitz[2] == "savethedatephoto") {
                    return "Save the date Photocards";
                }else if(splitz[2] == "savethedatepostcards") {
                    return "Save the date Postcards";
                }
            }

            if(splitz[1].indexOf("?") != -1) {
                return splitz[1].charAt(0).toUpperCase() + splitz[1].slice(1).replace(/-/g, " ").replace(/_/g, " ")
            }
            
            return splitz[2].charAt(0).toUpperCase() + splitz[2].slice(1).replace(/-/g, " ").replace(/_/g, " ");
        }
    }
}

/**
 *
 * @param theUrls
 * @param labels
 */
function constructCookieCrumbs(theUrls, labels) {
    var theDiv = $(cookieDivId);

    for(i=0; i<theUrls.length; i++) {
        var a = new Element('a', { href: theUrls[i] }).update(labels[i]);

        if(theUrls[i].indexOf("?") != -1) {
            // TODO: Popup of search terms...
        }

        if(theDiv){
            theDiv.appendChild(a);
            if(i != theUrls.length - 1) {
                theDiv.appendChild(document.createTextNode(" ~ "));
            } else {
                a.addClassName("currentCrumb");
            }
        }
    }
}

/**
 * 
 */
function parseCookieCrumbs() {
    var theCrumbs = readCookieCrumbs();
    var theLabels = new Array();
    
    if(theCrumbs == null) {
        return null;
    }
    
    var theUrls = theCrumbs.split("~");
    
    for(i=0; i<theUrls.length; i++) {
        // TODO: Uh, what to do if there is no label?
        theLabels[i] = assignLabel(theUrls[i]);
    }

    constructCookieCrumbs(theUrls, theLabels);
}

/**
 * 
 */
document.observe("dom:loaded", function() {
    var thisPath = document.location.pathname;

    if(document.location.search != null) {
        thisPath += document.location.search;
    }

    if(thisPath.indexOf("customization") == -1 &&
            thisPath.indexOf("cart") == -1 &&
            thisPath.indexOf("login") == -1 &&
            thisPath.indexOf("checkout") == -1 &&
            thisPath.indexOf("checkout") == -1 &&
            thisPath.indexOf("MyEasyRSVP") == -1 &&
            thisPath.indexOf("orderconfirmation") == -1) {
        if(readCookieCrumbs() == null) {
            createOrUpdateCookieCrumbs(thisPath);
        } else {
            createOrUpdateCookieCrumbs(readCookieCrumbs() + "~" + thisPath);
        }
    }

    parseCookieCrumbs();
});

