/*
 * The MIT License
 *
 * Copyright (c) 2008-2009 Olle Törnström 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.
 */

(function(b){var a={};b.fn.Slides=function(d){var c={};b.fn.Slides.setup(c,b.fn.Slides.defaults,d);var e=this;b.fn.Slides.init(this,function(){return e.each(function(){b(e).Slides.start()})})};b.fn.Slides.defaults={wait:0,pause:6000,fade:1000};b.fn.Slides.setup=function(c,e,d){a=b.extend({},c||{},e||{},d||{})};b.fn.Slides.init=function(d,f){if(typeof a.images==="undefined"){throw Error('Image array is not optional must be passed in the call $("#id").Slides({images : ["img1.jpg", "img2.jpg"]})')}if(typeof a.urls!="undefined"){if(a.urls.length!=a.images.length){throw Error("Urls length must match images length")}}if(typeof a.functions!="undefined"){if(a.functions.length!=a.images.length){throw Error("Functions length must match images length")}}a.main=b(d);a.pipes=new Object();a.pipes.urls=new Array();a.pipes.functions=new Array();var e=false;var c=function(){e=true;a.toggle=a.main.wrap("<span></span>").parent().css({display:"block",overflow:"hidden",height:a.main.height()+"px",width:a.main.width()+"px"});b.fn.Slides.preloadNextImage()};a.main.load(function(){if(e){return}c();f.call()});if(a.main[0].complete&&!e){c();f.call()}};b.fn.Slides.preloadNextImage=function(){var c=b.fn.Slides.getNextImage();var d=new Image();d.src=c;a.nextImage=d};b.fn.Slides.getNextImage=function(){var d=a.images.shift();a.images.push(d);if(a.urls){var c=a.urls.shift();a.urls.push(c);a.pipes.urls.push(c)}if(a.functions){var e=a.functions.shift();a.functions.push(e);a.pipes.functions.push(e)}return d};b.fn.Slides.start=function(){setTimeout(b.fn.Slides.execute,a.wait)};b.fn.Slides.execute=function(){var c=false;setInterval(function(){if(c){a.main.attr("src",a.nextImage.src).animate({opacity:1},a.fade);c=false}else{a.toggle.css({background:"transparent url("+a.nextImage.src+") left top no-repeat"});a.main.animate({opacity:0},a.fade);c=true}if(a.pipes.urls.length>0){a.main.click(function(){window.location.href=a.pipes.urls.shift()})}if(a.pipes.functions.length>0){a.pipes.functions.shift()()}b.fn.Slides.preloadNextImage()},a.pause)}})(jQuery);