$(document).ready(function() {

	if ( $("#salutation").text().indexOf("[firstname,fallback=]") > -1 ) {
		$("#salutation").text("");
	}
	
	// add wrapper divs to blockquotes for closing quote tags
	$("#content blockquote p").prepend("&#8220;").append("&#8221;");
	//$("#content blockquote").wrap('<div class="outerQuote"><div class="centerQuote"></div></div>');
	
	// Cufon font replacement
	Cufon.replace('h1#title, #content h2, #sidebar h2'); //, #content blockquote

	// zebra striping
	$(".package-list tr:even, .news-list tr:even").addClass("dark");

	/* Fix PNG transparency in IE 6 */
	$('img[src$=.png], #logo a').ifixpng();
	
	/* open new links in a popup window */
	$('a[rel=new]').click(function() {
		window.open($(this).attr('href'));
        return false;
	});
	
	//hide the gallery drop down if only one item
	if ( $('.sets ul').children("li").length == 1 ) {
			$('.sets > a').addClass("hide");
	}

	/* setup the main menu */
	$('#navigation.sf-menu ul').superfish({
		autoArrows:		false,
		dropShadows:	false,
		disableHI:		true
	});
	
	$('#navigation ul:first > li:lt(4)').addClass('main'); // add class to important nav items

	/* showcase image switcher */	
	$('#controls .sets a, a.gallerylink').click(function() {
		$('#controls .pause').click(); // swap back to paused state
		var selectedGallery = $(this).attr('href');
		// update the current gallery title
		$('#controls .sets a').each(function(){
			if ( selectedGallery == $(this).attr('href') ) {
				$('#controls .sets .title').html($(this).html()); 
			}
		});
		$('#images').cycle('stop').html('').addClass('loading'); // stop the slideshow, empty the images, display loader
		
		// get the images array string
		var galleryID = $(this).attr('href');
		var galleryImagesStr = $("#g"+galleryID).text();
		
		// off to the module to get our images!
		$.post("/modules/showcase_gallery/new_load.php", {
			gallery_id:	galleryID,
			gallery_images: galleryImagesStr
		},
		function(data) {
			$('#images').html(data).removeClass('loading');
			
			// get a random image to start on
			var num_imgs = $('#images').children().length;
			var random_img = Math.floor(Math.random()*(num_imgs-1));

			// not random start for homepage
			if ( $("body.home") ) { random_img = 0; }

			var action = "pause";
			if ( $("#showcase-gallery_87 #images, #showcase-gallery_97 #images, #showcase-gallery_105 #images, #showcase-gallery_30 #images").length > 0 ) {
					$('a.play').hide().siblings('a.pause').show();
					action = "resume";
			}

			$('#images').cycle({
				fx:     	'fade', 
				speed:  	1200, 
				timeout: 	4000, 
				next:   	'.next', 
				prev:   	'.previous' ,
				startingSlide: random_img
			})
			.cycle(action); // pause the slideshow 
			
			$('#controls .play').click(function() {
				$('#images').cycle('resume');
				$(this).hide().siblings('a.pause').show();
				
				return false;
			});
			
			$('#controls .pause').click(function() {
				$('#images').cycle('pause');
				$(this).hide().siblings('a.play').show();
				
				return false;
			});
		});
		
		return false;
	});
	
	/* activate the first set */
	$('#controls .sets ul a:first').click();
	
	/* search box */
	var defaultSearch = $('#search').val();
	$('#search').focus(function() {
		if ($(this).val() == defaultSearch) $(this).val('');
	}).blur(function() {
		if ($(this).val() == '') $(this).val(defaultSearch);
	});
	
	/* tell a friend */
	var tellfriendlink = $('.tell-a-friend a');
	$('#friend-modal').jqm({ajax: tellfriendlink.attr('href'), trigger: tellfriendlink, onLoad: function() {
		Cufon.replace('#friend-modal h2');
	}});
	
	/* subscribe*/
	var subscribelink = $('.subscribe a');
	$('#subscribe-modal').jqm({ajax: subscribelink.attr('href'), trigger: subscribelink, onLoad: function() {
		Cufon.replace('#subscribe-modal h2');
	}});
	
});