var swfExpressInstall = "/media/swf/expressInstall.swf";

function track(code){
	try {
		pageTracker._trackPageview(code);
	} 
	catch (err) {
	}
}

var Structure = 
{
	init: function()
	{
		track("/theride/home/theride/carregou");

		Structure.header.init();
		Structure.preloadImages();
		Structure.twitter();
		Structure.socialButtons();
	},
	
	header:
	{
		init: function()
		{
			Structure.header.loadSWF();
			Structure.header.openPermalinkRoute();
		},
		
		loadSWF: function()
		{
			var wl				= window.location,
				paramPairs		= wl.search.substring(1).split("&"),
				params			= {wmode: "transparent"},
				attributes		= {},
				flashvars		= {
                                    receiver: "gmap_changedCategory"
                                },
				playerVersion	= "10.0.0";
				
			if(!swfobject.hasFlashPlayerVersion(playerVersion)){wl = "noflash.html";}
			for( var i = paramPairs.length ; i-- ; ){
				var pair		   = paramPairs[i].split("=");
				flashvars[pair[0]] = pair[1];
			}
			swfobject.embedSWF("/media/swf/rider_menu.swf?menuXML=/media/xml/menu.xml&menu=ride&animate=true", "header", "100%", "80", playerVersion, swfExpressInstall, flashvars, params, attributes);
			swfobject.embedSWF("/media/swf/circle.swf", "filter-places", "125", "130", playerVersion, swfExpressInstall, flashvars, params, attributes);
		},
		
		openPermalinkRoute : function() {
		
			$( '#route-link' ).bind( 'click', function() {
			
				/* TRACKING */
				trace('/rider/home/rota/espalhar/clicou');
			
				if( $( '#permalink-route' ).is( ':visible' ) ) {
					$('#permalink-route').fadeOut( 500 );

				} else {
					$('#permalink-route').fadeIn( 500 );			

					// Hide this thing after 6 seconds
					window.setTimeout( function(){
						$('#route-link').trigger('click');	
					}.bind(this), 4500);
				}			
					
				if( $( '#permalink-route input' ).val() == '' ) {	
					$( '#permalink-route input' ).val('Você ainda não fez uma rota.');
				}

				track('/theride/home/rota/espalhar/clicou');	
				return false;
				
			} );
			
			$( '#permalink-route input' ).click( function() {						
				$( this ).select();											
			} );	
			
		}
		
	},
	
	preloadImages: function()
	{
		var images 			=	[	
								//'bg-page.jpg',
								'bg.jpg',
								'how-to-use-noalpha.png',
								'brazilian-states-noalpha.png'							
								],
			prefix			= 	'/media/img/';		

		for( var i=0 , len = images.length, arr = [] ; i < len ; i++ )
		{
			arr[i] = new Image();
			arr[i].src = prefix + images[i];
		}
	},
	
	twitter: function()
	{
		$(".stwitter").tweet({
			avatar_size: 44,
			count: 3,
			query: "%23the_ride",
			loading_text: " "		
		});
	},
	
	socialButtons : function() {
		
		$( '#share_fb' ).click( function() {
			
			var centerWidth  = (window.screen.width - 500) / 2;
			var centerHeight = (window.screen.height - 325) / 2;
	
			window.open( $('#share_fb').attr('href'), "", "width=500,height=325,top=" + centerHeight + ", left=" + centerWidth + ",scrollbars=no, statusbar=no, resizable=no, toolbar=0" );
			
			return false;
			
		} );
		
		$( '#share_twitt' ).click( function() {

			var centerWidth  = (window.screen.width - 560) / 2;
			var centerHeight = (window.screen.height - 350) / 2;

			window.open( $('#share_twitt').attr('href'), "", "width=560,height=350,top=" + centerHeight + ", left=" + centerWidth + ",scrollbars=no, statusbar=no, resizable=no, toolbar=0" );
			
			return false;			

		} );			
		
	}

};

$(document).ready(Structure.init);
