//here you place the ids of every element you want.
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').fadeOut();
    }       
}


$(document).ready(function(){
		
		$('.switch').click(
			function(){
				var target = $(this).attr('name');
				hideallids()
		 		$('.menu_info a').removeClass("selected");
		 		$('#'+target).fadeIn();
				$(this).addClass("selected");
			}
		);
 		
		$('.fix-z-index').bgiframe();
		
			 	
		$('#ajax_answer').hide();
        
        $('#ajax_search').click(function () {
                my_tempo = 0;
         })
		
		$("#ajax_answer").mouseover(function(){
	      	//$("p:first",this).text("mouse over");
	      	my_tempo = 500;
	    }).mouseout(function(){
	      	my_tempo = 0;
	    });
		
         		 
		$('.vote-star').rating({ 
			callback: function(value, link){ 
				var id_video = $('#id_video_hidden').val();
				$('input.vote-star').rating('disable');
				$('#captcha').html("<img src='/static/white/images/loading.gif' border=0 />");
				$('#captcha').load('/white/ajaxcaptcha/', {'id_video': id_video , 'value': value }); 
			} 
		});
		  
	   
	    $('#ajax_search').keyup(function (e) {
                    skey = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0; 
                    if (skey == 27) {
                            my_tempo = 0;
                            return;
                    }

                    if (this.value.length > 2) {
                    	
                    $('#ajax_answer').load('/white/ajaxsearch/', {'pattern' : this.value , 'cache' : new Date().getTime() }, function () { 
                        if (my_tempo <= 0) {
                                $('#ajax_answer').fadeIn()
                                my_tempo = 50;
                                decrease_tempo();
                        } else {
                                my_tempo = 50;
                        }
                    })
           }
        }) 	
 	 	
 	 	$('#form_twitterApp').click( function(){
 	 		$('#modal_form').remove();
 	 		$(this).after("<div id='modal_form' style='display:none'></div>");	
 	 		$('#modal_form').load('/white/ajaxtwitter/', function(){ $('#modal_form').show();	})
 	 	});
 	  
 	 	$('#bt_pen').click( function(){
 	 		$('#modal_form_tag').remove();
 	 		$("#bt_show_videotag").append("<div id='modal_form_tag' style='display:none; position:absolute;'></div>");	
 	 		$('#modal_form_tag').load('/white/ajaxtag/', function(){ $('#modal_form_tag').show();	})
 	 	});
 	  	
 	  	
 	  	if($.browser.msie && $.browser.version<7){
			$(document).pngFix();
			//$(body).append("<div>ISSO É IE6</div>");  	  		
 	  	}
 	  	   	 	
});

function hideFormAjax(){
	$('#modal_form').hide();
}

function hideFormTagAjax(){
	$('#modal_form_tag').hide();
}

function toggleComment(target){
	 var total = parseInt($('#total_videocomment').html()) 
	 var video = $('#id_video_hidden').val();
	 
	if($('.block_coments_list').html() == ''){
	 
		 $('.block_coments_list').load('/white/ajaxlistcomment/', {'video': video }, function(){	
			if(total > 0){
				$('#'+target).slideToggle('slow');
			} 	
		 });
	 
	 }else{
	 	
	 	$('#'+target).slideToggle('slow');
	 	
	 }
	 	
}

function paginacao(video, page) {
	$('.block_coments_list').load('/white/ajaxlistcomment/', {'video': video , 'page': page }, function() {
		$('#comment_list').fadeIn('slow');
	});
	return false;
}

function reflashComment(){
	 var video = $('#id_video_hidden').val(); 
	 $('.block_coments_list').load('/white/ajaxlistcomment/', {'video': video });
}

function sendForm(target, output , reflash){
	var options = {
		type: 'post',
		dataType: 'json',
		timeout: 1000,
		beforeSubmit: hide_bts(),
		success: function(data){
		
			$('#' + output).show()
			$('#' + output).html(data.msg);
			
			if (reflash) {
				var total = parseInt($('#total_videocomment').html());
				reflashComment(reflash);
				setTimeout("show_bt()", 1000);
			}
			
			if (data.status == true) {
				$('#total_videocomment').html(total += 1);
				parent.target = target;
				setTimeout("clear_response()", 1000);
			}else{
				setTimeout("show_bt()", 1000);
			}
		
			
		}
		
	// 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 show_bt(){
	 $('.sendbts').show();
}

function hide_bts(){
	$('.sendbts').hide();
}

function sendCaptcha(target, output){
	
	var options = { 
	    type:          'post',         
	    target:        '#'+output	 
	}; 
	
	$('#'+target).ajaxSubmit(options); 
    return false; 
	
}
 

function resetForm(target){
	
	$('#'+target).resetForm(); 
 	$('.sendbts').show();
	// !!! Important !!! 
    // always return false to prevent standard browser submit and page navigation 
    return false; 
	
}

function hideallids(){
	//loop through the array and hide each element by id
	for (var i=0;i<ids.length;i++){
		$('#'+ids[i]).hide();
	}
}

function clear_response(){
	 $('.sendbts').show();
	 $('.error').fadeOut('slow', function(){	$('#' + parent.target ).resetForm(); });
}




function check_upload(new_url){
	
	var _name  = $('#name').val();
	var _email  = $('#email').val();
	var _displayname  = $('#displayname').val();
	var _description  = $('#description').val();
	
	error = 0; 
	$("#Error_form_msg ").empty();
	
	if (!_name){
		$('#Error_form_msg').append('<p>O campo nome é obrigatório</p>');
		error=1;
	}
	
	if (!_email){
		$('#Error_form_msg').append('<p>O campo email é obrigatório </p>');
		error=1;
	}

	if (!_displayname){
		$('#Error_form_msg').append('<p>O campo titulo do vídeo é obrigatório </p>');
		error=1;
	}

	if (!_description){
		$('#Error_form_msg').append('<p>O campo descrição é obrigatório </p>');
		error=1;
	}
	
	if (error == 0 ){
			$('#form_upload').attr('action', new_url);
			$('#form_upload').submit()
	}
	
	
	
}
 


 
