

// -----------------
//
//	Document Size
//
// -----------------


 var viewportwidth;
 var viewportheight;
 
 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
 
 if (typeof window.innerWidth != 'undefined')
 {
	  viewportwidth = window.innerWidth,
	  viewportheight = window.innerHeight
 }
 
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

 else if (typeof document.documentElement != 'undefined'
	 && typeof document.documentElement.clientWidth !=
	 'undefined' && document.documentElement.clientWidth != 0)
 {
	   viewportwidth = document.documentElement.clientWidth,
	   viewportheight = document.documentElement.clientHeight
 }
 
 // older versions of IE
 
 else
 {
	   viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
	   viewportheight = document.getElementsByTagName('body')[0].clientHeight
 }
 
 //alert('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');
 
 var free_space = viewportheight - 580;

 if(free_space<10) free_space = 10
 
 var free_top = free_space * 0.25;
 var free_bottom = free_space * 0.75 + 90;
 
 

 
jQuery(document).ready(function() {


	jQuery("#main-area").css("margin-top",free_bottom);
	jQuery("#featured").css("margin-top",free_top);
	
	jQuery("#services").css("height",0);
	jQuery("#footer").hide();


});


// -----------------
//
//	Search
//
// -----------------


jQuery('#q_search').focus(function() {
	
	jQuery('#q_search').removeClass('q_search_google')

});

jQuery('#q_search').blur(function() {
	if(!jQuery('#q_search').val())
		jQuery('#q_search').addClass('q_search_google')

});




// -----------------
//
//	Old...
//
// -----------------





	//<![CDATA[
	jQuery.noConflict();
	
	var sf = jQuery.fn.superfish,
		is_ie = jQuery.browser.msie;
	
	jQuery.fn.hideSuperfishUl = function(){
		var o = sf.op,
			not = (o.retainPath===true) ? o.$path : '';
		o.retainPath = false;
		var $ul = jQuery(['li.',o.hoverClass].join(''),this).add(this).not(not).removeClass(o.hoverClass)
				.find('>ul').animate({opacity:'hide',height:'hide'},200);
		o.onHide.call($ul);
		return this;
	}
	
	var $top_menu = jQuery('ul.nav');
			
	$top_menu.superfish({ 
		delay:       200,                            // one second delay on mouseout 
		animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
		speed:       200,                          // faster animation speed 
		autoArrows:  true,                           // disable generation of arrow mark-up 
		dropShadows: false,                            // disable drop shadows
		onBeforeShow: function() {
			if ( this.parent('li').css('background-image') === 'none' ) {
				if (!is_ie)
					this.parent('li').find('>a').fadeTo('fast',.5).siblings('span.menu_arrow').stop(true, true).animate( {opacity:'show', top:'-5px'}, 400);
				else 
					this.parent('li').find('>a').siblings('span.menu_arrow').stop(true, true).animate( {opacity:'show', top:'-5px'}, 400);
			}
		},
		onHide: function() {
			if (!is_ie)
				this.parent('li').find('>a').fadeTo('fast',1).siblings('span.menu_arrow').stop(true, true).animate( {opacity:'hide', top:'-15px'}, 400);
			else
				this.parent('li').find('>a').siblings('span.menu_arrow').stop(true, true).animate( {opacity:'hide', top:'-15px'}, 400);
		}
	});
			
	$top_menu.find('> li > ul').parent('li').addClass('sf-ul');
	
	$top_menu.find('> li').each(function(index,domEle) {
		$li = jQuery(domEle);
		if ($li.css('background-image') === 'none')
			$li.append('<span class="menu_arrow"></span>');
	}).find('> ul').prepend('<span class="menu_top_arrow"><span>');
	
	var menu_arrow = 'span.menu_arrow';
			
	$top_menu.find('>li:not(.sf-ul) > a').hover(function(){
		if ( jQuery(this).parent('li').css('background-image') === 'none' ) {
			if (!is_ie) 
				jQuery(this).fadeTo('fast',.5).siblings('span.menu_arrow').stop(true, true).animate( {opacity:'show', top:'-5px'}, 400);
			else
				jQuery(this).siblings('span.menu_arrow').stop(true, true).animate( {opacity:'show', top:'-5px'}, 400);
		}
	},function(){
		if (!is_ie) 
			jQuery(this).fadeTo('fast',1).siblings('span.menu_arrow').stop(true, true).animate( {opacity:'hide', top:'-15px'}, 400);
		else
			jQuery(this).siblings('span.menu_arrow').stop(true, true).animate( {opacity:'hide', top:'-15px'}, 400);
	});
	
	if (!is_ie) {
		$top_menu.find('li ul a').hover(function(){
			jQuery(this).fadeTo('fast',.5);
		},function(){
			jQuery(this).fadeTo('fast',1);
		});
	}

// -----------------
//
//	Pages
//
// -----------------

	var $featured_content = jQuery('#featured_content'),
		$service_tabs = jQuery('#services'),
		$home_tabs = jQuery("ul#main-tabs");
	
	et_service_tabs($service_tabs);	
	
	var active_tab = 9
	var $service_div

	
	function et_service_tabs($service_tabs){
		var $service_div = $service_tabs.find('>div'),
		$service_li = $service_tabs.find('>ul>li');
					
		$service_li.find('a').click(function(){

			var next_tab = jQuery(this).parent('li').prevAll().length
				
			show_page_go(next_tab)	

			window.location.hash = '#' + jQuery(this).attr( 'data'  );
			
			return false;
			
			
		}).hover(function(){
			if ( !jQuery(this).parent('li').hasClass('ui-state-active') && !is_ie ) jQuery(this).fadeTo('slow',.7);
		}, function(){
			if (!is_ie) jQuery(this).fadeTo('slow',1);
		});
	}

	
	var maintabswidth = $home_tabs.width();
	var maintabsleft = Math.round((960 - maintabswidth) / 2);
	if ( maintabswidth < 960 ) $home_tabs.css('left',maintabsleft);
	
	jQuery('.js #featured .container').css('visibility','visible');
	jQuery('.js #featured').css('background','none');
		 
		 
	window.setInterval( function() {Cufon.refresh('#main-tabs a');}, 100 ); 


	function cufon_apply() {
	
		Cufon.set('fontFamily', 'Colaborate-Thin');
		Cufon.replace('#featured h2.title, #featured .description p, #entries-area h1, #entries-area h2, #entries-area h3, #entries-area h4, #entries-area h5, #entries-area h6',{textShadow:'1px 1px 1px #fff'})('ul#main-tabs span, span.fn')('.tab-slide h3.title, #footer-widgets h3',{textShadow:'1px 1px 1px #fff'});
		
		Cufon.set('fontFamily', 'Colaborate-Medium');
		Cufon.replace('#featured h2.title span, #featured .description p span',{textShadow:'1px 1px 1px #fff'})('.tab-slide h3.title span',{textShadow:'1px 1px 1px #fff'});
	
		Cufon.now();
	}
	
	cufon_apply();
	
	
	// handle the click, used for links.
	
	function show_page_go(next_tab) {	
		
		if ( next_tab != active_tab ) {
			
			active_tab = next_tab

			// new content div size		
			jQuery('.tab-slide').css({height: viewportheight-70});

			//clean old div
			//jQuery('.content_tab').animate( {opacity: 'hide'},500 )
			jQuery('.content_tab').hide()
			jQuery('.tab-slide').addClass('loading_page')
			
			
			
			
			// move to the top
			//jQuery('html, body').animate({scrollTop:viewportheight-120}, 2000);
			jQuery('html, body').scrollTop(viewportheight-120);

			
			// hilight selected tab
			jQuery('.home-tab').removeClass('ui-state-active')
			jQuery('#tab_link_' + next_tab).addClass('ui-state-active')
			
			// get page
			if(!jQuery('#content_tab_' + next_tab).hasClass('loaded')) {
			
				jQuery.ajax({
				  url: 'http://www.gsolidario.org/new2/',
				  data: {tab : next_tab},
				  success: function(data) {
					jQuery('#content_tab_' + next_tab).html(data);
					jQuery('#content_tab_' + next_tab).addClass('loaded');
					cufon_apply();
					show_page(next_tab);
				  }
				});
				
			} else {
				show_page(next_tab);
			}


		}		
	}
	
	function show_page(next_tab) {	

		// show content div
		jQuery('#content_tab_' + next_tab).animate( {opacity: 'show'},500)				
		jQuery('.tab-slide').removeClass('loading_page')
		//get_all_page(next_tab)
	
	}
	
	
	function get_all_page(next_tab) {
	
		for (i=1;i<=5;i=i+1) {
			
			if(i != next_tab) {
				if(!jQuery('#content_tab_' + i).hasClass('loaded')) {
					jQuery.ajax({
					  url: 'http://www.gsolidario.org/new2/',
					  data: {tab : i},
					  success: function(data) {
						jQuery('#content_tab_' + i).html(data);
						jQuery('#content_tab_' + i).addClass('loaded');
						cufon_apply();
					  }
					});
				}
			}
		}		
	}

	
	
	var hash = window.location.hash;
	var next_tab = 9
	
	if(hash=='#calendario') {
			next_tab = 0	
	} else 	if(hash=='#gsolidario') {
			next_tab = 1
	} else 	if(hash=='#instituicoes') {
			next_tab = 2
	} else 	if(hash=='#causas') {
			next_tab = 3	
	} else 	if(hash=='#utilizadores') {
			next_tab = 4
	} else 	if(hash=='#noticias') {
			next_tab = 5
	}
	if(next_tab<9)	setTimeout("show_page_go(next_tab)",1000);
	
	
// -----------------
//
//	Login
//
// -----------------

	jQuery('#logininfo').mouseover( function(){
		//jQuery('#loginbox').animate({ opacity: 'show', top: '105px' }, 600); 
		//jQuery('#logininfo').animate({ color: '#999999'}, 600); 
		jQuery('#loginbox').show()

	});		
	
	jQuery('#logininfo').mouseout( function(){
		jQuery('#loginbox').hide(); 
		//jQuery('#loginbox').animate({ opacity: 'hide', top: '120px' }, 600); 
		//jQuery('#logininfo').animate({ color: '#444444'}, 600); 
	});		

	jQuery('#login_form').submit( function(){
		alert("do");
		//jQuery('#loginbox').animate({ opacity: 'hide', top: '120px' }, 600); 
		//jQuery('#logininfo').animate({ color: '#444444'}, 600); 
	});		
	


