//OUR MODULE APPLICATION

var xStartHandle = 0;
var xStartMouse = 0;
var duration = 13;
var count = 0;
var showTextTimer = true;
var showLoadingBar = true;
var canSkipAd = true;
var doneLoading = false;
var textArray = new Array();
textArray['timerText'] = 'El juego se iniciará en %gameTimer% segundos...';


function advertisementCounter(){
    if(!doneLoading){
        count++;
        if(showTextTimer) $('#timertext').text(textArray['timerText'].replace('%gameTimer%', (duration - count)));
        if(showLoadingBar) $('#barra').width(200 - Math.round(((duration - count) / duration ) * 200));
        if(count - duration >= 0){
            prerollDone();
        } else {
			if (count == 4) $('#nomostrar').show();
            height = $('#obj').attr('height');
            if($('#game-swf')) $('#game-swf').attr('style', 'height: 0px; visibility: hidden;');
            setTimeout("advertisementCounter()", 1000);
        }
    }
}

function prerollDone(){
    doneLoading = true;
    duration = 0;
    height = $('#obj').attr('height');

    if($('#loader')) $('#loader').hide();
    if($('#game-swf')) $('#game-swf').attr('style', 'visibility: visible; height: '+height+'px;');
}

//OUR MODULE APPLICATION
var PEPIJUEGOS = function(){
	var onload = function(){

		var loader = function(){
			//if ($('body#game')[0]) {
				var doneLoading = false;
				advertisementCounter();
			//}
		}();    
			
		var mostrarjuego = function(){
			//link hacia arriba
			$('a.mostrarjuego').click(function(){
				prerollDone();
				return false;
			});     
		}(); 

		//buscador
		$('#search').submit(function(){
			var $$ = $(this), val = $$.find('#query').val();
			window.location = "http://" + document.domain + "/buscar/" + val;
			return false;
		});

		//boletin
		$('#boletin').submit(function(){
			var values = {};
			$.each($('input[name^=boletin]'),function(i, field) {
				    values[i] = field.value;
			});
	
			var mail = values[0];
			var id = values[1];
			var token = values[2];

			var url = location.href.split('/')[0]+'//'+location.href.split('/')[1]+location.href.split('/')[2];

			$.post(url+'/boletin/create', {mail:mail, id:id, token:token}, function(data){
				$('.bmessage').text(data);
			});
			return false;
		});


		$('#query').focus(function(){
			$(this).attr("value", "");
			return false;
		});

		$('.tooltip').tooltip({
		    bodyHandler: function() { 
        		return $(this).find('>p').text(); 
		    }, 
	    	showURL: false 
		});

		$('.vote-up').click(function(){
			var cat = location.href.split('/')[3].replace('#','');
			var juego = location.href.split('/')[4].replace('#','');
			var url = '/'+cat+'/'+juego+'/votar/1';
			$.getJSON(url, {}, function(data){
				$('.vote-up > img').attr('src', '/img/up-votado1.png');
				$('.vote-down > img').attr('src', '/img/down-votado.png');
			});
			return false;
		});

		$('.vote-down').click(function(){
            var cat = location.href.split('/')[3].replace('#','');
            var juego = location.href.split('/')[4].replace('#','');
            var url = '/'+cat+'/'+juego+'/votar/0';
            $.getJSON(url, {}, function(data){ 
                $('.vote-up > img').attr('src', '/img/up-votado.png');
                $('.vote-down > img').attr('src', '/img/down-votado1.png');
            });
			return false;
		});

		$('a.fullscreen').click(function(){ 
			var url = $('#game-swf').find('embed').attr('src') ? $('#game-swf').find('embed').attr('src') : $('#game-swf').find('iframe').attr('src');
			window.open(url,"null","toolbar=no,location=no, directories=no, status=no, menubar=no ,scrollbars=no, resizable=no, fullscreen=yes");
			return false;
		});


			
	}
	
	//initialization
	return {
		init: function(){
			onload();
		}
	}
}();

//execute the module
$(PEPIJUEGOS.init);



