/**
 mentos.com.es
 singulae.es
 2011
 
 JS/jQuery scripts
 
 2/jun/2011
 
**/

// <![CDATA[ 

var FUNCs = {		

	// Dimensiona escenario
	stageIni: function(ie){		
	
		var doc = $(document); 
		var bod = $('body');
		var rez = $('#mentos');
		
		var dims = {
			  docW : parseInt($(doc).width()),
			  docH : parseInt($(doc).height()),
			  bodW : parseInt($(bod).width()),
			  bodH : parseInt($(bod).height())
		};
		
		if(ie){	dims.docW = dims.bodW; };
		
		dims.docW = dims.docW > 1280 ? 1280 : dims.docW;
		dims.docW = dims.docW < 1024 ? 1024 : dims.docW;
		dims.docW = dims.docW-50;		

		$(rez).css({
			width:dims.docW+'px',
			height:'600px',
			margin: '-300px 0px 0px -'+(dims.docW/2)+'px'
		});	
		
		// Anims
		var mentos = $('#mentos');
		var center = {
				contH : parseInt($(mentos).width())/2,
				winH  : parseInt($(window).width())/2,
				contV : parseInt($(mentos).height())/2,
				winV  : parseInt($(window).height())/2
		};
		
		$('#tv')
			.spStop()
			.spRandom({
				top: 	(center.winV-center.contV)-50,
				bottom: (center.winV-center.contV), 
				left: 	(center.winH-center.contH)+100, 
				right: 	(center.winH+center.contH)-100
			})
			.spStart();
		
		$('#nube')
			.spStop()
			.spRandom({
				top: 	(center.winV-center.contV)+100, 
				bottom: (center.winV-center.contV)+100, 
				left: 	(center.winH+center.contH)-200, 
				right: 	(center.winH+center.contH)-100
			})
			.spStart();
				

	},
		
	// Animacion 'flotacion'
	flote: function(id,movpx)
	{
		if(!movpx){movpx = 5;};
		
		$(id).animate({marginTop:"-="+movpx+"px"},{queue:true, duration:450}).animate({marginTop:"+="+movpx+"px"},450, function() {
		setTimeout("FUNCs.flote('" + id + "',"+movpx+")", 0);
		});

	},
	
	// Random de numero
	randomz: function(hig,low)
	{
		return parseInt(rand_no = Math.floor((hig - (low - 1)) * Math.random()) + low);
	},
	
	// Random de simbolo + -
	randomSymb:function(){
		var num = FUNCs.randomz(2,1);
		if(num>1){var symb="+";}else{var symb="-";}; 
		return symb;
	},
	
	getRuta: function()
	{
		var rutatmp = location.href;
		var rutapos	= rutatmp.lastIndexOf("/");
		var rutaResult 	= rutatmp.substr(0,rutapos+1);
		return rutaResult;
	
	},	
	
	getPag: function()
	{	
		// En que pag. estamos?
		var pathurl=window.location.pathname;
		var ini = pathurl.lastIndexOf('/');
		var fin = pathurl.lastIndexOf('.');
		var pag = "";
		fin <=0  ? pag = pathurl.substring(ini+1): pag = pathurl.substring(ini+1,fin);	
		return pag;
	
	},
	
	
	verVideo: function(arguments)
	{
		
		var ops = arguments || {};
		var autostart = ops.autostart || false;
		var image = ops.image || false;
		var rutadir = ops.rutadir || 'http://'+document.location.hostname+'/';//"http://www.singulae.com/clientes/mentos/";
		var id = ops.id || false;
		var video = ops.video || false;

			
		var flashvars = {
			file:rutadir+video,
			image:rutadir+image,
			autostart:autostart,
			/*skin:'js/bekle.zip',*/
			/*skin:'js/beelden.zip',*/
			frontcolor:'000000',
			lightcolor:'00b3ed',
			controlbar:false/*'over'*/ 
		};

		
		var params = {
				allowfullscreen:"true", 
				allowscriptaccess:"always"
		};
		var attributes = {
				id:id,  
				name:"player_"+id
		};
		swfobject.embedSWF("js/player.swf", id, "210", "150", "9.0.115", false, flashvars, params, attributes);
	},



	// Telon transic pags
	/*
	anitelon: function(url){	
		
		$('#telon').css({
          top:      -$(window).height(),
          width:    $(window).width(),
          height:   $(window).height()+20
        });

		$('#telon_int').css({height:  $(window).height()-10});
		
		$('#telon').animate({top:[0, 'easeOutQuad']}, {
          queue:    false,
          duration: 600,
          complete: function() {
            $(this).css({top:'-5000px'});
			alert(url);
          }
        });
	},*/
	
	// Ani cam
	anicam: function(s){
		if(s){	$('#cam').sprite({no_of_frames: 8,play_frames: 4});	};
		
		var bgpos = $('#cam').css('background-position');
		
		if ($.browser.msie) {
			var bgX = $('#cam').css('background-position-x') || '0';
		} else {
			var bgX = ($('#cam').css('background-position') || ' ').split(' ')[0];
		};
		bgpos = bgX.replace('px','');		
		
		if(bgpos=='-270'){
			var higt = 50000; 
			var lowt = 1000;
			var zzz  = false;
		
		}else{
			var higt = 5000;
			var lowt = 3000;
			var zzz  = true;
		};

		var t = FUNCs.randomz(higt,lowt);
		
		if(zzz && s==true){
			var zt= t/5;
			setTimeout(function(){FUNCs.generatezz()},zt);
			setTimeout(function(){FUNCs.generatezz()},zt*2);
			setTimeout(function(){FUNCs.generatezz()},zt*3);
			setTimeout(function(){FUNCs.generatezz()},zt*4);
			setTimeout(function(){FUNCs.generatezz()},zt*5);
		}else{
			$('.zzz').remove();		
		};
		
		setTimeout(function(){FUNCs.anicam(true);}, t);
	},
	
	// Ani Zzz(cam)
	generatezz:   function (){
	
		var ids = Math.floor(Math.random()*1000);
		var conh  = $('#mentos').height()/2;
		var winh  = $(window).height()/2;
		var conw  = $('#mentos').width()/2;
		var winw  = $(window).width()/2;
		
		var topzzz  = (parseInt(winh-conh)-10);
		var leftzzz = (parseInt(winw+conw)-80);
		
		var animationLeft 	  = leftzzz-FUNCs.randomz(10,-10);
        var animationEndY     = topzzz-FUNCs.randomz(40,20);
        var animationDuration = 1500 + FUNCs.randomz(500,0);
        var particleSize      = FUNCs.randomz(20,5);        
		
        var div = jQuery('<div class="zzz '+ids+'"></div>').css({
          position: 'absolute',
          top:      topzzz,
          left:     leftzzz,
          width:    particleSize,
          height:   particleSize,
		  opacity: 0.4,
		  zIndex: 1000
        }).appendTo('body');
		
        div.animate({
          left:     animationLeft,
          top:      [animationEndY, 'easeOutQuad']
        }, {
          queue:    false,
          duration: animationDuration,
          complete: function() {
            $(this).remove();			
          }
        });	
      }

};


(function($) {

	$(document).ready(function() {	

		
		// INIT
		var mentos = $('#mentos');
		var center = {
				contH : parseInt($(mentos).width())/2,
				winH  : parseInt($(window).width())/2,
				contV : parseInt($(mentos).height())/2,
				winV  : parseInt($(window).height())/2
		};
		
		// ANIMS
		$('#tv')
			.click(function(e){
			/*
				e.preventDefault();
				$(this).animate({'left': '-220px'}, 600,'easeInBack',function(){ //IR a apdo. document.location.href=$(this).attr('href'); });
			*/
			})
			.sprite({fps: 12, no_of_frames: 3})
			.spRandom({
				top: 	(center.winV-center.contV)-50,
				bottom: (center.winV-center.contV),  
				left: 	(center.winH-center.contH)+100, 
				right: 	(center.winH+center.contH)-100,
				pause:	12000
			})
			.mousedown(function(){$('#tv').fps(36);})
			.isDraggable({start:function(){$('#tv').fps(36);},stop:function(){$('#tv').fps(12)}});
	
		
			
		$('#nube')
			.sprite({fps: 8, no_of_frames: 6})
			.spRandom({
				top: 	(center.winV-center.contV)+100, 
				bottom: (center.winV-center.contV)+100, 
				left: 	(center.winH+center.contH)-200, 
				right: 	(center.winH+center.contH)-100,
				pause:	10000
			})
			.isDraggable()
			.click(function(e){
			/*
				e.preventDefault();
				$('#nube').animate({'left': ($(window).width()+100)}, 600,'easeInBack',function(){ // IR a apdo. document.location.href=$(this).attr('href'); });
			*/
			});
			
		$('#gos')
			.sprite({fps: 12, no_of_frames: 2})
			.hover(function(){$(this).spState(2)},function(){$(this).spState(1)});

			
		// DEDO		
		var pagi = FUNCs.getPag();
		var patron = /sabor|pocket|bolsa|productos|caramelos|chicles|contacto|secreto|promociones|freshnews|new|freshcargables|lamarca|historia|publicidad/i; 
		var pag = pagi.search(patron); 
		//alert(pag);
		
	  
		if(pag >= 0){
			$('#dedo')
				.css({'width':'169px','height':'214px'})
				.sprite({fps: 4, no_of_frames: 4});	
			
			patron = /sabor|pocket|bolsa|productos|caramelos|chicles|lamarca|publicidad|historia/i; 
			pag = pagi.search(patron);
			if(pag >= 0){
				if($('ul.sf-menu').length > 0){
					$("ul.sf-menu").superfish({animation: {height:'show'},delay: 500}); 
				}
			};
			
			if(pagi === 'secreto'){
				$("#parchment").colorbox({inline:true, href:"#paperText", innerWidth:397, innerHeight:481, opacity:0.5});
			};
			
			if(pagi === 'publicidad-grafica'){
				$("a[rel='publi']").colorbox();
			};
			/*
			if(pagi === 'publicidad-tv'){
				$('#videos').bxSlider();
			};*/
			
			if(pagi === 'promociones'){
				
				$('.tirapromo').hover(function(){	
				
					// Las otras
					$('#promos li').not(this).stop().animate({height: '40'},{queue:false, duration:'fast'});	
					$('#promos li').not(this).children('.imgpromo').stop().animate({height: '40'},{queue:false, duration:'fast'});
					$('#promos li').not(this).children('.imgpromo').children('img').stop().animate({marginTop: '-120px'},{queue:false, duration:'fast'});
					$('#promos li').not(this).children('.mas').show();
					
					// Esta
					$(this).stop().animate({height: '120'},{queue:false, duration:'fast'});	
					$(this).children('.imgpromo').stop().animate({height: '120'},{queue:false, duration:'fast'});
					$(this).children('.imgpromo').children('img').stop().animate({marginTop: '0px'},{queue:false, duration:'fast'});
					$(this).children('.mas').hide();
					
					
					var texto = $(this).children('.txtpromo').html();
					$('#txtpromo').html(texto);
					
					
				},function(){/* */});
				
					$('#promos li:eq(0)').trigger("mouseover");
				
  
			};
			
		}else{
			
			$('#dedo').sprite({fps: 7, no_of_frames: 4});
			$('#promos_cont').coinslider({ width: 482, heihgt:278, navigationIndex:true, navigation:false, 	spw: 10, sph: 1,sDelay: 3,delay: 5000 });
			
		};
		

		

		
		// MENÚ GRAGEAS
		$(".opmenu").hover(
			function() {			
				$(this).find("a .globo").stop(true, true).animate({opacity: "show", top: "-30"}, {queue:false, duration:600, easing: 'easeOutElastic'});			
				$(this).children(".submenuGragea").stop(true, true).animate({opacity: "show",top: "60"}, "fast");
			}, 
			function() {
				$(this).find('a .globo').stop(true, true).animate({opacity: "hide", top: "-50"}, "fast",function(){$(this).css('top','0');});
				$(this).children(".submenuGragea").stop(true, true).animate({opacity: "hide",top: "70"}, "slow",function(){});
		});
		
		
		//Popups
		$("a[rel='popup']").colorbox({width:"700px", height:"500px", iframe:true});
		$("a[rel='popup2']").colorbox({width:"700px", height:"500px", iframe:true});

		
		// Resize
		var ie = $.browser.msie ? true : false;		
		FUNCs.stageIni(ie);
		$(window).resize(function() { FUNCs.stageIni(false);});
		
		// Anims		
		FUNCs.flote('#tv');
		FUNCs.flote('#nube');
		FUNCs.anicam();	
		
		$('#logo').animate({'top': '+=30px'}, 1000,'easeOutElastic');
		$("ul#news").liScroll();

		});
		
})(jQuery);

// ]]>
