/*
 * The MIT License
 * 
 * Copyright (c) 2008-2009 Olle Tornstrom studiomediatech.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
 * Very simple automatic image slideshow that degrades nice and is completely
 * unobtrusive. Simple to use, just apply to an image element by some selector:
 *
 *	<img src="start-image.jpg" alt="" id="my-image" />
 *
 *	$('#my-image').Slides({images : ['image1.jpg', 'image2.jpg']});
 *
 * This will create a slideshow that rotates over the images passed in the
 * array. You can also pass some optional settings for a bit of customization:
 *
 *	$('#my-image').Slides({
 *		  images : [...], // required image URL array
 *			wait : 2000,  // wait before start, default 0
 *		   pause : 5000,  // pause on each image, default 6000
 *			fade : 3000   // time to fade, default 1000
 *	});
 *
 * Passing these settings will create a slideshow that first waits 2s before
 * starting the slideshow timer. Each image is then displayed 5s and faded with
 * a transition time of 3s.
 *
 * @author  Olle Tornstrom olle[at]studiomediatech[dot]com
 * @author  Emil Bengtsson emil0r[at]gmail[dot]com
 * 
 * @since   2009-01-15
 * @version 1.3.0-ALPHA
 */
;(function ($) {

	var settings = {};

	$.fn.Slides = function (options) {
		
		var that = this;
		var id = $(this).attr('id');
		
		settings[id] = $.extend({}, $.fn.Slides.defaults, options || {});
		
		$.fn.Slides.init(that, settings[id], function() {
			return that.each(function () {
				$.fn.Slides.start(id);
			});
		});
	};

	$.fn.Slides.defaults = {
		wait : 0,
		pause : 3000,
		fade : 1000
	};

	$.fn.Slides.init = function (target, settings, callback) {

		if (typeof settings.images === 'undefined')
			throw Error('Image array is not optional must be passed in the call $("#id").Slides({images : ["img1.jpg", "img2.jpg"]})');
			
		if (typeof settings.urls != 'undefined' && (settings.urls.length != settings.images.length))
			throw Error('Urls length must match images length');
		
		if (typeof settings.functions != 'undefined' && (settings.functions.length != settings.images.length))
			throw Error('Functions length must match images length');

		settings = $.extend(settings, {
			main : $(target),
			pipes : {urls : [], functions : []},
			initialized : false
		});
		
		var initWrapper = function () {
			settings.toggle = settings.main.wrap('<span></span>')
					.parent()
					.css({
							display : 'block',
							overflow : 'hidden',
							height : settings.main.height() + 'px',
							width : settings.main.width() + 'px'
					});
			$.fn.Slides.preloadNextImage(settings);
			settings.initialized = true;
		};
		
		settings.main.load(function () {
			if (settings.initialized)
				return;
			initWrapper();
			callback();
		});

		if (settings.main[0].complete && !settings.initialized) {
			initWrapper();
			callback();
		}
	};

	$.fn.Slides.preloadNextImage = function (settings) {
		var nextImage = $.fn.Slides.getNextImage(settings);
		var image = new Image();
		image.src = nextImage;
		settings.nextImage = image;
	};

	$.fn.Slides.getNextImage = function (settings) {
		var nextImage = settings.images.shift();
		settings.images.push(nextImage);

		if (settings.urls) {
			var url = settings.urls.shift();
			settings.urls.push(url);
			settings.pipes.urls.push(url);
		}
		
		if (settings.functions) {
			var func = settings.functions.shift();
			settings.functions.push(func);
			settings.pipes.functions.push(func);
		}
		
		return nextImage;
	};

	$.fn.Slides.start = function (id) {
		setTimeout(function () {
			//$.fn.Slides.execute(id);
			$.fn.Slides.excuteRandTime(settings[id]);
		}, settings[id].wait);	
	};

	$.fn.Slides.execute = function (id) {

		var _settings = settings[id];
		_settings.isToggle = false;

		setInterval(function () {
			
			if (_settings.isToggle) {
				_settings.main.attr('src', _settings.nextImage.src).animate({opacity : 1}, _settings.fade);
				_settings.isToggle = false;
			} else {
				_settings.toggle.css({background : 'transparent url(' + _settings.nextImage.src + ') left top no-repeat'});
				_settings.main.animate({opacity : 0}, _settings.fade);
				_settings.isToggle = true;
			}
			
			if (_settings.pipes.urls.length > 0) {
				_settings.main.click(function () {
					window.location.href = _settings.pipes.urls.shift();
				});
			}
			
			if (_settings.pipes.functions.length > 0)
				_settings.pipes.functions.shift()();
				
			$.fn.Slides.preloadNextImage(_settings);
			
		}, _settings.pause);
	};
	
	var aryOldWaitTime = [];
	$.fn.Slides.excuteRandTime = function (_settings) {

		if(_settings.index == 0){
			$.fn.Slides.ShuffleWaittime();
		}
		var waitTime = _settings.pause - (aryOldWaitTime[_settings.index] ? aryOldWaitTime[_settings.index] : 0) + aryWaitTime[_settings.index];
		aryOldWaitTime[_settings.index] = aryWaitTime[_settings.index];

		setTimeout(function () {
			if (_settings.isToggle) {
				_settings.main.attr('src', _settings.nextImage.src).animate({opacity : 1}, _settings.fade);
				_settings.isToggle = false;
			} else {
				_settings.toggle.css({background : 'transparent url(' + _settings.nextImage.src + ') left top no-repeat'});
				_settings.main.animate({opacity : 0}, _settings.fade);
				_settings.isToggle = true;
			}
			
			if (_settings.pipes.urls.length > 0) {
				_settings.main.click(function () {
					window.location.href = _settings.pipes.urls.shift();
				});
			}
			
			if (_settings.pipes.functions.length > 0)
				_settings.pipes.functions.shift()();
				
			$.fn.Slides.preloadNextImage(_settings);
			
			$.fn.Slides.excuteRandTime(_settings);
		}, waitTime);
	};

	var aryWaitTime = [0, 500, 1000, 1500];
	$.fn.Slides.ShuffleWaittime = function(){
		for(var i=0; i<aryWaitTime.length; i++){
			var j = Math.floor(Math.random()*(aryWaitTime.length-1));
			var t = aryWaitTime[i];
			aryWaitTime[i] = aryWaitTime[j];
			aryWaitTime[j] = t;
		}
	}

})(jQuery);


$(function() {

$('#slideshow01').Slides({images : [
	'img/frm01_001.jpg',
	'img/frm01_002.jpg',
	'img/frm01_003.jpg',
	'img/frm01_004.jpg',
	'img/frm01_005.jpg',
	'img/frm01_006.jpg',
	'img/frm01_007.jpg',
	'img/frm01_008.jpg'
],index: 0});
$('#slideshow02').Slides({images : [
	'img/frm02_001.jpg',
	'img/frm02_002.jpg',
	'img/frm02_003.jpg',
	'img/frm02_004.jpg',
	'img/frm02_005.jpg',
	'img/frm02_006.jpg',
	'img/frm02_007.jpg',
	'img/frm02_008.jpg'
],index: 1});
$('#slideshow03').Slides({images : [
	'img/frm03_001.jpg',
	'img/frm03_002.jpg',
	'img/frm03_003.jpg',
	'img/frm03_004.jpg',
	'img/frm03_005.jpg',
	'img/frm03_006.jpg',
	'img/frm03_007.jpg',
	'img/frm03_008.jpg',
	'img/frm03_009.jpg'
],index: 2});
$('#slideshow04').Slides({images : [
	'img/frm04_001.jpg',
	'img/frm04_002.jpg',
	'img/frm04_003.jpg',
	'img/frm04_004.jpg',
	'img/frm04_005.jpg',
	'img/frm04_006.jpg',
	'img/frm04_007.jpg',
	'img/frm04_008.jpg',
	'img/frm04_009.jpg'
],index: 3});

});

