var $j = jQuery.noConflict();

$j(function(){
	$j(".gallery_thumbs img").addClass("inactive");

	cache=[];
	$j(".gallery_thumbs a").each(function(e) {
	//preload
	var cacheImage = document.createElement('img');
	cacheImage.src = $j(this).attr('href');
	cache.push(cacheImage);

	var wrapper = $j('<div class="thumb"></div>');
	$j(this).wrap(wrapper);

	
	// make play button
	if($j(this).attr('href').match(/(mpg|m4v|mp4|mov)$/i)){
	     var letimg = '<img src="http://www.judithfegerl.net/wp/wp-content/themes/judithfegerl/images/gallery-play.png" style="position: absolute; top: 11px; left: 19px;" />';
	     $j(this).children().after(letimg);
	}
	});
	
	
  var i = $j(".gallery_thumbs div a").click(function(e) {
	e.preventDefault();
	$j(".active").removeClass('active').addClass("inactive");
	$j(this).find(".inactive").removeClass('inactive').addClass('active');
	$j(".gallery_nav .num").html(i.index(this)+1 + '/' + $j(".gallery_thumbs a").size());
	$j('.gallery_viewer').hide();
    if($j(this).attr('href').match(/(mpg|m4v|mp4|mov)$/i)){
		$j('.gallery_viewer').html('<object width="500" height="375" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab"><param name="src" value="'+$j(this).attr('href')+'"/><param name="controller" value="true"/><param name="autoplay" value="true"/><embed src="'+$j(this).attr('href')+'" width="500" height="375" controller="true" autoplay="true"	scale="tofit" cache="true" pluginspage="http://www.apple.com/quicktime/download/"/></object>');
	 } else {
		$j('.gallery_viewer').html('<img src="'+$j(this).attr('href')+'"/>');
	}
	$j('.gallery_description').html($j(this).attr('title'));
	$j('.gallery_viewer').fadeIn('fast');
  });



next = function() {
	var next = $j("img.active").parent().parent().next().children();
	if(next.length==0){
		next=$j(".gallery_thumbs div a:first");
	};
	next.trigger('click');
};

prev = function() {
	var prev = $j("img.active").parent().parent().prev().children();
	if(prev.length==0){
		prev=$j(".gallery_thumbs div a:last");
	};
	prev.trigger('click');
};

$j(".gallery_viewer").click(function(e) {
	if ($j("img.active").parent().attr('href').match(/(jpg|jpeg|gif|png|pdf)$/i)){
	next();
}
});
$j(".gallery_nav .prev").click(function(e) {
	prev();
});
$j(".gallery_nav .next").click(function(e) {
	next();
});

$j(".gallery_thumbs div a:first").trigger('click');



	var start = $j('#rolloverImage').attr('src');
	$j('.menuRollover').hover(function() {
	if ($j(this).attr('hover')!="") $j('#rolloverImage').attr('src', $j(this).attr('hover'));
	}, function() {
    	$j('#rolloverImage').attr('src', start);
	});
});
