var Original_Lang = 'en';

$(document).ready( function()
{
    $('#newsSlider').anythingSlider(
    {
        buildArrows         : true,      // If true, builds the forwards and backwards buttons
        buildNavigation     : false,  
        autoPlay            : true,
        delay               : 7000,      // How long between slideshow transitions in AutoPlay mode (in milliseconds)
        animationTime       : 1500,
        /* stopAtEnd           : true,  // Enable this if you just want the slider to play through once then stop on the last slide */
        easing: 'easeInOutExpo'                                                   
    })                            
          
    //-------------------------------------------
    //Jquery needed for every page on the website
    //-------------------------------------------
    
    var FullscreenrOptions = {  width: 1600, height: 1200, bgID: '#bg' };
    jQuery.fn.fullscreenr(FullscreenrOptions);
    
    $('.subCol:nth-child(2n+1)').css('margin-left', '0'); 
    
    $('a#loginBtn').colorbox({
        inline: true
    });
    
            
    $("#searchForm label").inFieldLabels();                                                    
        
    //  Equalise space between nav items
        //var navWidth = $('#nav').width();
        var navWidth = 940;
        var navItemsWidth = 0;
        var navItemsNumber = 0;
        var navLeftMargin = 0;

        // Loop through all top level nav items, get the width for each (including padding and borders) and total them
        $("#nav > li > a").each(function() { 
            navItemsWidth += $(this).outerWidth();
            navItemsNumber++;
        });

        navItemsNumber--;
                       
        navLeftMargin = Math.floor((navWidth-navItemsWidth)/navItemsNumber); // Math.floor gets rid of any decimal points, can't have part of a pixel and it messes up later calculations
                              

        $('#nav > li > a').each(function() { 
             $(this).css('margin-left', navLeftMargin); // Set the margin for top level nav items  
             $(this).parent().find('ul').css('margin-left', navLeftMargin); // And set it for their dropdown as well                    
        });
        
        $('#nav > li:first-child a').css('margin-left', '0')
        $('#nav > li:first-child ul').css('margin-left', '0')
        
        var counter = 0;
        
        $('#nav ul').each(function() { 
             var thisWidth = $(this).outerWidth();
             if (counter == 0)
             {
                thisWidth = thisWidth-4; 
             }
             
             else
             {
                thisWidth = thisWidth-navLeftMargin-4; 
             }                             

             $(this).css('width', thisWidth);                  
             counter++;
        }); 
        
        $('#nav > li').hover(function(){
            var $t = $(this);
            $('ul',$t).show();
        },function(){
            var $t = $(this);
            $('ul',$t).hide();
        });
        
        // Load the translate library with the API key
//        $.translate.load("AIzaSyDcZoedki_uk_cMNY4DXux_V-5NVGUOPBs");
        //$.translate.load("301C79ACA55C1ECCD790B3131DE91DFA1205895A");
        //if(jQuery.cookie('glang') && jQuery.cookie('glang') != 'en') jQuery(function($){$('body').translate('en', $.cookie('glang'), {toggle:true});});
});

function language(lang_pair)
{
	lang_pair = Original_Lang+'|'+lang_pair; 
	
	if(lang_pair.value)
		lang_pair=lang_pair.value;
	
	var lang=lang_pair.split('|')[1];
	
	if(lang=='pt')
		lang='pt-PT';
		
	jQuery.cookie('glang', lang);
	
	$('body').translate('en', lang, {toggle:true,complete:fixMenu});
//	$('#wrapper').translate('en', lang, {toggle:true,complete:fixMenu});
};

function fixMenu()
{
//  Equalise space between nav items
    //var navWidth = $('#nav').width();
    var navWidth = 940;
    var navItemsWidth = 0;
    var navItemsNumber = 0;
    var navLeftMargin = 0;

    // Loop through all top level nav items, get the width for each (including padding and borders) and total them
    $("#nav > li > a").each(function() { 
        navItemsWidth += $(this).outerWidth();
        navItemsNumber++;
    });

    navItemsNumber--;
                   
    navLeftMargin = Math.floor((navWidth-navItemsWidth)/navItemsNumber); // Math.floor gets rid of any decimal points, can't have part of a pixel and it messes up later calculations
                          

    $('#nav > li > a').each(function() { 
         $(this).css('margin-left', navLeftMargin); // Set the margin for top level nav items  
         $(this).parent().find('ul').css('margin-left', navLeftMargin); // And set it for their dropdown as well                    
    });
    
    $('#nav > li:first-child a').css('margin-left', '0')
    $('#nav > li:first-child ul').css('margin-left', '0')
    
    var counter = 0;
    
    $('#nav ul').each(function() { 
         var thisWidth = $(this).outerWidth();
         if (counter == 0)
         {
            thisWidth = thisWidth-4; 
         }
         
         else
         {
            thisWidth = thisWidth-navLeftMargin-4; 
         }                             

         $(this).css('width', thisWidth);                  
         counter++;
    });
}
