template_directory = "/cms/wp-content/themes/white-rock/";


jQuery(document).ready(function() {
	
	
/****	SDC		****/
	var gallery_interval = 4000;

	if ( jQuery('.attachments li.type-video').size() )
		jQuery('.mediaplayer-overlay a.video').show();

//	if there is a video and and image, or no video and lots of images
	if ( jQuery('.attachments li.gallery-node.type-image').size() && jQuery('.attachments li.type-video').size() )
		jQuery('.mediaplayer-overlay a.image').show();
	else if ( jQuery('.attachments li.gallery-node.type-image').size() > 1) {
		jQuery('.mediaplayer-overlay a.image').show().css('margin-top','20px');
	}
	
	if ( jQuery('#mainmenu li.current-item').size() )
		jQuery('#mainmenu li.current-item').parents('li').addClass('current-item');
	
//	search form
	jQuery(".searchform input[name=s]").focus(
		function() {
			if ( jQuery(this).val().toLowerCase() == 'search' )
				jQuery(this).val('');
		}
	);
	
//	sociable
//	jQuery(".share").show();
	jQuery(".share > a").click(
		function () {
			if ( jQuery(this).parent().find(".sociable:visible").size() )
				jQuery(this).parent().find(".sociable").hide();
			else
				jQuery(this).parent().find(".sociable").show();
		}
	);

//	other flyouts
	jQuery('li.reveal-on-click > a').click(
		function() {
			child = jQuery(this).parent('li').find('ul:first');
			if ( child.is(':visible') ) {
				child.hide();
				jQuery(this).parent('li').removeClass('active-item');
				return;
			}
			if ( ! jQuery(this).parent('li').hasClass('no-hide') ) {
				jQuery(this).parent('li').parent('ul').find(' > li > ul').hide()
				jQuery(this).parent('li').parent('ul').find('li ul').parent('li').removeClass('active-item');		
			}
			child.show();
			jQuery(this).parent('li').addClass('active-item');
		}
	);
		
//	mainmenu flyouts
	jQuery('#mainmenu ul > li').hover(
		function() {
			jQuery("#mainmenu").stopTime("close");
			
			child = jQuery(this).find('ul:first');
			if ( child.is(':visible') )
				return;
			jQuery(this).parent('ul').find('li ul').hide();
			child.show();
			jQuery(this).addClass('active-item');
			jQuery(this).parents('li').addClass('active-item').prev().addClass('previous-sibling-of-active-item');
			
			
//	if the menu is overheight try and fix it .
//	ie6 gets jQuery(this).height() incorrect ( the li is stretchy )
			_lih = jQuery(this).height();
			if ( jQuery.browser.msie && parseInt(jQuery.browser.version) < 7 )
				_lih = 26;
			_h = _lih * child.children().size();
			
			var browserheight = jQuery(window).height();
			var offset = child.offset();
			
			_b = ( _h + offset.top)
			
			if ( _h + offset.top > browserheight ) {
				vertical_nodes = Math.floor((browserheight - offset.top) / _lih) - 1;
				columns = Math.ceil(child.children().size() / vertical_nodes);
				_w = jQuery(this).width() * columns;
				child.width(_w);
			//	fix up the top border ( :first )
				child.children().removeClass('item-0');
				for ( i = 0; i < columns; i++ )
					child.children().eq(i).addClass('item-0');
			}
		},
		function() {
			jQuery("#mainmenu").oneTime(1000, "close", function() {
				jQuery('#mainmenu li').removeClass('active-item').removeClass('previous-sibling-of-active-item');
				jQuery('#mainmenu li ul').hide();
			});
			jQuery(this).removeClass('active-item').prev().removeClass('previous-sibling-of-active-item');		
		}
	);
	
	
//	front page in lieu of flash
	var settings = jQuery.extend({
		container_selector : 'body',
		fill :		true,
		centre :	true,
		top_ignore_selectors	: new Array(),
		left_ignore_selectors	: new Array()
	}, new Array());


	_scaleImage = function(img, bg_container) {
//	get the images parent ( this gives us our size )
		if ( ! bg_container )
			bg_container = img.parent();

//	get the original image sizes if we don't have them already
		if ( img.attr('rel') ) {
			sizes = img.attr('rel').split(',');
			_iw = sizes[0];
			_ih = sizes[1];
		} else {
			_iw = img.width();
			_ih = img.height();
			img.attr('rel', _iw + ',' + _ih);
		}
						
		_pw = bg_container.width();
		_ph = bg_container.height();
		
		_pratio = _pw/_ph;
		_iratio = _iw/_ih;
		
//	are we scaling to fit or fill ?
		if ( settings.fill ) {
			if ( _pratio > _iratio ) {
				_w = _pw;
				_h = _pw / _iratio;
			} else {
				_w = _ph * _iratio;
				_h = _ph;					
			}
		} else {
			if ( _pratio < _iratio ) {
				_w = _pw;
				_h = _pw / _iratio;
			} else {
				_w = _ph * _iratio;
				_h = _ph;					
			}				
		}
		
		if ( settings.centre ) {
			_ml = (_pw - _w) / 2;
			_mt = (_ph - _h ) / 2;
			img.css('margin-top', _mt).css('margin-left', _ml);
		}				
		
		img.css('position', 'absolute').css('width',_w).css('height',_h);
	}
			
	jQuery('body.home #slide-container .slide .background li:not(.overlay)').hide();
	jQuery('body.home #slide-container .slide .background li:not(.overlay):first').show();
	
	jQuery('body.home #slide-container .slide .background li img').css('width', 'auto').css('height', 'auto');
	_scaleImage(jQuery('body.home #slide-container .slide .background li:not(.overlay):first img'));
	
	jQuery(window).resize(
		function() {
			jQuery('body.home #slide-container .slide .background li').each(
				function() {
					_scaleImage(jQuery(this.find('img')));
				}
			);
		}
	);
	
	window.onorientationchange = function() {
		jQuery('body.home #slide-container .slide .background li img').each(
			function() {
				_scaleImage(jQuery(this));
			}
		);
	}
	
	jQuery('body.home #slide-container .slide .background').everyTime(7000, 'next-slide', function() {
		if ( jQuery('body.home #slide-container .slide .background li').size() < 2 ) {
			jQuery('body.home #slide-container .slide .background').stopTime('next-slide');
			return;
		}
			
		current = jQuery(this).find('li:visible:first');
		next = current.next();
		
		if ( ! next.size() ) {
			next = jQuery(this).find('li:not(.overlay):first')
//			alert( ' no next ' );
		}
		
			
		current.removeClass('current').fadeOut('slow');
		next.addClass('current').fadeIn('slow');
		_scaleImage(next.find('img'));

	});
	
	
/****	FORMS	****/
//	hide checkboxes
	jQuery('.option :checkbox').hide().parent().addClass('checkbox');

//	global checkboxes
	jQuery('.checkbox').click(
		function() {

//	its checked so uncheck it
			if ( jQuery(this).find(':checkbox:checked').size() ) {
		//		alert(jQuery(this).find(':checkbox:checked').size());
				jQuery(this).find(':checkbox').attr('checked', false);
				jQuery(this).removeClass('checked');
				
			} else {
		//		alert(jQuery(this).find(':checkbox:checked').size());
				if ( jQuery(this).hasClass('check-single') ) {
					jQuery(this).parent().find('li.checkbox').removeClass('checked')
					jQuery(this).parent().find(':checkbox:checked').attr('checked', false);
				}
				jQuery(this).find(':checkbox').attr('checked', true);
				jQuery(this).addClass('checked');
			}
		}
	);

//	timetable filter	
	jQuery('.option').click(
		function() {
			check = jQuery(this).find(':checkbox');

//	all this has moved into the global checkbox click function . it depends on .check-single being added to the .option
//	if its already checked uncheck it 
//			if ( check.attr('checked') ) {
//				check.attr('checked', false);
//			} else {
//				check.attr('checked', true);
//			if ( check.attr('checked') ) {
//	uncheck others in the group that are not this one
//				check.parents('ul').find(':checkbox:checked').not(check).attr('checked', false);
//			}
			
			args = new Array();
			args_show = new Array();
			
			jQuery('.option input:checked').each(
				function() {
//	elements not containing this class
					args.push(':not(.' + jQuery(this).attr('name') + ')');
					args_show.push('.' + jQuery(this).attr('name'));
				}
			);
			
//	filter takes a comma separated array
			tags = args.join(',')
			tags_show = args_show.join(',')
//	show everything and hide things that DONT have all the tags

//	show those that are hidden that need showing
			if ( tags_show ) {
//				alert(tags_show);
				jQuery('ul.timetable li').filter(tags_show).show();
			} else {
				jQuery('ul.timetable li').show();
			}
			
//	hide those that are visible that need hiding
			jQuery('ul.timetable li').filter(tags).hide();
//	indescriminate show all then hide
//			jQuery('ul.timetable li').show().filter(tags).hide();
//					jQuery('.section ul:empty').parent().hide();

//	show all days and iterate over the days
			jQuery('.section_key').show().each(
				function() {
//	hiding days with zero visible list items
					if (jQuery(this).find('ul.timetable li:visible').size() == 0 )
						jQuery(this).hide();
				}
			);
			
			update_counts();
			update_query();
			
		}
	);


//	loop through options and update counts
	update_counts = function() {			
			jQuery('.option').find(':checkbox').each(
				function() {
					n = jQuery(this).attr('name');
					c = jQuery('ul.timetable .' + n + ':visible').size();
					l = "label[for='" + n + "'] .count";
					if ( c )
						jQuery(l).text('(' + c + ')');
					else
						jQuery(l).text('');
				}
			);
	}
	
	
	update_query = function() {
//		alert(jQuery.query);
		query = new Array();
		query_string = '';
		jQuery('.option').find(':checked').each(
			function() {
				v = jQuery(this).attr('name');
				k = jQuery(this).parents('li.reveal-on-click').find('a').attr('rel');
//				alert(k + " " + v);
				query.push(escape(k) + '=' + escape(v));
			}
		);
		query_string = '#' + query.join('&');
//		alert(query_string);
//	alert(window.location.pathname);
		url = window.location.protocol + '//' + window.location.hostname + window.location.pathname + query_string
//		alert(url)
//		window.location.hash = query_string;
		document.location.replace(url);
		return false;
			
	}
	
	

/*
	jQuery('.optionOLD :checkbox').click(
		function() {
		
//	uncheck others in the group that are not this one
			jQuery(this).parents('ul').find(':checkbox:checked').not(this).attr('checked','');
			
		
			args = new Array();
			jQuery('.option input:checked').each(
				function() {
//	elements not containing this class
					args.push(':not(.' + jQuery(this).attr('name') + ')');
				}
			);

//	filter takes a comma separated array
			tags = args.join(',')
//	show everything and hide things that DONT have all the tags
			jQuery('ul.timetable li').show().filter(tags).hide();
//	show all days and iterate over the days
			jQuery('.section').show().each(
				function() {
//	hiding days with zero visible list items
					if (jQuery(this).find('ul.timetable li:visible').size() == 0 )
						jQuery(this).hide('slow');
				}
			);
		}
	);
*/

	jQuery('.reset-form').click(
		function() {
			n = jQuery(this).attr('rel');
			f = jQuery('#' . n).find('.checked').click();
		}
	);


/****	GALLERIES	****/
	
//	should be rewritten with all attachments in one UL array
//	skipping video or image as appropriate

//	display gallery on click
	jQuery('.attachments-list a').click(function() {
		src = jQuery(this).attr('href');
		rel = jQuery(this).attr('rel');

//alert(10 + " " + jQuery(this).parent('li').attr('class'))


		if ( jQuery(this).parent('li').hasClass('type-video') ) {
			playItem(jQuery(this));
		} else if ( jQuery(this).attr('href') == '#' ) {
			target = jQuery(".attachments li." + rel);
			showItem(target);		
		} else if ( jQuery(this).parent('li').hasClass('type-image') ) {
			playItem(jQuery(this));
		} else {
			target = jQuery(".attachments li." + rel);
			showItem(target);
		}

		if ( rel ) {
			target = jQuery(".attachments li." + rel);
		
//	also display the first child if it has child galleries
			if ( target.children('.gallery-parent').size() ) {
		//	target.find("li.gallery-node:first").css('display','block').addClass('active-item');
				showItem(target.find("li.gallery-node:first"));
//	start the slideshow
				resetTimer(image_play);
			}
		}
		
		
		if ( jQuery(this).parent().hasClass('type-image') ) {
			jQuery('.mediaplayer-overlay a.image').hide();
			jQuery('.mediaplayer-overlay a.video').hide();
			jQuery('.mediaplayer-overlay a.play').hide();
			if ( jQuery('.mediaplayer-overlay .thumbs ul li').size() > 1 )
				jQuery('.mediaplayer-overlay .thumbs').show();
		} else if ( jQuery(this).parent().hasClass('type-video') ) {
			jQuery('.mediaplayer-overlay a.image').hide();
			jQuery('.mediaplayer-overlay a.video').hide();
			jQuery('.mediaplayer-overlay .thumbs').hide();
//			jQuery('.mediaplayer-overlay a.play').show();
		}
		
		return false;
	});
	
	
	jQuery('.mediaplayer-overlay a.image').click(function() {
		galleries = jQuery('.attachments .gallery-parent:first');
		if ( galleries.size() ) {
			jQuery('.mediaplayer-overlay a.image').hide();
			jQuery('.mediaplayer-overlay a.video').hide();
			jQuery('.mediaplayer-overlay a.play').hide();
			if ( jQuery('.mediaplayer-overlay .thumbs ul li').size() > 1 )
				jQuery('.mediaplayer-overlay .thumbs').show();
			showItem(galleries.parent());
			showItem(galleries.find("li.gallery-node:first"));
			resetTimer(image_play);
		} else {
			alert('no galleries');
		}
	});

	jQuery('.mediaplayer-overlay a.video').click(function() {
//	simulate a click on the first video item	
		video = jQuery('.attachments-list li.type-video:first a');
		jQuery('.mediaplayer-overlay a.image').hide();
		jQuery('.mediaplayer-overlay a.video').hide();
		jQuery('.mediaplayer-overlay a.play').hide();
		video.click();
	});



//	image gallery
	jQuery('.attachments li ul li.gallery-parent a').click(function() {
//	alert(30)
		nextItem(jQuery(this).parent('.gallery-node'));
		return false;
	});


//	all medium items	
	jQuery('.attachments li.gallery-node a').click(function() {
//	single image as loaded and displayed by default
		if ( jQuery(this).parent().hasClass('type-video') ) {
			container = createPlayContainer('mediaplayer');
			jQuery('.mediaplayer-overlay a.image').hide();
			jQuery('.mediaplayer-overlay a.video').hide();
			jQuery('.mediaplayer-overlay a.play').hide();

			showItem(jQuery('#' + container));			
			playSocial(jQuery(this).attr('href'), null, container);
		} else {
//	start the gallery 		
	
//	find the gallery if there is on
			galleries = jQuery(this).parents('.gallery-parent:first');
			if ( galleries.size() ) {
				jQuery('.mediaplayer-overlay a.image').hide();
				jQuery('.mediaplayer-overlay a.video').hide();
				jQuery('.mediaplayer-overlay a.play').hide();
				if ( jQuery('.mediaplayer-overlay .thumbs ul li').size() > 1 )
					jQuery('.mediaplayer-overlay .thumbs').show();
				showItem(galleries.parent());
				showItem(this);
				resetTimer(image_play);
			} else {
	//			alert('no galleries');
			}

		}
		return false;
	});


	jQuery('.mediaplayer-overlay a.play').click(function() {		
		current_item = jQuery('ul.attachments > li.gallery-node.active-item a');
		container = createPlayContainer('mediaplayer');
		showItem(jQuery('#' + container));

		jQuery('.mediaplayer-overlay a.image').hide();
		jQuery('.mediaplayer-overlay a.video').hide();
		jQuery('.mediaplayer-overlay a.play').hide();
		
		playSocial(jQuery(current_item).attr('href'), null, container);
		
	});


	_build_thumbnails = function(parent_id) {
	
		
//	count = 0;
	thumb_w = 53;	//	from css = width + padding_left + padding_right
	thumbs_max = 9;
	jQuery('.attachments li.type-image li.gallery-node img').each(function() {
		parent_node = jQuery('.mediaplayer-overlay .thumbs ul');
		count = parent_node.children().size();
		
		li = document.createElement('li');
		a = document.createElement('a');
		img = document.createElement('img');
		
		a.setAttribute('href', jQuery(this).attr('src'));
		a.setAttribute('rel', jQuery(this).parents('.gallery-node').attr('id'));
//	we need to get the thumbnail here
		img.setAttribute('src', jQuery(this).attr('lowsrc'));
		
		a.setAttribute('onclick', 'showItemFromThumb(this); return false;');
//	ie7
		a.onclick = function() { showItemFromThumb(this); return false;}
		a.appendChild(img);
		li.appendChild(a);

		obj = parent_node.append(li);
		parent_node.width((count+1)*thumb_w + 'px');
		
		count++;
		
//	we're overwidth
		if ( count > thumbs_max ) {
			jQuery('.mediaplayer-overlay .thumbs .button').addClass('active');
		}
		
	});
	
	
	jQuery('.mediaplayer-overlay .thumbs .button').click(
		function() {
//	we're overwidth
			if ( jQuery(".mediaplayer-overlay .thumbs .thumbs_container ul li").size() > thumbs_max ) {
//	do nothing
			};


			if ( jQuery(this).hasClass('active') ) {
			
//	where are we in the sequence ?
				index = jQuery('.attachments ul.gallery-parent li').index(jQuery('.attachments ul.gallery-parent li.active-item'));
//	how many pages are we along ?
				index = Math.floor(index/thumbs_max);
//	jump to the start of the next page
				if ( jQuery(this).hasClass('next') )
					index = (index + 1) * thumbs_max;
				else
					index = (index - 1) * thumbs_max;

//	over value					
				if ( index > jQuery(".mediaplayer-overlay .thumbs .thumbs_container ul li").size() )
					index = 0;
				if ( index < 0 ) {
					index = jQuery(".mediaplayer-overlay .thumbs .thumbs_container ul li").size()
					index = Math.floor(index/thumbs_max) * thumbs_max;
				}
								
				next_item = jQuery('.attachments ul.gallery-parent li:eq('+index+')');
				showItem(next_item);
				resetTimer(image_play);
			}
		}
	);
	}
	
	_build_thumbnails();
	
	
	nextItem = function(obj) {
		if ( ! obj ) 
			current_item = jQuery('.attachments ul.gallery-parent li.active-item');
		else
			current_item = jQuery(obj);
			
		next_item = current_item.next('').length ? current_item.next() : current_item.parent().find('.gallery-node:first');
		showItem(next_item, current_item);
	}
	

	showItem = function(next_item, current_item) {
		if ( ! jQuery(next_item).attr('id') )
			return;

		stopVideo();
		if ( next_item.hasClass('type-video') )
			stopTimer(image_play);
		
		if ( ! current_item )
			current_item = next_item.parent().find('.gallery-node.active-item')		
		
//	its already shown
		if ( next_item.hasClass('active-item') )
			return false;
			
//	unset all previous items
		jQuery('.attachments .previous-item').each(function() {
//			alert(1);
			jQuery(this).removeClass('previous-item');
		});

//	set up block
		current_item.addClass('previous-item').removeClass('active-item');
//	turn off all other active items
		jQuery('.attachments .active-item').addClass('previous-item').removeClass('active-item');
		
		next_item.css({opacity : 0, display : 'block'}).addClass('active-item');
//	transition
		next_item.fadeTo(parseInt(gallery_interval/10), 1);

//	do we have a thumbnail ?
		jQuery(".mediaplayer-overlay .thumbs a").removeClass('active-item');
		next_thumb = jQuery(".mediaplayer-overlay .thumbs a[rel='" + next_item.attr('id') + "']");
		next_thumb.addClass('active-item');
		
		centreThumb(next_thumb);
		
/*
//	we no longer do this as it was not what the designer had in mind
//	centre the current thumbnail
		if ( jQuery(".mediaplayer-overlay .thumbs ul").width() > jQuery(".mediaplayer-overlay .thumbs").width() ) {
			index = jQuery(".mediaplayer-overlay .thumbs ul li").index(next_thumb.parent());
			_w = next_thumb.parent().width() + parseInt(next_thumb.parent().css('margin-left')) + parseInt(next_thumb.parent().css('margin-right'));
			_x = _w * (index + 0.5);
			_p = jQuery(".mediaplayer-overlay .thumbs").width();
			_left = _p/2 - _x;
			_left = parseInt(_left) + 'px';
			jQuery(".mediaplayer-overlay .thumbs ul").animate({"left" : _left}, 400);
		}
*/	
	}
	
	
	centreThumb = function(obj) {
		node = jQuery(".mediaplayer-overlay .thumbs .thumbs_container");
		index = node.find('ul li a').index(obj);
		paged = Math.floor(index/thumbs_max);
		
		_l = -1 * paged * (node.width()-3);
		if ( _l != 0 )
			_l += parseInt(node.css('margin-left'));
//		node.find('ul').css('left', _l + 'px');
		node.find('ul').animate({"left" : _l}, 400);
	}
	
	
	showItemFromThumb = function (obj) {
		next_item = jQuery("#" + obj.rel);
		if ( ! next_item.hasClass('active-item') ) {
			showItem(next_item);
			resetTimer(image_play);
		}
		return false;
	}
	

	playItem = function(current_item) {
//	stop the slideshow
		stopTimer(image_play)

		src = jQuery(current_item).attr('href');
		img = jQuery(current_item).attr('rel');	
		if ( src.indexOf('youtube') > 0 || src.indexOf('vimeo') > 0 ) {
			container = createPlayContainer('mediaplayer');
			showItem(jQuery('#' + container));
			playSocial(src, null, container);
			return false;
		} else if ( src.indexOf('.jpg') > 0 || src.indexOf('.png') > 0 ) {
			playImage(current_item);
		} else {
			container = createPlayContainer('mediaplayer');
			showItem(jQuery('#' + container));
			playFLV(src, img, container);
		}
		return false;
	}
	
	
	playImage = function (obj) {
//		alert(jQuery(obj).attr('rel'));
		parent_node = jQuery('.attachments');

//	create a correctly tagged image container and add it to the array
		li = document.createElement('li');
		a = document.createElement('a');
		img = document.createElement('img');
		
		li.setAttribute('id', jQuery(obj).attr('rel'));
		li.setAttribute('class', 'gallery-node type-image');
		li.className = 'gallery-node type-image';
		
		a.setAttribute('href', jQuery(obj).attr('href'));
		img.setAttribute('src', jQuery(obj).attr('href'));
		
		a.appendChild(img);
		li.appendChild(a);

//	alert('appending');
		parent_node.append(li);
		return false;

	}
	
	
	createPlayContainer = function(id) {
		id = 'target-' + id;

//	have we already created this container ?		
		if ( jQuery('#' + id).size() ) {
			return id;
		}
		
		parent_node = jQuery('.attachments');
		
		div = document.createElement('li');
		div.setAttribute('id', id);
		div.setAttribute('class', 'play-container active-item');
		div.className = 'play-container active-item';

		parent_node.append(div);
		return id;
	}
	
	
	playSocialOembed = function(src, img, target) {
//	clean the parent
		stopVideo();
		jQuery('#' + target).addClass('active-item');
//	clone the target and insert it within as oembed will replace it.
		jQuery('#' + target).clone().attr('id', target + '-oembed').appendTo('#' + target);

//		$(".oembed").oembed(null, {vimeo: {color: "ff0000", portrait: false}});
		_w = jQuery('#' + target).width();
		_h = jQuery('#' + target).height();

//	specify maxHeight ( camel case ) to the oembed object . specifying maxWidth results in an overwide player
		obj = jQuery('#' + target + '-oembed').oembed(
			src, 
			{
				maxHeight: _h, 
				vimeo: {
					color: "e21d0a", 
					autoplay: true, 
					portrait: false, 
					title: false, 
					byline: false
				},
				youtube: {
					color: "e21d0a", 
					autoplay: 1, 
					rel: 0, 
					title: false, 
					byline: false
				}
			}
		);
	}


	playSocial = function(src, img, target) {
//	check for flash first
		if ( ! flashembed.isSupported([7, 0]) ) {
			location.href = src;
			return false;
		}
		
	
//	clean the parent
		stopVideo();
		jQuery('#' + target).addClass('active-item');

		_w = jQuery('#' + target).width();
		_h = jQuery('#' + target).height();

		if ( src.indexOf('youtube') > 0 ) {
			temp = src.match(/v=([a-zA-z0-9\-]+)/);
			if ( temp[1] ) {
				src = "http://www.youtube.com/v/"+temp[1]+"&hl=en&fs=1&rel=0&hd=1&autoplay=1";
			}
		} else if ( src.indexOf('vimeo') > 0 ) {
			temp = src.match(/\/([0-9]+)$/);
			if ( temp[1] ) {
				src = "http://www.vimeo.com/moogaloop.swf?clip_id="+temp[1]+"&color=e21d0a&fullscreen=1&title=false&byline=false&portrait=false&autoplay=1";
			}
		}
		configuration = {
			allowfullscreen: true,
			src: src,
			wmode: 'opaque',
			allowscriptaccess: true,
			width: _w,
			height: _h,
			onFail: function() { location.href = src; return true; }
		};
		flashvars = {};

/*
<object width="853" height="505">
<param name="movie" value="http://www.youtube.com/v/TGsI6CM9Jik&hl=en&fs=1&rel=0&hd=1"></param>
<param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always"></param>
<embed src="http://www.youtube.com/v/TGsI6CM9Jik&hl=en&fs=1&rel=0&hd=1" 
type="application/x-shockwave-flash" 
allowscriptaccess="always" 
allowfullscreen="true" width="853" height="505">
</embed>
</object>		
*/

/*
<object width="396" height="317">
<param name="allowfullscreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="movie" value="http://vimeo.com/moogaloop.swf?
clip_id=7403532&amp;
server=vimeo.com&amp;
show_title=0&amp;
show_byline=0&amp;
show_portrait=0&amp;
color=e21d0a&amp;
fullscreen=1" />

<embed src="http://vimeo.com/moogaloop.swf?clip_id=7403532&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=e21d0a&amp;fullscreen=1" 
type="application/x-shockwave-flash" 
allowfullscreen="true" 
allowscriptaccess="always" 
width="396" 
height="317">
</embed>
</object>
*/
		obj = jQuery('#' + target).flashembed(
			src, 
			configuration,
			flashvars	
		);		

	}


	playFLV = function(src, img, target) {
		if ( ! flashembed.isSupported([7, 0]) ) {
			alert('You need Adobe Flash Player to view this video');
			return false;
		}
		
	
//	clean the parent
		stopVideo();
		jQuery('#' + target).addClass('active-item');

		_w = jQuery('#' + target).width();
		_h = jQuery('#' + target).height();

		type = 'video';

		if ( src.indexOf('youtube') > 0 ) {
			temp = src.match(/v=([a-zA-z0-9]+)/);
			if ( temp[1] ) {
				src = "http://www.youtube.com/v/"+temp[1]+"&hl=en&fs=1&rel=0&hd=1&autoplay=1";
			}
		} else if ( src.indexOf('vimeo') > 0 ) {
			temp = src.match(/\/([0-9]+)$/);
			if ( temp[1] ) {
				src = "http://www.vimeo.com/moogaloop.swf?clip_id="+temp[1]+"&color=e21d0a&fullscreen=1&title=false&byline=false&portrait=false&autoplay=1";
			}
		}
		
		
		
		player = template_directory + 'libs/mediaplayer-licensed/player-licensed.swf';

		configuration = {
			allowfullscreen: true,
			src: player,
			wmode: 'opaque',
			allowscriptaccess: true,
			width: _w,
			height: _h
		};

		var flashvars = {
			width:				_w,
			height:				_h,
			file:				src,
			image:				img,
			type:				type,
//  			autostart:			true,
			shownavigation:		'true',
			showstop:			'true',
			showdigits:			'true',
			frontcolor:			'0xffffff',
			backcolor:			'0x000000',
			lightcolor:			'0xf63a1b',
			screencolor:		'0xffffff',
			enablejs:			'true',
			javascriptid:		'video_player2'
		};

		obj = jQuery('#' + target).flashembed(
			player, 
			flashvars,	
			configuration	
		);		

	}
	

//	this might be a better way to do the loading
//	http://www.longtailvideo.com/support/forum/JavaScript-Interaction/10456/3-16-Javascript-loadFile-youtube
	playFLV_OLD = function(src, img, target) {
	
//		alert(40 + " " + src)

			the_player = document.getElementById('video_player');
		if ( the_player ) {
//	alert(50 + " " + src)
		
			if ( src.indexOf('youtube') > 0 ) {
				file_type = 'youtube';
				args =  {
					file: src, 
					image: img, 
					type: file_type
				};
			} else {
				args =  {
					file: src, 
					image: img
				};
			}
//	alert(60 + " " + src)
			
//			the_player = document.getElementById('video_player');
			if ( the_player ) {
//				alert(the_player)
//				alert(70 + " " + the_player.sendEvent)
//				if ( ! the_player.sendEvent ) {
//					alert(the_player.tagName);
//					return false;
//				}
//				return false;

//	it takes a little while for the_player to come ready if its been hidden
				if ( ! the_player.sendEvent ) {
					window.status = ('the player is being a bit lazy. please stand by.');
					jQuery(this).oneTime(500, "startflv", function() {
						the_player.sendEvent("LOAD", args);
						window.status = '';
					});
				} else {
					the_player.sendEvent("LOAD", args);				
				}
//				return false;
//				the_player.sendEvent("PLAY", "true");
			} else {
				alert("no player");
			}
			return false;	
		} else {
			createPlayer(src, img, target);
		}
		
		return false;
	}
	
	
	createPlayer = function(src, img, target) {
		obj = jQuery('#' + target);
		_w = obj.width();
		_h = obj.height();

		if ( src.indexOf('youtube') > 0 )
			type = 'youtube';
		else
			type = null;

		var flashvars = {
			width:				_w,
			height:				_h,
			file:				src,
			image:				img,
			type:				type,
//  			autostart:			true,
			shownavigation:		'true',
			showstop:			'true',
			showdigits:			'true',
			frontcolor:			'0xffffff',
			backcolor:			'0x000000',
			lightcolor:			'0xf63a1b',
			screencolor:		'0xffffff',
			enablejs:			'true',
			javascriptid:		'video_player2'
		};

		var params = {
			allowscriptaccess:  'always',
			allowfullscreen:    'true',
			bgcolor:			'#ffffff'
		};

		var attributes = {
			id:					'video_player',
			name:				'video_player'
		};
		
//	create the target container as swfobj2 replaces rather than inserts
		id = target + '-swfobj2';
		div = document.createElement('div');
		div.setAttribute('id', id);
		div.setAttribute('class', 'play-container');
		div.className = 'play-container';

		jQuery('#'+target).append(div);		
		swfobject.embedSWF(template_directory + 'libs/mediaplayer-licensed/player-licensed.swf', id, _w, _h, '8.0.0', false, flashvars, params, attributes);
	}
	
	/*
function sendEvent(swf, typ, prm)
      {
        thisMovie(swf).sendEvent(typ, prm);
      };

      function loadFile(swf, obj)
      {
        thisMovie(swf).loadFile(obj);
      };

      function addItem(swf, obj, idx)
      {
        thisMovie(swf).addItem(obj, idx);
      };

      function thisMovie(movieName)
      {
        if(navigator.appName.indexOf('Microsoft') != -1)
        {
          return window[movieName];
        }
        else
        {
          return document[movieName];
        }
      };
      */
	
	
	stopVideo = function() {
		jQuery('#target-mediaplayer').removeClass('active-item');
//	remove children of the player target div
		jQuery('#target-mediaplayer embed').remove();
		jQuery('#target-mediaplayer object').remove();
//		jQuery('.play-container').remove();
//		the_player = document.getElementById('video_player');
//		if ( the_player ) {
//			the_player.sendEvent("PLAY", "false");
//		}
	}
	
	
	resetTimer = function(timer) {
		stopTimer(timer);
		startTimer(timer);
	}
	
	
	startTimer = function(timer) {
		stopTimer(timer);
		jQuery('body').everyTime(timer.interval, timer.name, function(){
			nextItem();
		});
	}
	
	
	stopTimer = function(timer) {
		jQuery('body').stopTime(timer.name);	
	}

/*
jQuery('a.image-link').live('click', function() {
	alert('jabanga');
	return false;
});
*/

	jQuery('#media-browser .catalogue li.type-video a').live(
		'click',
		function() {
			playItem(jQuery(this));
			jQuery('html, body').animate({scrollTop:0}, 'fast');
			return false;
		}
	);
	
	jQuery('#media-browser .catalogue li.type-image a').live(
		'click',
		function() {
//	not the shopp
			if ( jQuery(this).parents('#shopp').size() )
				return true;
				
			parent_id = jQuery(this).attr('rel');
			if ( parent_id ) {
			//	alert(parent_id);
				
				if ( ! jQuery(".parent-" + parent_id).size() ) {

					jQuery.post(ajaxurl,
						{ 
							action : 'is_attachments',
							subaction : 'is_drawImageGallery',
							type : 'image',
							id : parent_id
						},
						function(data) {	
							data = "<li class='parent-container images-"+parent_id+" parent-"+parent_id+" type-image'><ul class='gallery-parent'>" + data + "</ul></li>";
			//				alert(data)
							jQuery('.gallery-container .attachments').append(data);
			//	parent
							parent_node = jQuery(".parent-" + parent_id);
							showItem(parent_node);
			//	firstchild
							first_child = jQuery(".parent-" + parent_id + " li.gallery-node:first");
							showItem(first_child);
							jQuery('html, body').animate({scrollTop:0}, 'fast');

							if ( jQuery(".parent-" + parent_id + " li.gallery-node").size() > 1 ) {
								resetTimer(image_play);
							}
						}
					);
				} else {
					showItem(jQuery(".parent-" + parent_id));
					showItem(jQuery(".parent-" + parent_id + " li.gallery-node:first"));				
					jQuery('html, body').animate({scrollTop:0}, 'fast');
					if ( jQuery(".parent-" + parent_id + " li.gallery-node").size() > 1 ) {
						resetTimer(image_play);
					}
				}
			}
			return false;
		}
	);	

	image_play = new Object();
	image_play.name = 'image_play';
	image_play.interval = 4000;
	
//	google analytics
//	function _analytics(link, title, leaf) {

	

});

function is_array(input){
	return typeof(input)=='object'&&(input instanceof Array);
}

function reset_form(n) {
	document.forms[n].reset();
}


function _build_ga_url(link, title, leaf) {
	
}


function _analyticsOLD(url) {
//	url = _build_ga_url(link, title, leaf);
	alert(url.length);
	if ( url.length )
		url = url.join('/');
		
	try {
		alert(url);
//		pageTracker._trackPageview(link);
	} catch (err) {
	
	}
}


//	runs separately so that links within ajax loads can be processed too .
function _analytics(parent_node) {

	jQuery(document).ready(function() {

		_addClick = function(obj, url) {
			jQuery(obj).click(function() {
				if ( is_array(url)) 
					url = '/' + url.join('/');
				if ( location.search.indexOf('debug') > 1 )
					alert(url);
				else
					pageTracker._trackPageview(url);
			});
		}


		_replaceClick = function(obj, url) {
			jQuery(obj).unbind('click').click(function() {
				if ( is_array(url)) 
					url = '/' + url.join('/');

				pageTracker._trackPageview(url);
			});
		}


		_addLeafClick = function(obj, leaf, title) {
				url = new Array();
				url.push(leaf);

//	incoming slashes are converted to commas so convert back
				url.push(title.split(','));

				_addClick(obj, url);
		}
		
		
		jQuery('li.external a').each(
			function() {
				title = jQuery(this).attr('rel');
				_addLeafClick(this, 'external', title);
			}
		);
		
		
		jQuery('li.download a').each(
			function() {
				title = jQuery(this).attr('rel');
				if ( jQuery(this).parent().hasClass('pdf') )
					title = 'pdf/' + title;
				else if ( jQuery(this).parent().hasClass('tiff') || jQuery(this).parent().hasClass('tif')  || jQuery(this).parent().hasClass('jpg')  || jQuery(this).parent().hasClass('jpeg')  || jQuery(this).parent().hasClass('png') )
					title = 'image/' + title;
				else if ( jQuery(this).parent().hasClass('msword') || jQuery(this).parent().hasClass('doc') || jQuery(this).parent().hasClass('docx')  || jQuery(this).parent().hasClass('rtf') || jQuery(this).parent().hasClass('xls') )
					title = 'doc/' + title;
					
				_addLeafClick(this, 'download', title);
			}
		);
		
		
		jQuery('li.type-video a').each(
			function() {
			//	download type title
			//	container li
				parent_node = jQuery(this).parent('li');
				if ( parent_node.hasClass('mimetype-video-x-youtube') )
					type = 'youtube';
				else if  ( parent_node.hasClass('mimetype-video-x-vimeo') )
					type = 'vimeo';
				else
					type = 'video';
				
				title = jQuery(this).attr('title');
				title = title.split(" ").join('-').toLowerCase();
				
				url = new Array();
				url.push('view');
				url.push(type);
				url.push(title);
				
				_addClick(this, url);
			}
		);
		
		
//	button classes should only be used by editors
		jQuery('a.button').each(
			function() {
			
			}
		);
		
		
//	shop - this should all be redone based on the receipt using  http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=55528
//	look at the no override on receipt coming from paypal
//	So from paypal your return url needs to be www.example.com/thankyou.html?utm_nooverride=1 as indicated above.
//	http://www.google.com/support/forum/p/Google+Analytics/thread?tid=55b3caafb25b87af&hl=en

		jQuery('form.wp_cart input:submit').each(
			function() {
				//	/shop/add/item-price				
				url = new Array();
				url.push('shop');

				price = jQuery(this).parent('form').find('[name="price"]').attr('value');

//	we're using the select menu now 
				if ( ! price ) {
					price = jQuery(this).parent('form').find('select').val();
					price = price.split(" ").join('-').toLowerCase();
					url.push('checkout');
					url.push('item-' + price);
				} else {		
					url.push('add');
					url.push('item-' + price);
				}				
				
//	at this stage we only need one tracker per click
				_replaceClick(this, url);
			}
		);
		
		jQuery('input.wp_cart_checkout_button').each(
			function() {
				//	/shop/checkout/total
//				price = jQuery(this).parent('form').find('[name="price"]').attr('value');				
				parent_node = jQuery(this).parent('form');
				total = 0;
				parent_node.find('[name^="amount_"]').each(
					function() {
//	get the index number
						name = jQuery(this).attr('name');
						temp = name.split('_');
						index = temp[1];
//	item value
						value = jQuery(this).attr('value');

//	quantity
						quantity = jQuery(this).parent().find('[name="quantity_' + index + '"]').attr('value');
						
						subtotal = parseInt(value) * parseInt(quantity);
						total += subtotal;
					}
				);
//				alert(total);

				url = new Array();
				url.push('shop');
				url.push('checkout');
				url.push('total-' + total);
				
				_addClick(this, url);
			}
		);
		
		
		
		jQuery('a.tickets').each(
			function() {
//	ticketing			/tickets/show/tickethost
				link = jQuery(this).attr('href');

//	title of the work - we store post_name in the rel of the parent
				work = jQuery(this).parents('li.work-item').attr('rel');
			
//	name of ticket host
				host = jQuery(this).attr('rel');

				url = new Array();
				url.push('tickets');
//	hosts with slashes in them already contain the name of the work
				if ( ! host.indexOf('/') ) {
					url.push(work);
				}
				url.push(host);
				
				_addClick(this, url);
//				return false;
			}
		);


/*	
select menu style - relies on ui-core.js and
-	jquery-ui-1.7.2.custom.css
-	ui.selectmenu.css
-	ui.selectmenu.js
see overrides to custom.css in style.css
*/
		_w = 160;
		if ( jQuery.browser.msie ) {
			_mw = 154;
		} else if ( jQuery.browser.mozilla ) {
			_mw = 158;
			_w = 160;
		} else {
			_mw = 158;
		}
		
		if ( jQuery.browser.msie && jQuery.browser.version < 8 ) {
//	too flakey in ie7		
		} else {					
			jQuery('select.formatted:not(.unstyled)').selectmenu(
				{
					menuWidth: _mw,
					width: _w,
					style:'dropdown',
					format: addressFormatting
				}
			);
	//	for the shop
			jQuery('.variations select, .cart select').selectmenu(
				{
					menuWidth: 158,
					width: _w,
					style:'dropdown',
					format: addressFormatting
				}
			);
	
			jQuery('select:not(.unstyled)').selectmenu(
				{
					menuWidth: _mw,
					width: _w,
					style:'dropdown'
				}
			);
		}


	});
}

_analytics();


		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;
		}
