$(function() {
	
	/* 
	 * External Links
	 */
	
	$('a[rel=external]').click(function(e) {
		open(this.href);
		e.preventDefault();
	}).attr('title', 'Opens in a new window');
	
	/* 
	 * Random Little Niceties
	 */
	
	$('li:last-child').addClass('last-item');
	
	$('li:nth-child(odd)').addClass('alternate');
	
	// Forms
	$('.form input[type="text"]').addClass('text');	
	$('.form input[type="submit"]').addClass('submit');
	
	//My Account
	$('#SideBarWebPart').remove().insertAfter('#search');
		
	/* 
	 * Manipulate WYSIWYG
	 */
	
	$('#content img').each(function(e) {
		if ($(this).css('float') == 'left') {
			$(this).addClass('left').removeAttr('style');
		}
		else if ($(this).css('float') == 'right') {
			$(this).addClass('right').removeAttr('style');
		}
	});
	
	/*
	 * jCaption
	 */
	
	$('#content img.caption').jcaption({
		
	});
	
	/* 
	 * Fancybox
	 */
	
	$('.fancybox-image').fancybox({
		titlePosition : 'over',
		transitionIn  : 'elastic',
		transitionOut : 'elastic',
		type          : 'image'
	});
	
	/*
	 * Search Box
	 */
	
	$('.searchfield').focus(function() {
		if ($(this).val() == 'what are you looking for?') {
			$(this).val('');
		}
	});
	
	$('.searchfield').blur(function() {
		if ($(this).val() == '') {
			$(this).val('what are you looking for?');
		}
	});
	
	$('.searchfield').blur();
	
	/*
	 * Videos
	 */
	
	if (document.cookie && document.cookie.indexOf('MyGreatPlace_ReturningVisitor') > -1) {
		$('.videotile-home').flash({
			flashvars : {
				FLVAutoPlay : false
			},
			height : 295,
			swf    : '/Style Library/GreatPlaces/_flash/videotile-mygreatplaceNewSitev2.swf',
			width  : 260,
			wmode  : 'transparent'
		});
	}
	else {
		$('.videotile-home').flash({
			height : 295,
			swf    : '/Style Library/GreatPlaces/_flash/videotile-mygreatplaceNewSitev2.swf',
			width  : 260,
			wmode  : 'transparent'
		});
	
		// Create a cookie that expires in a year
		var now = new Date();
			now.setTime(now.getTime() + 1000 * 60 * 60 * 24 * 365);
		
		document.cookie = 'MyGreatPlace_ReturningVisitor=true; path=/; expires=' + now.toGMTString();
	}
	

	
	/*
	 * Toggle
	 */
	$('.account h4:first').after('<a class="toggle" href="#">- Hide</a>');
	
	$('a.toggle').toggle(function() {
		$(this)
			.text('- Show')												// Change Text
			.parent().find('.collapse').stop(true, true).slideUp();		// Animate
	}, function() {
		$(this)
			.text('- Hide')												// Change Text
			.parent().find('.collapse').slideDown();					// Animate
	});
	
	/*
	 * Accordian
	 */
	$('.accordian h5:first').addClass('expanded');
	
	$('.accordian .information').hide();
	$('.accordian .information:first').show();
	
	$('.accordian h5').click(function(e) {
		if ($(this).hasClass('expanded')) {
			$('.accordian h5').removeClass('expanded');
			$('.accordian .information').stop(true, true).slideUp();
		}
		else {
			$('.accordian h5').removeClass('expanded');
			$('.accordian .information').slideUp();
			
			$(this).addClass('expanded');
			$(this).next('.information').slideDown();
		}
		e.preventDefault();
	});
	
});
