var folha = {};

var player_home = '<object width="457" height="368" id="player_12346510" ><param value="true" name="allowfullscreen"/><param value="http://storage.mais.uol.com.br/embed_v2.swf?mediaId=12346510" name="movie"/><param value="always" name="allowscriptaccess"/><param value="window" name="wmode"/><embed id="player_12346510" width="457" height="368" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" src="http://storage.mais.uol.com.br/embed_v2.swf?mediaId=12346510" wmode="transparent" /></embed><noscript><a href="http://mais.uol.com.br/view/12346510">Conheça o site Mapa do Brincar</a></noscript></object>';

folha.mapadobrincar = {
	player: {
		dimensions: {
			video: {
				width: 290,
				height: 220
			},
			audio: {
				width: 290,
				height: 60
			},
			gallery_audio: {
				width: 590,
				height: 150
			}
		},
		
		firstplay: true,
		
		pattern: /^http:\/\/mais\.uol\.com\.br\/view\/([\d\w\-_]+).*$/ ,
		
		video_embed: '<object width="{width}" height="{height}" id="player_{id}" ><param value="true" name="allowfullscreen"/><param value="http://storage.mais.uol.com.br/embed_v2.swf?mediaId={id}" name="movie"/><param value="always" name="allowscriptaccess"/><param value="transparent" name="wmode"/><embed id="player_{id}" width="{width}" height="{height}" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" src="http://storage.mais.uol.com.br/embed_v2.swf?mediaId={id}" wmode="transparent" /></embed><noscript><a href="http://mais.uol.com.br/view/{id}">Veja a vídeo</a></noscript></object>' ,
		audio_embed: '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="{width}" height="{height}" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param value="transparent" name="wmode"/><param name="movie" value="http://storage.mais.uol.com.br/embed_audio2.swf?mediaId={id}" /><param name="quality" value="high" /><embed src="http://storage.mais.uol.com.br/embed_audio2.swf?mediaId={id}" quality="high" width="{width}" height="{height}" swLiveConnect=true align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent" /></embed></object>',
		gallery_audio_embed: '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="{width}" height="{height}" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param value="transparent" name="wmode"/><param name="movie" value="http://media.folha.uol.com.br/folhinha/2011/12/09/mapadobrincar_player.swf" /><param name="quality" value="high" /><param name="flashVars" value="mp3=http://storage.mais.uol.com.br/{id}.mp3&titulo={title}&descricao={description}&autoplay={autoplay}" /><embed src="http://media.folha.uol.com.br/folhinha/2011/12/09/mapadobrincar_player.swf" type="application/x-shockwave-flash" width="{width}" height="{height}" play="true" loop="true" menu="false" quality="high" scale="showall" wmode="transparent" allowfullscreen="true" seamlesstabbing="true" allowscriptaccess="always" allownetworking="true" devicefont="" salign="" bgcolor="" base="" flashvars="mp3=http://storage.mais.uol.com.br/{id}.mp3&titulo={title}&descricao={description}&autoplay={autoplay}"></object>',
	
		
		video: function (id, width, height) {
			var that = this ;

			width = width || this.dimensions.video.width ;
			height = height || this.dimensions.video.height ;
			return that.video_embed.replace(/{id}/g, this.pattern.exec( id )[1]).replace(/{width}/g, width).replace(/{height}/g, height) ;
		},
		
		home: function () {
			$('#mapadobrincar').append('<div class="lightbox"><div class="veil"></div><div class="player"><object width="457" height="368" id="player_12346510" ><param value="true" name="allowfullscreen"/><param value="http://storage.mais.uol.com.br/embed_v2.swf?mediaId=12346510" name="movie"/><param value="always" name="allowscriptaccess"/><param value="window" name="wmode"/><embed id="player_12346510" width="457" height="368" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" src="http://storage.mais.uol.com.br/embed_v2.swf?mediaId=12346510" wmode="window" /></embed><noscript><a href="http://mais.uol.com.br/view/12346510">Conheça o site Mapa do Brincar</a></noscript></object></object></div>')
		},
		
		audio: function (id, width, height) {
			var that = this ;

			width = width || this.dimensions.audio.width ;
			height = height || this.dimensions.audio.height ;
			return that.audio_embed.replace(/{id}/g, this.pattern.exec( id )[1]).replace(/{width}/g, width).replace(/{height}/g, height) ;
		},
		
		gallery_audio: function(id, title, description, autoplay, width, height) {
			var that = this ;
			
			autoplay = true ;
			
			if( that.firstplay === true ) {
				autoplay = false ;
				that.firstplay = false ;
			}

			width = width || this.dimensions.gallery_audio.width ;
			height = height || this.dimensions.gallery_audio.height ;
			title = title || '' ;
			description = description || '' ;
			autoplay = autoplay  ? 'true' : 'false' ;
			
			title = encodeURI( title );
			description = encodeURI( description );
			
			return that.gallery_audio_embed.replace(/{id}/g, this.pattern.exec( id )[1]).replace(/{title}/g, title).replace(/{description}/g, description).replace(/{width}/g, width).replace(/{height}/g, height).replace(/{autoplay}/g, autoplay) ;			
		}
	},
	
	hints: {
		object: '#footer .hints dl',
		animation_speed: 400,
		actual: null,
		total_items: null,
		
		init: function () {
			var that = this;
			
			if( $('#mapadobrincar').hasClass('morpheus') ) {
				$.get('/hints.inc', function(data) {
					$(that.object).html(data);
					that.setup();
				});
			}
			else {
				that.setup();
			}
		},
		
		setup:  function () {
			var that = this;
			
			$(that.object).data('in_transition', false)
			that.total_items = $('dt', that.object).length ;
			that.next() ;
			
			$('#footer .hints a.control').bind('click', function (e) {
				e.preventDefault();				
				that.next();
			});	
		},
		
		rand: function (min, max) {			
			return Math.floor(Math.random() * (max - min + 1)) + min;
		},
		
		random_index: function () {
			var index = this.rand(0, this.total_items - 1) ;			
			return this.actual == index ? this.random_index() : index;
		},
		
		next: function () {
			var that = this;
			
			if( $(that.object).data('in_transition') ) {
				return false;
			}
			
			$(that.object).data('in_transition', true)
			
			that.actual = that.random_index() ;
			
			if( $('dt:visible',that.object).length > 0 ) {
				$('dt:visible, dd:visible', that.object).fadeOut(that.animation_speed, function () {
					$('dt:eq('+that.actual+') , dd:eq('+that.actual+')', that.object).fadeIn(that.animation_speed, function () {
						$(that.object).data('in_transition', false);
					});
				});
			}
			else {
				$('dt:eq('+that.actual+') , dd:eq('+that.actual+')', that.object).show();
				$(that.object).data('in_transition', false);
			}
		}
	} ,
	ads: function(){
		var js = '<script type="text/javascript" src="http://bn.uol.com.br/js.ng/site=folha&amp;chan=homepage&amp;size=220x90&amp;page=3&amp;expble=1&amp;conntype=1&amp;tile=' + ( Math.random() * 10000 ) + '?"><\/script>';
		
		document.write( js ) ;
	}
}

$(document).ready(function() {
	var $mapa = $('#mapadobrincar') ;
	
	//Busca
	$("#sidebar .search form").bind('submit', function () {
		var q = $("input[type='text']",this).val().trim() ;
		
        if (q.length < 4) {
			alert('O termo deve conter mais de 3 caracteres');
			return false;
		}

        // q = q.slice(0,1).toUpperCase() + q.slice(1).toLowerCase() ;
		
		location.href = $(this).attr('action') + '?q=' + encodeURI( q ) ;
		
		return false ;
	});
	
	//Barras de rolagem
	$('.scrollable', $mapa).jScrollPane({scrollbarWidth: 8});
	
	//Balões da home
	var $object_game = $('.object.game' ,$mapa);
	$object_game.bind('mouseover', function () {
		if( $.browser.msie && $.browser.version < 9 ) { // fix png/fade bug on IE
			$('.ballon', this).show();
		}
		else {
			$('.ballon', this).stop().fadeTo(400,1);
		}
	});
	$object_game.bind('mouseout', function () {
		if( $.browser.msie && $.browser.version < 9 ) { // fix png/fade bug on IE
			$('.ballon', this).hide();
		}
		else {
			$('.ballon', this).stop().fadeTo(400,0);
		}
	});
	
	$('.pagination.gallery .wrapper ul a', $mapa).live('click', function (e) {
		e.preventDefault();
		$('.pagination.gallery .wrapper ul li')
		$('.pagination.gallery .wrapper ul li').removeClass('selected')
		$(this).parent().addClass('selected')
		$('.box.gallery span.font.sub.legend').text( $('img', this).attr('rel') )
		$('.box.gallery div.image img').attr('src', $(this).attr('href') )
		$('.box.gallery p.description').text( $('img', this).attr('alt') )
	});
	
	$('.pagination.gallery .wrapper ul li:eq(0) a', $mapa).click();
	
	//Paginação imagens
	$('.pagination.gallery .wrapper', $mapa).jCarouselLite({
        btnNext: ".pagination.gallery .control.next",
        btnPrev: ".pagination.gallery .control.prev",
		visible: 5,
		scroll: 5,
		speed: 600
	});	
		
	$('.box.gallery.videos .image_with_text_horizontal a', $mapa).bind('click', function (e) {
		e.preventDefault();
		$('.box.gallery.videos .image_with_text_horizontal li', $mapa).removeClass('selected');
		$(this).parent().addClass('selected');
		$('.video_container', $mapa).html( folha.mapadobrincar.player.video( $(this).attr('href') ) );
	});
	
	// Dicas da Home	
	folha.mapadobrincar.hints.init();
	
	// Player 
	$('.media a.video' ,$mapa).bind('click', function (e) { 
		e.preventDefault() ;
		$('.media' ,$mapa).html( folha.mapadobrincar.player.video( $(this).attr('href') ) );
	});
	
	$('.media a.audio' ,$mapa).bind('click', function (e) {
		e.preventDefault() ;
		$('.media' ,$mapa).html( folha.mapadobrincar.player.audio( $(this).attr('href') ) );
	});
	
	$('.gallery.audio ul li a' ,$mapa).bind('click', function (e) {
		e.preventDefault() ;
		$('.player' ,$mapa).html( folha.mapadobrincar.player.gallery_audio( $(this).attr('href'), $(this).text(), $(this).attr('title') ) );
	}).eq(0).click();
	
	$('a.watch_video').bind('click', function ( e ) {
        e.preventDefault();
		$('.lightbox').fadeTo(600, 1);
	});
	
	$('.lightbox .veil').bind('click', function () {
		$(this).parent().hide();
	});
	
	
	if( window.PIE ) {
		$('#sidebar ul.menu ul li > a').each(function () {
			PIE.attach(this);
		});
		$('#sidebar ul.menu ul li:first-child, #sidebar ul.menu ul li:last-child').each(function () {
			PIE.attach(this);
		});
		
		$('.jScrollPaneDrag').each(function () {
			PIE.attach(this);
		});
	}
});

