﻿var ie = document.all;
function iecp() { return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body; }
function getScrollX() { return (ie && !window.opera) ? iecp().scrollLeft : window.pageXOffset; }
function getScrollY() { return (ie && !window.opera) ? iecp().scrollTop : window.pageYOffset; }

var isLogin = 0;
var _waiting = "<div class='waiting'></div>";

// Menu
var timeout         = 500;
var closetimer		= 0;
var ddmenuitem      = 0;
var p =0;
var sp=-1;
var delayMenu;

function jsddm_setclass(page,subpage)
{
    p=page;
    sp=subpage;

    $(document).ready(function(){
        $("#menu li.menu-item:eq(" +page+ ")").addClass("selected");
        $("#menu li.menu-item:eq(" +page+ ")").find('ul').eq(0).css('visibility', 'visible');
        $("#menu li.menu-item:eq(" +page+ ")").find('li:eq(' +subpage+ ') a').css("color","red");
    });
}
function jsddm_callback()
{
    jsddm_close();
    $("#menu li").removeClass("selected");
    $(document).ready(function(){
        $("#menu li.menu-item:eq(" +p+ ")").addClass("selected");
        $("#menu li.menu-item:eq(" +p+ ")").find('ul').eq(0).css('visibility', 'visible');
        $("#menu li.menu-item:eq(" +p+ ")").find('li:eq(' +sp+ ') a').css("color","red");
    });
    clearTimeout(delayMenu);
}

function jsddm_open()
{	
    clearTimeout(delayMenu);
	jsddm_close();
	$("#menu li.menu-item:eq(" +p+ ")").find('ul').eq(0).css('visibility', 'hidden');
	ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');
	delayMenu = setTimeout("jsddm_callback()",2000);
}

function jsddm_close()
{	
    if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
}

function jsddm_timer()
{
	closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer()
{	if(closetimer)
	{	
	    window.clearTimeout(closetimer);
		closetimer = null;
	}
}
//menu close

document.onclick = jsddm_close;

function vote_success() {
    alert("Cảm ơn bạn đã tham gia bình chọn");
}
function Vote() {
    if (isLogin == 0) {
        Login();
    }
    else {
        $('#f-vote').submit();
    }
}

function vote_success() {
    sNotify.addToQueue("Cảm ơn bạn đã tham gia bình chọn");
}
function Vote() {
    if (isLogin == 0) {
        Login();
    }
    else {
        $('#f-vote').submit();
    }
}
function GetVideo(id) {
    $.ajax({
        url: '/WebService.asmx/TutorialVideo',
        data: '{id:' + id + '}',
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            $.fancybox({ content: data.d });
        }
    });
}
function GetWiki(url) {
    $('#wiki-content').html(_waiting);
    $.get('/Wiki/GetWiki?type=' + url + '&r=' + Math.random(), function (data) {
        $('#wiki-content').html(data);
        lta();
    });
}

function ExpirationDate(s) {
    var s = s - 1;
    var html = '';
    if (s >= 0) {
        var h = Math.floor(s / 3600);
        var m = Math.floor((s - h * 3600) / 60);
        var ss = s - h * 3600 - m * 60;
        html = '<span class="red">' + h + '</span> giờ <span class="red">' + m + '</span> phút <span class="red">' + ss + '</span> giây';
    }
    else {
        html = '<span class="red">Hết hạn</span>';
    }
    $('#expiration-date').html(html);
    var t = setTimeout('ExpirationDate(' + s + ')', 1000);
}

function Login() {
    $.get('/Account/Login', function (d) {
        $.fancybox({ content: d });
    });
}

function Buy(id, quantity) {
    $.post('/Cart/Buy/', { id: id, quantity: quantity }, function (data) {
        $('.cart span').html(data.number);
        $('.nvc-d').css('display', 'none');
        sNotify.addToQueue(data.status);
    });
}

function Like(t,url) {
    if (isLogin == 0) {
        Login();
    }
    else {

        $.post(url, function (d) {
            if (d == true) {
                var l = $(t).parents('.like-box:first').find('.like-count:first');
                $(l).html(Math.abs($(l).html()) + 1);
                $(t).css('display','none');
            }
        });
    }
}
function DisLike(t, url) {
    if (isLogin == 0) {
        Login();
    }
    else {
        $.post(url, function (d) {
            if (d == true) {
                var l = $(t).parents('.like-box:first').find('.like-count:first');
                $(l).html(Math.abs($(l).html()) - 1);
                $(t).css('display', 'none');
            }
        });
    }
}

function LikeList(url) {
    $('.nvc-d').css('display', 'none');
    $.post(url, function (d) {
        $.fancybox({ content: d });
    });
}

function AddFriend(id) {
    if (isLogin == 0) {
        Login();
    }
    else {
        $('.nvc-d').css('display', 'none');
        $.post('/Profile/AddFriend/' + id);
    }
}

function MutualList(url) {
    $('.nvc-d').css('display', 'none');
    $.post(url, function (d) {
        $.fancybox({ content: d });
    });
}

var arrBoxUrl = new Array();

function lta() {
    $('.nvc-a').mouseover(function (e) {
        var t = this;
        var d = $('.nvc-d');
        var cd = $(t).find('.nvc-cd');
        d.css('left', $(t).offset().left + 300 > $(window).width() ? $(window).width() - 300 : $(t).offset().left);
        d.css('top', $(t).offset().top - 10 - $(d).height() > getScrollY() ? $(t).offset().top - $(d).height() : $(t).offset().top + $(t).height());
        d.css('display', 'block');
        d.html(_waiting);

        var flag = false;
        for (var i = 0; i < arrBoxUrl.length; i++) {
            if (arrBoxUrl[i] == $(t).attr('nvc-u')) {
                flag = true;
            }
        }
        if (flag == false) {
            arrBoxUrl.push($(t).attr('nvc-u'));
            $.post($(t).attr('nvc-u'), function (data) {
                d.html(data);
                $('body').append('<div class="nvc-r" nvc-u="' + $(t).attr('nvc-u') + '" style="display:none;">' + data + '</div>');
            });
        }
        else {
            var rd = $('.nvc-r');
            for (var i = 0; i < rd.length; i++) {
                if ($(rd[i]).attr('nvc-u') == $(t).attr('nvc-u')) {
                    d.html($(rd[i]).html());
                }
            }
        }
    });

    $('.nvc-a').mouseout(function () {
        $('.nvc-d').css('display', 'none');
    });
    $('.nvc-d').mouseover(function () {
        $(this).css('display', 'block');
    });
    $('.nvc-d').mouseout(function () {
        $(this).css('display', 'none');
    });
}

function ChangeSize(c, w, h) {

    $("." + c).attr("id", function (arr) {
        return c + arr;
    });

    var outW = w;
    var outH = h;

    for (var i = 0; i < document.images.length; i++) {
        var imgName = c + i;
        var testImg = document.images[imgName];
        if (testImg != null && testImg.width != null) {
            var inW = testImg.width;
            var inH = testImg.height;

            var perW = Math.abs(outW / inW);
            var perH = Math.abs(outH / inH);

            if (perW <= 1 || perH <= 1) {
                if (perW < perH) {
                    testImg.width = outW;
                    testImg.height = Math.floor(outW * Math.abs(inH / inW));
                }
                else {
                    testImg.height = outH;
                    testImg.width = Math.floor(outH * Math.abs(inW / inH));
                }
            }
        }
    }
}
function ReComment() {
    $('.txt-recomment').keydown(function (e) {
        var t = this;
        if (e.which == 13) {
            if (isLogin == 0) {
                Login();
            }
            else {
                if ($(t).val() != '') {
                    var cmd = {
                        id: oid,
                        des: $(t).val(),
                        p: $(t).attr("rel"),
                        ot: otid
                    };
                    $(t).val('');
                    $.post('/Comment/ReComment', cmd, function (d) {
                        $(t).parents('.box-recomment:first').find('.recomment').append(d);
                        lta();
                    });
                }
            }
        }
    });
}
function ViewAllReCommet(t, id) {
    $(t).css('display', 'none');
    $.post('/Comment/ReComments/' + id, function (d) {
        $(t).parents('.box-recomment:first').html(d + $(t).parents('.box-recomment:first').html());
        lta();
        ReComment();
    });
}
function MoreComment(t) {
    page += 1;
    var cmd = {
        id: oid,
        p: page,
        ot: otid
    };
    $.post('/Comment/MoreComment', cmd, function (d) {
        $('#html-comment').append(d);
        lta();
        ReComment();
        if (d == "") {
            $(t).html("Không còn tin khác");
        }
    });
}
function PostsDelete(t,id) {
    if (isLogin == 0) {
        Login();
    }
    else {
        $.post('/Posts/Delete', { id: id }, function (d) {
            if (d == true) {
                $(t).parents(".post-item:first").remove();
            }
        });
    }
}
function PostsComment() {
    $('.txt-posts-comment').keydown(function (e) {
        var t = this;
        if (e.which == 13) {
            if (isLogin == 0) {
                Login();
            }
            else {
                if ($(t).val() != '') {
                    var cmd = {
                        id: $(t).attr('rel'),
                        des: $(t).val()
                    };
                    $(t).val('');
                    $.post('/Posts/Comment', cmd, function (d) {
                        $(t).parents('.box-posts-comment:first').find('.posts-comment').append(d);
                        lta();
                    });
                }
            }
        }
    });
}
function MorePosts(t) {
    page += 1;
    var cmd = {
        id: oid,
        p: page
    };
    $.post('/Posts/MorePosts', cmd, function (d) {
        $('#html-posts').append(d);
        lta();
        PostsComment();
        if (d == "") {
            $(t).html("Không còn tin khác");
        }
    });
}
function MoreNewsFeed(t) {
    page += 1;
    var cmd = {
        id: oid,
        p: page
    };
    $.post('/Posts/MoreNewsFeed', cmd, function (d) {
        $('#html-posts').append(d);
        lta();
        PostsComment();
        if (d == "") {
            $(t).html("Không còn tin khác");
        }
    });
}
function ViewAllPostsCommet(t, id) {
    $(t).css('display', 'none');
    $.post('/Posts/Comments/' + id, function (d) {
        $(t).parents('.box-posts-comment:first').html(d + $(t).parents('.box-posts-comment:first').html());
        lta();
        PostsComment();
    });
}
$(document).ready(function () {
    

    //$('.tipsy-s').tipsy({gravity: 'sw'});

    $.post('/Account/CheckLogin', function (d) {
        isLogin = d.isLogin;
        avatar = d.avatar;
    });
    $('#btn-login').fancybox();

    $('#txt-search').keydown(function (e) {
        if (e.which == '13') {
            location.href = '/Search/Index?q=' + $('#txt-search').val().replace(/"/gi, " ");
        }
    });

    $('#txt-search').autocomplete({
        delay: 0,
        source: function (request, response) {
            $.get('/Search/AutoComplete?q=' + $('#txt-search').val(), function (data) {
                response($.map(data, function (item) {
                    return {
                        value: item.name
                    }
                }))
            });
        },
        minLength: 1
    });

    $('.fc-box').fancybox();

    lta();

    //menu
    $('#menu > li').bind('mouseover', jsddm_open);
    $("#menu li.menu-item").mouseover(function () {
        var t = this;
        $("#menu li").removeClass("selected");
        $(t).toggleClass("selected");
    });
    //close menu

    $('input:text,input:password,textarea').focus(function () {
        $(this).css("border-color", "#999999");
    });
    $('input:text,input:password,textarea').blur(function () {
        $(this).css("border-color", "#cccccc");
    });

    $('#product-num').keyup(function (e) {
        if (isNaN($('#product-num').val()) == true || $('#product-num').val() < 0 || e.which == '190') {
            $('#product-num').val('1');
        }
    });

    $('.news-top-menu li').click(function () {
        var t = this;
        $('.news-top-content').html('<span class="white">Đăng tải ...</span>');
        $('.news-top-menu li').removeClass('top-select');
        $(t).addClass('top-select');
        $.ajax({
            url: '/WebService.asmx/GetNewsTop',
            data: '{id:' + $(t).attr('rel') + '}',
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                var html = "";
                for (var i = 0; i < data.d.length; i++) {
                    html += '<li><a href="/News/Detail/' + data.d[i].id + '">' + data.d[i].title + '</a></li>';
                }
                $('.news-top-content').html(html);
            }
        });

    });

});
