Cufon('a.package-login')('body.landing-page h3')('#main-content h1')('.accordion_toggle h4')('.accordion_toggle strong')('#common-content ul li')('#content h2')('#content h2')('fieldset h1')('.location span')('.location h1')('.global-search label')('#errorExplanation h2')('#page-content h2')('#page-content h3')('#supporting-content.news h2')('#supporting-content.jobs h2')('#supporting-content.events h2')('#supporting-content.showcases h2')('#supporting-content h2')('#main-content h2')('#supporting-content h3')('#jobs-latest h3')('#jobs-search h3')('#members-search h3')('#content h2.member-area')('#content h1.member-area')('#content #new_questionnaire h2')('body.index #main-content h1')('body.archive #main-content h1')('body.find-by-date #main-content h1')('body.tagged #main-content h1')('body.search #main-content h1')('body#sector #main-content h1')('body#members.new #main-content h1')('form h3')('th.monthName')('h3#respond');
// For IE
Cufon.now();

if($('carousel-container')){
  new Carousel($('carousel-container').down('.middle'), $('carousel-container').select('.slide'), $('carousel').select('.controls a'), {duration: 0.4, circular: true, frequency: 8, auto:true, wheel:false });
}

// Logo SWFU
var logo_swfu;
Event.observe(window, 'load', function() {
  form = $('logo-upload-form');
  if (form) {

    UploadHandler.uploadSuccess = function(fileObj, server_data) {
      json = server_data.evalJSON();
      UploadProgress.finish();

      $$('#logo-image').invoke('remove');
      $(this.customSettings.upload_form).up('div').insert({bottom: '<img alt="'+ json.logo.title +'" id="logo-image" src="' + json.logo.public_filename + '" /> &nbsp;<span class="profile-button"><a href="/member/profile/logo" onclick="if (confirm(\'Are you sure?\')) { var f = document.createElement(\'form\'); f.style.display = \'none\'; this.parentNode.appendChild(f); f.method = \'POST\'; f.action = this.href;var m = document.createElement(\'input\'); m.setAttribute(\'type\', \'hidden\'); m.setAttribute(\'name\', \'_method\'); m.setAttribute(\'value\', \'delete\'); f.appendChild(m);var s = document.createElement(\'input\'); s.setAttribute(\'type\', \'hidden\'); s.setAttribute(\'name\', \'authenticity_token\'); s.setAttribute(\'value\', \''+ AUTH_TOKEN +'\'); f.appendChild(s);f.submit(); };return false;">Delete this logo</a></span>'});
      // window.location = server_data.evalJSON().location;
    };

    UploadHandler.uploadComplete = function(fileObj) {
      /*  I want the next upload to continue automatically so I'll call startUpload here */
      if (this.getStats().files_queued > 0) {
        this.startUpload();
      } else {
        UploadProgress.finish();
      }
    };

    logo_swfu = new SWFUpload({
      // Create the custom swfupload_photos_path in the routes.rb file
      // Session name must match with environment.rb
      upload_url : form.action.split('?').join('.js?'),
      flash_url : '/flash/swfupload.swf',
      file_post_name: 'logo[uploaded_data]',

      file_types : "*.jpg;*.gif;*.png",
      file_types_description: "Web Image Files",
      file_size_limit : '2 MB',
      file_upload_limit : 1,
      file_queue_limit : 1,

      file_queue_error_handler : UploadHandler.fileQueueError,
      file_dialog_complete_handler : UploadHandler.fileDialogComplete,
      upload_progress_handler : UploadHandler.uploadProgress,
      upload_start_handler: UploadHandler.uploadStart,
      upload_error_handler : UploadHandler.uploadError,
      upload_success_handler : UploadHandler.uploadSuccess,
      upload_complete_handler : UploadHandler.uploadComplete,
      custom_settings : {
        upload_form : 'logo-upload-form'
      },

      button_placeholder_id: 'flash-button',
      button_height: 19,
      button_width: 157,
      button_image_url: '/images/admin/file-uploads.png',
      button_action : SWFUpload.BUTTON_ACTION.SELECT_FILES,
      button_disable : false,
      button_window_mode : SWFUpload.WINDOW_MODE.TRANSPARENT,

      debug: false

    });
    if (logo_swfu) {
      // form.onsubmit = function(){return false;};
      form.select('p').invoke('hide');
    }
  }

});

var switch_news_tab = function(new_tab){
	if($(new_tab+'-news-tab').hasClassName('hidden')){
		$$('div.news-tab').each( function(el){ el.addClassName('hidden'); });
		$(new_tab+'-news-tab').removeClassName('hidden');
	}
};

open_questionaire_prompt = function(q_container){

	top_offset = (document.body.getHeight() - q_container.getHeight()) /2;
	left_offset = ($('content-area').getWidth() - q_container.getWidth()) /2;
	q_container.setStyle({top:top_offset+'px', left:left_offset+'px'});

	//fade in quickly
  $('q_vale').fade({duration:1.5,from:0,to:0.3});
	$('q_container').fade({duration:1.5,from:0,to:1});
};

close_questionaire_prompt = function(pop_up){
	pop_up.fade({duration:0.4,from:1,to:0});
	return false;
};

document.observe( 'dom:loaded', function() {

	//flash message make byebye on clickyclick
	var flash = $('flash');
	if(flash) {
  	flash.observe('click', function(){ this.hide(); });
	}

  // Charecter counter
	$$('textarea').each(function(ta) {
	  if (ta.previous('.counter')) {
      ta.counter = ta.previous('.counter');
      ta.maximum = ta.previous('.maximum').innerHTML;
      ta.observe('keyup', function() {
        if (this.maximum) {
          max_value = parseInt(this.maximum);
          if (this.value.length >= max_value) {
            this.value = this.value.truncate(max_value, '');
          }
        }
        if (this.counter) {
          this.counter.update(this.value.length);
        }
      });
	  }
  });


  //adds the pseudo classes needed for ie6,7
  if(Prototype.Browser.IE) {
    $$("#navigation li").each(function(item) {
      item.onmouseover = function() {
        this.addClassName("sfhover");
      };
      item.onmouseout = function() {
        this.removeClassName("sfhover");
      };
    });
  }

  $$('#national-news-tab').invoke('hide');
  $$('#news-tab-switch li a').each(function(a, i) {
    if (i == 0) {
      a.toggleClassName('on');
    }
    a.onclick = function() {
      $$('#news-tab-switch li a').invoke('toggleClassName','on');
      $$('div.news-tab').invoke('toggle');
      return false;
    };
  });


  // Set search feilds default
  $$('input[value=Keywords]').each(function(q) {
    default_keywords_value = 'Keywords';
    q.onfocus = function() {
      if (this.value == default_keywords_value) {
        this.value = '';
      }
    };
    q.onblur = function() {
      if (this.value == '') {
        this.value = default_keywords_value;
      }
    };
    q.up('form').onsubmit = function() {
      if (q.value == default_keywords_value) {
        q.value = '';
        return false;
      }
    };
  });

  // Set search feilds default
  $$('form[id!=contact-form] input[name=commit]').invoke('observe', 'click', function() {
    search_form = this.up('form');
    if (window.hiddenCommit) {
      window.hiddenCommit.setAttribute('value', this.value);
    } else {
      hiddenCommit = this.cloneNode(false);
      hiddenCommit.setAttribute('type', 'hidden');
      search_form.appendChild(hiddenCommit);
    }
    this.setAttribute('originalValue', this.value);
    this.disabled = true;
    this.value= 'please wait...';
    result = (search_form.onsubmit ? (search_form.onsubmit() ? search_form.submit() : false) : search_form.submit());
    if (result == false) {
      this.value = this.getAttribute('originalValue');
      this.disabled = false;
    }
    return result;
  });

	download_link = $('direct-download');
	if(download_link){
  	download_link.up('ul').insert({before:'<div id="player"></div>'});
    swfobject.embedSWF("/flash/mp3_player.swf", "player", "451", "52", "9","expressInstall.swf", {track:escape(download_link.href)}, {wmode:"transparent"});
  }

});

// accordion

//
//	Set up all accordions
//
var bottomAccordion = null;
function loadAccordions() {
  if(! $('vertical_container'))
    return false;

  anchor = window.location.hash.replace(/^#/,'');
  var initial_expand = -1;
  $$('.accordion_toggle').each(function(el,i){
    if(el.id == anchor) {
      initial_expand = i;
      $(anchor).scrollTo();
    }
  });

  bottomAccordion = new Accordion('vertical_container', initial_expand + 1);

}

//
//  In my case I want to load them onload, this is how you do it!
//
Event.observe(window, 'load', loadAccordions, false);

// Is this IE7 (ew)
Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7;


