﻿// is called "on load"
 jQuery(function(){

    if ($.facebox)
        jQuery('a[rel*=facebox]').facebox() 

    // get the menu souped up
    jQuery('ul.sf-menu').superfish();
    setSelectedMenuItem();
    
    // check whether we need to show the voting form
    if (jQuery.query)
    {

        var vote = jQuery.query.get('vote');
        // show the vote modal popup if so
       if (vote=="yes" && $('#voteAnchor'))
       {        
            if ($('#mapWrapper'))
                $('#mapWrapper').hide();               

             showModal();
       }
       else if (window.location.pathname == "/Homepage.aspx")
       {
            if ($('#wrapper'))
                $('#wrapper').hide();               
                  
            // don't show the map if ?intro=no
            var intro = jQuery.query.get('intro');
            if (!intro || !(intro=="no"))             
             showMap();
        }
        else {}
     }
          
    if (jQuery.fn.liScroll)
        $("ul#ticker").liScroll({travelocity: 0.03});      
 });     
 
 // show map overlay
 function showMap() 
 {      

      $('#mapAnchor').click();
       // hook up the close button
       $('.close').click($.facebox.close)     
 }
  
 // show voting modal
 function showModal()
 {
      $('#voteAnchor').click();
       $('.close').click($.facebox.close)     
      initVotingForm();
 }
 
// based on selected language and skin, generate the code for the iframe
 function changeWidgetCode()
 {
    var lang = jQuery('#country').val();
    var skin =  jQuery('#skin').val();  
 
    var code = "<iframe src=\"http://www.earthhour.org/widget/vote/Widget.htm?lang=" + lang + "&skin=" + skin + "\" frameborder=\"0\" width=\"620\" height=\"680\"><p>Your browser does not support iframes.</p></iframe>"
 
    jQuery('#codeSnippet').text(code); 
 }

// based on selected banner and size, generate the code for the iframe
 function changeBannerCode(banner)
 {
    if (banner==1)
    {
    // the people banner
        var size = jQuery('#banner_1').val();
        var height= 600;
        var width = 120;

        if (size=="160x600"){width=160; height=600;}
        else if (size=="300x250"){width=300; height=250;}
        else if (size=="728x90"){width=728; height=90;}
                
        var code = "<object height=\"" + height + "\" width=\"" + width + "\" type=\"application/x-shockwave-flash\" data=\"http://earthhour.org/assets/flash/tools/banners/EarthHour_" + size + "_2010.swf\"><param value=\"http://earthhour.org/assets/flash/tools/banners/EarthHour_" + size + "_2010.swf\" name=\"movie\" /><param value=\"transparent\" name=\"wmode\" /><param value=\"true\" name=\"allowFullScreen\" /><param value=\"always\" name=\"allowscriptaccess\" /></object>";
    
    jQuery('#taBanner1').text(code); 
    }
    else if (banner==2)
    {
        // the dates banner
        var size = jQuery('#banner_2').val();
        var height= 250;
        var width = 300;

        if (size=="300x250"){width=300; height=250;}
        else if (size=="728x90"){width=728; height=90;}
                
        var code = "<object height=\"" + height + "\" width=\"" + width + "\" type=\"application/x-shockwave-flash\" data=\"http://earthhour.org/assets/flash/tools/banners/EarthHour_" + size + "_dates.swf\"><param value=\"http://earthhour.org/assets/flash/tools/banners/EarthHour_" + size + "_dates.swf\" name=\"movie\" /><param value=\"true\" name=\"allowFullScreen\" /><param value=\"always\" name=\"allowscriptaccess\" /></object>";
    
    jQuery('#taBanner2').text(code); 
    
    }
    else{}
 }


// go to a countries specific earth hour site
function goToCountryWebsite(select)
{    

    // a hack for scotland since great britain is a umbrella region in the countries info object
    if(select.value=="SCOTLAND")
    {
        window.open("http://www.wwfscotland.org.uk/how_you_can_help/campaign_with_wwf_scotland/it_s_time_to_vote_earth_/");
        return false;
    }
        // check that theres a url specified for it
        if (countriesInfo[select.value] && countriesInfo[select.value].url)
        {
            window.open(countriesInfo[select.value].url);         
            return false; 
        }
        else
        {
            window.open("http://www.earthhour.org");
            return false;
        }
}

function setSelectedMenuItem()
{
   switch(window.location.pathname)
   {
        case("/Homepage.aspx"):
            $("#home").addClass("homeSelected");
            break;
        case("/"):
            $("#home").addClass("homeSelected");
            break;
        case("/SupportersMap.aspx"):
            $("#mapnav").addClass("mapnavSelected");
            break;
        case("/About.aspx"):
            $("#about").addClass("aboutSelected");
            break;
        case("/TakeAction.aspx"):
            $("#action").addClass("actionSelected");
            break;
        case("/SpreadTheWord.aspx") :
            $("#action").addClass("actionSelected");
            break;        
        case("/tools/TellTheWorld.aspx") :
            $("#action").addClass("actionSelected");
            break;        
        case("/tools/widget/Widget.aspx") :
            $("#action").addClass("actionSelected");
            break;        
        case("/tools/logos/Logos.aspx") :
            $("#action").addClass("actionSelected");
            break;        
        case("/tools/banners/Banners.aspx") :
            $("#action").addClass("actionSelected");
            break;        
        case("/tools/posters/Posters.aspx") :
            $("#action").addClass("actionSelected");
            break;        
        case("/howto/Guides.aspx") :
            $("#action").addClass("actionSelected");
            break;        
        case("/howto/Organisers.aspx") :
            $("#action").addClass("actionSelected");
            break;        
        case("/kids/MakeALantern.aspx"):
            $("#lantern").addClass("lanternSelected");
            break;
        case("/kids/Game.aspx") :   
            $("#kids").addClass("selected");
            break;
        case("/Innovators.aspx"):
            $("#innovators").addClass("innovatorsSelected");
            break;
        case("/LatestNews.aspx"):
            $("#news").addClass("newsSelected");
            break;
        case("/Media.aspx"):
            $("#media").addClass("mediaSelected");
            break;
        case("/Spokespeople.aspx"):
            $("#media").addClass("mediaSelected");
            break;
        case("/History.aspx"):
            $("#media").addClass("mediaSelected");
        break;
        case("/mediareleases/Archive.aspx"):
            $("#media").addClass("mediaSelected");
        break;
        default :
            break;
    }
}

// render the lantern in the users chosen language
function changeOnLanternLanguage(select)
{
    // if they've chosen a language
    if (select.value != "-1")
    {        
       var flashString = "<div id=\"flashcontent\" class=\"content\"><p>To make a lantern you need the latest version of <a href=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\">flash player</a>.</p></div>"
    
        $('#flashContainer').html(flashString)

	    var sitemap = "/assets/lantern/sitemaps/" + select.value + ".xml";
	    swfobject.embedSWF("/assets/lantern/main.swf", "flashcontent", "920", "510", "9.0.0", false, {days: 20, created:1000, sitemap:sitemap, trackingID:''}, {}, {id: "Main", wmode: "Transparent"});

    
    }


}
