// JavaScript Document


$(function() {

	// IMAGES WITH ROLLOVERS
	$('.rollover').each(function() {
		var src = $(this).attr('src');
		if (src) {
			var over = src.replace(/(\.){1}(.{3,4})$/, "-over.$2");
			if ($(this).hasClass('active')) $(this).attr('src', over);
			else {
				var pic = new Image();
				pic.src = over; 
				$(this).hover( function() {
					$(this).attr('src', over);
				}, function() {
					$(this).stop(true, false).attr('src', src);
				});
			}
		}
	});
	
	// IF NOT IE, AND NOT 'class="rollover"'
	// ALL OTHER IMAGES NESTED INSIDE AN '<a>' TAG WILL FADE ON ROLLOVER
	if (navigator.appName != 'Microsoft Internet Explorer') {
		$("a img").each( function() {
			var img = $(this);
			if (!img.hasClass('rollover')) {
				img.parents('a').hover( function() {
					img.css({ opacity: 0.5 });
				}, function() {
					img.stop(true, false).css({ opacity: 1.0 });
				});
			}
		});
	}
	
	
	// INPUT TEXT WITH PLACEHOLDERS
	$('.placeholder').each( function() {
		var ph = $(this).addClass('faded').val();
		$(this).bind('focus', function() { if ($(this).val() == ph) $(this).val('').removeClass('faded'); });
		$(this).bind('blur', function() { if ($(this).val() == '') $(this).val(ph).addClass('faded'); });
	});
	
	
	
	//
	// IMAGE GALLERY
	//
		// ON TIMER MOVE TO NEXT IMAGE
		onGalleryTimer = function(gallery, action) {
			switch(action) {
				case 'next':
					// GET NEXT BUTTON INDEX
					var idx = $('.gallery-nav-btn span.active', gallery).text();
					if (idx == $(gallery).data('lgn')) idx = 0;
					
					// DEACTIVATE / ACTIVATE BUTTONS
					$('.gallery-nav-btn span.active', gallery).removeClass('active');
					$('.gallery-nav-btn:eq(' + idx + ') span', gallery).addClass('active');
					
					// SHOW BUTTON GROUP OF ACTIVE BUTTON
					if ( !$('.gallery-nav-btn span.active', gallery).parents('.gallery-btngroup:visible').length ) {
						$('.gallery-btngroup:visible', gallery).hide();
						$('.gallery-nav-btn span.active', gallery).parents('.gallery-btngroup').show();
					}
					
					// HIDE / SHOW IMAGES
					$('.gallery-image:visible', gallery).hide();
					$('.gallery-image:eq(' + idx + ')', gallery).show();
					break;
				
				case 'play':
					// ADD THE TIMER
					$('.gallery-nav-play', gallery).hide().next('div').show();
					$(gallery).data('timer', setInterval( function(e) { onGalleryTimer(gallery, 'next'); }, 2000) );
					break;
					
				case 'stop':
					// REMOVE THE TIMER
					$('.gallery-nav-stop', gallery).hide().prev('div').show();
					if (gallery.data('timer')) clearInterval( gallery.data('timer') );
					break;
			}
		};
	
		// INITIALIZE EACH GALLERY
		$('.gallery-cont').each( function() {
			
			var lgn = $('.gallery-nav-btn', this).length;
			if (lgn > 0) {
				$(this).data('lgn', lgn);
				
				// ON IMAGE CLICK - START GALLERY
				$('.gallery-image a', this).fancybox({
					titleFormat: function(title, currentArray, currentIndex, currentOpts) {
						// GET TITLE AND DESCRIPTION FROM FAKE ATTRIBUTE
						var html = '';
						html += '<div style="float:right"> &nbsp; ' + (currentIndex+1) + ' / ' + currentArray.length + '</div>';
						html += '<p align="left" style="margin:0">' + $(currentArray[currentIndex]).attr('fancydescr') + '</p>';
						return html;
					},
					titlePosition: 'inside'
				});
				
				// GALLERY BUTTON
				$('.gallery-nav-btn', this).mouseover(function() {
					// IF NOT ACTIVE
					if ($('span.active', this).length == 0) {
						// GET CURRENT BUTTON INDEX AND TARGET GALLERY
						var idx = ($('span', this).text() - 1);
						var gallery = $(this).parents('.gallery-cont');
						
						// DEACTIVATE / ACTIVATE BUTTONS
						$('.gallery-nav-btn span.active', gallery).removeClass('active');
						$('span', this).addClass('active');
						
						// HIDE / SHOW IMAGES
						$('.gallery-image:visible', gallery).hide();
						$('.gallery-image:eq(' + idx + ')', gallery).show();
						
						// REMOVE TIMER
						onGalleryTimer(gallery, 'stop');
					}
				});
				
				// PREV BUTTON
				$('.gallery-nav-prev', this).click(function() {
					// TARGET GALLERY AND SHOW PREV BUTTON GROUP
					var gallery = $(this).parents('.gallery-cont');
					$('.gallery-btngroup:visible', gallery).hide().prev('.gallery-btngroup').show();
					
					// REMOVE TIMER
					onGalleryTimer(gallery, 'stop');
				});
				
				// NEXT BUTTON
				$('.gallery-nav-next', this).click(function() {
					// TARGET GALLERY AND SHOW NEXT BUTTON GROUP
					var gallery = $(this).parents('.gallery-cont');
					$('.gallery-btngroup:visible', gallery).hide().next('.gallery-btngroup').show();
					
					// REMOVE TIMER
					onGalleryTimer(gallery, 'stop');
				});
				
				// PLAY BUTTON
				$('.gallery-nav-play', this).click(function() {
					// ADD TIMER
					var gallery = $(this).parents('.gallery-cont');
					onGalleryTimer(gallery, 'play');
				});
				
				// STOP BUTTON
				$('.gallery-nav-stop', this).click(function() {
					// REMOVE TIMER
					var gallery = $(this).parents('.gallery-cont');
					onGalleryTimer(gallery, 'stop');
				});
				
				// ADD TIMER
				var gallery = this;
				onGalleryTimer(gallery, 'play');
			}
		});
	// end image gallery
	
	
});

