﻿var active_menu = null;
var mntimeout, mntoiout, mnanhdepout, mnbanbeout, mncongcuout, mngioithieuout = null;

$(document).ready(function() {
    gopy_init();

    $('.hd_main').parent().css('position', 'inherit');

    $('a.home').hover(function() {
        var offset = $(this).offset();
        clearTimeout(mntimeout);

        if ($("#mn_home").css('display') == 'none') {
            mntimeout = setTimeout(function() {
                $('#mn_home')
                .css({
                    top: offset.top + 26,
                    left: offset.left + 17
                })
                .animate({ "top": "+=5px", "opacity": "toggle" }, "fast");
            }, 100);

        }
    }, function(eventObject) {

        clearTimeout(mntimeout);
        if ($("#mn_home").css('display') != 'none') {
            mntimeout = setTimeout(function() {
                $("#mn_home").animate({
                    "top": "-=5px", "opacity": "toggle"
                }, "fast");
            }, 100);
        }
    });

    $('#mn_home').hover(function() { clearTimeout(mntimeout) }, function(eventObject) {
        if ($("#mn_home").css('display') != 'none') {
            mntimeout = setTimeout(function() {
                $("#mn_home").animate({
                    "top": "-=5px", "opacity": "toggle"
                }, "fast");
            }, 300);
        }
    });


    $('a.toi').hover(function() {
        var offset = $(this).offset();
        clearTimeout(mntoiout);
        if ($("#mn_toi").css('display') == 'none') {

            mntoiout = setTimeout(function() {
                $('#mn_toi')
                .css({
                    top: offset.top + 26,
                    left: offset.left + -5
                })
                .animate({ "top": "+=5px", "opacity": "toggle" }, "fast");
            }, 100);
        }
    }, function(eventObject) {
        clearTimeout(mntoiout);
        if ($("#mn_toi").css('display') != 'none') {
            mntoiout = setTimeout(function() {
                $("#mn_toi").animate({
                    "top": "-=5px", "opacity": "toggle"
                }, "fast");
            }, 100);
        }
    });

    $('#mn_toi').hover(function() { clearTimeout(mntoiout) }, function(eventObject) {
        if ($("#mn_toi").css('display') != 'none') {
            mntoiout = setTimeout(function() {
                $("#mn_toi").animate({
                    "top": "-=5px", "opacity": "toggle"
                }, "fast");
            }, 300);
        }
    });

    $('a.anhdep').hover(function() {
        var offset = $(this).offset();
        clearTimeout(mnanhdepout);
        if ($("#mn_anhdep").css('display') == 'none') {
            mnanhdepout = setTimeout(function() {
                $('#mn_anhdep')
                .css({
                    top: offset.top + 26,
                    left: offset.left + -5
                })
                .animate({ "top": "+=5px", "opacity": "toggle" }, "fast", function() { });
            }, 100);
        }
    }, function(eventObject) {

        clearTimeout(mnanhdepout);
        if ($("#mn_anhdep").css('display') != 'none') {
            mnanhdepout = setTimeout(function() {
                $("#mn_anhdep").animate({
                    "top": "-=5px", "opacity": "toggle"
                }, "fast");
            }, 100);
        }
    });

    $('#mn_anhdep').hover(function() { clearTimeout(mnanhdepout) }, function(eventObject) {
        if ($("#mn_anhdep").css('display') != 'none') {
            mnanhdepout = setTimeout(function() {
                $("#mn_anhdep").animate({
                    "top": "-=5px", "opacity": "toggle"
                }, "fast");
            }, 300);
        }
    });


    $('a.banbe').hover(function() {
        var offset = $(this).offset();
        clearTimeout(mnbanbeout);
        if ($("#mn_banbe").css('display') == 'none') {
            mnbanbeout = setTimeout(function() {
                $('#mn_banbe')
                .css({
                    top: offset.top + 26,
                    left: offset.left + -5
                })
                .animate({ "top": "+=5px", "opacity": "toggle" }, "fast");
            }, 100);
        }
    }, function(eventObject) {
        clearTimeout(mnbanbeout);
        if ($("#mn_banbe").css('display') != 'none') {
            mnbanbeout = setTimeout(function() {
                $("#mn_banbe").animate({
                    "top": "-=5px", "opacity": "toggle"
                }, "fast");
            }, 100);
        }
    });

    $('#mn_banbe').hover(function() { clearTimeout(mnbanbeout) }, function(eventObject) {
        if ($("#mn_banbe").css('display') != 'none') {
            mnbanbeout = setTimeout(function() {
                $("#mn_banbe").animate({
                    "top": "-=5px", "opacity": "toggle"
                }, "fast");
            }, 300);
        }
    });

    $('a.congcu').hover(function() {
        var offset = $(this).offset();
        clearTimeout(mncongcuout);
        if ($("#mn_congcu").css('display') == 'none') {
            mncongcuout = setTimeout(function() {
                $('#mn_congcu')
                .css({
                    top: offset.top + 26,
                    left: offset.left + -5
                })
                .animate({ "top": "+=5px", "opacity": "toggle" }, "fast");
            }, 100);
        }
    }, function(eventObject) {
        clearTimeout(mncongcuout);
        if ($("#mn_congcu").css('display') != 'none') {
            mncongcuout = setTimeout(function() {
                $("#mn_congcu").animate({
                    "top": "-=5px", "opacity": "toggle"
                }, "fast");
            }, 100);
        }
    });

    $('#mn_congcu').hover(function() { clearTimeout(mncongcuout) }, function(eventObject) {
        if ($("#mn_congcu").css('display') != 'none') {
            mncongcuout = setTimeout(function() {
                $("#mn_congcu").animate({
                    "top": "-=5px", "opacity": "toggle"
                }, "fast");
            }, 300);
        }
    });

    $('a.gioithieu').hover(function() {
        var offset = $(this).offset();
        clearTimeout(mngioithieuout);
        if ($("#mn_gioithieu").css('display') == 'none') {
            mngioithieuout = setTimeout(function() {
                $('#mn_gioithieu')
                .css({
                    top: offset.top + 26,
                    left: offset.left + -5
                })
                .animate({ "top": "+=5px", "opacity": "toggle" }, "fast");
            }, 100);
        }
    }, function(eventObject) {
        clearTimeout(mngioithieuout);
        if ($("#mn_gioithieu").css('display') != 'none') {
            mngioithieuout = setTimeout(function() {
                $("#mn_gioithieu").animate({
                    "top": "-=5px", "opacity": "toggle"
                }, "fast");
            }, 100);
        }
    });

    $('#mn_gioithieu').hover(function() { clearTimeout(mngioithieuout) }, function(eventObject) {
        if ($("#mn_gioithieu").css('display') != 'none') {
            mngioithieuout = setTimeout(function() {
                $("#mn_gioithieu").animate({
                    "top": "-=5px", "opacity": "toggle"
                }, "fast");
            }, 300);
        }
    });

    $('a.onoff').toggle(function() {
        $(this).text('Tắt');
        $("#hidFilter").val(1);
        doFilter(true);
    }, function() {
        $(this).text('Bật');
        $("#hidFilter").val(0);
        doFilter(false);
    });

    $('#q')
    .val("Gõ từ khóa tìm kiếm")
    .focus(function() {
        if ($(this).val() == "Gõ từ khóa tìm kiếm")
            $(this).val("");
    })
    .blur(function() {
        if ($(this).val() == "")
            $(this).val("Gõ từ khóa tìm kiếm");
    })
    .keypress(function(e) {
        if (e.which == '13') {
            $("form")
            .submit(function() {
                return false;
            });
            $('#s').trigger("click");
        }
    });

    $("#txtSearchFoot")
    .focus(function() {
        $(this).val("");
    })
    .blur(function() {
        if ($(this).val() == "")
            $(this).val("Bạn muốn tìm gì?");
    })
    .keypress(function(e) {
        if (e.which == '13') {
            $("form")
            .submit(function() {
                return false;
            });
            $('#q').val($(this).val());
            $('#s').trigger("click");
        }
    });

    $('#s').click(function() {
        var keyword = $('#q').val();
        if (keyword == "" || keyword == "Gõ từ khóa tìm kiếm") return;

        if ($("#txtSearch_PhotoAlbum").length < 1) {
            self.location = 'http://anhso.net/search?q=' + encodeURIComponent(keyword);
        }
        else {
            $("#txtSearch_PhotoAlbum").val(keyword);
            $(".butSearch input").trigger("click");
        }
    });
});

$(document).click(function(e) {
    var target = (e && e.target) || (event && event.srcElement);

    if (active_menu != null && target.id.indexOf('_img') == -1) {
        $(active_menu).click();
    }

    if (e.target.id.indexOf('_pop') != -1) return
    if (avar_active) {
        clearTimeout(toutclock);
        hide_avamenu(document.getElementById(avar_active));
    }
});

function gopy_init() {
    reset_gopy();
    $('#txt_ct_link').val(document.location);
    $('#gopy_btn').toggle(function() {
        $('#mainform').show('fast');
        $(this).removeClass('gopy_btn').addClass('gopy_btn_active');

    }, function() {
        reset_gopy();
        $('#mainform').hide('fast');
        reset_gopy();
        $(this).removeClass('gopy_btn_active').addClass('gopy_btn');
    });
}
function closeBoxGopY() {
    reset_gopy();
    $('#mainform').hide('fast');
    reset_gopy();
    $(this).removeClass('gopy_btn_active').addClass('gopy_btn');

}
function showBoxGopY() {
    $('#mainform').show('fast');
    $("#gopy_btn").removeClass('gopy_btn').addClass('gopy_btn_active');
}
var ori_content = [];

function click_toggle(elem) {
    if (!ori_content[elem.id]) {
        ori_content[elem.id] = elem.value;
        elem.value = "";
    }
    else {
        if (ori_content[elem.id] == elem.value) elem.value = "";
    }
}
function blur_toggle(elem) {
    if (elem.value == "") elem.value = ori_content[elem.id];
}

function reset_gopy() {
    document.getElementById('txt_ct_email').value = "Nhập email của bạn";
    document.getElementById('txt_ct_content').value = "Cho chúng tôi biết bạn nghĩ gì ?";
}

function show_vipham() {

    $('#btn_gopy').removeClass('gopy').addClass('gopy_in');
    $('#btn_vipham').removeClass('vipham_in').addClass('vipham');
    $('#gopy_pn').hide('slow');
    $('#vipham_pn').show('slow');
}
function show_gopy() {

    $('#btn_gopy').removeClass('gopy_in').addClass('gopy');
    $('#btn_vipham').removeClass('vipham').addClass('vipham_in');
    $('#gopy_pn').show('slow');
    $('#vipham_pn').hide('slow');
}

var timming = 0;
function start_timmer() {
    if (timming == 0) return;
    else {
        timming = timming - 1;
        setTimeout("start_timmer()", 1000);
    }
}

function validating(contactmail) {
    if (contactmail == '' || contactmail.match(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i) == null) { return false; }
    return true;
}

var issendding = false;
function send_feedback() {
    var emailvl = document.getElementById('txt_ct_email');
    var contentvl = document.getElementById('txt_ct_content');
    click_toggle(emailvl);
    click_toggle(contentvl);
    if (timming > 0) {
        document.getElementById('ct_loading').innerHTML = "<img src='/images/error.gif' style='float:left' />  Bạn chỉ có thể gửi phản hồi sau " + timming + " giây nữa";
        $(document.getElementById('ct_loading')).show("fast", setTimeout(function() { $(document.getElementById('ct_loading')).hide("fast"); }, 2000));
        return;
    }
    if (issendding) return;
    if (!validating(emailvl.value)) {
        alert('Bạn cần điền email hợp lệ');
        return;

    }

    if (contentvl.value != "") {
        issendding = true;
        document.getElementById('ct_loading').innerHTML = "<img src=\"/images/ct_ajaxload.gif\" />  Đang gửi ...";
        $(document.getElementById('ct_loading')).show("fast");
        var feedback = true;
        if ($('btn_gopy').attr('class') != "gopy") feedback = false;
        document.getElementById('txt_ct_link').value = window.location;
        var thisurl = document.getElementById('txt_ct_link').value;
        $.post("/farm/sendcomment.aspx", { email: document.getElementById('txt_ct_email').value, content: document.getElementById('txt_ct_content').value, isfeedback: feedback, fromurl: thisurl },
              function(data) {
                  timming = 120;
                  start_timmer();
                  document.getElementById('ct_loading').innerHTML = "Nội dung phản hồi của bạn đã được gửi đi, xin chân thành cảm ơn";
                  setTimeout(function() {
                      $(document.getElementById('ct_loading')).hide("slow", function() { $('#gopy_btn').click(); });

                      issendding = false;
                  }, 2000);

              }, "text");
    }
    else {
        alert('bạn chưa nhập nội dung liên hệ');
        blur_toggle(emailvl);
        blur_toggle(contentvl);
    }
}
