/* ManhTienpt :  By ManhTienpt - admin@viet.pro*/ 
$(function() {
	phimso_com();
	$('ul.spy').simpleSpy();
});
function phimso_com () {
	$(".formvalidate").validate();
	$('#slideTop').bxSlider({
	    easing: 'easeOutBounce',
	    mode: 'vertical',
	    speed: 2000,
	    pager: true,
	    infiniteLoop: true,
	    auto: true
	});
	$('#slideBotom').bxSlider({
		easing: 'easeOutBounce',
		speed: 2000,
		displaySlideQty: 3,
	    moveSlideQty: 3,
	    auto: true,  
	    pager: true
    });
	$(".tab_content").hide();
	$("ul.tabs li:first").addClass("active").show();
	$(".tab_content:first").show();
	$("ul.tabs li").click(function(){
		$("ul.tabs li").removeClass("active");
		$(this).addClass("active");
		$(".tab_content").hide();
		var b=$(this).find("a").attr("href");
		$(b).fadeIn();
		return false;
	});
	$(".tab_att_content").hide();
	$("ul.tabs_att li:first").addClass("active").show();
	$(".tab_att_content:first").show();
	$("ul.tabs_att li").click(function(){
		$("ul.tabs_att li").removeClass("active");
		$(this).addClass("active");
		$(".tab_att_content").hide();
		var b=$(this).find("a").attr("href");
		$(b).fadeIn();
		return false;
	});
	
	$("a[class='more_info']").click(function(){
		$(".show_info_taxonomy").slideDown();
		$(".hide_taxonomy_info").click(function(){
			$(".show_info_taxonomy").slideUp();
		});
	});
	$(".menu_taxonomy_info").click(function(){
		var infoshow = $(this).attr("rel");
		$(".menu_taxonomy_info").removeClass("active");
		$(this).addClass("active");
		$(".content_taxonomy_info").slideUp('fast');
		$(".taxonomy_"+infoshow).slideDown();
	});

	$("#u_rate").mouseover(function(){
		$(".rate_frame").slideDown();
		$(".user_rate").slideDown();
	});
	$(".report_ep").click(function(){
		$(".ep_error").addClass("loadding");
		var ep_id = $(".ep_play").attr("ep");
		var sv_id = $(".ep_play").attr("sv");
		var data_post = {
				method:'report_ep',
				ep:ep_id
		}
		$.post(ajaxurl, data_post, function(response){
			if(response.length > 0) {
				$(".ep_error").html(response).slideDown();
				$(".ep_error").removeClass("loadding");
			}else {
				$('.ep_error').html('<span class="error_box">Lỗi, mong bạn thử lại.</span>').fadeIn(300);
				$('.ep_error').removeClass('loadding');
			}
		});
	});
	/* ManhTienpt: comment */
	$('.send_comment').click(function(){
		$('.tb_comment').html('');
		$('.tb_comment').addClass('loadding');
		var comment_post_ID = parseInt($("input[name='comment_post_ID']").val());
		var film_id = parseInt($("input[name='film_id']").val());
		var comment_name = $("input[name='author']").val(); 
		var comment_email = $("input[name='email']").val();
		var comment_url = $("input[name='url']").val();
		var comment_content = $("textarea[name='comment']").val();
		var data_post = {
				method:'post_comment',
				comment_post_ID:comment_post_ID,
				author:comment_name,
				email:comment_email,
				url:comment_url,
				comment:comment_content
			}
		$.post(ajaxurl, data_post, function(response){
			if(response.length>0){
				$('.tb_comment').html(response).slideDown('slow');
				$('.tb_comment').removeClass('loadding');
				$("ul[class='comment']").addClass('loadding');
				var data_get = {
						method:'get_comment',
						comment_post_ID:comment_post_ID,
						author:comment_name,
						email:comment_email,
						url:comment_url,
						comment:comment_content
					}
				if(comment_content.length>0) {
					$.post(ajaxurl, data_get, function(response){
						if(response.length>0) {
								setTimeout(function(){
									$("ul[class='comment loadding']").prepend(response).fadeIn('slow');
									$("ul[class='comment loadding']").removeClass('loadding');
									}, 1000);
								$(".f_comment").slideUp();
						} else {
							$("ul[class='comment loadding']").removeClass('loadding');
						}
					});
				} else {
					$('.tb_comment').html('<div class="error_box">Bạn chưa  nhập nội dung cảm nhận</div>').slideDown('slow');
					$('.tb_comment').removeClass('loadding');
				}
				
			} else {
				$('.tb_comment').html('<div class="error_box">Gửi cảm nhận không thành công, mong bạn thử lại.</div>').slideDown('slow');
				$('.tb_comment').removeClass('loadding');
			}
		});
		return false;
	});
	$("ul.pages li a").click(function(){
		$("ul[class='comment']").addClass('loadding');
		var film_id = parseInt($("input[name='film_id']").val());
		var p 		= parseInt($(this).attr("rel"));
		
		var data_post = {
				method:'show_comment',
				film_id:film_id,
				p:p
		}
		$.post(ajaxurl, data_post, function(response){
			if(response.length > 0) {
				$("ul.pages li a").removeClass("active");
				$("ul.pages li a[rel='"+p+"']").addClass("active");
				setTimeout(function(){
					$("ul[class='comment loadding']").html(response).fadeIn('slow');
					$("ul[class='comment loadding']").removeClass('loadding');
					}, 1000);
			} else {
				$("ul[class='comment loadding']").html('<div class="error_box">Lỗi, không lấy được dữ liệu yêu cầu.</div>').fadeIn('slow');
				$("ul[class='comment loadding']").removeClass('loadding');
			}
		});
	});
/* ManhTienpt:  RATE STAR */ 
	$('.rate_submit').click(function(){
		$('.rate_loading').addClass('loadding');
		$('.rate_loading').html('Đang tải dữ liệu ...');
		var content 				= $('.rate_lage').contents();
		var film_id 				= parseInt($("input[name='film_id']").val());
		var noidung_phim 			= parseInt($("input[name='poll-1']").val());
		var chatluong_phim 			= parseInt($("input[name='poll-2']").val());
		var kyxao_phim 				= parseInt($("input[name='poll-3']").val());
		var dienxuat_phim 			= parseInt($("input[name='poll-4']").val());
		var camnhan_phim 			= parseInt($("input[name='poll-5']").val());
		var rating 					= parseInt($(".attr_rate").attr("vietpro_rating"));
		var rating_total			= parseInt($(".attr_rate").attr("vietpro_rating_total"));
		var vietpro_rating 			= parseInt(rating + noidung_phim + chatluong_phim + kyxao_phim + dienxuat_phim + camnhan_phim);
		var vietpro_rating_total 	= parseInt(rating_total + 1);
		var data_post = {
				method:'postdata',
				film_id:film_id,
				nd:noidung_phim,
				cl:chatluong_phim,
				kx:kyxao_phim,
				dx:dienxuat_phim,
				cn:camnhan_phim
		}
		$.post(ajaxurl, data_post, function(response){
			if(response.length > 0) {
				$('.rate_loading').html(response).fadeIn(300);
				$('.rate_loading').removeClass('loadding');
				$('.rate_lage').addClass('loadding');
				$('.rate_lage').html('Đang tải dữ liệu ...');

				var data_get = {
						method:'getdata',
						film_id:film_id,
						rating:vietpro_rating,
						rating_total:vietpro_rating_total,
						nd:noidung_phim,
						cl:chatluong_phim,
						kx:kyxao_phim,
						dx:dienxuat_phim,
						cn:camnhan_phim
				}
				$.post(ajaxurl, data_get, function(response){
					if(response.length > 0) {
						$('.rate_lage').removeClass('loadding');
						$('.rate_lage').html(response).fadeIn(1000);
						$(".rate_frame").removeClass('rate_frame').addClass('rate_notice');
					} 
					else {
						$('.rate_lage').removeClass('loadding');
						$('.rate_lage').html(content).fadeIn(1000);
					}
				});
			}
			else {
				$('.rate_lage').append('<span>Lỗi, mong bạn thử lại.</span>').fadeIn(300);
				$('.rate_lage').removeClass('loadding');
			}
		});
		return false;
	});
	
	
	var $caption, $cap = $("<span/>").addClass("caption");

	$(".multiField").children().hide();


	// Poll-1
	$caption = $cap.clone();
	$("#noidung-phim")
		.stars({
			cancelValue: 999,
			cancelShow: false,
			captionEl: $caption
		})
		.append($caption);

	// Poll-2
	$caption = $cap.clone();
	$("#chatluong-phim")
		.stars({
			cancelValue: 999,
			cancelShow: false,
			captionEl: $caption
		})
		.append($caption);

	// Poll-3
	$caption = $cap.clone();
	$("#kyxao-phim")
		.stars({
			cancelValue: 999,
			cancelShow: false,
			captionEl: $caption
		})
		.append($caption);

	// Poll-4
	$caption = $cap.clone();
	$("#dienxuat-phim")
		.stars({
			cancelValue: 999,
			cancelShow: false,
			captionEl: $caption
		})
		.append($caption);

	// Poll-5
	$caption = $cap.clone();
	$("#camnhan-phim")
		.stars({
			cancelValue: 999,
			cancelShow: false,
			captionEl: $caption
		})
		.append($caption);
	// Ket thuc RATE STAR
	
	
}
/* ManhTienpt:  ket thuc rate */ 


(function ($) {
    
$.fn.simpleSpy = function (limit, interval) {
    limit = limit || 3;
    interval = interval || 4000;
    
    return this.each(function () {
        // 1. setup
            // capture a cache of all the list items
            // chomp the list down to limit li elements
        var $list = $(this),
            items = [], // uninitialised
            currentItem = limit,
            total = 0, // initialise later on
            height = $list.find('> li:first').height();
            
        // capture the cache
        $list.find('> li').each(function () {
            items.push('<li>' + $(this).html() + '</li>');
        });
        
        total = items.length;
        
        $list.wrap('<div class="spyWrapper" />').parent().css({ height : height * limit });
        
        $list.find('> li').filter(':gt(' + (limit - 1) + ')').remove();

        // 2. effect        
        function spy() {
            // insert a new item with opacity and height of zero
            var $insert = $(items[currentItem]).css({
                height : 0,
                opacity : 0,
                display : 'none'
            }).prependTo($list);
                        
            // fade the LAST item out
            $list.find('> li:last').animate({ opacity : 0}, 1000, function () {
                // increase the height of the NEW first item
                $insert.animate({ height : height }, 1000).animate({ opacity : 1 }, 1000);
                
                // AND at the same time - decrease the height of the LAST item
                // $(this).animate({ height : 0 }, 1000, function () {
                    // finally fade the first item in (and we can remove the last)
                    $(this).remove();
                // });
            });
            
            currentItem++;
            if (currentItem >= total) {
                currentItem = 0;
            }
            
            setTimeout(spy, interval)
        }
        
        spy();
    });
};
    
})(jQuery);
