var rsvpUrls = [];
var rsvpText = [];
var eventsUrls = [];
var idx = 0;

var event_name = "";
var event_date = "";
var event_rsvpCode = "";


function findGalleryItems()
{
   var rsvps = $('div.text-holder > ul.event-list > li > a.rsvp');
   rsvps.each(function() {
     // .val() assuming its an option element
     // .text() might be better.
     //alert($(this).attr('href'));
     rsvpUrls.push($(this).attr('href')); 
     rsvpText.push($(this).text());
   });
   
   var events = $('div.text-holder > ul.event-list > li.active > a');
   events.each(function(){
     eventsUrls.push($(this).attr('href'));
   });
   
}

function fadeMain(){
	var wait_time = 5000; // in ms
	var change_speed = 800; // in ms
	var _hold = $('div.gallery-holder');
		
	if(_hold.length){
		var _t;
		var _f = true;
		var _list = _hold.find('ul.gallery > li');
		var _btn = $('<ul class="gallery-nav"></ul>');

		if($.browser.msie){
		  $('div.text-holder > ul.event-list > li > a.rsvp').text(rsvpText[0]);
		}
		
		_hold.parent().find('div.gal-nav-holder').append(_btn);
		_list.each(function(_i){
			_btn.append('<li><a href="#">'+(_i+1)+'</a></li>');
		});
		_btn = _btn.find('a');
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1)
		  _a = 0;
        
		if($.browser.msie && $.browser.version < 7)
		  _list.removeClass('active').hide().eq(_a).addClass('active').show();
		else
		  _list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);

		_btn.eq(_a).parent('li').addClass('active');
		_btn.click(function(){
			changeEl(_btn.index(this));
			return false;
		});
		if($.browser.msie)
		{
		  changeEl(0);
		}
		_hold.mouseenter(function(){
		  
	      if($.browser.msie){
	        /* Change the URL for the events due to a bug in IE */
		    $('div.text-holder > ul.event-list > li.active > a').attr('href', eventsUrls[_a]);
		
		    /* Change the URL for the rsvp button due to a bug in IE */
		    $('div.text-holder > ul.event-list > li > a.rsvp').attr('href', rsvpUrls[_a]);
            event_name = _list.eq(_a).children('div.text-holder').children('h2').text();
            event_date = _list.eq(_a).children('div.text-holder').children('h3').text();
            event_rsvpCode = _list.eq(_a).children('div.text-holder').children('font').text();
		  }
			_f = false;
			if(_t) clearTimeout(_t);
		}).mouseleave(function(){
			_f = true;
			if(_t) clearTimeout(_t);
			if(_f && wait_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, wait_time);
			}
		});
		if(_f && wait_time){
			_t = setTimeout(function(){
				if(_a < _list.length - 1) changeEl(_a + 1);
				else changeEl(0);
			}, wait_time);
		}
		function changeEl(_ind){
	    if($.browser.msie){
		  idx = _a+1;
		  if(idx > 3)
		    idx = 0;
          $('div.text-holder > ul.event-list > li > a.rsvp').text(rsvpText[idx]);
        
          event_name = _list.eq(idx).children('div.text-holder').children('h2').text();
          event_date = _list.eq(idx).children('div.text-holder').children('h3').text();
          event_rsvpCode = _list.eq(idx).children('div.text-holder').children('font').text();
        }
        $('div.text-holder').css('visibility','visible')
        
		if(_t) clearTimeout(_t);
			if(_ind != _a){
				if($.browser.msie && $.browser.version < 7){
					_list.eq(_a).removeClass('active').hide();
					_list.eq(_ind).addClass('active').show();
				}
				else{
					_list.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration:change_speed});
					_list.eq(_ind).addClass('active').animate({opacity: 1}, {queue:false, duration:change_speed});
				}
				_btn.eq(_a).parent('li').removeClass('active');
				_btn.eq(_ind).parent('li').addClass('active');
				_a = _ind;
			}
			if(_f && wait_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, wait_time);
			}
		}
	}
}
function fadeGall(){
	var wait_time = 5000; // in ms
	var change_speed = 800; // in ms
	var _hold = $('div.media-holder');
	if(_hold.length){
		var _t;
		var _f = true;
		var _list = _hold.find('ul.fader > li');
		var _btn = $('<ul class="gallery-nav"></ul>');
		_hold.parent().find('div.gal-nav-holder').append(_btn);
		_list.each(function(_i){
			_btn.append('<li><a href="#">'+(_i+1)+'</a></li>');
		});
		_btn = $('ul.thumbnails').find('li');
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		_btn.eq(_a).parent('li').addClass('active');
		_btn.click(function(){
			changeEl(_btn.index(this));
			return false;
		});
		
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				_list.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration:change_speed});
				_list.eq(_ind).addClass('active').animate({opacity: 1}, {queue:false, duration:change_speed});
				_btn.eq(_a).parent('li').removeClass('active');
				_btn.eq(_ind).parent('li').addClass('active');
				_a = _ind;
				stopAllYouTubeVideos();
			}
		}
	}
}
function thumbsGall(){
	var wait_time = 5000; // in ms
	var change_speed = 800; // in ms
	var _hold = $('div.thumbnails-holder');
	
	if(_hold.length){
		var _t;
		var _f = true;
		var _list = _hold.find('ul.thumbs-fader > li.fade-item');
		var _btn = $('<ul class="gallery-nav"></ul>');
		_hold.parent().find('div.gal-nav-holder').append(_btn);
		_list.each(function(_i){
			_btn.append('<li><a href="#">'+(_i+1)+'</a></li>');
		});
		_btn = $('ul.thumbnails').find('li');
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		
		_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		_btn.eq(_a).parent('li').addClass('active');
		var _prev = _hold.find('a.prev');
		var _next = _hold.find('a.next');
		
		_prev.click(function(){
			if(_a <= 0) changeEl(_list.length-1);
			else changeEl(_a-1);
			return false;
		});
		_next.click(function(){
			if(_a < _list.length - 1) changeEl(_a + 1);
			else changeEl(0);
			return false;
		});
		
		
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				_list.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration:change_speed});
				_list.eq(_ind).addClass('active').animate({opacity: 1}, {queue:false, duration:change_speed});
				_btn.eq(_a).parent('li').removeClass('active');
				_btn.eq(_ind).parent('li').addClass('active');
				_a = _ind;
			}
		}
	}
}
function initTabs() {
	$('ul.tabset').each(function(){
		var _list = $(this);
		var _links = _list.find('a.tab');
		var _count = _links.length-1;
		
		var _next = _list.find('a.next');
		var _prev = _list.find('a.prev');
		
		var _active = _links.index(_links.filter('.active'));
		
		_next.click(function(){
			_active++;
			if (_active > (_count)) _active = 0;
			
			_links.filter('.active').each(function(){
				$($(this).removeClass('active').attr('href')).hide();
			});
			
			_links.eq(_active).addClass('active');
			$(_links.eq(_active).attr('href')).show();
			return false;
		});
		
		_prev.click(function(){
			_active--;
			if (_active < 0) _active = _count;
			_links.filter('.active').each(function(){
				$($(this).removeClass('active').attr('href')).hide();
			});
			
			_links.eq(_active).addClass('active');
			$(_links.eq(_active).attr('href')).show();
			return false;
		});

		_links.each(function() {
			var _link = $(this);
			var _href = _link.attr('href');
			var _tab = $(_href);

			if(_link.hasClass('active')) _tab.show();
			else _tab.hide();

			_link.click(function(){
				_links.filter('.active').each(function(){
					$($(this).removeClass('active').attr('href')).hide();
				});
				_link.addClass('active');
				_tab.show();
				_active = _links.index(_links.filter('.active'));
				return false;
			});
		});
	});
}
function initGalleryEventTabs() {
	$('ul.paging-tabset').each(function(){
		var _list = $(this);
		var _links = _list.find('a.tab');
		var _count = _links.length-1;
		
		var _next = _list.find('a.next');
		var _prev = _list.find('a.prev');
		
		var _active = _links.index(_links.filter('.active'));
		
		_next.click(function(){
			_active++;
			if (_active > (_count)) _active = 0;
			
			_links.filter('.active').each(function(){
				$($(this).removeClass('active').attr('href')).hide();
			});
			
			_links.eq(_active).addClass('active');
			$(_links.eq(_active).attr('href')).show();
			return false;
		});
		
		_prev.click(function(){
			_active--;
			if (_active < 0) _active = _count;
			_links.filter('.active').each(function(){
				$($(this).removeClass('active').attr('href')).hide();
			});
			
			_links.eq(_active).addClass('active');
			$(_links.eq(_active).attr('href')).show();
			return false;
		});

		_links.each(function() {
			var _link = $(this);
			var _href = _link.attr('href');
			var _tab = $(_href);

			if(_link.hasClass('active')) _tab.show();
			else _tab.hide();

			_link.click(function(){
				_links.filter('.active').each(function(){
					$($(this).removeClass('active').attr('href')).hide();
				});
				_link.addClass('active');
				_tab.show();
				_active = _links.index(_links.filter('.active'));
				return false;
			});
		});
	});
}

var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function init_nav_functions()
{
  $('#nav > li > a.book').bind('mouseover', nav_open);
  $('#nav > li > a.book').bind('mouseout',  nav_timer);
  $('div.dropdowns-holder').bind('mouseover', nav_open2);
  $('div.dropdowns-holder').bind('mouseout',  nav_timer);
  
  nav_close();
}

function nav_open()
{
   var position = $(this).offset();

   
   nav_canceltimer();
   nav_close();
   
   //Do something
   $('div.dropdowns-holder').css('top',position.top+$(this).height()-7);
   $('div.dropdowns-holder').css('left',position.left-$(this).width()-15);
   $('div.dropdowns-holder').css('visibility', 'visible');
}

function nav_open2()
{
   nav_canceltimer();
   nav_close();
   
   //Do something
   $('div.dropdowns-holder').css('visibility', 'visible');
}


function nav_close()
{
  //Do something
  $('div.dropdowns-holder').css('visibility', 'hidden');
}

function nav_timer()
{
  closetimer = window.setTimeout(nav_close, timeout);
}

function nav_canceltimer()
{
  if(closetimer)
  {
      window.clearTimeout(closetimer);
      closetimer = null;
  }
}

$(document).ready(function(){

	if($.browser.msie){
      findGalleryItems();
    }
	fadeMain();
	fadeGall();
	initTabs();
	initGalleryEventTabs();
	thumbsGall();
	initLightbox();
	ZoominFlashMovie();
	bindRSVPClicks();
	hideRSVP();
	init_nav_functions();
	setBreadcrumbPos();
	stopAllYouTubeVideos();
});

function setBreadcrumbPos()
{
  var position = 0;
  if($('div.breadcrumbs').length)
  {
    if($('div.title2').length)
    {
      position = $('div.title2 > h2').offset();
      $('div.breadcrumbs').css('left',position.left);
    }
    else
    {
      position = $('div.main-frame > h2').offset();
      $('div.breadcrumbs').css('left',position.left);
    }
  }
}

// fancybox init
function initLightbox() {
	var _share, btn;
	if(typeof($.fn.fancybox)=='function'){
		$('a[rel*="lightbox"]').fancybox({
			transitionIn : 'fade',
			transitionOut : 'fade',
			overlayShow : true,
			overlayColor : '#000',
			overlayOpacity : 0.65,
			padding: 10,
			titleFormat : function(title, currentArray, currentIndex, currentOpts) {
				btn = $(this.orig).parent();
				_share = btn.next();
				return _share;
			},
			onCancel: function(){
				if(btn.length) btn.after(_share);
			},
			onClosed: function(){
				if(btn.length) btn.after(_share);
			},
			onCleanup: function(){
				if(btn.length) btn.after(_share);
			}
		});
	}
}

function getFlashMovieObject(movieName)
{
  var browserName=navigator.appName; 

  if (browserName == "Microsoft Internet Explorer")
  {
    return document.getElementById(movieName);
  }
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (document.embeds && document.embeds[movieName])
  {
      return document.embeds[movieName]; 
  }
}

function stopAllYouTubeVideos()
{
  var i=1;
  var flashMovie = 1;
  for(i = 1; flashMovie; i++)
  {
   // videostr="video";
   // for (j = 0; j < i; j++)
   // {
   //   videostr += "a";
   // }
    //alert(videostr);
    flashMovie=getFlashMovieObject("video"+i);
    try
    {
      var state = flashMovie.getPlayerState();
    }
    catch(err)
    {
    }
    
    if(flashMovie && state == 1)
    {
      try
      {
        flashMovie.pauseVideo();
      }
      catch(error)
      {
      }
    }
  }
}

function ZoominFlashMovie()
{
	var flashMovie=getFlashMovieObject("tbl-service-main");
	//if(flashMovie && jQuery.browser != 'msie')
	//{
	//  flashMovie.Zoom(90);
    //}
}

function bindRSVPClicks()
{
  // Home Page
  $('div.text-holder > ul.event-list > li > a.rsvp').click(showRSVP);

  // Events Detail Page
  $('div.event-detail-information > ul > li.rsvp > a').click(showEventsDetailRSVP);

  // On the form
  $('div.rsvp-form > div.rsvp-form-holder > h3 > a').click(hideRSVP);

  // Nav Menu nav-menuDiv
  $('td.navMenuDiv > a.rsvp').click(showRSVP);

}
function showRSVPVD(){    if(!$.browser.msie){    event_name = $(this).parents('div.text-holder').children('h2').text();    event_date = $(this).parents('div.text-holder').children('h3').text();    event_rsvpCode = $(this).parents('div.text-holder').children('font').text();  }    $('div.rsvp-form > div.rsvp-form-holder > h2#head > span').text('RSVP: '+event_name);  $('div.rsvp-form > div.rsvp-form-holder > h2#date > span').text('Date: '+event_date);    $(':input:hidden.rsvpCode').val(event_rsvpCode);  var iebody = (document.compatMode && document.compatMode != "BackCompat")?document.documentElement:document.body;  var event_position_top = (document.all)?iebody.scrollTop+30:window.pageYOffset+30;  var event_position_left = $(window).width()/2 - $('div.rsvp-form').width()/2;  $('div.rsvp-form').css('top',event_position_top);  $('div.rsvp-form').css('left',event_position_left);  $('div.rsvp-form').show('slow');  if($.browser.msie){    $('div.rsvp-form').css('visibility', 'visible');  }}
function showRSVP()
{  
  if($(this).attr('href') != "#rsvp")
  {
    return;
  }

  if(!$.browser.msie){
    event_name = $(this).parents('div.text-holder').children('h2').text();
    event_date = $(this).parents('div.text-holder').children('h3').text();
    event_rsvpCode = $(this).parents('div.text-holder').children('font').text();
  }
  
  $('div.rsvp-form > div.rsvp-form-holder > h2#head > span').text('RSVP: '+event_name);
  $('div.rsvp-form > div.rsvp-form-holder > h2#date > span').text('Date: '+event_date);
  
  $(':input:hidden.rsvpCode').val(event_rsvpCode);

  var iebody = (document.compatMode && document.compatMode != "BackCompat")?document.documentElement:document.body;
  var event_position_top = (document.all)?iebody.scrollTop+30:window.pageYOffset+30;
  var event_position_left = $(window).width()/2 - $('div.rsvp-form').width()/2;

  $('div.rsvp-form').css('top',event_position_top);
  $('div.rsvp-form').css('left',event_position_left);
  $('div.rsvp-form').show('slow');
  if($.browser.msie){
    $('div.rsvp-form').css('visibility', 'visible');
  }
}

function hideRSVP()
{
  $('div.rsvp-form').hide('slow');
  if(jQuery.browser == 'msie')
  {
    $('div.rsvp-form').css('visibility', 'hidden');
  }
}

function showEventsDetailRSVP()
{
  var event_name = $(this).parents('div.event-detail-information').children('h3').text();
  var strong_txt = $(this).parents('div.event-detail-information').children('h3').children('span').text();
  var rsvpCode = $(this).parents('div.event-detail-information').children('font').text();
  event_name = event_name.replace(eval('/' + strong_txt + '/'), "");
  var date = $(this).parents('div.event-detail-information').children('h4').text();
  var position = $(this).offset();
  
  $('div.rsvp-form > div.rsvp-form-holder > h2#head > span').text('RSVP: '+event_name);
  $('div.rsvp-form > div.rsvp-form-holder > h2#date > span').text('Date: '+date);
  
  $(':input:hidden.rsvpCode').val(rsvpCode);
  
  /*
  for ( var i = 0; i < s.options.length; i++ ) {        
  		if ( s.options[i].value == v ) {            
				s.options[i].selected = true;            
				return;        
			}    
			
		} */

  var iebody = (document.compatMode && document.compatMode != "BackCompat")?document.documentElement:document.body;
  var event_position_top = (document.all)?iebody.scrollTop+30:window.pageYOffset+30;
  var event_position_left = $(window).width()/2 - $('div.rsvp-form').width()/2;
  
  $('div.rsvp-form').css('top',event_position_top);
  $('div.rsvp-form').css('left',event_position_left);
  $('div.rsvp-form').show('slow');
  if(jQuery.browser == 'msie')
  {
    $('div.rsvp-form').css('visibility', 'visible');
  }
}

//Hide dropdowns
document.onclick = nav_close;
