//	functions.js

jQuery(document).ready(
	function() {

		var banner = new Object();
		banner.name = 'banner';
		banner.interval = 10000;	
		

/****	show mainmenu flyouts	****/
/*
		jQuery('#mainmenu > li').hover(
			function() {
//	if its already shown do nothing 
				if ( jQuery(this).children('ul:visible').size() )
					return;
					
//	hide other shown menus
				jQuery('#mainmenu li ul').hide('fast');
//	show this one
				if ( jQuery(this).children('ul').size() )
					jQuery(this).children('ul').show('fast');
			},
			function() {
				if ( jQuery(this).children('ul').size() )
					jQuery(this).children('ul').hide('fast');
			}
		);
*/

//	snow's mainmenu fixes
		jQuery('#mainmenu ul').addClass('jquery-enabled').each(function() {
			_h = (jQuery(this).height());
//			alert(_h);
			jQuery(this).css('clip', 'rect(0px 0px ' + _h + 'px 0px)');
		});

/*		
		jQuery('#mainmenu > li object').hover(
			function() {
				alert('object');	
			}
		);
*/

		jQuery('#mainmenu > li').hover(
			function() {
				node = jQuery(this);
				node.stopTime('closing');
				_w = (node.find('ul').width());
				_h = (node.find('ul').height());
//				jQuery(this).find('ul').show('fast');
//				jQuery(this).find('ul').css('opacity', 0).css('left', '-500px').css('display', 'block').animate({'left': '0', 'opacity' : 1});
//				jQuery(this).find('ul').css('opacity', 0).css('width', '0').css('display', 'block').animate({'width': '1000px', 'opacity' : 1});

//	hide everything
				node.siblings().not(node).find('ul').each(function() {
					_hh = (jQuery(this).height());						
					jQuery(this).animate({'clip': 'rect(0px 0px ' + _hh + 'px 0px)'}, 100);
				});
//	if there is no ul ( children ) then finish up now
				if ( ! _w )
					return;
				
//	show the correct one
				clip_over = 'rect(0px ' + _w + 'px ' + _h + 'px 0px)';
//				alert(clip_over);
				node.find('ul').animate({'clip': clip_over}, 350);
				return false;
			},
			function() {
				out_node = jQuery(this);
//				jQuery(this).find('ul').hide('fast');
				out_node.oneTime(1000, 'closing', function() {
					clip_out = 'rect(0px 0px ' + _h + 'px 0px)';
//					alert(clip_out);
					out_node.find('ul').animate({'clip': clip_out, 'opacity' : 1}, 100);
				});			
			}
		);
//	end snow's mainmenu fixes



		/*
		jQuery('#footermenu > li').hover(
			function() {
//	if its already shown do nothing 
				if ( jQuery(this).children('ul:visible').size() )
					return;
					
//	hide other shown menus
				jQuery('#footermenu li ul').hide('fast');
//	show this one
				if ( jQuery(this).children('ul').size() )
					jQuery(this).children('ul').show('slow');
			},
			function() {
				if ( jQuery(this).children('ul').size() )
					jQuery(this).children('ul').hide('fast');
			}
		);
		*/
		
		animate = true;

//	set the footer to pop just over the bottom of the screen
		_collapse_footer = function(obj) {
//	height of the container ul
			_h = (jQuery(obj).height());
//	min-height of the first li
			_hli = parseInt(jQuery(obj).find('>li').css('min-height'));
//	animate back so that the first li is visible
			jQuery(obj).animate( {'bottom' : _h * -1 + _hli + 16 }, 400 );
		}
		


//	called onload
		jQuery('#footermenu').each(
			function() {
				if ( ! animate )
					return false;
					
//	push it off screen while we figure out its height
				jQuery(this).css('bottom', -1000);
				jQuery(this).find('ul').css('height', 'auto');
				
				
				_collapse_footer(this);
			}
		);
		
		jQuery('#footermenu > li').hover(
//	on hover move the bottom of the container to 0
			function() {
				if ( ! animate )
					return false;
				jQuery('#footermenu').stopTime('closing');
				jQuery(this).parent().animate( { 'bottom' : 0 }, 200);
			},
//	on exit animate back so that the first li is visible
			function() {
				if ( ! animate )
					return false;

				jQuery('#footermenu').oneTime(1000, 'closing', function() {
					_collapse_footer(jQuery(this));
				});
			}
		);
		
		
		//background resize
		options = {
				left_ignore_selectors : new Array('#header')
		}			
		jQuery().scaleBackground(options);
		

//	gallery - captions always on
		jQuery('#gallery .caption').show();


/****	image gallery	****/
			jQuery(".gallery-node.type-image").each(
				function() {
					jQuery(this).find('a').addClass('lightbox');
				}
			);
			
			jQuery(".lightbox").lightbox({
			    fitToScreen: true,
			    imageClickClose: false
		    });
		    
		    if ( jQuery(".gallery-node").size() > 0 ) {
		    	caption = jQuery('ul.gallery li.gallery-node:first a').attr('title');
//		    	alert(caption);
		    	jQuery('ul.gallery').append("<li><span class='description'>" + caption + "</a></li>");
		    	
			}

		    if ( jQuery(".gallery-node").size() > 1 ) {
//		    	alert(1);
		    	jQuery('ul.gallery').append("<li class='gallery-nav'><a class='next'>Next</a></li>");
		    }

			
		    jQuery('.gallery li a.next').bind(
		    	'click',
		    	function() {
//		    		alert(2);
					current_item = jQuery(this).parents('.gallery').find('li.gallery-node:visible');
					if ( current_item.size() > 0 ) {
						current_item.hide();
						current_item.siblings(':first').show();
					}
		    		
		    	}
		    );
		    

/****	video gallery	****/
		jQuery('.gallery-node.type-video').each(
			function() {
				jQuery(this).find('a').prepend('<div class="video-overlay">&nbsp;</div>');
//				_w = jQuery(this).width();
//				_h = jQuery(this).height();
				_w = 490;
				_h = 390;
				jQuery(this).find('.video-overlay').width(_w).height(_h);
				
				jQuery(this).find('a').click(
					function() {

						if ( ! jQuery.flash.available ) 
							return true;
							
						_w = jQuery(this).find('img').width();
						_h = jQuery(this).find('img').height();
		
						vars = {
							file:				jQuery(this).parent().find('a').attr('href'),
							image:				jQuery(this).parent().find('img').attr('src'),
							width:				_w,
							height:				_h,
							shownavigation:		'true',
							controlbar:			'over',
							showstop:			'true',
							showdigits:			'true',
							frontcolor:			'0xffffff',
							backcolor:			'0x000000',
							lightcolor:			'0xcccccc',
							screencolor:		'0xffffff',
							enablejs:			'true',
							javascriptid:		'video_player',
							autostart:			'true',
							loop:				'true'
						};
						
						params = {
							allowfullscreen:	true,
							flashvars:			vars
						}
						
						jQuery(this).find('img').remove();
						jQuery(this).find('.video-overlay').remove();
						
						jQuery(this).parent().flash(
							{
								swf:		'/cms/wp-content/themes/laudanum/libs/mediaplayer-licensed/player-licensed.swf',
								params:		params,
								width:		_w,
								height:		_h,
								allowfullscreen: true
							}
						);

						return false;
					}
				);
			}
		);
		
	
/****	select menu jquery ui style	
****/
		if ( jQuery.browser.msie )
			_mw = 301;
		else
			_mw = 305;
			
		jQuery('select[name=service]:not(.unstyled)').selectmenu(
			{
				width:		_mw,
				menuWidth: _mw,
				style:'dropdown',
				format: addressFormatting
			}
		);

		jQuery('select:not(.unstyled)').selectmenu(
			{
				width:		_mw,
				menuWidth:	_mw,
				style:		'dropdown'
			}
		);
	
//	signature thumbnail caption flyouts
/*
		jQuery('#gallery .posthead').hover(
			function() {
				caption = jQuery(this).find('.caption');
				caption.show();
			},
			function() {
				jQuery(this).find('.caption').hide();
			}
		);
*/
/*
		jQuery('.posthead').mousemove(function(e) {
//				alert(this.offsetTop);
//			window.status = jQuery('#content').css('top');
//			jQuery(this).find('.caption').css('top', e.pageY - parseInt(jQuery('#content').css('top')));
		});
*/

		if ( jQuery.browser.msie && jQuery.browser.version < 7 ) {
			_h = jQuery(window).height();
			jQuery('#sidebar').css('background-image', 'none').css('height', _h).css('background-color', '#f7f7f7');
			jQuery('#page').css('background-image', 'none');

			jQuery('#footer').hide();
			
			jQuery('.navigation').css('top', 0).css('left', -180).css('position', 'absolute');
			
		}
		


	}
);

function showZoom(id) {
		jQuery("#zoom" + id).show('fast');
	}
	
function hideZoom(id) {
		jQuery("#zoom" + id).hide('slow');
	}
	
		var addressFormatting = function(text){
			var newText = text;
			//array of find replaces
			var findreps = [
				{find:/^([^\-]+) \- /g, rep: '<span class="ui-selectmenu-item-header">$1</span>'},
				{find:/([^\|><]+) \| /g, rep: '<span class="ui-selectmenu-item-content">$1</span>'},
				{find:/([^\|><\(\)]+) (\()/g, rep: '<span class="ui-selectmenu-item-content">$1</span>$2'},
				{find:/([^\|><\(\)]+)$/g, rep: '<span class="ui-selectmenu-item-content">$1</span>'},
				{find:/(\([^\|><]+\))$/g, rep: '<span class="ui-selectmenu-item-footer">$1</span>'}
			];
			
			for(var i in findreps){
				newText = newText.replace(findreps[i].find, findreps[i].rep);
			}
			return newText;
		}