// page init
$(function(){
	initFancybox();
	initAccordion();
	initNavigation();
	initCustomForms();
	initYouTube();
});

// accordion init
function initAccordion() 
{
	$('ul.accordion').accordion(
	{
		active: ".selected",
		autoHeight: false,
		header: ".opener",
		collapsible: true,
		event: "click", 
		changestart: function(event, ui) 
		{ 
			//if there is no sublist then go to href
			if (!ui.newContent[0] && ui.newHeader[0])
			{
				if (ui.newHeader[0].href != '')
				{
					window.location = ui.newHeader[0].href;
				}
			}
		}
	});
}

	function initFancybox()
	{
		if (jQuery.fancybox)
		{
			$("a.openLightbox").fancybox({ titlePosition : 'inside'});
		}
	}

function initYouTube()
{
	$('a.youtube-video').click(function() 
	{
		$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'		: 949,
				'height'		: 495,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
  	 		'wmode'		: 'transparent',
				'allowfullscreen'	: 'true'
				}
			});
	
		return false;
	});
}

// fade navigation init
function initNavigation() 
{
	$('#nav li, #menu li').fadeDropdown({
		hoverClass:'hover',
		drop:'>ul, >div',
		reactSpeed: 200,
		fadeSpeed: ($.browser.msie ? 0 : 350)
	});
}

// fade dropdown plugin
jQuery.fn.fadeDropdown = function(_options)
{
	var _options = jQuery.extend({
		hoverClass:'hover',
		drop:'.slide',
		reactSpeed: 200,
		fadeSpeed: 150
	},_options);

	return this.each(function(){
		// options
		var _holder = jQuery(this);
		var _fadeSpeed = _options.fadeSpeed;
		var _hoverClass = _options.hoverClass;
		var _reactClass = _options.reactSpeed;
		var _drop = jQuery(_options.drop, _holder).hide();
		var _timer;

		_holder.hover(function(){
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(function(){
				_holder.addClass(_hoverClass);
				_drop.fadeIn(_fadeSpeed);
			},_reactClass)
		},function(){
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(function(){
				_holder.removeClass(_hoverClass);
				_drop.fadeOut(_fadeSpeed);
			},_reactClass)
		});
	});
}
