$(document).ready(function () {

	$("form#selector").show();

        var JURISDICTION_COOKIE = 'JURISDICTION';
        var selectorPath = "#selector select";
        var INVESTMENT_PATH = "/core/investments/investments.html";
        var TAB_NAME = '#invest-tabbed-data';
        var cookie = new Cookie(document, JURISDICTION_COOKIE, 8760 , "/", null, false);
        var CHANNEL_SELECTOR_URL = "#";

        function UpdateLocationStatus () {

            
            var statusText =  jQuery.trim($("#LocationStatus").text());
            var statusTextParts = statusText.split(/-/);
            var str = statusTextParts[0];
            var currentRegion = "";
            
            $(selectorPath).val('');
            
            if(cookie.load()) {
                currentRegion = cookie.region;
            }
            
          $("select option").each(function () {
               if ($(this).val() == currentRegion && currentRegion != '')
                 str = jQuery.trim($(this).text());
            });

              statusText = str;
              statusText += " - ";
              statusText += jQuery.trim(statusTextParts[1]);

              $("#LocationStatus").text(statusText);
        }
       
        
        $('#invest-tabbed-data').bind('tabsshow ', function(event, ui) {

            var selected = $('#invest-tabbed-data').data('selected.tabs');
            
            
            var tab = $('#invest-tabbed-data > DIV.ui-tabs-panel:eq(' +  selected  + ')');
            
            var id = tab.attr('id');
            
            cookie.region = jQuery.trim(id);
            cookie.store();
            UpdateLocationStatus();
        
        });

        
         $(selectorPath).select(function() {
        
            if(cookie.load()) {
                $(selectorPath).val(cookie.region);
            }
                   
           $(TAB_NAME).tabs('select', cookie.region);

         }).trigger('select');
         

        $(selectorPath).change(function () {
            
            var str = "";
            $("select option:selected").each(function () {
                str = jQuery.trim($(this).val());
              });
            
            // Only change jurisdiction if it's a valid region
            if (str.length > 0) {
            cookie.region = str;
            cookie.store();
            
            // redirect to investment page
            if (document.location.pathname.toLowerCase().indexOf(INVESTMENT_PATH.toLowerCase()) <= 0)
              document.location.replace(INVESTMENT_PATH);
            
            } 
            
            $(selectorPath).trigger('select');
        

        });
        UpdateLocationStatus();
        
});


