$(document).ready(function(){
    $("#mobile-number").numeric();    
    $('#player-option').click(function() {
                    $(this).addClass("selected-export-option");
                    $(this).siblings().removeClass("selected-export-option")
                  });
	$(".default-info").css("display", "block");

	//ACCORDION VIDEOS
	$("#video-right-col div:first").siblings("a:first").css({background:"url('/images/accordion-active-icon.png') no-repeat 0px 3px"});

	$("#video-right-col .accordion-items").mouseover(function()
	{
	   $(this).css({background:"url('/images/accordion-active-icon.png') no-repeat 0px 3px"}).next("div").slideDown(500).siblings("div").slideUp("slow").addClass("current-accordion");
	   $(this).siblings("a").css({background:"url('/images/accordion-default-icon.png') no-repeat 0px 3px"}).removeClass("current-accordion");
	});

	$("#slider").easySlider({
		prevText: "",
		nextText: "",
		continuous: true
	});

	// SCROLL DOWN TO THE COMMENT AREA
	function getPageSizeWithScroll(){
		if (window.innerHeight && window.scrollMaxY) {// Firefox
			yWithScroll = window.innerHeight + window.scrollMaxY;
			xWithScroll = window.innerWidth + window.scrollMaxX;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			yWithScroll = document.body.scrollHeight;
			xWithScroll = document.body.scrollWidth;
		} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
			yWithScroll = document.body.offsetHeight;
			xWithScroll = document.body.offsetWidth;
	  	}
		arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
		return arrayPageSizeWithScroll;
	}

	$(".medium-link-btn").click(function(){
		var position = getPageSizeWithScroll();
		$('html,body').animate({scrollTop: position[1] - 600}, 1000);
	});

	// EVENT MARK VIDEO
	$(".marks").mouseover(function() {
		$(this).attr({src: "/images/large-mark-star.png"});
		$(this).prevAll().attr({src: "/images/large-mark-star.png"});
		$(this).nextAll().attr({src: "/images/large-mark-empty-star.png"});
	})
        $(".marks").click(function() {
                $.post("video/ajaxwebrating", { id : $("#video-info-link").attr("name") ,rate : $(this).attr("title") },function(message){
                    $(".marks").each(function(){
                        $(this).hide();
                    });
                    $(".marks").parent("li").html(message);
                });
        });

    // EVENT RECOMMAND A VIDEO BY SMS
    $("#recommandBySms-form").submit(function(){
        var submitedmobilenumber=$("#mobile-number").val();
		var nb = submitedmobilenumber.substring(0,2);
		if(submitedmobilenumber.length==10 && nb=='06') {
	          $.post("video/ajaxwebsms", {
	                idvideo:$("#videoid").attr("value"),
	                mobilenumber:submitedmobilenumber
	                },function(message){
	                    $('#recommandBySms-form').fadeOut().remove();
	                    $("#recommandBySms-form-result").html('<br/>'+message);
	              });
	        }
	        else
	            {
				$('#recommandBySms-form-result').children('div').remove();
	            $("#recommandBySms-form-result").append('<div id="recommand-sms-error" style="position: absolute; margin-top: 25px;"><span style="color:red;">Ce numéro est invalide.</span></div>');
	        }
			return false;
	     });

         // Doesn't work
         /*var a = $(".sms-result").children("span").attr("style");
         if(a=="color: green;")
            {
                window.setTimeout(function() {
                    $(".sms-result").children("span").remove();
                }, 4000);
            }*/

	// SWITCH INFO VIDEO DIV
	$(".info-vid-link").click(function(){
		$(".info-vid-link").each(function(){
			$(this).css({background: "url('/images/picto/black/"+ $(this).attr("id") +".png') no-repeat"});
		});

		$(this).parent("li").parent("ul").parent("div").siblings().css("display", "none");
		$(this).parent("li").parent("ul").parent("div").siblings("."+$(this).attr("id")).fadeIn("slow");
		$(this).css({background: "url('/images/picto/colored/"+ $(this).attr("id") +".png') no-repeat"});
	});

	$("#video-info-link").click(function() {
		$(this).parent().siblings().css("display", "none");
		$(".default-info").fadeIn("slow");
	});

    $('#socialnetworks-paste-links .medium-textbox-socialnetworks').live("click", function(){
        $(this).select();
    });

    $('.medium-customize-textbox').live("click", function(){
        $(this).select();
    });

       var defaultExportValue = $('#medium-player-option').attr("name");
       $('#export-script').attr("value",defaultExportValue);
       $('#export-script').select();

    $('#select-export-size').children('li').click(function() {
       $(this).addClass('selected-export-option');
       $(this).siblings().removeClass('selected-export-option');
       var newValue = $(this).attr("name");
       $('#export-script').attr("value",newValue);
       $('#export-script').select();
    });

     $('#help-embed').click(function(){
         $('#export-size-notification').animate({
         height: 'toggle', opacity: 'toggle'
         }, "slow");
         return false;
     });


    //COMMENTS PAGINATION
    function onPaginatorClick(){
        $("span.orang-span.paginator").replaceWith('<a class="paginator" title="' + $("span.orang-span.paginator").attr("title") + '" href="#" >' + $("span.orang-span.paginator").text() + '</a>');
        $(this).replaceWith('<span class="paginator orang-span" title="'+ $(this).attr("title") +'" ><b>' + $(this).text() + '</b></span>');

            $.get("/video/video/ajaxshowcomments", { videoid: $("#video-info-link").attr("name") , pagecom: $(this).attr("title") }, function(response){
                $("#comments-bubbles").html(response);
            });

            //reaplique l'evènement click a tout les paginators
            $("a.paginator").each(function(){
                $(this).click(onPaginatorClick);
        });
        return false;
    }
    
    $("a.paginator").each(function(){
        $(this).click(onPaginatorClick);
    });

    //POST COMMENT
    function TrimComment(string){
        return string.replace(/^\s+/g,'').replace(/\s+$/g,'');
    }

    $("#post_comment_form").submit(function(){
        var currentTime = new Date();
        var day = currentTime.getDate();
        var month = currentTime.getMonth();
        var year = currentTime.getFullYear();

        var hour = currentTime.getHours();
        var minute = currentTime.getMinutes();
        var second = currentTime.getSeconds();

        var Time = year+"-"+(parseInt(month)+1)+"-"+day+" "+hour+":"+minute+":"+second;
        var comment = TrimComment($("#comment-area").val());
        if(comment==''){
            $("#post-error-comment").html('<span style="color:red;"><b>Merci de saisir votre commentaire.</b></span>');
        }
        else{
            $.post($("#post_comment_form").attr("action"), $("#post_comment_form").serialize(), function(response){
                var targetOffset = $("#vid-comments-box").offset().top;
                $('html,body').animate({scrollTop: targetOffset}, 500);
                if($("#vid-comments-box").attr("name")==0) $("#comments-bubbles").html('<div id="new_comment" class="bubble" ><cite><span class="comment-author-span">Commentaire Ajouté</span><br/><span class="comment-date-span">'+ Time +'</span></cite><blockquote><p>' + $("#comment-area").val() + '</p></blockquote></div>');
                else $("#comments-bubbles").prepend('<div class="bubble" ><cite><span class="comment-author-span">Commentaire Ajouté</span><br/><span class="comment-date-span">'+ Time +'</span></cite><blockquote><p>' + $("#comment-area").val() + '</p></blockquote></div>');
                $('html,body').animate({scrollTop: targetOffset},500);
                $('#new_comment').fadeIn("slow").show("slow");
                $("#post-comment").html(response);
            });
        }
        return false;
    });

});
