﻿function log(){var args=$A(arguments);if(window.opera)opera.postError(args.inspect());if(window.console)console.log.apply(this,args);};function $eF(){};function pix(num){return Math.round(num)+"px";};function $nE(tag,parent,attributes,namespace){var el;if(namespace&&!BGMap.b.msie)el=document.createElementNS(namespace,tag);else el=document.createElement(tag);for(var att in attributes){if(att=="className")el.className=attributes[att];else el.setAttribute(att,attributes[att]);};if(parent)parent.appendChild(el);return el;};function $mU(img){if(arguments.length==1)var elements=[img];else var elements=arguments;var isIE=/msie/.test(navigator.userAgent.toLowerCase());var isFF=/firefox/.test(navigator.userAgent.toLowerCase());$A(elements).each(function(image){if(isIE){if(image.tagName.toLowerCase()=='img'){image.unselectable="on";image.galleryImg="no";image.onselectstart=function(){return false;};};Event.observe(window,'unload',function(){this.unselectable=null;this.galleryImg=null;this.onselectstart=null;this.onload=null}.bind(image));}else if(isFF){image.style.MozUserSelect="none";image.oncontextmenu=function(){return false;};}});};function $RCR(node){if(!node)return;while(node.hasChildNodes()){$RCR(node.firstChild);node.removeChild(node.firstChild);}};function toAbs($e){$e.style.position="absolute";};function makeRelative($e){$e.style.position="relative";};function $sP($e,x,y){var pos;if(x instanceof Pos&&y==undefined)pos=x;else pos=new Pos(x,y);$e.style.left=pix(pos.x);$e.style.top=pix(pos.y);};function numToStr(number,length){var str=number+"";while(str.length<length){str="0"+str;};return str;};var $D=Class.create({initialize:function($e,options){this.options=Object.extend({$osd:$eF,$od:$eF,ondrop:$eF,onneardrop:$eF,onnodrag:$eF,axis:false,constrain:false},options||{});this.$e=$($e);this.axis=this.options.axis;if(!(this.$e.getStyle("position")=="absolute"))this.$e.absolutize();this.dragging=false;this.$eMD=this.$iiD.bindAsEventListener(this);Event.observe(this.$e,"mousedown",this.$eMD);},remakeConstrain:function($con){var cPoint=$con.cumulativeOffset();var cDim=[parseInt($con.style.width)||$con.offsetWidth,parseInt($con.style.height)||$con.offsetHeight];return{left:cPoint[0],top:cPoint[1],right:cPoint[0]+cDim[0],bottom:cPoint[1]+cDim[1],width:cDim[0],height:cDim[1]}},$gP:function(){return[parseInt(this.$e.style.left)||0,parseInt(this.$e.style.top)||0];},$iiD:function(event){if(!event.isLeftClick())return;if(this.dragging)this.$eeD(event);this.dragging=true;this._pointer=this.startPointer=[event.pointerX(),event.pointerY()];if(this.options.constrain){this.constrain=this.remakeConstrain($(this.options.constrain));this.elementOffset=this.$e.cumulativeOffset();};this.$eMM=this.$uuD.bindAsEventListener(this);this.$eMU=this.$eeD.bindAsEventListener(this);Event.observe(document,"mousemove",this.$eMM);Event.observe(document,"mouseup",this.$eMU);if(typeof this.options.$osd=="function")this.options.$osd();event.stop();},$uuD:function(event){var pointer=[event.pointerX(),event.pointerY()];var dx=pointer[0]-this._pointer[0];var dy=pointer[1]-this._pointer[1];var pos=this.$gP();var style=this.$e.style;var newX=pos[0]+dx;var newY=pos[1]+dy;if(this.constrain){if(newX<0||pointer[0]<this.constrain.left)newX=0;else if((newX+this.$e.offsetWidth)>=this.constrain.width||pointer[0]>this.constrain.right)newX=this.constrain.width-this.$e.offsetWidth;if(newY<0||pointer[1]<this.constrain.top)newY=0;else if((newY+this.$e.offsetHeight)>=this.constrain.height||pointer[1]>this.constrain.bottom)newY=this.constrain.height-this.$e.offsetHeight;};if(!this.axis||this.axis=="x")style.left=pix(newX);if(!this.axis||this.axis=="y")style.top=pix(newY);if(typeof this.options.$od=="function")this.options.$od(event);if(!this.neardropexec&&(Math.abs(dx)+Math.abs(dy))<=1&&(typeof this.options.onneardrop=="function")){this.neardropexec=true;setTimeout(function(){if(pointer[0]==this._pointer[0]&&pointer[1]==this._pointer[1])this.options.onneardrop();this.neardropexec=false;}.bind(this),0);};this._pointer=pointer;event.stop();},$eeD:function(event){this.dragging=false;Event.stopObserving(document,"mousemove",this.$eMM);Event.stopObserving(document,"mouseup",this.$eMU);delete this.$eMM;delete this.$eMU;if(typeof this.options.ondrop=="function"){if(event.pointerX()==this.startPointer[0]&&event.pointerY()==this.startPointer[1])this.options.onnodrag(this);else this.options.ondrop(event);};event.stop();},$ddd:function(){Event.stopObserving(this.$e, "mousedown", this.$$eMD);this.$e=null;}});var isIE7=/msie 7\./.test(navigator.userAgent.toLowerCase());var isIE=/msie/.test(navigator.userAgent.toLowerCase());var gg;document.observe("dom:loaded",function(){gg=new $G($("map"),{sid:sid,$q:"question.ashx",$qF:"text"});});var $G=Class.create({initialize:function($f,options){this.options=Object.extend({qt:10,lt:5,nt:3,sid:"",$q:"question.ashx",$qF:"text",$qC:questionCount},options||{});$("newGame").observe("click",this.onNewGameClick.bindAsEventListener(this));this.$f=$f;$mU(this.$f);this.$qF=$(this.options.$qF);this.$ggg={};},onNewGameClick:function(event){$("newGame").hide();this.$sG();},$sG:function(){this.$pt=false;this.options.$gF=false;this.options.currentQuestion=0;this.$qF.show();this.$oSHandler=this.$oS.bindAsEventListener(this);this.$f.observe("click",this.$oSHandler);this.$qLHandler=this.$qL.bind(this);this.$qF.observe("load",this.$qLHandler);this.$gQ("newGame");if(this.$sW)this.$sW.remove();this.$sW=new $G.$SW(this);},next:function(){this.$qF.hide();this.$pt=new $G.$T(this.options.nt,{$sT:true,onend:this.$gQ.bind(this,"next")});},$eG:function(){this.$f.stopObserving("click",this.$oSHandler);this.$qF.stopObserving("load",this.$qLHandler);this.$qF.src=this.options.$q+"?sid="+sid+"&r="+(new Date()-1)+Math.random();$("distance").update("");$("newGame").show();this.options.$gF=true;},$gQ:function(command){if(this.options.$gF)return;this.$cG();var srcArr=this.$qF.src=this.options.$q+"?sid="+sid+"&cmd="+command+"&r="+(new Date()-1)+Math.random();return;},$qL:function(){this.$sW.hide();this.$qF.show();$("distance").update("");this.lastQuestion=++this.options.currentQuestion>=this.options.$qC;this.$t=new $G.$T(this.options.qt,{$sT:true,onend:(this.lastQuestion?this.$eG.bind(this):this.next.bind(this)),$ot:this.$oT.bind(this,this.lastQuestion)});},$oT:function(isLastQuestion){var req=new Ajax.Request("Default.aspx",{asynchronous:false,parameters:{sid:this.options.sid,cmd:"score"}});var response=req.transport.responseText.evalJSON();this.$sW.$sP();this.$sW.show({hideButtons:isLastQuestion,update:{time:0,precision:0,total:response.total,message:isLastQuestion?"Играта приключи!":""}});},$cG:function(){if(this.$ggg.$l){this.$ggg.$l.parentNode.removeChild(this.$ggg.$l);this.$ggg.$l=null;};if(this.$ggg.$rG){this.$ggg.$rG.hide();this.$ggg.$rG=null;};if(this.$ggg.$gG){this.$ggg.$gG.hide();this.$ggg.$gG=null;}},$oS:function(event){if(!this.$t||this.$t.left<=0)return;event.stop();var offset=event.element().cumulativeOffset();var pos={x:event.pointerX()-offset[0]-(isIE7?2:0),y:event.pointerY()-offset[1]-(isIE7?2:0)};new Ajax.Request("Default.aspx",{method:"post",parameters:Object.extend(pos,{sid:this.options.sid}),onSuccess:function(req,json){this.$t.stop();this.$t=null;try{var $rG=$("red");$rG.setStyle({display:"",left:pos.x-1+"px",top:pos.y-37+"px"});this.$ggg.$rG=$rG;var $gG=$("green");$gG.setStyle({display:"",left:json.point.x-19+"px",top:json.point.y-37+"px"});this.$ggg.$gG=$gG;if(isIE)this.$ggg.$l=this.$dL(pos.x,pos.y,json.point.x,json.point.y,json.distance);else this.$ggg.$l=this.$dLNS(pos.x,pos.y,json.point.x,json.point.y,json.distance);$("distance").update(json.distance);if(json.score){$("level").update("");};this.$sW.$sP(pos,json.point);this.$sW.show({hideButtons:!!this.lastQuestion,update:{time:json.time,precision:json.precision,total:json.total}});}catch(e){$("text").update(json.error);}}.bind(this)});this.$t.pause();},$dL:function(x1,y1,x2,y2,distance){var $l=document.createElement("v:line");$l.setAttribute("from",x1+","+y1);$l.setAttribute("to",x2+","+y2);$l.setAttribute("strokecolor","gray");$l.setAttribute("strokeweight",2);$l.style.position="absolute";$l.style.left=Math.min(x1,x2)+"px";$l.style.top=Math.min(y1,y2)+"px";$("svg-container").appendChild($l);return $l;},$dLNS:function(x1,y1,x2,y2,distance){var svg=document.createElementNS("http://www.w3.org/2000/svg","svg");svg.setAttribute("width",$("map").offsetWidth);svg.setAttribute("height",$("map").offsetHeight);$("svg-container").appendChild(svg);var $l=document.createElementNS("http://www.w3.org/2000/svg","line");$l.setAttribute("x1",x1);$l.setAttribute("x2",x2);$l.setAttribute("y1",y1);$l.setAttribute("y2",y2);$l.setAttribute("style","stroke:gray;stroke-width:2;");svg.appendChild($l);return svg;}});$G.$SW = Class.create({ initialize: function($g) { this.$g = $g; this.$e = $("score-window"); this.$drg = new $D(this.$e); this.pause = $("score-pause"); this.$tPHandler = this.$tP.bind(this, "clicked"); this.pause.observe("click", this.$tPHandler); this.$tPHandlerWithEvent = this.$tP.bindAsEventListener(this); document.observe("keypress", this.$tPHandlerWithEvent); this.next = $("score-next"); this.$nCH = this.nextClicked.bind(this.$g, "clicked"); this.next.observe("click", this.$nCH); this.$nCHWithEvent = this.nextClicked.bindAsEventListener(this.$g); document.observe("keypress", this.$nCHWithEvent); }, $tP: function(event) { if (!this.$g.$pt || !this.visible || (event.type == "keypress" && (event.which || event.charCode || event.keyCode) != 32)) return; if (!this.pause.paused) { this.$g.$pt.pause(); this.pause.paused = true; this.pause.update("Поднови"); } else { this.$g.$pt.resume(); this.pause.paused = false; this.pause.update("Пауза"); }; if (event.stop) event.stop(); }, nextClicked: function(event) { if (!this.$pt || !this.$sW.visible || (event.type == "keypress" && event.keyCode != Event.KEY_RETURN)) return; this.$pt.stop(); this.$gQ.bind(this, "next"); if (event.stop) event.stop(); }, hide: function() { this.visible = false; this.$e.hide(); if (this.pause.paused) { this.pause.paused = false; this.pause.update("Пауза"); } }, show: function(options) { this.visible = true; this.$e.show(); if (options.hideButtons) this.hideButtons(); else this.$sB(); if (options.update) { $("score-total-value").update(options.update.total); $("score-precision-value").update(options.update.precision); $("score-time-value").update(options.update.time); this.$sM(options.message); } }, remove: function() { this.$drg.$ddd(); this.$e = null; this.next.stopObserving("click", this.$nCH); document.stopObserving("keypress", this.$nCHWithEvent); this.next = null; this.pause.stopObserving("click", this.$tPHandler); document.stopObserving("keypress", this.$tPHandlerWithEvent); this.pause = null; }, hideButtons: function() { $$("#score-window .buttons")[0].hide(); }, $sB: function() { $$("#score-window .buttons")[0].show(); }, $sM: function(message) { if (message) { var $bC = $("score-window .buttons")[0]; $bC.update(message); $bC.show(); } }, $sP: function(p1, p2) { this.$e.style.left = pix(540); this.$e.style.top = pix(370); return; this.$e.style.left = pix(250); this.$e.style.top = pix(150); if (!p1 || !p2) return; var buffer = 10; var rect = { width: 220, height: 120, center: { x: 360, y: 210} }; var p1min = { x: Math.min(p1.x, p2.x), y: Math.min(p1.y, p2.y) }; var p1max = { x: Math.max(p1.x, p2.x), y: Math.max(p1.y, p2.y) }; var rect1 = { width: p1max.x - p1min.x, height: p1max.y - p1min.y }; rect1.center = { x: p1min.x + Math.ceil(rect1.width / 2), y: p1min.y + Math.ceil(rect1.height / 2) }; var centerRect = { width: Math.abs(rect.center.x - rect1.center.x), height: Math.abs(rect.center.y - rect1.center.y) }; var intercept = centerRect.width <= (rect.width / 2 + rect1.width / 2 + buffer) && centerRect.height <= (rect.height / 2 + rect1.height / 2 + buffer); if (intercept) { this.$e.style.left = pix(540); this.$e.style.top = pix(370); } } }); $G.$T=Class.create({initialize:function(time,options){this.options=Object.extend({$con:$("timer-container"),$sT:false},options||{});this.left=time;this.$con=$(this.options.$con)||$("timer-container");if(this.options.$sT)this.$sT();},$sT:function(){if(this.scale)this.$rS();this.scale=this.$cS();if(this.$t)this.stop();this.$t=setInterval(this.$aa.bind(this),1000);this.event("onstart");},$aa:function(){if(this.left<=0){this.event("$ot");return this.stop();};this.$uS(--this.left);this.event("onupdate");},stop:function(){this.$rS();clearInterval(this.$t);this.$t=null;this.event("onend");},$cS:function(){var scale=$($nE("div",this.$con,{id:"timeline"}));this.$scT=$($nE("span",this.$con,{id:"time"}));this.$scT.update(this.left);return scale;},$uS:function(){if(!this.scale)return;this.scale.style.width=pix(this.scale.offsetWidth-this.scale.offsetWidth/(this.left+1));this.$scT.update(this.left);},$rS:function(){if(this.scale)this.scale.parentNode.removeChild(this.scale);this.scale=null;if(this.$scT)this.$scT.parentNode.removeChild(this.$scT);this.$scT=null;},pause:function(){clearInterval(this.$t);},resume:function(){this.$t=setInterval(this.$aa.bind(this),1000);},event:function(type){if(typeof this.options[type]=="function")this.options[type]();},$ddd:function(){this.$con=null;this.scale=null;}});
