﻿var search = {
    el: {
        form: null,
        seach_key: null,
        landing_btn_search: null,
        landing_keyword: null,
        btn_search: null
    },
    init: function() {
        this.el.form = $("#frm-search");
		this.el.form.submit(function(){
			return false;
		});
        this.el.seach_key = $("#searh-key");
        this.el.btn_search = $("a", this.el.form);
        this.el.landing_btn_search = $("a.landing-search");
        this.el.landing_keyword = $("input[name=keyword]");
    },
    initEvents: function() {
        var _this = this;
        this.el.btn_search.click(function() {
            if (_this.el.seach_key.val() && _this.el.seach_key.val() != "กรุณาพิมพ์คำที่ต้องการค้นหา" && _this.el.seach_key.val() != "") {
                window.location.href = directory_host+"/article-search-result.rhtml?keyword=" + _this.el.seach_key.val();
            } else {
                //_this.qtipValidate($(_this.el.seach_key), "xxx");
            }
        });

        this.el.landing_btn_search.click(function() {
            if (_this.el.landing_keyword.val() && _this.el.landing_keyword.val() != "") {
                window.location.href = directory_host+"/article-search-result.rhtml?keyword=" + _this.el.landing_keyword.val();
            } else {
            }
        });

        this.el.landing_keyword.keydown(function(event) {
            if (event.keyCode == 13) {
                if (_this.el.landing_keyword.val() && _this.el.landing_keyword.val() != "") {
                    window.location.href = directory_host+"/article-search-result.rhtml?keyword=" + _this.el.landing_keyword.val();
                }
            }
        });

        this.el.seach_key.keydown(function(event) {
            if (event.keyCode == 13) {
                if (_this.el.seach_key.val() && _this.el.seach_key.val() != "กรุณาพิมพ์คำที่ต้องการค้นหา" && _this.el.seach_key.val() != "") {
                    window.location.href = directory_host+"/article-search-result.rhtml?keyword=" + _this.el.seach_key.val();
                }
            }
        });

        /*document.onkeypress = function(evt) {
            var evt = (evt) ? evt : ((event) ? event : null);
            var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
            if (evt.keyCode == 13) { return false; }
        };*/
    },
    qtipValidate: function(e, message) {

        // Destroy currrent tooltip if present
        if (e.data("qtip")) e.qtip("destroy");

        e.qtip({
            content: message, // Set the tooltip content to the current corner
            position: {
                corner: {
                    tooltip: 'rightMiddle', // Use the corner...
                    target: 'leftMiddle' // ...and opposite corner
                }
            },
            show: {
                when: false, // Don't specify a show event
                ready: true, // Show the tooltip when ready
                effect: {
                    type: 'fade',
                    length: 700
                }
            },
            hide: { when: { event: 'keyup'} },
            style: {
                border: {
                    width: 5,
                    radius: 5
                },
                padding: 7,
                //textAlign: 'center',
                tip: true, // Give it a speech bubble tip with automatic corner detection
                name: 'light' // Style it according to the preset 'cream' style
            },
            api: {
                onShow: function() {
                    setTimeout(function() {
                        if (e.data("qtip")) e.qtip("destroy");
                    }, 3000);
                }
            }
        });
    }
};

$(function() {
    search.init();
    search.initEvents();
});
