/*
 * SweetPresentation - jQuery Plugin
 * Simple changing block function
 *
 * Examples and documentation at: http://chelnynet.info/
 * 
 * Copyright (c) 2011 Dmitry Hrunov
 * 
 * Version: 1.1b (13/11/2011)
 * Requires: jQuery v1.4+
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */

jQuery.fn.SweetPresentation = function(options){
	var options = jQuery.extend({
		swithInterval: 10,
		additionalNav: false
		},options);
	return this.each(function() {
		var swithInterval = options.swithInterval*1000;
		var SwitchBlockClassName = $(this).attr('class');
		var SwitchBlockSw = false;
		var SwitchBlockProcess = false;
		var SwitchBlockHeight = $('.'+SwitchBlockClassName).height();
		var SwitchBlockNum = $('.'+SwitchBlockClassName+' ul li').length;
		var SwitchBlockPos = 0;
		var BlockItems = Array();
		var HiddenBlockName = '';
		for(i = 0; i < SwitchBlockNum; i++) {
			BlockItems[i] = $('.'+SwitchBlockClassName+' ul li:eq('+i+')').html();
			}
		$('.'+SwitchBlockClassName+' ul').remove();
		$('.'+SwitchBlockClassName).append('<div class="PresentationOne"></div><div class="PresentationTwo"></div>');
		$('.'+SwitchBlockClassName+' div.PresentationTwo').css('top', '-'+SwitchBlockHeight+'px');
		$('.'+SwitchBlockClassName+' div.PresentationTwo').html(BlockItems[SwitchBlockPos]);
		if (options.additionalNav == true) {
			var NavBlockItems = '';
			for(i = 0; i < SwitchBlockNum; i++) {
					NavBlockItems += "<em rel='"+i+"' class='pngfix'></em>";
				}
			$('.'+SwitchBlockClassName+' div.NavButtons').append('<div class="NavCounter">'+NavBlockItems+'</div>');
			navSwitch(0);
			}

		$('.'+SwitchBlockClassName).hover(
			function () { SwitchBlockSw = true; },
			function () { SwitchBlockSw = false; }
			);

		function Swither() {
			SwitchBlockProcess = true;
			HiddenBlockName = ($('.'+SwitchBlockClassName+' div.PresentationTwo').css('display') == 'none') ? 'PresentationTwo' : 'PresentationOne';
			$('.'+SwitchBlockClassName+' div.'+HiddenBlockName).html(BlockItems[SwitchBlockPos]);
			if (HiddenBlockName == 'PresentationOne') {
				$('.'+SwitchBlockClassName+' div.PresentationTwo').fadeOut('slow', function() {
					SwitchBlockProcess = false;
					});// end of fadein
				}
			if (HiddenBlockName == 'PresentationTwo') {
				$('.'+SwitchBlockClassName+' div.PresentationTwo').fadeIn('slow', function() {
					SwitchBlockProcess = false;
					});// end of fadeout
				}
			if (options.additionalNav == true) {
				navSwitch(SwitchBlockPos);
				}
			}

		function SwithBlock() {
			if (SwitchBlockSw == false) {
				var test1 = SwitchBlockPos;
				SwitchBlockPos = (SwitchBlockPos < SwitchBlockNum-1) ? SwitchBlockPos+1 : 0;
				Swither();
				}
			}

		function goSwitch(direction) {
			if(SwitchBlockProcess == false) {
				if (direction == 'Forward') {
					SwitchBlockPos = (SwitchBlockPos+1 < SwitchBlockNum) ? SwitchBlockPos+1 : 0;
					}
				else {
					SwitchBlockPos = (SwitchBlockPos-1 < 0) ? SwitchBlockNum-1 : SwitchBlockPos-1;
					}
				Swither();
				}
			}

		function navSwitch(num) {
			$('.NavCounter em:eq('+num+')').addClass('active').siblings().removeClass('active');
			}

			$('.'+SwitchBlockClassName+' div.NavCounter em').click(function () {
				SwitchBlockPos = parseInt($(this).attr('rel'));
				Swither();
				});

			$('.'+SwitchBlockClassName+' span.Forward').click(function () {
				goSwitch('Forward');
				});

			$('.'+SwitchBlockClassName+' span.Backward').click(function () {
				goSwitch();
				});

		$('.'+SwitchBlockClassName).mousewheel(function(event, delta) { 
			if (delta > 0) {
				goSwitch();
				}
			else if (delta < 0) {
				goSwitch('Forward'); 
				}
			event.preventDefault();
			});

		setInterval(SwithBlock, swithInterval);
		});
	};
