/*
 * jQuery spritely 0.2.1
 * http://spritely.net/
 *
 * Documentation:
 * http://spritely.net/documentation/
 *
 * Copyright 2010, Peter Chater, Artlogic Media Ltd, http://www.artlogic.net/
 * Dual licensed under the MIT or GPL Version 2 licenses.
 *
 * Change history:
 * Version 0.2.1
 *   - animate function will stop cycling after play_frames has completed
 * Version 0.2
 *   - added isDraggable method (requires jquery-ui) $('#sprite').sprite().isDraggable({start: null, stop: function() {alert('Ouch! You dropped me!')});
 *   - sprites may be set to play a limited number of frames when instantiated, e.g. $('#sprite').sprite({fps: 9, no_of_frames: 3, play_frames: 30})
 *   - sprite speed may be controlled at any point by setting the frames-per-second $('#sprite').fps(20);
 *   - sprites with multiple rows of frames may have there 'state' changed, e.g. to make the second row of frames
 *     active, use: $('#sprite').spState(2); - to return to the first row, use $('#sprite').spState(1);
 *   - background element speed may be controlled at any point with .spSpeed(), e.g. $('#bg1').spSpeed(10)
 *   - background elements may be set to a depth where 100 is the viewer (up close) and 0 is the horizon, e.g.:
 *     $('#bg1').pan({fps: 30, speed: 2, dir: 'left', depth: 30});
 *     $('#bg2').pan({fps: 30, speed: 3, dir: 'left', depth: 70});
 *     relative speed of backgrounds may now be set in a single action with $('#bg1, #bg2').spRelSpeed(20);
 *     which will make elements closer to the horizon (lower depths) move slower than closer elements (higher depths)
 */
(function(a){a._spritely={animate:function(b){var c=a(b.el),d=c.attr("id");b=a.extend(b,a._spritely.instances[d]||{});if(b.play_frames&&!a._spritely.instances[d].remaining_frames)a._spritely.instances[d].remaining_frames=b.play_frames+1;if(b.type=="sprite"&&b.fps){var e,g=function(i){var f=b.width;if(!e){e=[];for(var h=total=0;h<b.no_of_frames;h++){e[e.length]=0-total;total+=f}}if(a._spritely.instances[d].current_frame>=e.length-1)a._spritely.instances[d].current_frame=0;else a._spritely.instances[d].current_frame+=
1;f=a._spritely.getBgY(i);i.css("background-position",e[a._spritely.instances[d].current_frame]+"px "+f);if(b.bounce&&b.bounce[0]>0&&b.bounce[1]>0){f=b.bounce[0];h=b.bounce[1];var j=b.bounce[2];i.animate({top:"+="+f+"px",left:"-="+h+"px"},j).animate({top:"-="+f+"px",left:"+="+h+"px"},j)}};if(a._spritely.instances[d].remaining_frames&&a._spritely.instances[d].remaining_frames>0){a._spritely.instances[d].remaining_frames--;if(a._spritely.instances[d].remaining_frames==0){a._spritely.instances[d].remaining_frames=
-1;delete a._spritely.instances[d].remaining_frames;return}else g(c)}else a._spritely.instances[d].remaining_frames!=-1&&g(c)}else if(b.type=="pan")if(!a._spritely.instances[d]._stopped){a._spritely.instances[d].l=b.dir=="left"?a._spritely.instances[d].l-(b.speed||1)||0:a._spritely.instances[d].l+(b.speed||1)||0;g=a._spritely.getBgY(c);a(c).css("background-position",a._spritely.instances[d].l+"px "+g)}a._spritely.instances[d].options=b;window.setTimeout(function(){a._spritely.animate(b)},parseInt(1E3/
b.fps))},randomIntBetween:function(b,c){return parseInt(rand_no=Math.floor((c-(b-1))*Math.random())+b)},getBgY:function(b){return a.browser.msie?a(b).css("background-position-y")||"0":(a(b).css("background-position")||" ").split(" ")[1]},getBgX:function(b){return a.browser.msie?a(b).css("background-position-x")||"0":(a(b).css("background-position")||" ").split(" ")[0]}};a.fn.extend({spritely:function(b){b=a.extend({type:"sprite",do_once:false,width:null,height:null,fps:12,no_of_frames:2,stop_after:null},
b||{});var c=a(this).attr("id");if(!a._spritely.instances)a._spritely.instances={};a._spritely.instances[c]||(a._spritely.instances[c]={current_frame:-1});a._spritely.instances[c].type=b.type;a._spritely.instances[c].depth=b.depth;b.el=this;b.width=b.width||a(this).width()||100;b.height=b.height||a(this).height()||100;b.do_once?a._spritely.animate(b):window.setTimeout(function(){a._spritely.animate(b)},parseInt(1E3/b.fps));return this},sprite:function(b){b=a.extend({type:"sprite",bounce:[0,0,1E3]},
b||{});return a(this).spritely(b)},pan:function(b){b=a.extend({type:"pan",dir:"left",continuous:true,speed:1},b||{});return a(this).spritely(b)},flyToTap:function(b){b=a.extend({el_to_move:null,type:"moveToTap",ms:1E3,do_once:true},b||{});b.el_to_move&&a(b.el_to_move).active();if(a._spritely.activeSprite)if(window.Touch)a(this)[0].ontouchstart=function(c){var d=a._spritely.activeSprite,e=c.touches[0];c=e.pageY-d.height()/2;e=e.pageX-d.width()/2;d.animate({top:c+"px",left:e+"px"},1E3)};else a(this).click(function(c){var d=
a._spritely.activeSprite;a(d).stop(true);var e=d.width(),g=d.height();d.animate({top:c.pageY-g/2+"px",left:c.pageX-e/2+"px"},1E3)});return this},isDraggable:function(b){if(!a(this).draggable){alert("To use the isDraggable method you need to load jquery-ui.js");return this}b=a.extend({type:"isDraggable",start:null,stop:null,drag:null},b||{});var c=a(this).attr("id");a._spritely.instances[c].isDraggableOptions=b;a(this).draggable({start:function(){var d=a(this).attr("id");a._spritely.instances[d].stop_random=
true;a(this).stop(true);a._spritely.instances[d].isDraggableOptions.start&&a._spritely.instances[d].isDraggableOptions.start(this)},drag:b.drag,stop:function(){var d=a(this).attr("id");a._spritely.instances[d].stop_random=false;a._spritely.instances[d].isDraggableOptions.stop&&a._spritely.instances[d].isDraggableOptions.stop(this)}});return this},active:function(){a._spritely.activeSprite=this;return this},activeOnClick:function(){var b=a(this);if(window.Touch)b[0].ontouchstart=function(){a._spritely.activeSprite=
b};else b.click(function(){a._spritely.activeSprite=b});return this},spRandom:function(b){b=a.extend({top:50,left:50,right:290,bottom:320,speed:4E3,pause:0},b||{});var c=a(this).attr("id");if(!a._spritely.instances[c].stop_random){var d=a._spritely.randomIntBetween,e=d(b.top,b.bottom);d=d(b.left,b.right);a("#"+c).animate({top:e+"px",left:d+"px"},b.speed)}window.setTimeout(function(){a("#"+c).spRandom(b)},b.speed+b.pause);return this},makeAbsolute:function(){return this.each(function(){var b=a(this),
c=b.position();b.css({position:"absolute",marginLeft:0,marginTop:0,top:c.top,left:c.left}).remove().appendTo("body")})},spSet:function(b,c){var d=a(this).attr("id");a._spritely.instances[d][b]=c;return this},spGet:function(b){var c=a(this).attr("id");return a._spritely.instances[c][b]},spStop:function(b){a(this).each(function(){var c=a(this).attr("id");a._spritely.instances[c]._last_fps=a(this).spGet("fps");a._spritely.instances[c]._stopped=true;a._spritely.instances[c]._stopped_f1=b;a._spritely.instances[c].type==
"sprite"&&a(this).spSet("fps",0);if(b){c=a._spritely.getBgY(a(this));a(this).css("background-position","0 "+c)}});return this},spStart:function(){a(this).each(function(){var b=a(this).attr("id"),c=a._spritely.instances[b]._last_fps||12;a._spritely.instances[b]._stopped=false;a._spritely.instances[b].type=="sprite"&&a(this).spSet("fps",c)});return this},spToggle:function(){var b=a(this).attr("id"),c=a._spritely.instances[b]._stopped_f1||false;a._spritely.instances[b]._stopped?a(this).spStart():a(this).spStop(c);
return this},fps:function(b){a(this).each(function(){a(this).spSet("fps",b)});return this},spSpeed:function(b){a(this).each(function(){a(this).spSet("speed",b)});return this},spRelSpeed:function(b){a(this).each(function(){var c=a(this).spGet("depth")/100;a(this).spSet("speed",b*c)});return this},spChangeDir:function(b){a(this).each(function(){a(this).spSet("dir",b)});return this},spState:function(b){a(this).each(function(){var c=(b-1)*a(this).height()+"px";c=a._spritely.getBgX(a(this))+" -"+c;a(this).css("background-position",
c)});return this}})})(jQuery);try{document.execCommand("BackgroundImageCache",false,true)}catch(err){};
