var ctrlVista = {
	interviewOne : false,
	interviewTwo : false,
	interviewThree : false
};

var FlashHelper = {
	movieIsLoaded : function(theMovie) {
		if(typeof(theMovie) != 'undefined') {
			//console.log(theMovie.PercentLoaded());
			return theMovie.PercentLoaded() == 100;
		}
		else return false;
	},

	getMovie : function(movieName)	{
  	if(navigator.appName.indexOf('Microsoft') !=-1) return window[movieName];
	  else return document[movieName];
	}
};


var scripts = {


	niftyplayer:function() {
		this.obj = FlashHelper.getMovie('innerPlayer');
		//console.log(this.obj);
		if(!FlashHelper.movieIsLoaded(this.obj)) return;

		this.play = function() {
			this.obj.TCallLabel('/','play');
		};
		this.stop = function() {
			this.obj.TCallLabel('/','stop');
		};
		this.pause = function() {
			this.obj.TCallLabel('/','pause');
		};
		this.playToggle = function() {
			this.obj.TCallLabel('/','playToggle');
		};
		this.reset = function() {
			this.obj.TCallLabel('/','reset');
		};
		this.load = function(url) {
			this.obj.SetVariable('currentSong', url);
			this.obj.TCallLabel('/','load');
		};
		this.loadAndPlay = function(url) {
			this.load(url);
			this.play();
		};
		this.getState = function() {
			var ps = this.obj.GetVariable('playingState');
			var ls = this.obj.GetVariable('loadingState');
			if(ps == 'playing')
				if (ls == 'loaded') return ps;
				else return ls;
			if(ps == 'stopped')
				if(ls == 'empty') return ls;
				if(ls == 'error') return ls;
				else return ps;
			return ps;
		};
		this.getPlayingState = function() {
			ps = this.obj.GetVariable('playingState');
			return ps;
			
		};
		this.getLoadingState = function() {
			return this.obj.GetVariable('loadingState');
		};
		this.registerEvent = function(eventName, action) {
			this.obj.SetVariable(eventName, action);
		};
		return this;
	},

	player:function() {
		jq(".playPodcast").click(function(){
			
			/* Player Related */
			scripts.niftyplayer().stop();
			var w = jq(this).parent();
			w.each(function(){
				var dirUrl = w.children('input').val();
				scripts.niftyplayer().loadAndPlay(dirUrl);
			});
		});
	},


	popUpPlayer:function(){
                jq(".playPodcast").click(function(){

                        /* Player Related */
                        //scripts.niftyplayer().stop();
                        var w = jq(this).parent();
                        w.each(function(){
                                var dirUrl = w.children('input').val();
				window.open ("php/popUpPlayer.php?podcast_id=" + dirUrl, "Exa Player","menubar=1,resizable=1,width=64,height=32"); 
				//scripts.niftyplayer().loadAndPlay(dirUrl);
                        });
                });
	},

	popUpPlay : function(podcast) {
		var j;
		j = jq('#podcast').val();
		var k = 0;
		//alert(podcast);
		var w = 0;
		while(w < 65001) {
			if(w >= 65000) {
				//console.log(podcast);
				//alert(' ');
				scripts.niftyplayer().loadAndPlay(podcast);
			}
			w++;
		}
		
	},
	
	guardaComentario : function() {
		jq('#postear').click(function() {
			var c, nid, bid, comment;
			c = jq('#comentarios').val();
			nid = jq('#noticia_id').val();
			bid = jq('#billboard_id').val();
			id = (nid == null)?bid:nid;
			comment = (nid == null)?'billboard':'noticia';
			if(c) {
				jq.ajax({
					data     : {'c' : c, 'id' : id, 'comment':comment},
					dataType : 'html',
					type     : 'post',
					url      : '../php/postcomment.php',
					async    : false,
					success  : function(msg) {
						jq('#comentarios').val('')
						jq('#post').slideUp();
						jq('#post').html(msg);
						jq('#post').slideDown();
					}
				});
			}
		});
		
		jq('cancelar').click(function() {
			jq('#comentarios').val('');
		});
	},
	bandera : {'elemento' : "", 'estado' : ""},
	playexa:function() {
		
		jq('.playexa').click(function() {
			var cancionurl = jq(this).siblings('.rola').val();
			var el, es, imgPlay, imgPause, miPadre;

			//Busco a mi padre, es un hombre noble, avergonzado...
			miPadre = jq(this).parent().attr('id');
			
			//console.log(miPadre);
			if(miPadre == 'iAmPodcast') {
				imgPlay  = 'buttons/playPodcast.png';
				imgPause = 'buttons/pausePodcast.png';
			}
			else {
				imgPlay  = 'botones/playTop.png';
				imgPause = 'botones/pauseTop.png';
			}

			// obtenemos el elemento y su estado de la variable bandera
			el = scripts.bandera.elemento;
			es = scripts.bandera.estado;

			//scripts.niftyplayer().load();


			// asignamos los valores del elemento al que se le da play
			scripts.bandera.elemento = jq(this).attr('id');
			scripts.bandera.estado = scripts.niftyplayer().getState();


			//console.log('elem ' + el + ' es ' + es + ' flagEl ' + scripts.bandera.elemento + ' flagEs ' + scripts.bandera.estado)

			//Si el elemento al que se le dio play es el mismo que estaba tocando...
			if(el == jq(this).attr('id')) { // si es el mismo btn...
				//Revisa estado
				var st = scripts.niftyplayer();


				switch(st.getState()) {
					case 'paused':
						st.play();
						//jq(this).attr('src', conf.url_img + imgPause);
						jq(this).attr('src', "http://www.fmglobo.com/img/" + imgPause);
						//console.log(jq(this).attr('src'));
						break;

					case 'stopped':
						st.play();
						//jq(this).attr('src', conf.url_img + imgPause);
						jq(this).attr('src', "http://www.fmglobo.com/img/" + imgPause);
						//console.log(jq(this).attr('src'));
						break;

					case 'playing':
						st.pause();
						//jq(this).attr('src', conf.url_img + imgPlay);
						jq(this).attr('src', "http://www.fmglobo.com/img/" + imgPlay);
						//console.log(jq(this).attr('src'));
						break;

					case 'loading':
						st.pause();
						//jq(this).attr('src', conf.url_img + imgPlay);
						jq(this).attr('src', "http://www.fmglobo.com/img/" + imgPlay);
						//console.log(jq(this).attr('src'));
						break;
				}

			}
			else { // si no es el mismo, interrumpimos la reproduccion y cargamos la cancion del nuevo elemento.
                //vmc para filtro de seguridad
		//console.log(cancionurl);
                jq.cookie('myvmc',cancionurl,{'path' : '/'});
				scripts.niftyplayer().stop();
				scripts.niftyplayer().load(cancionurl);
				scripts.niftyplayer().play();
				//console.log('b4 get state');
				//console.log(scripts.niftyplayer().getState());
				//y sustituimos los botones
				//jq('.playexa').attr('src', conf.url_img + imgPlay);
				jq('.playexa').attr('src', "http://www.fmglobo.com/img/" + imgPlay);
				//jq(this).attr('src', conf.url_img + imgPause);
				jq(this).attr('src', "http://www.fmglobo.com/img/" + imgPause);
			}

			var finCancion, comodin;

			finCancion = jq(this).attr('id');
			//console.log(finCancion);
			scripts.niftyplayer().registerEvent('onSongOver','scripts.niftyplayer().stop();document.getElementById("' + finCancion + '").setAttribute("src","' + conf.url_img + imgPlay + '")');
		});
	},

	playVideo : function() {
		jq('#Searchresult').click(function(e){
			var target = e.target;
			if(jq(target).hasClass('youtube')) {
				var video_id = jq(target).attr('id');
				window.location.href = '../php/switchVideo.php?v=' + video_id;
			}
			
		}); 
	},
	
	playRecomendado : function() {
		jq('.youtube').click(function(){
			var video_id = jq(this).attr('id');
			window.location.href = '../php/switchVideo.php?v=' + video_id;
		});
	},

	/** Votacion **/                   
    votarEncuesta : function() {
		jq('#votar').click(function() {
			var chosenOption;
			chosenOption = jq('#opciones input:radio:checked').val();
			
			if(chosenOption) {
				jq.ajax({
					'url'      : conf.url_scripts+'votePoll.php',
					'type'     : 'post',
					'data'     : {'respuestaencuesta_id' : chosenOption},
					'dataType' : 'html',
					'async'    : false,
					'success'  : function(response) {
						if(response != 'Error !!!') {
							var encuesta_id;
							encuesta_id = jq('#encuesta_id').val();
							jq.cookie('globoEncuesta', encuesta_id, {'path' : '/'});
							jq('#encuesta').html(response);
						}
					}
				});
			}
		});
	}
};

