/*!jQuery Kontrol*/
/**
 * Small extensible jQuery library of new UI controls ;
 * XY pad.
 *
 * Version: 0.9.0 (15/07/2012)
 * Requires: jQuery v1.7+
 *
 * Copyright (c) 2012 Anthony Terrien
 * Under MIT and GPL licenses:
 *  http://www.opensource.org/licenses/mit-license.php
 *  http://www.gnu.org/licenses/gpl.html
 *
 * Thanks to vor, eskimoblood, spiffistan, FabrizioC
 */
(function(c){var e,f,g,h=Math.max,k=Math.min;e={};e.d=c(document);e.t=function(a){return a.originalEvent.touches.length-1};f=function(){var a=this;this.cv=this.v=this.g=this.i=this.$=this.o=null;this.my=this.mx=this.y=this.x=0;this.c=this.$c=null;this.t=0;this.isInit=!1;this.rH=this.eH=this.cH=this.dH=this.sH=this.pColor=this.fgColor=null;this.run=function(){var b=function(b,d){for(var c in d)a.o[c]=d[c];a.init();a._configure()._draw()};if(!this.$.data("kontroled"))return this.$.data("kontroled",
!0),this.extend(),this.o=c.extend({min:this.$.data("min")||0,max:this.$.data("max")||100,stopper:!0,readOnly:this.$.data("readonly"),noScroll:this.$.data("noScroll"),cursor:!0===this.$.data("cursor")&&30||this.$.data("cursor")||0,thickness:this.$.data("thickness")||0.35,width:this.$.data("width")||200,height:this.$.data("height")||200,displayInput:null==this.$.data("displayinput")||this.$.data("displayinput"),displayPrevious:this.$.data("displayprevious"),fgColor:this.$.data("fgcolor")||"#87CEEB",
inline:!1,start:null,draw:null,change:null,cancel:null,release:null},this.o),this.$.is("fieldset")?(this.v={},this.i=this.$.find("input"),this.i.each(function(b){var d=c(this);a.i[b]=d;a.v[b]=d.val();d.bind("change",function(){var c={};c[b]=d.val();a.val(c)})}),this.$.find("legend").remove()):(this.i=this.$,this.v=this.$.val(),""==this.v&&(this.v=this.o.min),this.$.bind("change",function(){a.val(a.$.val())})),!this.o.displayInput&&this.$.hide(),this.$c=c('<canvas width="'+this.o.width+'px" height="'+
this.o.height+'px"></canvas>'),this.c=this.$c[0].getContext("2d"),this.$.wrap(c('<div style="'+(this.o.inline?"display:inline;":"")+"width:"+this.o.width+"px;height:"+this.o.height+'px;"></div>')).before(this.$c),this.v instanceof Object?(this.cv={},this.copy(this.v,this.cv)):this.cv=this.v,this.$.bind("configure",b).parent().bind("configure",b),this._listen()._configure()._xy().init(),this.isInit=!0,this._draw(),this};this._draw=function(){var b=!0,c=document.createElement("canvas");c.width=a.o.width;
c.height=a.o.height;a.g=c.getContext("2d");a.clear();a.dH&&(b=a.dH());!1!==b&&a.draw();a.c.drawImage(c,0,0)};this._touch=function(b){var c=function(b){b=a.xy2val(b.originalEvent.touches[a.t].pageX,b.originalEvent.touches[a.t].pageY,"touch");b==a.cv||a.cH&&!1===a.cH(b)||(a.change(b),a._draw())};this.t=e.t(b);if(!this.sH||!1!==this.sH())return c(b),e.d.bind("touchmove.k",c).bind("touchend.k",function(){e.d.unbind("touchmove.k touchend.k");a.rH&&!1===a.rH(a.cv)||a.val(a.cv)}),this};this._mouse=function(b){var c=
function(b){b=a.xy2val(b.pageX,b.pageY,"mouse");b==a.cv||a.cH&&!1===a.cH(b)||(a.change(b),a._draw())};if(!this.sH||!1!==this.sH())return a.mx=b.pageX,a.my=b.pageY,c(b),e.d.bind("mousemove.k",c).bind("keyup.k",function(b){27===b.keyCode&&(e.d.unbind("mouseup.k mousemove.k keyup.k"),a.eH&&!1===a.eH()||a.cancel())}).bind("mouseup.k",function(b){e.d.unbind("mousemove.k mouseup.k keyup.k");a.rH&&!1===a.rH(a.cv)||a.val(a.cv)}),this};this._xy=function(){var a=this.$c.offset();this.x=a.left;this.y=a.top;
return this};this._listen=function(){this.o.readOnly?this.$.attr("readonly","readonly"):(this.$c.bind("mousedown",function(b){b.preventDefault();a._xy()._mouse(b)}).bind("touchstart",function(b){b.preventDefault();a._xy()._touch(b)}),this.listen());return this};this._configure=function(){this.o.start&&(this.sH=this.o.start);this.o.draw&&(this.dH=this.o.draw);this.o.change&&(this.cH=this.o.change);this.o.cancel&&(this.eH=this.o.cancel);this.o.release&&(this.rH=this.o.release);this.o.displayPrevious?
(this.pColor=this.h2rgba(this.o.fgColor,"0.4"),this.fgColor=this.h2rgba(this.o.fgColor,"0.6")):this.fgColor=this.o.fgColor;return this};this._clear=function(){this.$c[0].width=this.$c[0].width};this.listen=function(){};this.extend=function(){};this.init=function(){};this.change=function(a){};this.val=function(a){};this.xy2val=function(a,c,d){};this.draw=function(){};this.clear=function(){this._clear()};this.h2rgba=function(a,c){var d;a=a.substring(1,7);d=[parseInt(a.substring(0,2),16),parseInt(a.substring(2,
4),16),parseInt(a.substring(4,6),16)];return"rgba("+d[0]+","+d[1]+","+d[2]+","+c+")"};this.copy=function(a,c){for(var d in a)c[d]=a[d]}};g=function(){f.call(this);this.m=[];this.p=[];this.f=[];this.s={0:1,1:-1};this.cursor=this.cur2=0;this.v={};this.div=null;this.extend=function(){this.o=c.extend({min:this.$.data("min")||0,max:this.$.data("max")||100,width:this.$.data("width")||200,height:this.$.data("height")||200},this.o)};this._coord=function(){for(var a in this.v)this.m[a]=~~(0.5+(this.s[a]*this.v[a]-
this.o.min)/this.f[a]+this.cur2),this.p[a]=this.m[a]};this.init=function(){this.cursor=this.o.cursor||30;this.cur2=this.cursor/2;this.f[0]=(this.o.max-this.o.min)/(this.o.width-this.cursor);this.f[1]=(this.o.max-this.o.min)/(this.o.height-this.cursor);this.isInit||this._coord();if(this.o.displayInput){var a=this;this.$.css({"margin-top":"-30px",border:0,font:"11px Arial"});this.i.each(function(){c(this).css({width:a.o.width/4+"px",border:0,background:"none",color:a.o.fgColor,padding:"0px","-webkit-appearance":"none"})})}else this.$.css({width:"0px",
visibility:"hidden"})};this.xy2val=function(a,b){this.m[0]=h(this.cur2,k(a-this.x,this.o.width-this.cur2));this.m[1]=h(this.cur2,k(b-this.y,this.o.height-this.cur2));return{0:~~(this.o.min+(this.m[0]-this.cur2)*this.f[0]),1:~~(this.o.min+(this.o.height-this.m[1]-this.cur2)*this.f[1])}};this.change=function(a){this.cv=a;this.i[0].val(this.cv[0]);this.i[1].val(this.cv[1])};this.val=function(a){if(null!==a)this.cv=a,this.copy(this.cv,this.v),this._coord(),this._draw();else return this.v};this.cancel=
function(){this.copy(this.v,this.cv);this.i[0].val(this.cv[0]);this.i[1].val(this.cv[1]);this.m[0]=this.p[0];this.m[1]=this.p[1];this._draw()};this.draw=function(){var a=this.g,b=1;this.o.displayPrevious&&(a.beginPath(),a.lineWidth=this.cursor,a.strokeStyle=this.pColor,a.moveTo(this.p[0],this.p[1]+this.cur2),a.lineTo(this.p[0],this.p[1]-this.cur2),a.stroke(),b=this.cv[0]==this.v[0]&&this.cv[1]==this.v[1]);a.beginPath();a.lineWidth=this.cursor;a.strokeStyle=b?this.o.fgColor:this.fgColor;a.moveTo(this.m[0],
this.m[1]+this.cur2);a.lineTo(this.m[0],this.m[1]-this.cur2);a.stroke()}};c.fn.xy=function(a){return this.each(function(){var b=new g;b.$=c(this);b.o=a;b.run()}).parent()}})(jQuery);