$(document).ready(function() {
    var language = ($("input[id *= hidLanguage]").val());
    if ($(".displayDate a").size() > 0) {
        $(".displayDate a").removeClass("active");
        $(".displayDate a:eq(" + (language - 1) + ")").addClass("active");
    }

    $("body").ajaxComplete(function() {        
        $(".itemMenu").hover(function() {
            $(".itemMenu .sub").css("display", "none");
        }, function() { })

        $(".header").hover(function() { }, function() {
            $(".itemMenu .sub").css("display", "none");
        })

        $(".itemMenu .main").hover(function() {
            sub = $(this).next();
            $(sub).css({ "top": $(this).css("top"), "left": $(this).css("top") }).slideDown();
        }, function() {
        })
    })
$(".detailNews .img").hide();
})

function doSearch() {
    window.location.href = "/search/" + $(".txtSearch").val();
}
function doConfirm(text) {
    if (!confirm(text))
        return false;
    return true;
}
function doCheckAll(obj) {
    $(obj).bind("click", function() {
        $("INPUT[type='checkbox'][group=check]").attr('checked', $(obj).is(':checked'));
    })
}
function displayTime() {
    var today = new Date();
    var iHour = today.getHours();
    var iMinute = today.getMinutes();

    iHour = (iHour < 10) ? "0" + iHour : iHour;
    iMinute = (iMinute < 10) ? "0" + iMinute : iMinute;
    var strTimeOut = iHour + " : " + iMinute;

    jQuery("#header #time").text("").text(strTimeOut);
    setTimeout("displayTime();", 1000)
}
function displayDate(language) {
    var today = new Date();
    var strYear = today.getFullYear();
    var iMonth = today.getMonth() + 1; // +1, we do NOT want zero-based month index
    var iQuarter = Math.ceil((iMonth / 12) * 4);
    var iDate = today.getDate();
    var iDay = today.getDay();
    var strDateOut = "";

    /// begin_: leading zeropad single-digit numbers
    iMonth = (iMonth < 10) ? "0" + iMonth : iMonth;
    iDate = (iDate < 10) ? "0" + iDate : iDate;

    /// begin_: display output 

    if (language == "vn") {
        switch (iDay) {
            case (0): iDay = "Chủ nhật"; break;
            case (1): iDay = "Thứ 2"; break;
            case (2): iDay = "Thứ 3"; break;
            case (3): iDay = "Thứ 4"; break;
            case (4): iDay = "Thứ 5"; break;
            case (5): iDay = "Thứ 6"; break;
            case (6): iDay = "Thứ 7"; break;

        }
        strDateOut = iDay + ", ngày " + iDate + " tháng " + iMonth + " năm " + strYear;
    }
    else {
        switch (iDay) {
            case (0): iDay = "Sunday"; break;
            case (1): iDay = "Monday"; break;
            case (2): iDay = "Tuesday"; break;
            case (3): iDay = "Wednesday"; break;
            case (4): iDay = "Thursday"; break;
            case (5): iDay = "Friday"; break;
            case (6): iDay = "Saturday"; break;
        }


        //        switch (today.getMonth() + 1) {
        //            case (1): iMonth = "January"; break;
        //            case (2): iMonth = "Febuary"; break;
        //            case (3): iMonth = "March"; break;
        //            case (4): iMonth = "April"; break;
        //            case (5): iMonth = "May"; break;
        //            case (6): iMonth = "June"; break;
        //            case (7): iMonth = "July"; break;
        //            case (8): iMonth = "August"; break;
        //            case (9): iMonth = "Septemper"; break;
        //            case (10): iMonth = "October"; break;
        //            case (11): iMonth = "November"; break;
        //            case (12): iMonth = "December"; break;


        //        }
        strDateOut = iDay + " , " + iDate + "/" + iMonth + "/" + strYear;
    }

    jQuery(".leftTime").text(strDateOut);
}
function doDel() {
    key = '';
    $('input[@type=checkbox][group=check]').each(function() {
        if ($(this).attr("checked"))
            key += $(this).attr("id") + "|";
    })

    if (key == "" || key == undefined) {

        alert("Chọn đối tượng để xóa");
        return false;
    }
    else {
        $("input[id*=hidKeyDel]").attr("value", key);
        return doConfirm('Xác nhận việc xóa');
    }
}

var newwindow;
function popup(url, width, height) {
    newwindow = window.open(url, 'name', 'width='+width+',height='+height+',scrollbars=1');
    if (window.focus) { newwindow.focus() }
    newwindow.moveTo(200, 150);
}

function doInitSort() {
    $(".child").sortable({ cursor: 'move', opacity: 0.6 });
    $("ul, li").disableSelection();
}

function extractString(source, length) {
    dest = '';
    if (length == 0 || source.length == 0) {
        return dest;
    }
    if (source.length < length) {
        dest = source;
    }
    else {
        tmp = source.substring(0, length);
        nSub = tmp.lastIndexOf(' ');

        dest = tmp.substring(0, nSub) + " ...";
    }

    return dest;
}
function fixFloat(perRow, classDivBound, classItemBound) {
    $("div[class=clear]", "." + classDivBound).remove();
    $("." + classItemBound, "." + classDivBound).each(function() {
        index = $("." + classItemBound, "." + classDivBound).index($(this));
        if ((index) % perRow == 0) {
            $(this).before("<div class='clear'></div>");
        }
    })
}


this.imagePreview = function() {
    /* CONFIG */

    xOffset = 10;
    yOffset = 30;

    // these 2 variable determine popup's distance from the cursor
    // you might want to adjust to get the right result

    /* END CONFIG */
    $("body").ajaxComplete(function() {
        $(".mainContent img").hover(function(e) {
            this.t = this.title;
            this.title = "";
            var c = (this.t != "") ? "<br/>" + this.t : "";
            var src = this.src;
            src = src.replace('thumb/', '');
            $("body").append("<p id='preview' title='halong cruise'><img src=" + src + "></p>");
            $("#preview")
			    .css("top", (e.pageY - xOffset) + "px")
			    .css("left", (e.pageX + yOffset) + "px")
			    .fadeIn("fast");
        },
	    function() {
	        this.title = this.t;
	        $("#preview").remove();
	    });
        $(".mainContent img").mousemove(function(e) {
            $("#preview")
			    .css("top", (e.pageY - xOffset) + "px")
			    .css("left", (e.pageX + yOffset) + "px");
        });
    })

    $(".mainContent img").hover(function(e) {
        this.t = this.title;
        this.title = "";
        var c = (this.t != "") ? "<br/>" + this.t : "";
        var src = this.src;
        src = src.replace('thumb/', '');        
        $("body").append("<p id='preview' title='halong cruise'><img src=" + src + "></p>");
        $("#preview")
			    .css("top", (e.pageY - xOffset) + "px")
			    .css("left", (e.pageX + yOffset) + "px")
			    .fadeIn("fast");
    },
	    function() {
	        this.title = this.t;
	        $("#preview").remove();
	    });
    $(".mainContent img").mousemove(function(e) {
        $("#preview")
			    .css("top", (e.pageY - xOffset) + "px")
			    .css("left", (e.pageX + yOffset) + "px");
    });
};


// starting the script on page load
$(document).ready(function() {
    imagePreview();
});

function doPrint(id, type) {
    popup("/print.aspx?id="+id+"&type="+type,1003,600);
}
function doSendToFriend(id,type) {
    popup("/SendToEmail.aspx?id=" + id + "&type=" + type,400,300);
}
