/**
 * jQuery (a)Slideshow plugin
 *
 * Copyright (c) 2009 Anton Shevchuk
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * @author 	Anton Shevchuk AntonShevchuk@gmail.com
 * @version 0.7.1
 */
;(function($){defaults={index:0,history:false,title:false,titleshow:false,callback:null,panel:false,play:false,loop:true,filter:true,nextclick:false,playclick:false,playhover:false,playhoverr:false,playframe:false,loadframe:false,fullscreen:false,imgresize:false,imgzoom:false,imgcenter:true,imgajax:false,imglink:false,linkajax:false,help:'Plugin homepage: <a href="http://slideshow.hohli.com">(a)Slideshow</a><br />'+'Author homepage: <a href="http://anton.shevchuk.name">Anton Shevchuk</a>',controls:{'hide':true,'first':true,'prev':true,'play':true,'next':true,'last':true,'help':true,'counter':true}};$.fn.slideshow=function(p){var q=this;this.each(function(){var o=$(this);this.playId=null;this.playFlag=false;this.playFrame=false;this.goToFlag=false;this.length=0;this.inited=new Array();this.titles=new Array();this.build=function(){var a=this;o.wrapInner('<div class="aslideshow"><div class="aslideshow-content"></div></div>');o=o.find('.aslideshow');if(this.options.filter){o.find('.aslideshow-content > br').remove();o.find('.aslideshow-content > p:empty').remove();o.find('.aslideshow-content > div:empty').remove()}if(this.options.fullscreen){$('body').css({overflow:'hidden',padding:0});this.options.width=$(window).width();this.options.height=($(window).height()>$(document).height())?$(window).height():$(document).height();o.addClass('slideshow-fullscreen')}this.length=o.find('.aslideshow-content > *').length;if(this.options.title){o.prepend('<div class="aslideshow-label-place"><div class="aslideshow-label aslideshow-opacity"></div></div>');if(!this.options.titleshow){o.find('.aslideshow-label-place').hover(function(){$(this).find('.aslideshow-label').fadeIn()},function(){$(this).find('.aslideshow-label').fadeOut()});o.find('.aslideshow-label').hide()}o.find('.aslideshow-label-place').css('width',this.options.width)}if(this.options.panel){o.append('<div class="aslideshow-panel-place"><div class="aslideshow-panel aslideshow-opacity"></div></div>');panel=o.find('.aslideshow-panel');if(this.options.controls.first)panel.append('<a class="first button" href="#first">First</a>');if(this.options.controls.prev)panel.append('<a class="prev button"  href="#prev">Prev</a>');if(this.options.controls.play)panel.append('<a class="play button"  href="#play">Play</a>');if(this.options.controls.next)panel.append('<a class="next button"  href="#next">Next</a>');if(this.options.controls.last)panel.append('<a class="last button"  href="#last">Last</a>');if(this.options.controls.help){panel.append('<a class="help button"  href="#help">Help</a>');panel.prepend('<div class="aslideshow-help">'+this.options.help+'</div>')}if(this.options.controls.counter){panel.append('<span class="counter">'+(this.options.index+1)+' / '+this.length+'</span>')}if(this.options.controls.hide){o.find('.aslideshow-panel-place').hover(function(){$(this).find('.aslideshow-panel').fadeIn()},function(){$(this).find('.aslideshow-panel').fadeOut()});panel.hide()}o.find('.aslideshow-panel-place').css('width',this.options.width)}o.css({width:this.options.width,height:this.options.height});var b=o.find('.aslideshow-content');b.css({width:this.options.width,height:this.options.height});if(this.options.playframe){this.playFrame=true;o.append('<div class="aslideshow-shadow aslideshow-opacity aslideshow-frame"><div></div></div>')}if(this.options.loadframe){o.append('<div class="aslideshow-shadow aslideshow-opacity aslideshow-load"><div></div></div>')}o.find('.aslideshow-shadow').css({width:this.options.width,height:this.options.height});this.events();o.find('.aslideshow-content > *').each(function(){$(this).wrap('<div class="aslideshow-slide"></div>')});if(this.options.play){this.play()}this.init(this.options.index);o.find('.aslideshow-slide:eq('+this.options.index+')').show();this._label();if(this.options.history){setInterval(function(){a._check()},300)}return true};this.init=function(b){for(var i=0,loopCnt=this.inited.length;i<loopCnt;i++){if(this.inited[i]===b){return true}}this.inited.push(b);slide=o.find('.aslideshow-slide:eq('+b+')');var c=this;var d='';var e=false;var f=slide.contents().attr('name');if(f!=''){var g=new RegExp("^((https?|ftp):\/\/)","i");if(g.test(f)){e=f}}if(slide.contents().is('a')){var h=slide.contents().attr('href');var j=document.domain;j=j.replace(/\./i,"\.");var k=new RegExp("\.(png|gif|jpg|jpeg|svg)$","i");var l=new RegExp("^((https?:\/\/"+j+")|(?!http:\/\/))","i");d=slide.contents().attr('title');if(d.length==0)d=slide.contents().html();d=d.replace(/\"/i,'\'');if(this.options.imgajax&&k.test(h)){var m=new Image();m.alt=d;this._load($(m),h,b);slide.contents().replaceWith(m)}else if(this.options.linkajax&&l.test(h)){$.get(h,function(a){c.goToSlide(b);slide.contents().replaceWith('<div>'+a+'</div>')})}else{this.goToSlide(b)}}else{if(slide.contents().is("img")){if($.browser.msie){var m=new Image();m.alt=slide.contents().attr("alt");this._load($(m),slide.contents().attr("src"),b);slide.contents().replaceWith(m)}else{this._load(slide.contents(),slide.contents().attr("src"),b)}}else{this.goToSlide(b)}if(slide.contents().attr('alt')){d=slide.contents().attr('alt')}else if(slide.contents().attr('title')){d=slide.contents().attr('title')}else if(slide.find('label:first').length>0){slide.find('label:first').hide();d=slide.find('label:first').html()}}if(e)d='<a href="'+e+'" title="'+d+'">'+d+'</a>';this.titles[b]=d;if(this.options.imglink&&e){$(slide).css({cursor:'pointer'}).click(function(){document.location=e;return false})}if(this.options.playclick)$(slide).css({cursor:'pointer'}).click(function(){if(c.playId){c.stop()}else{c.play()}return false});return false};this._load=function(a,b,c){var d=o.find('.aslideshow-load').show();var e=this;a.load(function(){e._zoom(a);e._resize(a);e._center(a);e.goToSlide(c);d.hide()}).error(function(){d.hide()}).attr('src',b);if(a.get(0).complete){e._zoom(a);e._resize(a);e._center(a);e.goToSlide(c);d.hide()}return a};this._resize=function(a){if(!this.options.imgresize&&!this.options.fullscreen)return false;a.get(0).width=this.options.width;a.get(0).height=this.options.height;a.css({width:this.options.width,height:this.options.height});return a};this._zoom=function(a){if(!this.options.imgzoom)return false;var b=a.get(0).width;var c=a.get(0).height;var d=this.options.width/b;var e=this.options.height/c;var K=(e>d)?e:d;b=b*K;c=c*K;a.css({width:b,height:c});a.get(0).width=b;a.get(0).height=c;return a};this._center=function(a){if(!this.options.imgcenter)return false;var b=a.get(0).width?a.get(0).width:a.get(0).offsetWidth;var c=a.get(0).height?a.get(0).height:a.get(0).offsetHeight;var d=0;var e=0;if(b!=this.options.width){d=(Math.ceil((this.options.width-b)/2))+'px'}if(c!=this.options.height){e=(Math.ceil((this.options.height-c)/2))+'px'}a.css({left:d,top:e,position:'relative'});return a};this.events=function(){var a=this;if(a.options.nextclick)o.find('.aslideshow-content').click(function(){a.stop();a.next();return false});if(this.options.controls.first)o.find('a.first').click(function(){a.stop();a.goToSlide(0);return false});if(this.options.controls.prev)o.find('a.prev').click(function(){a.stop();a.prev();return false});if(this.options.controls.play)o.find('a.play').click(function(){if(a.playId){a.stop()}else{a.play()}return false});if(this.options.controls.next)o.find('a.next').click(function(){a.stop();a.next();return false});if(this.options.controls.last)o.find('a.last').click(function(){a.stop();a.goToSlide(a.length-1);return false});if(this.options.controls.help)o.find('a.help').click(function(){a.stop();o.find('.aslideshow-help').slideToggle();return false});if(this.options.playframe)o.find('.aslideshow-frame').click(function(){o.find('.aslideshow-frame').remove();if(a.options.playclick)setTimeout(function(){a.play()},a.options.time);return false});if(this.options.playhover)o.hover(function(){if(!a.playId){a.play()}},function(){if(a.playId){a.stop()}});if(this.options.playhoverr)o.hover(function(){if(a.playId){a.stop()}},function(){if(!a.playId){a.play()}})};this._label=function(){var a=this.getTitle();if(this.options.callback){this.options.callback(this.options.index,a)}if(!this.options.title)return false;o.find('.aslideshow-label').html(a)};this._hash=function(){if(this.options.history){document.location.hash='slide-'+(this.options.index+1)}};this._check=function(){if(this.goToFlag){return false}var a=document.location.hash;a=a.length?a.substr(1):'';if(a.length==0){this.goToSlide(0)}else{var b=new RegExp('slide-([0-9]+)','i');if(!b.test(a)){return false}var c=b.exec(a);if(c){c=parseInt(c[1])-1;if(c>=0&&c<this.length&&c!=this.options.index){if(this.playFrame){$(this).find('.aslideshow-frame').remove()}this.stop();this.goToSlide(c)}}}};this.getTitle=function(){return this.titles[this.options.index]};this.prev=function(){if(this.options.index==0){i=(this.length-1)}else{i=this.options.index-1}this.goToSlide(i)};this.play=function(){var a=this;this.playFlag=true;this.playId=setTimeout(function(){a.next()},this.options.time);o.find('a.play').addClass('stop')};this._play=function(){var a=this;if(this.options.index==(this.length-1)){this.stop();if(this.options.loop){this.play()}return false}this.playId=setTimeout(function(){a.next()},this.options.time);return true};this.stop=function(){this.playFlag=false;o.find('a.play').removeClass('stop');clearTimeout(this.playId);this.playId=null};this.next=function(){if(this.options.index==(this.length-1)){i=0}else{i=this.options.index+1}this.goToSlide(i)};this.goToSlide=function(n){switch(true){case(this.options.index==n):case(!this.init(n,true)):return false;default:this.goToFlag=true;this._goToSlide(n);return true}};this._goToSlide=function(n){var a=o.find('.aslideshow-content > *:eq('+n+')');var b=o.find('.aslideshow-content > *:eq('+this.options.index+')');b.css({zIndex:0});a.css({zIndex:1,top:0,left:0,opacity:1,width:this.options.width,height:this.options.height});this.options.index=n;if(this.options.effect=='random'){var r=Math.random();r=Math.floor(r*12)}else{r=-1}switch(true){case(r==0||this.options.effect=='scrollDown'):b.css({width:'100%'});a.css({top:-this.options.height,height:-this.options.height});prevAni={};break;case(r==1||this.options.effect=='scrollUp'):b.css({width:'100%'});a.css({top:this.options.height,height:0});prevAni={};break;case(r==2||this.options.effect=='scrollLeft'):b.css({right:0,left:'',height:'100%'});a.css({right:'',left:'100%',height:'100%',width:'0%'});prevAni={};break;case(r==3||this.options.effect=='scrollRight'):b.css({right:'',left:0,height:'100%'});a.css({right:'100%',left:'',height:'100%',width:'100%'});prevAni={};break;case(r==4||this.options.effect=='growX'):a.css({zIndex:2,opacity:1,left:this.options.width/2,width:'0%',height:'100%'});prevAni={opacity:0.8};break;case(r==5||this.options.effect=='growY'):a.css({opacity:1,top:this.options.height/2,width:'100%',height:'0%'});prevAni={opacity:0.8};break;case(r==6||this.options.effect=='zoom'):a.css({width:0,height:0,top:this.options.height/2,left:this.options.width/2});prevAni={width:0,height:0,top:this.options.height/2,left:this.options.width/2};break;case(r==7||this.options.effect=='zoomFade'):a.css({zIndex:1,opacity:0,width:0,height:0,top:this.options.height/2,left:this.options.width/2});prevAni={opacity:0,width:0,height:0,top:this.options.height/2,left:this.options.width/2};break;case(r==8||this.options.effect=='zoomTL'):a.css({zIndex:1,opacity:0,width:this.options.width/2,height:this.options.height/2,top:0,left:0});prevAni={opacity:0,width:0,height:0,top:this.options.height,left:this.options.width};break;case(r==9||this.options.effect=='zoomBR'):a.css({zIndex:1,opacity:0,width:this.options.width/2,height:this.options.height/2,top:this.options.height/2,left:this.options.width/2});prevAni={opacity:0,width:0,height:0,top:0,left:0};break;case(r==10||this.options.effect=='fade'):default:b.css({zIndex:0,opacity:1});a.css({zIndex:1,opacity:0});prevAni={opacity:0};break}var c=this;b.animate(prevAni,this.options.effecttime);a.show().animate({top:0,left:0,opacity:1,width:this.options.width,height:this.options.height},this.options.effecttime,function(){b.hide();if(c.playFlag)c._play();c._label();c._counter();c._hash();c.goToFlag=false})};this._counter=function(){if(this.options.controls.counter)o.find('.aslideshow-panel span.counter').html((this.options.index+1)+' / '+this.length)};this.options=$.extend({},defaults,p);if(typeof(p)!='undefined'){if(typeof(p.controls)!='undefined')this.options.controls=$.extend({},defaults.controls,p.controls)}this.build();o.show();return o});q.playSlide=function(){q.each(function(){this.play()})};q.stopSlide=function(){q.each(function(){this.stop()})};q.nextSlide=function(){q.each(function(){this.next()})};q.prevSlide=function(){q.each(function(){this.prev()})};q.getTitle=function(){q.each(function(){this.getTitle()})};q.goToSlide=function(n){q.each(function(){this.goToSlide(n)})};return this}})(jQuery);