﻿///<reference path="jquery.cookies.2.2.0.min.js" />

function initialProfile() {
    var profile = new Object();
    profile.age = 0;
    profile.accepted = false;
    profile.language = "th-TH"; // allow two language "en-US" and "th-TH"
    profile.currentpage = 0;
    setProfile(profile);
}

function getProfile() { return $.cookies.get('profile'); }

function setProfile(profile) {
    var date = new Date();
    date.setTime(date.getTime() + (30 * 60 * 1000));
    var newOptions = {
        path: '/',
        expiresAt: date
    }

    $.cookies.set('profile', profile); 
    $.cookies.setOptions(newOptions);
    return getProfile();
}

function deleteProfile() { $.cookies.del('profile'); }

function isProfileExist() {
    var profile = getProfile();
    return (profile != null && profile != undefined) ? true : false;
}

function redirectProfile() {
    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    var redirectURL = sPath.replace(sPage,'')+"verifyage.html";

    if (!isProfileExist()) {
        window.location = redirectURL;
    }
    else {
        var profile = getProfile();
        if (profile.accepted = false) {
            deleteProfile();
            window.location = redirectURL;
        }
        else {
            profile.currentpage = getPageName();
            setProfile(profile);
        }
    }
}

function accepted() {
    initialProfile();
    var profile = getProfile();
    if (isProfileExist()) {
        var sPath = window.location.pathname;
        var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
        var redirectURL = sPath.replace(sPage, '') + "index.html";

        profile.accepted = true;
        setProfile(profile);
        window.location = redirectURL;
    }
}

function verifyAge() {
    deleteProfile();
    $('#verifyage').fadeIn('slow', function () {
        $(this).delay(5000).fadeOut('slow');
    });
}

function changeLanguage(lang) {
    var profile = getProfile();
    if (isProfileExist()) {
        profile.language = lang;
        setProfile(profile);
    } else {
        redirectProfile();
    }
    window.location.reload();
    //alert(profile.language);
}

function getPageName() {
    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    var extension = sPage.substring(sPage.lastIndexOf('.'));
    return sPage.replace(extension, "");
}

function getMenu() {
    var menucode = getPageName();
    var menu = "menu_home";
    switch(menucode)
    {
    case "index":
        menu = "menu_home";
      break;
    case "activities":
        menu = "menu_event";
        break;
    case "activities_inner":
        menu = "menu_event";
        break;
    case "gallery":
        menu = "menu_gallery";
        break;
    case "gallery_inner":
        menu = "menu_gallery";
        break;
    case "tigerstyle":
        menu = "menu_list";
        break;
    default:
      menu = "menu_home"; 
      break;
    }
    return menu;
}
