/*
	Headlines - jQuery plugin
	
	Copyright (c) 2007, CSTV Networks, Inc.
	
	Dual licensed under the MIT and GPL licenses:
	http://www.opensource.org/licenses/mit-license.php
	http://www.gnu.org/licenses/gpl.html

	Author: Justin Sepulveda
	Date: 2007.05.21
	Version: 1.0
 */

(function($) {
	
	$.fn.picswitcher = function(initial, settings) {

		if (typeof initial == 'object') settings = initial;

		settings = $.extend({
		
			initial: (initial && typeof initial == 'number' && initial > 0) ? --initial : 0,
			selectedClass: '.selected',
			tabClass: '.tab',
			controlClass: '.control',
			panelClass: '.panel',
			photoClass: '.photo',
			backId: '#previous-pic',
			forwardId: '#next-pic',
			pauseId: '#pause-pic',
			playId: '#play-pic',
			controlsId: '#controls-picswitcher',
			navId: '#nav-picswitcher',
			fxShowSpeed: 'normal',
			fxHideSpeed: 'slow',
			fxFade: null,
			setHeight: true,
			setRotate: true,
			rotateTimer: 5000
			
		 }, settings || {});

		return this.each(function() {
			
			var container = this;

			var tab = settings.tabClass;
			var tabSet = $(tab, container);
			
			var panel = settings.panelClass;
			var panelSet = $(panel, container);
			var panelCount = panelSet.size();

			var control = settings.controlClass;
			var controlSet = $(control, container);
		
			var photo = settings.photoClass;
			var photoSet = $(photo, container);
		
			var pause = settings.pauseId.replace('#', '');
			var play = settings.playId.replace('#', '');
		
			var selected = settings.selectedClass.replace('.', '');
			
			var showFx = {};
			var hideFx = {};
			var showSpeed = settings.fxShowSpeed;
			var hideSpeed = settings.fxHideSpeed;

			panelSet.parent().css('position', 'relative');
			panelSet.css('position', 'absolute');
			panelSet.children().css('position', 'relative');
			photoSet.css('z-index', '1');
			photoSet.siblings().css('z-index', '2');

			$(tab).eq(settings.initial).addClass(selected);
			photoSet.show().siblings().css('visibility', 'visible');
			
			if (settings.setHeight) { setHeight(); }
			
			photoSet.not(':eq(' + settings.initial + ')').hide(1).siblings().css('visibility', 'hidden');

			if (settings.setRotate) { interval = setInterval(rotatePanel, settings.rotateTimer); }

			if (settings.fxFade) {
				showFx['opacity'] = 'show';
				hideFx['opacity'] = 'hide';
			} else {
				showFx['opacity'] = 'show';
				hideFx['opacity'] = 'hide';
				showSpeed = 1;
				hideSpeed = 1;
			}
		
			function setHeight() {
				var heights = $.map(panelSet.get(), function(el) {
					var h = $(el).height();
					return h;
				}).sort(function(a, b) {
					return b - a;
				});
				panelSet.parent().height(heights[0]);
			}
		
			function hidePanel() {
				var hideThis = photoSet.filter(':visible');
				hideThis.siblings().css('visibility', 'hidden');
				photoSet.filter(':visible').animate(hideFx, hideSpeed);
				$(tabSet).removeClass(selected);
			}
			
			function showPanel() {
				photoSet.eq(current).siblings().css('visibility', 'visible');
				photoSet.eq(current).animate(showFx, showSpeed);
				$(tabSet).eq(current).addClass(selected);
			}
			
			function rotatePanel() {
				var old = $(panelSet).index($(photoSet.filter(':visible').parent())[0]);
				current = (old + 1) % panelCount;
				hidePanel();
				showPanel();	
			}			
		
			tabSet.find('>a').click(function() {
				if (settings.setRotate) { clearInterval(interval); }
				if (photoSet.filter(':visible').length > 1 || $(this.parentNode).is('.' + selected)) { return false; }
				var clicked = $(this.hash);
				current = $(panelSet).index($(clicked)[0]);
				hidePanel();
				showPanel();
				$(settings.pauseId).removeAttr('id').attr('id', play);				
				return false;
			});
			
			controlSet.find('>a').click(function() {
				if (settings.setRotate) { clearInterval(interval); }
				if (photoSet.filter(':visible').length > 1) { return false; }
				var controlId =  '#' + $(this.parentNode).attr('id');
				var old = $(panelSet).index($(photoSet.filter(':visible').parent())[0]);
				switch(controlId) {
					case settings.backId:
						current = (old - 1) % panelCount;
						if (current == -1) { current = panelCount - 1; }
						hidePanel();
						showPanel();
						$(settings.pauseId).removeAttr('id').attr('id', play);
						break;
					case settings.forwardId:
						current = (old + 1) % panelCount;
						hidePanel();
						showPanel();
						$(settings.pauseId).removeAttr('id').attr('id', play);
						break;
					case settings.pauseId:
						clearInterval(interval);
					  	$(settings.pauseId).removeAttr('id').attr('id', play);
						break;						
					case settings.playId:    
						interval = setInterval(rotatePanel, settings.rotateTimer);
						$(settings.playId).removeAttr('id').attr('id', pause);
						break;
				}
				return false;		
			});
		});
	};
	
})(jQuery);

//THIS FUNC CALLED FROM PAGE TO WRITE OUT THE 1-2-3 NAV AND START THE ROTATION
function makeSlideShow(num_pics) {
	var str_pics = '<ul id="nav-picswitcher">';
	for (var i=1; i<=num_pics; i++) {
		str_pics += '<li class="tab"><a href="#pic' + i + '">' + i + '</a></li>';
	}
	str_pics += '</ul>';
	document.write(str_pics);
	jQuery(function($) {
		$("#picswitcher").picswitcher();
	});
}

