/*
 * jQuery spritely 0.3 beta
 * 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.3b
 *   - added lockTo method for locking sprites to background images. $('#sprite').lockTo('#background, {'left': 380, 'top': -60, 'bg_img_width': 1110});
 * Version 0.2.1
 *   - animate function will stop cycling after play_frames has completed
 * Version 0.2 beta
 *   - 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(e){var f=a(e.el);var b=f.attr("id");e=a.extend(e,a._spritely.instances[b]||{});if(e.play_frames&&!a._spritely.instances[b]["remaining_frames"]){a._spritely.instances[b]["remaining_frames"]=e.play_frames+1}if(e.type=="sprite"&&e.fps){var g;var d=function(p){var k=e.width,o=e.height;if(!g){g=[];total=0;for(var n=0;n<e.no_of_frames;n++){g[g.length]=(0-total);total+=k}}if(a._spritely.instances[b]["current_frame"]>=g.length-1){a._spritely.instances[b]["current_frame"]=0}else{a._spritely.instances[b]["current_frame"]=a._spritely.instances[b]["current_frame"]+1}var q=a._spritely.getBgY(p);p.css("background-position",g[a._spritely.instances[b]["current_frame"]]+"px "+q);if(e.bounce&&e.bounce[0]>0&&e.bounce[1]>0){var j=e.bounce[0];var l=e.bounce[1];var m=e.bounce[2];p.animate({top:"+="+j+"px",left:"-="+l+"px"},m,e.easing,null,e.slot).animate({top:"-="+j+"px",left:"+="+l+"px"},m,e.easing,null,e.slot)}};if(a._spritely.instances[b]["remaining_frames"]&&a._spritely.instances[b]["remaining_frames"]>0){a._spritely.instances[b]["remaining_frames"]--;if(a._spritely.instances[b]["remaining_frames"]==0){a._spritely.instances[b]["remaining_frames"]=-1;delete a._spritely.instances[b]["remaining_frames"];return}else{d(f)}}else{if(a._spritely.instances[b]["remaining_frames"]!=-1){d(f)}}}else{if(e.type=="pan"){if(!a._spritely.instances[b]["_stopped"]){if(e.dir=="left"){a._spritely.instances[b]["l"]=(a._spritely.instances[b]["l"]-(e.speed||1))||0}else{a._spritely.instances[b]["l"]=(a._spritely.instances[b]["l"]+(e.speed||1))||0}var c=a._spritely.getBgY(f);a(f).css("background-position",a._spritely.instances[b]["l"]+"px "+c)}}}a._spritely.instances[b]["options"]=e;window.setTimeout(function(){a._spritely.animate(e)},parseInt(1000/e.fps))},randomIntBetween:function(c,b){return parseInt(rand_no=Math.floor((b-(c-1))*Math.random())+c)},getBgY:function(b){if(a.browser.msie){var c=a(b).css("background-position-y")||"0"}else{var c=(a(b).css("background-position")||" ").split(" ")[1]}return c},getBgX:function(c){if(a.browser.msie){var b=a(c).css("background-position-x")||"0"}else{var b=(a(c).css("background-position")||" ").split(" ")[0]}return b},get_rel_pos:function(d,b){var c=d;if(d<0){while(c<0){c+=b}}else{while(c>b){c-=b}}return c}};a.fn.extend({spritely:function(d){var d=a.extend({type:"sprite",do_once:false,width:null,height:null,fps:12,no_of_frames:2,stop_after:null,easing:null,slot:null},d||{});var b=a(this).attr("id");if(!a._spritely.instances){a._spritely.instances={}}if(!a._spritely.instances[b]){a._spritely.instances[b]={current_frame:-1}}a._spritely.instances[b]["type"]=d.type;a._spritely.instances[b]["depth"]=d.depth;d.el=this;d.width=d.width||a(this).width()||100;d.height=d.height||a(this).height()||100;var c=function(){return parseInt(1000/d.fps)};if(!d.do_once){window.setTimeout(function(){a._spritely.animate(d)},c(d.fps))}else{a._spritely.animate(d)}return this},sprite:function(b){var b=a.extend({type:"sprite",bounce:[0,0,1000]},b||{});return a(this).spritely(b)},pan:function(b){var b=a.extend({type:"pan",dir:"left",continuous:true,speed:1},b||{});return a(this).spritely(b)},flyToTap:function(b){var b=a.extend({el_to_move:null,type:"moveToTap",ms:1000,do_once:true},b||{});if(b.el_to_move){a(b.el_to_move).active()}if(a._spritely.activeSprite){if(window.Touch){a(this)[0].ontouchstart=function(g){var f=a._spritely.activeSprite;var h=g.touches[0];var d=h.pageY-(f.height()/2);var c=h.pageX-(f.width()/2);f.animate({top:d+"px",left:c+"px"},1000)}}else{a(this).click(function(j){var g=a._spritely.activeSprite;a(g).stop(true);var d=g.width();var i=g.height();var c=j.pageX-(d/2);var f=j.pageY-(i/2);g.animate({top:f+"px",left:c+"px"},1000)})}}return this},isDraggable:function(c){if(!a(this).draggable){alert("To use the isDraggable method you need to load jquery-ui.js");return this}var c=a.extend({type:"isDraggable",start:null,stop:null,drag:null},c||{});var b=a(this).attr("id");a._spritely.instances[b].isDraggableOptions=c;a(this).draggable({start:function(){var d=a(this).attr("id");a._spritely.instances[d].stop_random=true;a(this).stop(true);if(a._spritely.instances[d].isDraggableOptions.start){a._spritely.instances[d].isDraggableOptions.start(this)}},drag:c.drag,stop:function(){var d=a(this).attr("id");a._spritely.instances[d].stop_random=false;if(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(c){a._spritely.activeSprite=b}}else{b.click(function(c){a._spritely.activeSprite=b})}return this},spRandom:function(d){var d=a.extend({top:50,left:50,right:290,bottom:320,speed:4000,pause:0},d||{});var b=a(this).attr("id");if(!a._spritely.instances[b].stop_random){var f=a._spritely.randomIntBetween;var e=f(d.top,d.bottom);var c=f(d.left,d.right);a("#"+b).animate({top:e+"px",left:c+"px"},d.speed)}window.setTimeout(function(){a("#"+b).spRandom(d)},d.speed+d.pause);return this},makeAbsolute:function(){return this.each(function(){var b=a(this);var c=b.position();b.css({position:"absolute",marginLeft:0,marginTop:0,top:c.top,left:c.left}).remove().appendTo("body")})},spSet:function(d,c){var b=a(this).attr("id");a._spritely.instances[b][d]=c;return this},spGet:function(d,c){var b=a(this).attr("id");return a._spritely.instances[b][d]},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;if(a._spritely.instances[c]["type"]=="sprite"){a(this).spSet("fps",0)}if(b){var d=a._spritely.getBgY(a(this));a(this).css("background-position","0 "+d)}});return this},spStart:function(){a(this).each(function(){var b=a(this).attr("id");var c=a._spritely.instances[b]["_last_fps"]||12;a._spritely.instances[b]["_stopped"]=false;if(a._spritely.instances[b]["type"]=="sprite"){a(this).spSet("fps",c)}});return this},spToggle:function(){var b=a(this).attr("id");var d=a._spritely.instances[b]["_stopped"]||false;var c=a._spritely.instances[b]["_stopped_f1"]||false;if(d){a(this).spStart()}else{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 d=((b-1)*a(this).height())+"px";var e=a._spritely.getBgX(a(this));var c=e+" -"+d;a(this).css("background-position",c)});return this},lockTo:function(c,b){a(this).each(function(){var d=a(this).attr("id");a._spritely.instances[d]["locked_el"]=a(this);a._spritely.instances[d]["lock_to"]=a(c);a._spritely.instances[d]["lock_to_options"]=b;window.setInterval(function(){if(a._spritely.instances[d]["lock_to"]){var g=a._spritely.instances[d]["locked_el"];var j=a._spritely.instances[d]["lock_to"];var f=a._spritely.instances[d]["lock_to_options"];var m=f.bg_img_width;var i=j.height();var k=a._spritely.getBgY(j);var l=a._spritely.getBgX(j);var h=(parseInt(l)+parseInt(f.left));var e=(parseInt(k)+parseInt(f.top));h=a._spritely.get_rel_pos(h,m);a(g).css({top:e+"px",left:h+"px"})}},b.interval||20)});return this}})})(jQuery);try{document.execCommand("BackgroundImageCache",false,true)}catch(err){};
