
(function($)
{$.extend({fightstick:{skin:false,pressed:{"81":{down:false,duration:false},"87":{down:false,duration:false},"69":{down:false,duration:false},"82":{down:false,duration:false},"65":{down:false,duration:false},"83":{down:false,duration:false},"68":{down:false,duration:false},"70":{down:false,duration:false},"38":{down:false,duration:false},"40":{down:false,duration:false},"37":{down:false,duration:false},"39":{down:false,duration:false}},direction:{"38":"up","40":"down","37":"left","39":"right"},limits:{stick:/38|40|37|39/g,buttons:/(81|87|69|82|65|83|68|70)/g},keys:[],multi:{two:{th:{regex:/QA|AQ/g,icon:"throw"},fo:{regex:/WS|SW/g,icon:"focus"},ta:{regex:/ED|DE/g,icon:"taunt"},pex:{regex:/QW|WQ|QE|EQ|WE|EW/g,icon:"pex"},kex:{regex:/AS|SA|AD|DA|SD|DS/g,icon:"kex"}},three:{ppp:{regex:/QWE|QEW|WQE|WEQ|EQW|EWQ/g,icon:"ppp"},kkk:{regex:/ASD|ADS|SAD|SDA|DAS|DSA/g,icon:"kkk"}}},interval:false,helper:true,message:{},timed:{},timer:{time:0,interval:{},delay:100},record:function()
{$("#Fightstick #Message, #Fightstick #Message div").hide();$("#Fightstick #Message .super p").html("ready?");$("#Fightstick #Message, #Fightstick #Message .super").show();$("#Fightstick #Message .super .top").css({left:570}).animate({left:0},250);$("#Fightstick #Message .super .bottom").css({left:-570}).animate({left:0},250);this.message=setInterval(function()
{var word=$("#Fightstick #Message .super p").html();switch(word)
{case"ready?":$("#Fightstick #Message .super .top").css({left:570}).animate({left:0},250);$("#Fightstick #Message .super .bottom").css({left:-570}).animate({left:0},250);$("#Fightstick #Message .super p").hide().html("set.").fadeIn();break;case"set.":$("#Fightstick #Message .super .top").css({left:570}).animate({left:0},250);$("#Fightstick #Message .super .bottom").css({left:-570}).animate({left:0},250);$("#Fightstick #Message .super p").hide().html("fight!").fadeIn();break;case"fight!":$("#Fightstick #Message").fadeOut('fast');clearInterval($.fightstick.message);$.fightstick.init();break;}},500);},init:function()
{this.state("record");$(document).bind('keydown',function(e){var code=e.charCode==0?e.keyCode:e.which;var type=false;if(code.toString().match($.fightstick.limits.buttons)){type="button"}
if(code.toString().match($.fightstick.limits.stick)){type="stick"}
if(type)
{if(!$.fightstick.pressed[code].down)
{$.fightstick.pressed[code].down=true;switch(type)
{case"button":$.fightstick.buttons.press(String.fromCharCode(code));return false;break;case"stick":$.fightstick.stick.move($.fightstick.direction[code]);break;}}
else{return false;}}
return false;});$(document).bind('keyup',function(e){var code=e.charCode==0?e.keyCode:e.which;var type=false;if(code.toString().match($.fightstick.limits.buttons)){type="button"}
if(code.toString().match($.fightstick.limits.stick)){type="stick"}
if(type)
{$.fightstick.pressed[code].down=false;switch(type)
{case"button":$.fightstick.buttons.release(String.fromCharCode(e.which));return false;break;case"stick":$.fightstick.stick.move("release-"+$.fightstick.direction[code]);break;}}
return false;});this.timer.interval=setInterval(function()
{$.fightstick.timer.time+=$.fightstick.timer.delay;},$.fightstick.timer.delay)},buttons:{press:function(id,play,icon)
{if(!play){$.fightstick.add(id,'press');}
if(id.length>1)
{$.fightstick.output(icon);for(n in id)
{$("#"+id[n].toUpperCase()).addClass("press");}}
else
{$("#"+id.toUpperCase()).addClass("press");$.fightstick.output(id);}},release:function(id,play)
{if(!play){$.fightstick.add(id,'release');}
$("#"+id.toUpperCase()).removeClass("press");}},stick:{direction:false,stack:{up:false,down:false,left:false,right:false},position:{left:96,top:149},shift:30,move:function(dir,play)
{var stick=$("#Stick");switch(dir)
{case"up":stick.css({top:(this.position.top-this.shift)});break;case"down":stick.css({top:(this.position.top+this.shift)});break;case"left":stick.css({left:(this.position.left-this.shift)});break;case"right":stick.css({left:(this.position.left+this.shift)});break;case"release-up":case"release-down":stick.css({top:this.position.top});break;case"release-left":case"release-right":stick.css({left:this.position.left});break;}
this.stack[dir.replace(/(release-)/g,"")]=dir.indexOf("release")==-1?true:false
var direction="";for(prop in this.stack)
{if(this.stack[prop]==true){direction+=prop}}
if(direction.length>0){$.fightstick.output(direction);}
if(!play){$.fightstick.add('Stick',dir);}}},stop:function()
{clearInterval(this.timer.interval);for(prop in this.timed)
{clearTimeout(this.timed[prop])}
$("#Fightstick #Output div").empty();this.state('complete');},done:function()
{clearInterval(this.timer.interval)
this.state('complete')
$(document).unbind('keyup');$(document).unbind('keydown');$("#Fightstick #Output div").attr({left:254})},play:function()
{this.done();this.state('playing');$("#Fightstick #Output div").empty();this.interval=false;for(timestamp in this.keys)
{this.step({id:timestamp,actions:this.keys[timestamp]})}},step:function(step)
{if(!this.interval){this.interval=parseInt(step.id);}
var interval=(step.id-this.interval);step.interval=interval
this.last=interval;this.timed[step.id]=setTimeout(function()
{(function(step){for(action in step.actions)
{if(interval==$.fightstick.last)
{$.fightstick.state("complete");}
var move=step.actions[action];switch(move['action'])
{case"press":case"release":var icon=!move["icon"]?false:move['icon'];$.fightstick.buttons[move['action']](move['item'],true,icon)
break;default:$.fightstick.stick.move(move['action'],true);break;}}})(step)},interval)},add:function(item,action)
{var timestamp=this.timer.time;if(!this.keys[timestamp]){this.keys[timestamp]=[]};this.keys[timestamp].push({item:item,action:action});if($.fightstick.keys[timestamp].length==2&&action=="press")
{setTimeout(function()
{if($.fightstick.keys[timestamp].length>=2&&$.fightstick.keys[timestamp].length<=3&&action=="press"){$.fightstick.combo($.fightstick.keys[timestamp],timestamp);}},50)}},combo:function(keys,timestamp)
{var stack="";for(i in keys)
{stack+=keys[i].item;}
var multi=stack.length==3?this.multi.three:this.multi.two
for(i in multi)
{if(multi[i].regex.test(stack))
{var icon=multi[i].icon;var length=stack.length;var output=$("#Fightstick #Output div");var images=$("#Fightstick #Output div img");var min=images.length-length;var max=min+length;$(images).slice(min,max).remove();$.fightstick.output(icon);$.fightstick.keys[timestamp]=Array({item:stack,action:"press",icon:icon});break;}}},output:function(icon)
{$("#Fightstick #Output div").append("<img src=\"images/icons/"+icon+".png\" />");$("#Fightstick #Output div").css({width:($("#Fightstick #Output div img").length*24)});},reset:function()
{$("#Fightstick #Output div").empty();this.state('reset');this.keys=[];this.interval=false;},state:function(state)
{$("#Actions buttons").css({"border-radius":"6px","-moz-border-radius":"6px","-webkit-border-radius":"6px"})
switch(state)
{case"default":$("#Record, #Reset","#Stop, #Save").hide();$("#Done, #Play").css({width:"50%"}).show();$("#Done").css({"border-radius":"6px 0 0 6px","-moz-border-radius":"6px 0 0 6px","-webkit-border-radius":"6px 0 0 6px"})
$("#Play").css({"border-radius":"0 6px 6px 0","-moz-border-radius":"0 6px 6px 0","-webkit-border-radius":"0 6px 6px 0"})
break;case"reset":$("#Play, #Stop, #Reset, #Save").hide();$("#Record").css({width:"100%"}).show();break;case"record":$("#Record, #Play, #Reset, #Save").hide();$("#Stop, #Done").css({width:"50%"}).show();$("#Stop").css({"border-radius":"6px 0 0 6px","-moz-border-radius":"6px 0 0 6px","-webkit-border-radius":"6px 0 0 6px"})
$("#Done").css({"border-radius":"0 6px 6px 0","-moz-border-radius":"0 6px 6px 0","-webkit-border-radius":"0 6px 6px 0"})
break;case"playing":$("#Record, #Play, #Reset, #Done, #Save").hide();$("#Stop").css({width:"100%"}).show();$("#Stop").css({"border-radius":"6px","-moz-border-radius":"6px","-webkit-border-radius":"6px"})
break;case"complete":$("#Record, #Stop, #Done").hide();$("#Reset, #Save, #Play").css({width:"33%"}).show();$("#Reset").css({"border-radius":"6px 0 0 6px","-moz-border-radius":"6px 0 0 6px","-webkit-border-radius":"6px 0 0 6px"})
$("#Save").css({"border-radius":"0","-moz-border-radius":"0","-webkit-border-radius":"0"})
$("#Play").css({width:"34%","border-radius":"0 6px 6px 0","-moz-border-radius":"0 6px 6px 0","-webkit-border-radius":"0 6px 6px 0"})
break;}},hints:function()
{this.helper=this.helper==false?true:false;this.helper==false?$("#Fightstick div span").fadeOut():$("#Fightstick div span").fadeIn();},save:function()
{var htmlCode,bbCode,linkCode;linkCode="";$("#Fightstick #Output div").empty();for(timestamp in this.keys)
{actions=this.keys[timestamp];for(action in actions)
{var move=actions[action];switch(move['action'])
{case"press":case"release":var icon=!move["icon"]?false:move['icon'];$.fightstick.buttons[move['action']](move['item'],true,icon);linkCode+=timestamp+"|"+move['item']+"|"+move['action'];if(icon.length>0){linkCode+="|"+icon;}
break;default:$.fightstick.stick.move(move['action'],true);linkCode+=timestamp+"|"+move['item']+"|"+move['action'];break;}
linkCode+=",";}}
linkCode="http://fightstick.fakesite.com?moves="+linkCode.slice(0,-1);if($.fightstick.skin){linkCode+="&skin="+$.fightstick.skin;}
htmlCode=$("#Fightstick #Output div").html();htmlCode=htmlCode.replace(/(images)/g,"http://fightstick.fakesite.com/images");bbCode=htmlCode;bbCode=bbCode.replace(/(<img src=\")/g,"[IMG]");bbCode=bbCode.replace(/(\">)/g,"[/IMG]");htmlCode=htmlCode.replace(/(<)/g,"&lt;");htmlCode=htmlCode.replace(/(>)/g,"&gt;");$("#htmlCode").html(htmlCode);$("#bbCode").html(bbCode);$("#linkCode").html(linkCode);$("#Fightstick #Message .super").hide();$("#Fightstick #Message .save").show();$("#Fightstick #Message").fadeIn('fast');},linked:function(saved)
{this.keys=[];this.interval=false;this.state("complete");for(i in saved)
{var move=saved[i].split("|");var timestamp=move[0];var item=move[1];var action=move[2];if(!this.keys[timestamp]){this.keys[timestamp]=[]}!move[3]?this.keys[timestamp].push({item:item,action:action}):this.keys[timestamp].push({item:item,action:action,icon:move[3]});}
$("#Fightstick #Message, #Fightstick #Message div").hide();$("#Fightstick #Message .super p").html("here")
$("#Fightstick #Message, #Fightstick #Message .super").show();$("#Fightstick #Message .super .top").css({left:570}).animate({left:0},250);$("#Fightstick #Message .super .bottom").css({left:-570}).animate({left:0},250);this.message=setInterval(function()
{var word=$("#Fightstick #Message .super p").html();switch(word)
{case"here":$("#Fightstick #Message .super .top").css({left:570}).animate({left:0},250);$("#Fightstick #Message .super .bottom").css({left:-570}).animate({left:0},250);$("#Fightstick #Message .super p").hide().html("we").fadeIn();break;case"we":$("#Fightstick #Message .super .top").css({left:570}).animate({left:0},250);$("#Fightstick #Message .super .bottom").css({left:-570}).animate({left:0},250);$("#Fightstick #Message .super p").hide().html("GO!").fadeIn();break;case"GO!":$("#Fightstick #Message").fadeOut('fast');clearInterval($.fightstick.message);$.fightstick.play();break;}},500);}}})})(jQuery)
