/**
 * Global Script
 * 
 * @author Wayne
 * @
 */

jQuery(function() {
    TreeNav.loadStatus();
    // 二级菜单
    jQuery("#treeNav > h3 > a").toggle(TreeNav.expand, TreeNav.fold).focus(function() {
        jQuery(this).blur();
    }).each(function() {
        // 默认展开三项
        var elem = jQuery(this);

        var status = TreeNav.status[elem.attr("rel")];
        if (typeof status == "undefined") {
            if (elem.hasClass("clicked")) {
                elem.trigger("click");
            }
        } else if (status == 1) {
            elem.trigger("click");
        }
    });
    // 三级菜单
    jQuery("#treeNav li.hasSub").hover(TreeNav.showLeaf, TreeNav.hideLeaf);
});

// 搜索行为
var SearchHandler = {
    onfocus: function(obj) {
        if (obj.value == "Tales of Fantasy") {
            obj.value = "";
        }
    },
    onblur: function(obj) {
        if (obj.value == "") {
            obj.value = "Tales of Fantasy";
        }
    },
    onsubmit: function(obj) {
        if (obj.value == "") {
            return false;
        }
        if (obj.value == "Tales of Fantasy") {
            jQuery(obj).trigger("focus");
            return false;
        }
    }
};

// 左侧菜单相关函数
var TreeNav = {
    icons: { plus: "/themes/images/icon_plus.png", minus:"/themes/images/icon_minus.png" },
    status: [],

    expand: function() {
        jQuery("img", this).attr("src", TreeNav.icons.minus)
            .parents("h3:eq(0)").next("ul").show();
        TreeNav.saveStatus(jQuery(this).attr("rel"), 1);
    },
    
    fold: function() {
        jQuery("img", this).attr("src", TreeNav.icons.plus)
            .parents("h3:eq(0)").next("ul").hide();
        TreeNav.saveStatus(jQuery(this).attr("rel"), 0);
    },
    
    showLeaf: function() {
        jQuery("ul", this).show();
    },
    
    hideLeaf: function() {
        jQuery("ul", this).hide();
    },

    loadStatus: function() {
        var str = jQuery.cookie("menu_status");
        if (str) {
            str = str.split("||");
        } else {
            return false;
        }

        TreeNav.status = [];
        for (var k in str) {
            var tmp = str[k].split("=");
            TreeNav.status[tmp[0]] = tmp[1];
        }
    },

    saveStatus: function(id, status) {
        TreeNav.status[id] = status;
        
        var str = [];
        for (var k in TreeNav.status) {
            str.push(k + "=" + TreeNav.status[k]);
        }
        if (typeof console != "undefined") {
            console.log(str.join("||"));
        }
        jQuery.cookie("menu_status", str.join("||"));
    }
};

// TODO Set global error handler
var JSONResponse = function(json) {
    this.init(json);
};

JSONResponse.prototype = (function() {
    return {
        init: function(json) {
            this.data = json.data;
            this.error = json.error;
            this.status = json.status;
        },
        getData: function() {
            return this.data;
        },
        getError: function() {
            return this.error;
        },
        hasError: function() {
            return this.status == 0;
        }
    };
})();

// jQuery cookie
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
