//here you place the ids of every element you want.
var ids= new Array('incorporar', 'indicar', 'comentar');
var my_tempo = 30;
var target = "";
var my_intervalo; 
var status = true;


function init(){
		
	$('.switch').click(function(e){
		var target = $(this).attr('name');
		hideallids();
 		$('.switch').removeClass("selected");
 		$('#' + target).fadeIn();
		$(this).addClass("selected");
	});
	
	carregaPopular();
		
	$('.fix-z-index').bgiframe();
	
	$('#ajax_answer').hide();
    
    $('#ajax_search').click(function(e) {
	    my_tempo = 0;
	});
		
	$("#ajax_answer").mouseover(function(r){
      	//$("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(e){
 		$('#modal_form').remove();
 		$(this).after("<div id='modal_form' style='display:none'></div>");	
 		$('#modal_form').load('/white/ajaxtwitter/', function(e){ $('#modal_form').show();	})
 	});
  
 	$('#bt_pen').click( function(e){
 		$('#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>");  	  		
  	}
		
	$("tr.incorporar li.embed").click(function(e){
		
		var valor = $(this).attr('title');
		var arr = new Array();
		arr = valor.split('x');
		
		var idvideo = $("tr.incorporar").attr('id');
		
		$("tr.incorporar li.embed").each(function(e){
			$(this).attr('class', 'embed');
		});
		
		$(this).attr('class', 'embed selected');
		
		$('#flash_embed').attr('disabled', 'disabled');
		
		$.ajax({
			type: "POST",
			url: "/white/optionembed/",
			dataType: "script",
			data: {width: arr[0], height: arr[1], idvideo: idvideo},
			success: function(e){
				$('#flash_embed').val(e);
				$('#flash_embed').attr('disabled', '');
			}
		});

		
	});
		
	//invocar corner
	$('.corner').corner("7px"); 
 	  	   	 	
}

function carregaPopular(){
	$('.popular').click(function(e){
		var target = $(this).attr('name');
		var url    = $(this).attr('url');
		var idcat  = $(this).attr('id');
		$('.list_videos').load(url, {'target': target, 'idcat': idcat }, function(e){
        	$('html, body').animate({scrollTop: 1200}, 1000);
			$(this).css('opacity', 1);
		});
		$('.popular').removeClass("selected");
		$('.list_videos').css('opacity', .3);
		$(this).addClass("selected");
	});
}

function decrease_tempo() {
    my_tempo = my_tempo - 1;
    if (my_tempo > 0) {
       my_intervalo = setTimeout('decrease_tempo()', 50);
    } else {
            $('#ajax_answer').fadeOut();
    }       
}

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 closeWindow(target, voted){
	
	$(target).animate({
    	opacity: .6
    }, 2500, "linear", 
		function(e){
			$(this).animate({opacity: 0});
			$(this).html('');
			if (voted == 'false'){
				$(target).animate({opacity: 1});
				$('input.vote-star').rating('enable');
			}
		} 
	);

	
}

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);
			}
		
			
		}
	
	};
	
	$('#' + target).ajaxSubmit(options);
	
    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 carregaVideos(target) {
	
	$('.list_videos').load('/white/ajaxlistvideos/', {'target': target });
	
}

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()
	}

}

//invoca jquery
$(document).ready(init);
