
var ecosphere = {
		
	bootstrap: function()
	{	
		$('.primary-links-submenu').each(function (){
			parentLeft = Math.round($(this).parent().offset().left - $('#wrapper').position().left + ($(this).parent().width() / 2));
			var right = 955 - parentLeft - $(this).width() + 35;
			
			if(right > 10 && !$(this).is($('li.submenu ul').first())){
				$(this).css('right', right);
			}
		});
		
		ecosphere.projects();
		
		$('#primary-links > ul > li').hover(function() {
			// mouse over

			if ($('#primary-links > ul > li.fixed').length > 0 && $(this).hasClass('fixed') == false)
			{
				$('#primary-links > ul > li.fixed > ul').hide();
				$('#primary-links > ul > li.fixed').addClass('no-bg');
			}
		}, function() {
			// mouse out
			
			if ($('#primary-links > ul > li.fixed').length > 0 && $(this).hasClass('fixed') == false)
			{
				$('#primary-links > ul > li.fixed > ul').show();
				$('#primary-links > ul > li.fixed').removeClass('no-bg');
			}
		});
		
		$('.sub .title p').prepend('<img class="arrow" style="vertical-align:middle;margin-left:-5px!important;margin-right:5px!important" src="'+SITE_LAYOUT_URL+'images/elements/bullet-arrow.png"/>');
		
		$('.sub .title').bind('click', function(e) {
			element = $(e.target).parents(".sub");

			if ($(element).hasClass('selected')) {
				$(element).find('.body').slideUp('slow');
			}
			else {
				$(element).siblings('.sub').removeClass('selected');
				$(element).siblings('.sub').find('.body').slideUp();
				$(element).find('.body').slideDown('slow');
			}

			$(element).toggleClass('selected');
		})
	},
	
	toggleNews: function()
	{
		$('#main-right-news .links div:visible').fadeOut('slow', function(){$('#main-right-news .links div.hidden').fadeIn('slow').removeClass('hidden');$(this).addClass('hidden');});
		
		setTimeout('ecosphere.toggleNews()', 5000);
	},
	
	projects: function()
	{
		$('#main-content table thead').each(function(){
			
			$(this).find('tr td').first().css({			
				'-webkit-border-top-left-radius':'5px',
				'-moz-border-radius-topleft':'5px',
				'border-top-left-radius':'5px'
			});
			
			$(this).find('tr td').last().css({
				'-webkit-border-top-right-radius':'5px',
				'-moz-border-radius-topright':'5px',
				'border-top-right-radius':'5px'
			});
			
		});
		
		
	},
	
	countries: function()
	{	
		$.ajax({
			url: SITE_URL + 'ajax/projectos/getCountriesForRegion/' + $('#filter-region').val(),
			type: 'GET',	
			dataType:'json',
			success: function(data) {
				
				var options = '';
				
				for (i in data)
				{					
					options += '<option value="' + i + '">' + data[i] + '</option>';		
				}				
				
				$('#filter-country').html(options); 
				$('#filter-country').selectmenu('destroy');
				$('#filter-country').selectmenu({
					style:'dropdown',
					menuWidth: 170,
					width: 140,
					maxHeight: 200						
				});
			}
		});
		
		return false;
		
		
	},
	
	map: function ( switchId, mapId){
		$('#map-switch').children().each( function () {
			$(this).hide();
		});		
		$('#' + switchId).show();
		
		
		$('#map').children().each( function () {
			$(this).hide();
		});		
		$('#' + mapId).show();
	},
	
	fancybox: function()
	{		
		$(".fancybox-seed").fancybox({
            'scrolling':'no',
            'centerOnScroll':true,
            'titleShow': false
		});
	},
	
	initTableSorter: function ()
	{
		$("#project-list").bind("sortStart",function() { 
	        $("#project-list tr.separator").remove(); 
	    }).bind("sortEnd",function() {
	    	$('tbody tr:odd').removeClass("even"); 
	    	$('tbody tr:even').addClass("even");	    	
	    	$("#project-list tbody tr").after('<tr class="separator">');
	    });
		
		$("#project-list tbody tr").after('<tr class="separator"></tr>');		
	},
	
	newsletter : {
		message: function(data)
		{			
			if ($('#newsletter-signup form input[type="submit"]').parent('p').find('span').length == 0)
				$('#newsletter-signup form input[type="submit"]').parent('p').prepend('<span style="float:left"></span>');
			
			$('#newsletter-signup form input[type="submit"]').parent('p').find('span').html(data);
		},
		
		submit: function(errorMsg)
		{
			ecosphere.newsletter.message('<img src="' + SITE_LAYOUT_URL + 'images/elements/loader.gif' + '" />');				
						
			var _data = "";
			
			$('#newsletter-signup form input[type!="submit"]').each(function () {				
				if ($(this).attr('name').length > 0)
					_data += "&" + $(this).attr('name') + "=" + $(this).val();
			});			
						
			$.ajax({
				url: SITE_URL + 'ajax/newsletter/subscribe',
				type: 'POST',
				dataType: 'json',
				data: _data.substring(1),
				success: function(data) {					
					ecosphere.newsletter.message(data[1]);
					
					if (data[0] == true)
						$('#newsletter-signup form input[type="text"]').val('');
				},
				error: function() {					
					ecosphere.newsletter.message(errorMsg);
				}
			});
			
			return false;
		}
	},
	
	contact : {
		message: function(data)
		{
			if ($('#contact-form form input[type="submit"]').parent('p').find('span').length == 0)
				$('#contact-form form input[type="submit"]').parent('p').prepend('<span style="float:left"></span>');
			
			$('#contact-form form input[type="submit"]').parent('p').find('span').html(data);
		},
		
		submit: function(errorMsg)
		{			
			var _data = "";
			
			ecosphere.contact.message('<img src="' + SITE_LAYOUT_URL + 'images/elements/loader.gif' + '" />');
			
			$('#contact-form form input[type!="submit"], #contact-form form textarea').each(function () {
				if ($(this).attr('name').length > 0)
					_data += "&" + $(this).attr('name') + "=" + $(this).val();
			});			
			
			$.ajax({
				url: SITE_URL + 'ajax/contacto/submeter-mensagem',
				type: 'POST',
				dataType: 'json',
				data: _data.substring(1),
				success: function(data) {					
					ecosphere.contact.message(data[1]);

					if (data[0] == true)
					{
						$(':input','#contact-form form')
						 .not(':button, :submit, :reset, :hidden')
						 .val('')
						 .removeAttr('checked')
						 .removeAttr('selected');
					}
				},
				error: function() {					
					ecosphere.contact.message(errorMsg);
				}
			});
			
			return false;
		}
	},
	
	news: {
		ellipsis: function()
		{
			$('.links-list ul li').each(function (){
				var h = 48 + 8; //height  + padding;
				var t = $(this).find('a').text()
				var cutaway = -1;
				
				while (t.length > 0 && $(this).height() > h)
				{
					if (cutaway == -1)
						cutaway = -4;
						
					t = $(this).find('a').text();
					
					$(this).find('a').text(
						$(this).find('a').text().substr(0, t.length + cutaway)
						+
						(cutaway == -1 ? '' : '...')
					);
				}
				
				if (cutaway == -4)
				{
					var text = $(this).find('a').text();
					
					splits = text.split(' ');
					splits.pop();
					splits.pop();
					
					$(this).find('a').text(
						splits.join(' ') + '...'
					);
				}
				
				$(this).find('.project-teaser').height(32); // Fixed height			
			});		
		}
		
	},

	banner : {
		hideBanner: function()
		{
			$('#birthday-celebration,#banner-news').fadeOut('500');
			setTimeout(400); 				
		}
	}
}




