
jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

$(function(){

   var textEmail = "Email *";
   var textAuthor = "Họ Tên *";
   var textPhone = "Số điện thoại";
   var textContent = "Nội Dung *";
   $('#comment input[type=text],#comment textarea').focus(function() {
       if($(this).val() == textAuthor || $(this).val() == textEmail || $(this).val() == textPhone || $(this).val() == textContent)
           $(this).val('');
   });

   $('#comment input[type=text],#comment textarea').blur(function() {
       if($('#comment #author').val() == "") $(this).val(textAuthor);
       if($('#comment #email').val() == "") $(this).val(textEmail);
       if($('#comment #phone').val() == "") $(this).val(textPhone);
       if($('#comment #content').val() == "") $(this).val(textContent);
   });
    var postid = $("#comment #postid").val();
    $(".submit").click(function(){
        var author = $("#comment #author").val();
        if(author == "" || author == textAuthor) {
            alert("Vui lòng điền họ tên của bạn !");
            return false;
        }
        var email = $("#comment #email").val();
        if(email == "" || email == textEmail) {
            alert("Vui lòng điền email của bạn !");
            return false;
        }
        var phone = $("#comment #phone").val();
        var content = $("#comment #content").val();
        if(content == "" || content == textContent) {
            alert("Vui lòng điền nội dung !");
            return false;
        }
        var dataString = 'author='+ author + '&email=' + email + '&phone=' + phone + '&content=' + content + '&postid=' +postid;
        $("#result").html('Loading Comment...');
        $.ajax({
            type: "POST",
            url: "/home/index.php?site/comment/add/",
            data: dataString,
            cache: false,
            success: function(html){
                $("#result").show().html('Bình luận của bạn sẽ được xét duyệt trước khi hiển thị ở tin này, xin cảm ơn !'); 
            }
        }); 
        
        return false;
    });
    
    listcomment(postid);
    
    $(".list-thread li, #relatednews li, #pdiscount li").hover(
        function(){
            var height = $(this).find(".popup").height();
            var top = height/2-7;
            $(this).find(".popup").css({"top": -top}).show();
        }, function() {
            $(this).find(".popup").hide();
    });
    
    $(".quickview").click(function(){
        var width = $(document).width(); 
        var height = $(document).height(); 
        $("#background").css({"opacity" : "0.8","width" : width, "height" : height}).fadeIn(250);
        var link = $(this).find("a").attr("href");
        $("#quickview").center().fadeIn(250);
        $("#viewcontent").html('<iframe width="100%" height="500" frameborder="0" src="'+link+'" id="frameview"></iframe>');
        return false;
    });
    
    $("#close, #background").click(function(){
        close();
    });
    
    $("#newsdetail .ilike").click(function(){
        var postid = $(this).find("a").attr("id");
        $.ajax({
            url: "/home/index.php?site/news/like/id/"+postid,
            success: function() {
                var totallike = $("#totallike").text();
                if(totallike == 0) var mess = "Bạn là người đầu tiên thích bài này";
                else var mess = "Có "+ totallike + " người cùng thích bài này với bạn"
                $(".ilike").css({"background" : "none","padding-left": "0px","padding-right": "0px"}).html(mess);
            }
        });
    });
    
    $("#videodetail .ilike").click(function(){
        var videoid = $(this).find("a").attr("id");
        $.ajax({
            url: "/home/index.php?site/video/like/id/"+videoid,
            success: function() {
                var totallike = $("#totallike").text();
                if(totallike == 0) var mess = "Bạn là người đầu tiên thích video này";
                else var mess = "Có "+ totallike + " người cùng thích video này với bạn"
                $(".ilike").css({"background" : "none","padding-left": "0px","padding-right": "0px"}).html(mess);
            }
        });
    });

    $(".toplink .thumb").live("mouseenter",
    function(){
                    var image = $(this).find("img").attr("src");
        $(this).find(".popup-image").show();
    }).live("mouseleave",function() {
        $(this).find(".popup-image").hide();
    });

	toplink(1);

	$("#linkpage li").click(function(){
		var page = $(this).attr("id");
		toplink(page);
	});
	
	var autoload = function () {
		var page = $("#linkpage li.current").attr("id");
		if(page==3) page = 1;
		else if(page ==1) page = 2;
		else if(page ==2) page = 3;
		toplink(page);
	}

	var timer = setInterval(autoload, 8000);

	setInterval(function(){ 
                
		 $('#pdiscount li:first-child').appendTo('#pdiscount');
			discount();
	},4000);

	var discount = function() {
		$('#pdiscount li').each(function(index) {
				if(index < 6) $(this).show();
				else $(this).hide();
		});
	}
	
	discount();

	$("#toplink").live("mouseenter", function(){
        	clearInterval(timer);
		}).live("mouseleave",function() {
            var timer = setInterval(autoload, 8000);
			$("#toplink").live("mouseenter", function(){
        		clearInterval(timer);
			});
    });

	var menuid = $("#menu").attr('class');

	if(menuid != '') {
		$("#menu li[id="+menuid+"]").addClass('active');
		$("#support,#cdmp-icon").hide();
	}

	$("#menu").hover(function(){

		$("#menu li.active").removeClass('active');
		$("#support,#cdmp-icon").hide();

	},function() {
		$("#support,#cdmp-icon").show();
	});

});

function toplink(page) {

	$("#linkpage li").removeClass("current");

	$("#linkpage li:nth-child("+page+")").addClass("current");
	$("#toplink").fadeOut('slow',function(){
		$.ajax({
	       url: "/home/?site/widget/toplink/page/"+page,
	       success: function(html) {
	           $("#toplink").html(html).fadeIn('slow');
	       }
	    });
	});
}

function close() {
    $("#background").fadeOut(250);
    $("#quickview").fadeOut(250);
}

function listcomment(postid) {
    $.ajax({
       url: "/home/index.php?site/comment/index/postid/"+postid,
       success: function(comment) {
           $("#list-comment").html(comment);
       }
    });
}

function popup(url) {
   popupWindow(url,'name',700,500,'yes');
   return false;
}

function popupWindow(mypage, myname, w, h, scroll) {
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    winprops = 'height='+ h + ',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable';
    win = window.open(mypage, myname, winprops);
    if (parseInt(navigator.appVersion) >= 4) {win.window.focus();}
}


