﻿(function($) {
	// slider for full screeen
	$.fn.fullSlider = function(option) {
		var params = jQuery.extend({
			animation: 1350,
			random: true,
			interval: 5000
		}, option);

		var container = $(this)
			, items = container.find('li')
			, amount = items.size()
			, w = parseInt(items.outerWidth())
			, h = items.height()
			, bannerIndex = 0
			, beforeIndex = amount
			, num
			, direction
			, action = (params.interval > 0) ? true : false
			, motion = (params.animation == false) ? 0 : params.animation
		;

		// random image
		var random = (params.random) ? Math.floor(Math.random() * 2) : 0;
		if (random == 1) {
			items.find('span.side img').each(function() {
				var img = $(this);
				var src = img.attr('src');
				img.attr('src', src.replace("1_", "2_"));
			});
		}

		var $1st = container.find('li:first-child').clone()
			, $2nd = container.find('li:first-child').next().clone()
			, $last = container.find('li:last-child').clone()
		;
		$('.slides ul').append($1st).append($2nd).prepend($last);
		container.find('ul').css('width', (amount + 3) * w + 'px');
		container.find('li').each(function(n) {
			this.style.zIndex = (amount + 10) - n;
		});

		var $animationController = container.find('div.slides')
			, $mask = $('<div style="overflow:hidden;position:relative;height:' + h + 'px" />')
			, setbannerNav = $('<div class="bannerNav" />')
		;
		$animationController.wrap($mask);
		setbannerNav.prependTo(container);
		if (amount > 1) {
			for (i = 0; i < amount; i++) {
				$('<button type="button"><span class="blind">' + (i + 1) + '</span></button>').appendTo(setbannerNav);
			}
		}

		var $bannerNav = container.find('.bannerNav button');
		$bannerNav.each(function(n) {
			var $thisNum = $(this);
			function bannerShow() {
				setBanner(n, bannerIndex, motion);
				on($thisNum);
				action = false;
			}
			$thisNum.bind('click', bannerShow);
		});

		// centering
		var calibration
			, $win = $(window)
		;
		calibration = ($win.width() > 980) ? ($win.width() - 1750) / 2 : -300;
		$win.resize(function() {
			calibration = ($win.width() - 1780) / 2;
			$animationController.css({ 'marginLeft': '-' + (w * (bannerIndex + 1) - calibration) + 'px' });
		});

		function bannerMotion(to, from, ani) {
			bannerIndex = to;
			num = to + 1;
			if (from == amount - 1 && to == 0) {
				num = amount + 1;
			}
			if (from == 0 && to == amount - 1) {
				num = 0;
			}
			(from < to) ? direction = 'next' : direction = 'prev';
			$animationController.stop().animate({ 'marginLeft': '-' + (w * num - calibration) + 'px' }, ani, 'swing', function() {
				$animationController.css({ 'marginLeft': '-' + (w * (bannerIndex + 1) - calibration) + 'px' });
			});
			sideMotion(ani, bannerIndex, direction);
		}

		function sideMotion(ani, bannerIndex, direction) {
			var i = bannerIndex + 1;
			var n;
			if (i == amount) {
				n = 1;
			} else {
				n = i + 1;
			}
			// console.log(i + ', next:' + n + ', prev:');
			container.find('li.slide' + i).find('span.side img:odd').fadeOut();
			container.find('li.slide' + i).find('span.side img:even').fadeIn();
			container.find('li.slide' + n).find('span.side img:odd').fadeIn();
			container.find('li.slide' + n).find('span.side img:even').fadeOut();
		}

		function on(nObj) { nObj.addClass('on'); }
		function off(nObj) { nObj.removeClass('on'); }

		function setBanner(setup, from, ani) {
			$bannerNav.removeClass('on');
			on(setbannerNav.find('button:eq(' + setup + ')'));
			bannerMotion(setup, from, ani);
		}
		function nextBanner() {
			beforeIndex = bannerIndex;
			(bannerIndex >= (amount - 1)) ? bannerIndex = 0 : bannerIndex++;
			setBanner(bannerIndex, beforeIndex, motion);
		}
		function prevBanner() {
			beforeIndex = bannerIndex;
			(bannerIndex <= 0) ? bannerIndex = (amount - 1) : bannerIndex--;
			setBanner(bannerIndex, beforeIndex, motion);
		}
		setBanner(bannerIndex, amount - 1, 0);
		container.find('li.slide1').find('span.side img:odd').hide();

		container.find('button.next').click(nextBanner);
		container.find('button.prev').click(prevBanner);

		function autorun() {
			if (!action) return;
			nextBanner();
		}
		if (action) {
			setInterval(function() { autorun() }, params.interval);
			container.hover(function() { action = false; }, function() { action = true; });
		}
	}
})(jQuery);
