/*
 *
 * FlickDragClick: jQuery /jQuery UI Plugin
 * http:// pluginpage
 * 
 * Totally awesome - iOS compatible - automatic full screen slider for lazy non-webbernet people 
 * (every single one of them I love because the world is a glitter-drenched lollypop, HAIL SLAYER)
 * 
 * Copyright (c) 2011 Wannes Van der Weyden (http://wa.nnnnn.es/)
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * 
 * Version 0.5
 * 
 *	 - needs 3 images to work (also in non loop?)
 *
 *	 - w + h in resize listeners not the window but the root width, height
 *   - add mousewheel in lib
 *	 - make element width instead of window width? more modular
 *	 - cursors
 *	 - easing animation based on swipe / drag speed
 *	 - return false; on all <a> to prevent next/prev ?
 *
 */

function alive(a){return a.length>0}var glitter,gold=1.618;(function($){function onKey(a){if(!lock()){switch(a.keyCode){case 37:prev();break;case 39:next();break}}}function onDragTouchEnd(a){if(!lock()){var b=parseFloat(control.css("left"));if(Math.abs(b)<w*d.swipeDistance){reset()}else{b<0?next():prev()}}}function onClick(a){if(!lock())x>w/2?next():prev()}function onTouchMove(a){a.preventDefault();if(!lock(true)){var b=a.touches[0].pageX-x;if(b<x1){b=x1}else if(b>x2){b=x2}control.css("left",b);lock(false)}}function onTouchStart(a){if(!lock())x=a.touches[0].pageX}function onMove(a){if(!lock())x=event.clientX}function setView(a){a=typeof a=="object"?true:false;var b="newView";if(a||!w){b=!w?"init":"resize";w=$(window).width();h=$(window).height();var c={width:w,height:h};control.css(c);list.css(c)}if(!a||!w){list.not(s.all).hide();s.all.show()}s.prev.css("left",-w);s.show.css("left",0);s.next.css("left",w);control.css("left",0);x1=alive(s.next)?-w:0,x2=alive(s.prev)?w:0;control.draggable("option","containment",[x1,0,x2,0]);triggerListener(b,[w,h]);if(!a)lock(false)}function getRandom(a){var b=!a?list:list.not(a);return b[Math.floor(Math.random()*b.length)]}function getNext(a){var b=a.next();if(!alive(b)&&d.loop)b=a.parent().children().first();return b}function getPrev(a){var b=a.prev();if(!alive(b)&&d.loop)b=a.parent().children().last();return b}function updateSlides(a){switch(a){case true:s.prev.removeClass("prev");s.prev=s.show.removeClass("show").addClass("prev");s.show=s.next.removeClass("next").addClass("show");s.next=(!d.random?$(getNext(s.show)):$(getRandom(".prev, .show"))).addClass("next");break;case false:s.next.removeClass("next");s.next=s.show.removeClass("show").addClass("next");s.show=s.prev.removeClass("prev").addClass("show");s.prev=(!d.random?$(getPrev(s.show)):$(getRandom(".next, .show"))).addClass("prev");break;default:s.show=(!d.random?$(list[0]):$(getRandom())).addClass("show");s.prev=(!d.random?$(getPrev(s.show)):$(getRandom(".show"))).addClass("prev");s.next=(!d.random?$(getNext(s.show)):$(getRandom(".show, .prev"))).addClass("next")}s.all=$(".prev, .show, .next");setView()}function animate(a,b){if(!lock(true,b)){var c=a?d.slideSpeed:d.resetSpeed,e=a<0?true:false,f=a?function(){updateSlides(e)}:function(){lock(false)},a=a?a:0;control.animate({left:a},c,f)}}function goto(a){s.next.removeClass("next").hide();s.next=$("#slide-"+a).addClass("next").show().css("left",w);next()}function reset(){if(!lock())animate()}function prev(){if(!lock()&&alive(s.prev))animate(w)}function next(a){if(!lock()&&alive(s.next))animate(-w,a)}function autoplayControl(){if(autoplay){next(true)}clearTimeout(autoplay);var a=autoplay?d.autoplayInterval:d.autoplayInitWait;autoplay=setTimeout(autoplayControl,a)}function lock(a,b){if(a==undefined)return locked;if(a!=locked){locked=!locked;control.draggable("disabled",locked);if(!b&&locked){clearTimeout(autoplay);if(d.autoplay)autoplay=setTimeout(autoplayControl,d.autoplayWaitAfterUse)}return!locked}else{return locked}}function triggerListener(l,p){l=eval("e."+l);if(!l)return false;var i=0;while(i<l.length){l[i].apply(this,p);i++}}function addListener(l,f){var t=eval("e."+l);if(t!=undefined){t.push(f)}}function init(a){if(!this)return false;if(a)$.extend(defaults,a);root=this.append('<div id="controller" style="position:absolute;"></div>');control=$("#controller").mousemove(onMove).click(onClick).draggable({axis:"x",stop:onDragTouchEnd});list=root.children(".slide").appendTo(control);$(window).focus().keyup(onKey).resize(setView);try{document.addEventListener("touchstart",onTouchStart,false);document.addEventListener("touchmove",onTouchMove,false);document.addEventListener("touchend",onDragTouchEnd,false)}catch(b){touch=false}root.css({position:"relative",overflow:"hidden"});list.css({position:"absolute",overflow:"hidden",top:0});if(d.autoplay)autoplayControl();updateSlides()}$.fn.flickDragClick=function(a){if(methods[a]){return methods[a].apply(this,Array.prototype.slice.call(arguments,1))}else if(typeof a==="object"||!a){return methods.init.apply(this,arguments)}};var methods={init:init,next:next,prev:prev,"goto":goto,addListener:addListener},defaults={random:false,loop:false,slideSpeed:300,resetSpeed:200,swipeDistance:.4,autoplay:false,autoplayAfterUse:false,autoplayInterval:4e3,autoplayInitWait:6e3,autoplayWaitAfterUse:1e4},events={init:[],newView:[],resize:[]},root,control,list,autoplay,w,h,x,x1,x2,d=defaults,e=events,s={},locked=true,touch=true})(jQuery)
