var program_areas = null;
var program_nav = null;
var facilitators = null;
var facilitator_images = null;
var facilitator_names = null;
var facilitator_video_el = null;
var contact_form = null;
var client_images = null;
var facilitator_player = null;
var facilitator_video = null;
var man_clicked = false;
var man_el = null;
var facilitator_video_toggle = null;
var facilitator_video_toggle_a = null;
var promoVideoToggle = null;
var promo_video = null;
var homeLinkTimeout = 5000;
var homeLinkDuration = 1000;
var homeLinkFx = [];
var homeLinkCurrIndex = 0;
var homeLinkPrevIndex = 0;
var homeLinks = [];
window.addEvent('domready', function() {

  // check for program page
  program_areas = $('program_areas');
  if (program_areas) {
    program_nav = new ProgramNav(program_areas);
  }

  facilitator_images = $$('.facilitator_image');
  facilitator_names = $$('.facilitator_name');
  if (facilitator_images && facilitator_images.length &&
      facilitator_names && facilitator_names.length) {
    var facilitator = new FacilitatorImages(facilitator_images, facilitator_names);
  }

	if (Browser.Engine.trident && Browser.Engine.version <= 4) {
		$$("#main_nav li").each(function(el) {
			el.addEvent('mouseenter', menuOver.bind(el));
			el.addEvent('mouseleave', menuOut.bind(el));
		});
	}

  contact_form = $('contact_form');
  if (contact_form) {
    new ClearTextInput(contact_form.your_name);
    new ClearTextInput(contact_form.your_email);
    new ClearTextInput(contact_form.your_phone);
    new ClearTextInput(contact_form.message);
  }

	client_images = $$('.client_logo_inner');
	if (client_images) {
		client_images.each(function(client) {
			client.addEvent('mouseenter', function(e) {
				e = new Event(e);
				var tar = $(e.target);
				while (!tar.hasClass('client_logo_inner')) {
					tar = $(tar.parentNode);
				}
        if (tar.getAttribute('ref').length) {
          tar.setStyle('background-image', 'url('+tar.getAttribute('ref')+')');
        }
			});
			client.addEvent('mouseleave', function(e) {
				e = new Event(e);
				var tar = $(e.target);
				while (!tar.hasClass('client_logo_inner')) {
					tar = $(tar.parentNode);
				}
				tar.setStyle('background-image', 'url('+tar.getAttribute('rel')+')');
			});
		});
	}

	// check for facilitator video
	facilitator_video = $('facilitator_video');
	if (facilitator_video) {

		// add click event to person
		man_el = $('man');
		facilitator_video_toggle = $('facilitator_video_toggle');
		facilitator_video_toggle_a = $$('#facilitator_video_toggle a')[0];
		facilitator_video_toggle.addEvent('click', function(e) {
			e = new Event(e).stop();
			if (facilitator_video.getStyle('display') == 'none') {
				facilitator_video.setStyle('display', 'block');
			}
			else {
				facilitator_video.setStyle('display', 'none');
			}
			var so = new SWFObject('swf/player.swf','facilitator_video_el','512','310','9','#');
			so.addParam('allowfullscreen','false');
			so.addParam('allowscriptaccess','always');
			so.addParam('wmode','transparent');
			so.addVariable('file',facilitator_video_toggle_a.getAttribute('rel'));
			so.addVariable('skin','swf/skin.swf');
			so.addVariable('autostart',true);
			so.addVariable('bufferlength',10);
			so.write('facilitator_video');

			// add a close button
			var anchor = new Element('a', {
				'href': '#',
				'class': 'facilitator_close',
				'html': 'Close',
				'events': {
					'click': function(e) {
						e = new Event(e).stop();
						facilitator_video.setStyle('display', 'none');
					}
				}
			});
			anchor.inject(facilitator_video);
		});

		facilitator_video.addEvent('click', function(e) {
			e = new Event(e).stop();
		});

		document.addEvent('click', function() {
			if (facilitator_video.getStyle('display') == 'block') {
				facilitator_video.setStyle('display', 'none');
				facilitator_player.sendEvent('STOP');
			}
		});
	}
	
	promoVideoToggle = $('promo_video_toggle');
	if (promoVideoToggle) {
		promo_video = $('promo_video');
		homeLinks = $$('#home_ourstory a');
		homeLinks.each(function(home_link, index) {
			homeLinkFx[index] = new Fx.Tween(home_link);
			if (home_link.getStyle('display') == 'none') {
				home_link.setOpacity(0);
				home_link.setStyle('display', 'block');
			}
		});
		setTimeout("animateLinks()", homeLinkTimeout);
		
		promoVideoToggle.addEvent('click', function(e) {
			e = new Event(e).stop();
			if (promo_video.getStyle('display') == 'none') {
				promo_video.setStyle('display', 'block');
			}
			else {
				promo_video.setStyle('display', 'none');
			}
			var so = new SWFObject('swf/player.swf','promo_video_el','512','310','9','#');
			so.addParam('allowfullscreen','false');
			so.addParam('allowscriptaccess','always');
			so.addParam('wmode','transparent');
			so.addVariable('file',promoVideoToggle.getAttribute('rel'));
			so.addVariable('skin','swf/skin.swf');
			so.addVariable('autostart',true);
			so.addVariable('bufferlength',10);
			so.write('promo_video');

			// add a close button
			var anchor = new Element('a', {
				'href': '#',
				'class': 'promo_close',
				'html': 'Close',
				'events': {
					'click': function(e) {
						e = new Event(e).stop();
						promo_video.setStyle('display', 'none');
					}
				}
			});
			anchor.inject(promo_video);
		});
		
	}
});

function animateLinks() {
	// show next link
	newIndex = homeLinkCurrIndex;
	newIndex++;
	if (newIndex == homeLinks.length) {
		newIndex = 0;
	}
	
	homeLinkFx[homeLinkCurrIndex].start('opacity', 0);
	homeLinkFx[newIndex].start('opacity', 1);
	
	homeLinkPrevIndex = homeLinkCurrIndex;
	homeLinkCurrIndex = newIndex;
	setTimeout("animateLinks()", homeLinkTimeout);
}

function playerReady(thePlayer) {
	facilitator_player = window.document[thePlayer.id];
}



function menuOver() {
	tar = this;
	while (tar.tagName != 'LI') {
		if (tar.getAttribute('id') == 'main_nav') {
			break;
		}
		tar = tar.parentNode;
	}
	if (tar.tagName == 'LI') {
		tar.addClass("sfhover");
	}
}

function menuOut() {
	tar = this;
	while (tar.tagName != 'LI') {
		if (tar.getAttribute('id') == 'main_nav') {
			break;
		}
		tar = tar.parentNode;
	}
	if (tar.tagName == 'LI') {
		tar.removeClass("sfhover");
	}
}

var ClearTextInput = new Class({

	Implements: [Events, Options],

  options: {
/*  onFocus: $function,
    onBlur: $function  */
  },

  element: null,

  initialize: function(element, options)
  {
    this.setOptions(options);
    this.element = $(element);
    this.element.addEvent('focus', function(e) {
      if (this.element.value == this.element.defaultValue) {
        this.element.value = '';
      }
      this.fireEvent('focus', [this.element]);
    }.bind(this));
    this.element.addEvent('blur', function(e) {
      if (this.element.value == '') {
        this.element.value = this.element.defaultValue;
      }
      this.fireEvent('blur', [this.element]);
    }.bind(this));
  }

});

var FacilitatorImages = new Class({

  images: null,
  names: null,

  initialize: function(images, names)
  {
    this.images = images;
    this.names = names;
    this.names.each(function(name) {
      name.setStyle('display', 'none');
    });
    this.images.each(function(image) {
      var image_id = image.getAttribute('id').split('_');
      if (image_id[1] == 'serious') {
        image.addEvent('mouseenter', function(e) {
          e = new Event(e).stop();
          var tar = $(e.target);
          while (!tar.hasClass('facilitator_image')) {
            tar = $(tar.parentNode);
          }
          var ids = tar.getAttribute('id').split('_');
          tar.setStyle('display', 'none');
          $('facilitator_funny_'+ids[2]).setStyle('display', 'block');
          var idx = this.images.indexOf(tar)/2;
          this.names[idx].setStyle('display', 'block');
          this.applysIFR();
        }.bind(this));
      }
      else {
        image.addEvent('mouseleave', function(e) {
          e = new Event(e).stop();
          var tar = $(e.target);
          while (!tar.hasClass('facilitator_image')) {
            tar = $(tar.parentNode);
          }
          var ids = tar.getAttribute('id').split('_');
          tar.setStyle('display', 'none');
          $('facilitator_serious_'+ids[2]).setStyle('display', 'block');
          var idx = (this.images.indexOf(tar)-1)/2;
          this.names[idx].setStyle('display', 'none');
        }.bind(this));
      }
    }.bind(this));
  },

  applysIFR: function()
  {
    sIFR.replace(vb, {
      selector: '.facilitator_name',
      css: ['.sIFR-root { background-color: transparent; font-size:14px; cursor:pointer }',
            'a {color: '+header_colour+'; text-decoration:none }',
            'a:hover {color: #414141 }'
           ],
      wmode: 'transparent',
      tuneHeight: -6,
      ratios: [7, 1.47, 10, 1.43, 15, 1.36, 16, 1.34, 23, 1.32, 27, 1.31, 28, 1.3, 31, 1.29, 34, 1.3, 39, 1.28, 40, 1.29, 52, 1.28, 55, 1.27, 56, 1.28, 84, 1.27, 85, 1.26, 88, 1.27, 91, 1.26, 92, 1.27, 95, 1.26, 96, 1.27, 1.26]
    });
  }

});

var ProgramNav = new Class({

  areas: null,
  program_wrapper: [],
  content_wrapper: [],
  link_wrapper: [],
  programs: [],
  contents: [],
  content_links: [],
  last_area: null,
  curr_area: 0,
  last_program: [],
  curr_program: [],
  last_content: [],
  curr_content: [],

  initialize: function(areas)
  {
    this.areas = areas.getElements('.program_area');

    // attach events to areas
    this.areas.each(function(area) {
      var a_idx = area.getAttribute('id').substr(13);
      area.addEvent('click', function(e) {
        e = new Event(e).stop();
        // find current idx
        var tar = $(e.target);
        while (!tar.hasClass('program_area')) {
          tar = $(tar.parentNode);
        }
        this.showArea(tar.getAttribute('id').substr(13));
      }.bind(this));
      // populate programs
      this.program_wrapper[a_idx] = $('programs_'+a_idx);
      this.programs[a_idx] = $('programs_'+a_idx).getElements('.program_title_box');
      this.contents[a_idx] = [];
      this.content_wrapper[a_idx] = [];
      this.link_wrapper[a_idx] = [];
      this.last_program[a_idx] = null;
      this.curr_program[a_idx] = null;
      this.last_content[a_idx] = [];
      this.curr_content[a_idx] = [];
      this.content_links[a_idx] = [];
      // attach events to program content boxes
      this.programs[a_idx].each(function(program) {
        var p_idx = program.getAttribute('id').substr(18);
        program.addEvent('click', function(e) {
          e = new Event(e).stop();
          var tar = $(e.target);
          while (!tar.hasClass('program_title_box')) {
            tar = $(tar.parentNode);
          }
          // get id
          this.showProgram(tar.getAttribute('id').substr(18));
        }.bind(this));
        // get content links
        this.last_content[a_idx][p_idx] = null;
        this.curr_content[a_idx][p_idx] = null;
        this.content_links[a_idx][p_idx] = $('program_content_'+a_idx+'_'+p_idx).getElements('.program_content_header a');
        this.link_wrapper[a_idx][p_idx] = $('program_content_'+a_idx+'_'+p_idx).getElements('.program_content_wrapper div');
        this.content_links[a_idx][p_idx].each(function(content_link) {
          content_link.addEvent('click', function(e) {
            e = new Event(e).stop();
            var tar = $(e.target);
            // get id
            var parts = tar.getAttribute('id').split('_');
            this.showContent(parts[3]);
          }.bind(this));
        }.bind(this));
        // get content boxes
        this.content_wrapper[a_idx][p_idx] = $('program_content_'+a_idx+'_'+p_idx);
        this.contents[a_idx][p_idx] = $('program_content_'+a_idx+'_'+p_idx).getElements('.program_content_box');

      }.bind(this));
    }.bind(this));
    this.showProgram(0);

  },

  hideArea: function(area_id)
  {
    this.areas[area_id].removeClass('selected');
    this.hideProgramContent(area_id)
  },

  showArea: function(area_id)
  {
    if (area_id != this.curr_area) {
      this.hideArea(this.curr_area);
      this.last_area = this.curr_area;
      this.curr_area = area_id;
      this.areas[area_id].addClass('selected');
      if (!$chk(this.curr_program[area_id])) {
        this.curr_program[area_id] = 0;
      }
      this.showProgramContent(area_id);
      this.showProgram(this.curr_program[area_id]);
    }
  },

  hideProgramContent: function(area_id)
  {
    this.program_wrapper[area_id].setStyle('display', 'none');
    this.content_wrapper[area_id][this.curr_program[area_id]].setStyle('display', 'none');
  },

  showProgramContent: function(area_id)
  {
    this.program_wrapper[area_id].setStyle('display', 'block');
    this.content_wrapper[area_id][this.curr_program[area_id]].setStyle('display', 'block');
  },

  hideProgram: function(program_id)
  {
    this.programs[this.curr_area][this.curr_program[this.curr_area]].removeClass('selected');
    this.content_wrapper[this.curr_area][program_id].setStyle('display', 'none');
  },

  showProgram: function(program_id)
  {
    if (program_id != this.curr_program[this.curr_area]) {
      if ($chk(this.curr_program[this.curr_area])) {
        this.hideProgram(this.curr_program[this.curr_area]);
      }
      this.programs[this.curr_area][program_id].addClass('selected');
      this.content_wrapper[this.curr_area][program_id].setStyle('display', 'block');


      this.last_program[this.curr_area] = this.curr_program[this.curr_area];
      this.curr_program[this.curr_area] = program_id;

      var show_content = this.curr_content[this.curr_area][this.curr_program[this.curr_area]];
      if (!show_content) {
        show_content = 0;
      }
      this.showContent(show_content);
    }
    // apply sIFR
    this.applysIFR(program_id);
  },

  hideContent: function(type_id)
  {
    this.content_links[this.curr_area][this.curr_program[this.curr_area]][type_id].removeClass('selected');
    this.link_wrapper[this.curr_area][this.curr_program[this.curr_area]][type_id].setStyle('display', 'none');

  },

  showContent: function(type_id)
  {
    if (type_id != this.curr_content[this.curr_area][this.curr_program[this.curr_area]]) {

      if ($chk(this.curr_content[this.curr_area][this.curr_program[this.curr_area]])) {
        this.hideContent(this.curr_content[this.curr_area][this.curr_program[this.curr_area]]);
      }

      this.content_links[this.curr_area][this.curr_program[this.curr_area]][type_id].addClass('selected');
      this.link_wrapper[this.curr_area][this.curr_program[this.curr_area]][type_id].setStyle('display', 'block');

      this.last_content[this.curr_area][this.curr_program[this.curr_area]] = this.curr_content[this.curr_area][this.curr_program[this.curr_area]];
      this.curr_content[this.curr_area][this.curr_program[this.curr_area]] = type_id;
    }
  },

  applysIFR: function(program_id)
  {
    sIFR.replace(vb, {
      selector: '#program_content_'+this.curr_area+'_'+program_id+' .program_content_header h2',
      css: '.sIFR-root { background-color: transparent; color: #393939; font-size:30px; }',
      wmode: 'transparent',
      tuneHeight: -6,
      ratios: [7, 1.47, 10, 1.43, 15, 1.36, 16, 1.34, 23, 1.32, 27, 1.31, 28, 1.3, 31, 1.29, 34, 1.3, 39, 1.28, 40, 1.29, 52, 1.28, 55, 1.27, 56, 1.28, 84, 1.27, 85, 1.26, 88, 1.27, 91, 1.26, 92, 1.27, 95, 1.26, 96, 1.27, 1.26]
    });

    sIFR.replace(vb, {
      selector: '#program_content_'+this.curr_area+'_'+program_id+' .program_content_wrapper h2',
      css: '.sIFR-root { background-color: transparent; color: #393939; font-size:18px; }',
      wmode: 'transparent',
      tuneHeight: -6,
      ratios: [7, 1.47, 10, 1.43, 15, 1.36, 16, 1.34, 23, 1.32, 27, 1.31, 28, 1.3, 31, 1.29, 34, 1.3, 39, 1.28, 40, 1.29, 52, 1.28, 55, 1.27, 56, 1.28, 84, 1.27, 85, 1.26, 88, 1.27, 91, 1.26, 92, 1.27, 95, 1.26, 96, 1.27, 1.26]
    });

    sIFR.replace(vb, {
      selector: '.program_gallery h3',
      css: '.sIFR-root { background-color: transparent; color: #393939; font-size:14px; }',
      wmode: 'transparent',
      tuneHeight: -6,
      ratios: [7, 1.47, 10, 1.43, 15, 1.36, 16, 1.34, 23, 1.32, 27, 1.31, 28, 1.3, 31, 1.29, 34, 1.3, 39, 1.28, 40, 1.29, 52, 1.28, 55, 1.27, 56, 1.28, 84, 1.27, 85, 1.26, 88, 1.27, 91, 1.26, 92, 1.27, 95, 1.26, 96, 1.27, 1.26]
    });
  }


});
