$(function() {
/*****************************FORMULARIOS**************************************/
	$('textarea')
	.elastic()
	.clearField();
	
	$('#link, #link2, #videos_vmlive')
	.clearField();
	
//buscar
$("#videosform").submit(function() {
	var busqueda = $('#videos_vmlive').val()
	var busqL = busqueda.length
	if(busqL>0){
		window.location="http://vmlive.tv/@"+busqueda;
	}else{
		window.location="http://vmlive.tv/";
	}
	return false;
})
	
/*****************************MOSTRAR/OCULTAR INFORMACION DE LAS NOTICIAS (FOTOS)**************************************/
	$('.noticias_fotos_div').hover(
		function () {
			$('.noticias_fotos_descripcion',$(this)).stop().animate({'paddingBottom':'40px'},200);
		},
		function () {
			$('.noticias_fotos_descripcion',$(this)).stop().animate({'paddingBottom':'0px'},200);
		}
	);

	
/*****************************ESTADOS EN VIVO (VJ'S)**************************************/
	ultimas_noticias('ultima_noticia_cont')
	//var interval_img = setInterval(ultimas_noticias_imgs('ultima_noticia_cont'), pausa);


/*****************************LINK MENU (ACTIVAR /DESACTIVAR)**************************************/
		$('.enca_menu_div').click(function(){
		var activo = $(this).attr('id')
		$('.enca_menu_div').attr('id', '')
		
		if(activo != 'activo'){
			$(this).attr('id', 'activo')
		}if(activo == 'activo'){                                                     
			$(this).attr('id','')
		}
	})
	
	$("#jquery_jplayer_1").jPlayer({
		ready: function(event) {
			$(this).jPlayer("setMedia", {
				oga: "http://vmlive.tv:8000/live.ogg",
				mp3: "http://vmlive.tv:8000/live.mp3",
			});
		},
		swfPath: "/js",
		supplied: "oga, mp3",
		preload:"auto",
	});
})

$('#radioonline').live('click',function(){
	//PLAY
	radio = $(this).find('li')
	radioicon = $(this).find('.icon')
	radio.toggleClass('activo')
	var radioactiva = $('#radioactiva').val()
	
	if(radioactiva==0){
		$("#jquery_jplayer_1").jPlayer("play")
		radioicon.addClass('cargando')
		$.timer(10000, function (timer) {
			var radioactiva = $('#radioactiva').val()
			if(radioactiva==1){
				$(radioicon).removeClass('cargando').addClass('activo')
				timer.stop();
			}
		});
		$('#radioactiva').val(1)
	}else{
		$("#jquery_jplayer_1").jPlayer("stop")
		radioicon.removeClass('activo').removeClass('cargando')
		$('#radioactiva').val(0)
	}

	return false;
})
/*****************************SUBMENUS MOSTRAR /OCULTAR (PROGRAMAS / VJS)**************************************/
function submenu(nombre_menu){
	ID = $(this).attr('id')
	if(nombre_menu==1){ var mostrar = '#vjs_submenu'; var ocultar = '#programas_submenu, #login_submenu'}
	if(nombre_menu==2){ var mostrar = '#programas_submenu'; var ocultar = '#vjs_submenu, #login_submenu'}
	if(nombre_menu==3){ var mostrar = '#login_submenu'; var ocultar = '#vjs_submenu, #programas_submenu'}
	
	$(ocultar).slideUp(800)
	$(mostrar).slideToggle(800)
}

/*****************************ULTIMAS NOTICIAS (TWITTER, FB, MISCOMPAS)**************************************/
function ultimas_noticias(id_contedor){
	
	var first = 0;
	var speed = 500;
	var pause = 5000;
	function removeFirst(){
		first = $('ul#'+id_contedor+' li:first').html();
		$('ul#'+id_contedor+' li:first')
		//.animate({opacity: 0}, speed)
		.slideUp(500, function() {$(this).remove();
		addLast(first);
		});			
	}
	function addLast(first){
		last = '<li style="display:none">'+first+'</li>';
		$('ul#'+id_contedor).append(last)
		$('ul#'+id_contedor+' li:last')
		//.animate({opacity: 1}, speed)
		.fadeIn(500)
	}

	interval = setInterval(removeFirst, pause);
}


/*****************************ULTIMAS NOTICIAS CON IMAGENES (ARTISTAS)**************************************/
function ultimas_noticias_imgs(id_contador){
	var interval_img;
	var pausa = 3000;
	var interval_img = setInterval(function(){
	//var continuar_noticia =  $('#continuar_noticia').val()
		var contador =  parseInt($('#'+id_contador).val())
		var continuar_noticia =  $('#continuar_noticia').val()
		if(continuar_noticia=='no'){clearInterval(interval_img)}
		if(contador >=4){ contador = 0}
		var nuevo_conteo = contador+1
		mostrar_notica_2(nuevo_conteo)	
	}, pausa);
	
	function mostrar_notica_2(nuevo_conteo){
		$('.noticias_recientes_div_cont').attr('id','')
		$('.noticias_recientes_img img').hide()
		$('.noticias_recientes_descripcion div:nth-child('+nuevo_conteo+')').attr('id','activo')
		//$('.noticias_recientes_img img:nth-child('+nuevo_conteo+')').fadeIn(1)
		$('#img_noticia'+nuevo_conteo).show()
		$('#'+id_contador).attr('value',nuevo_conteo) 
	}
}
function mostrar_notica_hover(nuevo_conteo){
	$('#continuar_noticia').attr('value','no')
	$('.noticias_recientes_div_cont').attr('id','')
	//$('#noticia_turno').attr('value',nuevo_conteo) 
	$('.noticias_recientes_img img').hide()
		$('.noticias_recientes_descripcion div:nth-child('+nuevo_conteo+')').attr('id','activo')
		$('#img_noticia'+nuevo_conteo).show()
	
}

/*****************************ELASTIC**************************************/
(function(jQuery){jQuery.fn.extend({elastic:function(){var mimics=['paddingTop','paddingRight','paddingBottom','paddingLeft','fontSize','lineHeight','fontFamily','width','fontWeight'];return this.each(function(){if(this.type!='textarea'){return false;}
var $textarea=jQuery(this),$twin=jQuery('<div />').css({'position':'absolute','display':'none','word-wrap':'break-word'}),lineHeight=parseInt($textarea.css('line-height'),10)||parseInt($textarea.css('font-size'),'10'),minheight=parseInt($textarea.css('height'),10)||lineHeight*3,maxheight=parseInt($textarea.css('max-height'),10)||Number.MAX_VALUE,goalheight=0,i=0;if(maxheight<0){maxheight=Number.MAX_VALUE;}
$twin.appendTo($textarea.parent());var i=mimics.length;while(i--){$twin.css(mimics[i].toString(),$textarea.css(mimics[i].toString()));}
function setHeightAndOverflow(height,overflow){curratedHeight=Math.floor(parseInt(height,10));if($textarea.height()!=curratedHeight){$textarea.css({'height':curratedHeight+'px','overflow':overflow});}}
function update(){var textareaContent=$textarea.val().replace(/&/g,'&amp;').replace(/  /g,'&nbsp;').replace(/<|>/g,'&gt;').replace(/\n/g,'<br />');var twinContent=$twin.html();if(textareaContent+'&nbsp;'!=twinContent){$twin.html(textareaContent+'&nbsp;');if(Math.abs($twin.height()+lineHeight-$textarea.height())>3){var goalheight=$twin.height()+lineHeight;if(goalheight>=maxheight){setHeightAndOverflow(maxheight,'auto');}else if(goalheight<=minheight){setHeightAndOverflow(minheight,'hidden');}else{setHeightAndOverflow(goalheight,'hidden');}}}}
$textarea.css({'overflow':'hidden'});$textarea.keyup(function(){update();});$textarea.live('input paste',function(e){setTimeout(update,250);});update();});}});})(jQuery);

/*****************************CLEARFIELD**************************************/
(function($){$.fn.clearField=function(s){s=jQuery.extend({blurClass:'clearFieldBlurred',activeClass:'clearFieldActive',attribute:'rel',value:''},s);return $(this).each(function(){var el=$(this);s.value=el.val();if(el.attr(s.attribute)==undefined){el.attr(s.attribute,el.val()).addClass(s.blurClass)}else{s.value=el.attr(s.attribute)}el.focus(function(){if(el.val()==el.attr(s.attribute)){el.val('').removeClass(s.blurClass).addClass(s.activeClass)}});el.blur(function(){if(el.val()==''){el.val(el.attr(s.attribute)).removeClass(s.activeClass).addClass(s.blurClass)}})})}})(jQuery);


/*****************************TIPSY**************************************/

(function($){function fixTitle($ele){if($ele.attr('title')||typeof($ele.attr('original-title'))!='string'){$ele.attr('original-title',$ele.attr('title')||'').removeAttr('title');}}
function Tipsy(element,options){this.$element=$(element);this.options=options;this.enabled=true;fixTitle(this.$element);}
Tipsy.prototype={show:function(){var title=this.getTitle();if(title&&this.enabled){var $tip=this.tip();$tip.find('.tipsy-inner')[this.options.html?'html':'text'](title);$tip[0].className='tipsy';$tip.remove().css({top:0,left:0,visibility:'hidden',display:'block'}).appendTo(document.body);var pos=$.extend({},this.$element.offset(),{width:this.$element[0].offsetWidth,height:this.$element[0].offsetHeight});var actualWidth=$tip[0].offsetWidth,actualHeight=$tip[0].offsetHeight;var gravity=(typeof this.options.gravity=='function')?this.options.gravity.call(this.$element[0]):this.options.gravity;var tp;switch(gravity.charAt(0)){case'n':tp={top:pos.top+pos.height+this.options.offset,left:pos.left+pos.width/2-actualWidth/2};break;case's':tp={top:pos.top-actualHeight-this.options.offset,left:pos.left+pos.width/2-actualWidth/2};break;case'e':tp={top:pos.top+pos.height/2-actualHeight/2,left:pos.left-actualWidth-this.options.offset};break;case'w':tp={top:pos.top+pos.height/2-actualHeight/2,left:pos.left+pos.width+this.options.offset};break;}
if(gravity.length==2){if(gravity.charAt(1)=='w'){tp.left=pos.left+pos.width/2-15;}else{tp.left=pos.left+pos.width/2-actualWidth+15;}}
$tip.css(tp).addClass('tipsy-'+gravity);if(this.options.fade){$tip.stop().css({opacity:0,display:'block',visibility:'visible'}).animate({opacity:this.options.opacity});}else{$tip.css({visibility:'visible',opacity:this.options.opacity});}}},hide:function(){if(this.options.fade){this.tip().stop().fadeOut(function(){$(this).remove();});}else{this.tip().remove();}},getTitle:function(){var title,$e=this.$element,o=this.options;fixTitle($e);var title,o=this.options;if(typeof o.title=='string'){title=$e.attr(o.title=='title'?'original-title':o.title);}else if(typeof o.title=='function'){title=o.title.call($e[0]);}
title=(''+title).replace(/(^\s*|\s*$)/,"");return title||o.fallback;},tip:function(){if(!this.$tip){this.$tip=$('<div class="tipsy"></div>').html('<div class="tipsy-arrow"></div><div class="tipsy-inner"/></div>');}
return this.$tip;},validate:function(){if(!this.$element[0].parentNode){this.hide();this.$element=null;this.options=null;}},enable:function(){this.enabled=true;},disable:function(){this.enabled=false;},toggleEnabled:function(){this.enabled=!this.enabled;}};$.fn.tipsy=function(options){if(options===true){return this.data('tipsy');}else if(typeof options=='string'){return this.data('tipsy')[options]();}
options=$.extend({},$.fn.tipsy.defaults,options);function get(ele){var tipsy=$.data(ele,'tipsy');if(!tipsy){tipsy=new Tipsy(ele,$.fn.tipsy.elementOptions(ele,options));$.data(ele,'tipsy',tipsy);}
return tipsy;}
function enter(){var tipsy=get(this);tipsy.hoverState='in';if(options.delayIn==0){tipsy.show();}else{setTimeout(function(){if(tipsy.hoverState=='in')tipsy.show();},options.delayIn);}};function leave(){var tipsy=get(this);tipsy.hoverState='out';if(options.delayOut==0){tipsy.hide();}else{setTimeout(function(){if(tipsy.hoverState=='out')tipsy.hide();},options.delayOut);}};if(!options.live)this.each(function(){get(this);});if(options.trigger!='manual'){var binder=options.live?'live':'bind',eventIn=options.trigger=='hover'?'mouseenter':'focus',eventOut=options.trigger=='hover'?'mouseleave':'blur';this[binder](eventIn,enter)[binder](eventOut,leave);}
return this;};$.fn.tipsy.defaults={delayIn:0,delayOut:0,fade:false,fallback:'',gravity:'n',html:false,live:false,offset:0,opacity:0.8,title:'title',trigger:'hover'};$.fn.tipsy.elementOptions=function(ele,options){return $.metadata?$.extend({},options,$(ele).metadata()):options;};$.fn.tipsy.autoNS=function(){return $(this).offset().top>($(document).scrollTop()+$(window).height()/2)?'s':'n';};$.fn.tipsy.autoWE=function(){return $(this).offset().left>($(document).scrollLeft()+$(window).width()/2)?'e':'w';};})(jQuery);
