var ids=new Array('copy','send','comment_video');
var my_tempo = 30;
var target = "";
var my_intervalo; 
var status = true;

function decrease_tempo() {
    my_tempo = my_tempo - 1;
    if (my_tempo > 0) {
       my_intervalo = setTimeout('decrease_tempo()', 50);
    } else {
            $('#ajax_answer').hide();
    }       
}


$(document).ready(function() {
   // do corner border
   $('#ajax_answer').hide(); 
   
   $('.logo_ig_topo').css('cursor','pointer');
   $('.logo_ig_topo').click(function(){
   		window.location.href='http://www.ig.com.br/';	   	
   });
   
   $("#ajax_answer").mouseover(function(r){
      	  	my_tempo = 500;
        }).mouseout(function(){
      	    my_tempo = 0;
    });
         		 
    $('.vote-star').rating({ 
	callback: function(value, link){ 
		var id_video = $('#id_video_hidden').val();
		$('.vote-star').css('display','none');
		$('#captcha').html("<img src='/static/white/images/loading.gif' border='0' />");
		$('#captcha').load('/videomensagem/ajax_captcha/', {'id_video': id_video , 'value': value }); 
	} 
    });

	$('.rating-cancel').remove();
	
	$('#listComment ul li').each( 
		function(e){
			$(this).hide();
		}
	);
	
	$('#compartilhar').show();
	
	$('.btn_player').fadeTo(0,0.60);
	
	$('.ajax_category').change(
			function(){
				$.ajax({
					type: "POST",
					url: "/videomensagem/ajax_select",
					data: {
						'filter': 'subcategory',
						'id': this.value
					},
					beforeSend: function(){
						$('ajax_select').empty()
						$('ajax_select').append("<option value='0'> Carregando ... </option>")
					},
					success: function(data){
						$('ajax_select').empty()
						$('ajax_select').append(data)
					},
					error: function(data){
						$('ajax_select').empty()
						$('ajax_select').append("<option value='0'> Erro ao carregar a página </option>")
					}
				})
			});
	
	
	$('.responder_comentario').click(
		function(){
			var target = $(this).attr('id');
			var usuario = $('#username_' + target).html();
			var content = $('#comment_content_' + target).html();
			$('#id_idvideocomment').val(target);
			$('h2#title_comment').html("Responder comentário de: <b>" + usuario + "</b>");
			window.location.hash = '#send_comentario'
		}
	);
	
	jQuery('#mycarousel').jcarousel({
		scroll: 6,
		itemFirstInCallback: hideCaroselBtn
	});
	
});


function hideCaroselBtn(carousel, state){
	if (carousel.last < 6 ){
		$('.jcarousel-prev').hide();		
		$('.jcarousel-next').hide();
		$('.jcarousel-container').css('padding',0);
		$('.jcarousel-container').css('margin',0);
	}
}


function sendCaptcha(target, output){
	var options = { 
	    type:          'post',         
	    target:        '#'+output	 
	}; 
	
	$('#'+target).ajaxSubmit(options); 
    return false; 
}


function ajax_tabsComment(action, id_target){
	// load function
	$('#tabsComment ul li a').each( 
		function(e){
			$(this).removeClass('selected');
		}
	);
	
	$('#listComment ul li').each( 
		function(e){
			$(this).hide();
		}
	);
	
	$('#' + id_target).show();
	$('#' + id_target + ' *').show();
	$('#' + action).addClass('selected');
	
}

function sendForm(target, output , refreshComment ){
	
	var options = {
		type: 'post',
		dataType: 'json',
		timeout: 1000,
		
		beforeSubmit: hide_bts(),
		
		success: function(data){
			$('#' + output).show()
			$('#' + output).html(data.msg);
			if (data.status == true) {
				if(refreshComment){
						//ajax_paginate(action, page , id_target, idvideo)
						ajax_paginate('comment',1,'comment_list', data.idvideo )
				}
				
				$('.error').show();
				setTimeout("clear_response()", 1000);
				
			}else{
				
				setTimeout("show_bt()", 500);
				
			}
		}
		
	//other available options: 
	//beforeSubmit:  showRequest,  // pre-submit callback 
	//target:         output,    // target element(s) to be updated with server response 
	//url:       url,      	// override for form's 'action' attribute 
	//dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
	//clearForm: true        // clear all form fields after successful submit 
	//resetForm: true        // reset the form after successful submit 
	
	// $.ajax options can be used here too, for example: 
	
	};
	
	
	$('#' + target).ajaxSubmit(options);
	
	 // !!! Important !!! 
    // always return false to prevent standard browser submit and page navigation 
    return false; 
	
}






function clear_response(){
	 $('.sendbts').attr('disable', 'false');
	 $('.sendbts').removeAttr( "disabled" );
	 $('form').resetForm(); 
	 $('.error').fadeOut('slow');
	 $('#' + parent.target ).resetForm(); 
}


function show_bt(){
	$('.error').show();
	$('.sendbts').attr('disable', 'false');
	$('.sendbts').removeAttr( "disabled" );
}

function hide_bts(){
	$('.sendbts').attr("disabled", "true");
}

function ajax_list_load(action, id_target , idcat, idsub){
	// load function
	$('#tabsList ul li a').each( 
		function(e){
			$(this).removeClass('selected');
		}
	);
	
	$('#' + id_target).load('/ig/ajax_list/', { 'action': action , 'idcat' : idcat , 'idsub' : idsub } );
	$('#' + action).addClass('selected');
	
}

function ajax_paginate(action, page , id_target, idvideo , idcat){
	$('#' + id_target).load('/ig/ajax_list/', { 'action': action , 'page' : page  , 'idvideo' : idvideo , 'idcat' : idcat } );
}


function navegate_to(url, value){
	var url = url + String(value);
	window.location.href = url;
}


function show_twitter(){
	$('#demo3').dialog('destroy');
	if ($('#demo3')){
		$('#demo3').remove();
	}
	
	
	idvideo= $('#id_idivideo').val();
	$('<div id="demo3">').load('/ig/ajax_twitter/', 'idvideo='+idvideo  ).dialog({
	 	maximize: false, 
        modal: true ,
		width: 385, 
		height: 150,
		bgiframe: true,
        overlay: { background:'#000000', opacity:0.8 } 
	});
}

function show_blig(){
	
	$('#demo3').dialog('destroy');
	if ($('#demo3')){
		$('#demo3').remove();
	}
	
	idvideo= $('#id_idivideo').val();
	$('<div id="demo3">').load('/ig/ajax_blig/', 'idvideo='+idvideo  ).dialog({
	 	maximize: false, 
        modal: true ,
		width: 385, 
		height: 150,
		bgiframe: true,
        overlay: { background:'#000000', opacity:0.8 } 
	});
}


function show_mn(){
	
	$('#demo3').dialog('destroy');
	if ($('#demo3')){
		$('#demo3').remove();
	}
	
	idvideo = $('#id_idivideo').val();
	$('<div id="demo3">').load('/ig/ajax_mn/?idvideo='+idvideo ).dialog({
	 	maximize: false, 
        modal: true ,
		width: 385, 
		height: 150,
		bgiframe: true,
        overlay: { background:'#000000', opacity:0.8 } 
	});
}




function closeWindow(target, id_video , value ){
	
    $(target).animate({
        alpha: 100
    	}, 2500, function(e){
			$('.vote-star').css('display', 'none');
			$('#captcha').html("<img src='/static/white/images/loading.gif' border='0' />");
			$('#captcha').load('/videomensagem/ajax_captcha/', {
				'id_video': id_video,
				'value': value
			});
    });

	
}

function fbs_click() { 
	u=location.href; 
	t=document.title; 
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t)+"'",'sharer', 'toolbar=0, status=0, width=626, height=436'); 
	return false; 
}
