// EuroCalc jQuery Plugin 1.0

var euroCalcHash=[];jQuery.extend({euroCalc:function(o){o=$.extend({target:[],source:[],exchange:{skk:30.1260},valuta:"skk",round:2,uri:"http://www.mattis.sk/admin/kurzovy_listok.txt",filter:["skk","czk","usd","gbp","chf","pln"]},o);if(o.uri!=null){var chacheKey=-1;$.each(euroCalcHash,function(key,val){chacheKey=key;});if(chacheKey>-1)
o.exchange=euroCalcHash[chacheKey].exchange;else{$.post(o.uri,{},function(data){if(data){var tmp={}
$.each(data,function(key,val){tmp[key]=val.kurz*1;});euroCalcHash.push({uri:o.uri,exchange:tmp});o.exchange=$.extend(tmp,o.exchange);}},"json");}}
o.round=Math.pow(10,o.round);var calcForm=$('<div id="_ec">').css({zIndex:101}).addClass("g_ec").html('\
                <div class="l"></div>\
                <input type="text" name="from" value="1" autocomplete="off" style="text-align:right" />&nbsp;\
                <span class="m"><span class="b">EUR</span></span>\
                <span class="e" title="=">&nbsp;</span>\
                <a href="#" class="m" id="_ec_c" title="Zvoliť menu"><span class="b"><span id="_ec_c_in">'+o.valuta.toUpperCase()+'</span><span class="c">+</span></span></a>\
                &nbsp;<input type="text" name="to" value="0" autocomplete="off" />\
                <a href="#" class="s" title="Zavrieť eurokalkulačku" id="_ec_ok">&nbsp;</a>\
                ').hide();var finp=$("[name=from]",calcForm);var tinp=$("[name=to]",calcForm);var darkSide=$('<div id="_ec_darkSide">').css({position:"absolute",display:"block",top:0,left:0,width:"100%",height:"100%",opacity:0.4,backgroundColor:"#000000",zIndex:100}).click(function(){close()});function getPageSize(){return dim={document:{width:$(document).width(),height:$(document).height()},window:{height:window.innerHeight?window.innerHeight:$(window).height(),width:window.innerWidth?window.innerWidth:$(window).width()},scroll:{top:$("html").scrollTop(),left:$("html").scrollLeft()}}}
function close(){calcForm.remove();darkSide.fadeOut(300,function(){$(this).remove()});}
function compute(from){from.val(from.val().replace(/(,+)/,","));from.val(from.val().replace(/(\.+)/,"."));var value=from.val().replace(/,/,".")*1;if(from.attr("name")=="from"){value=Math.round(value*o.exchange[o.valuta]*o.round)/o.round;tinp.val(value);}else{value=Math.round(value/o.exchange[o.valuta]*o.round)/o.round;finp.val(value);}}
function submit(){if(o.target.length){o.target.val(tinp.val());}
close();}
$("input",calcForm).keypress(function(e){key=e.charCode||e.keyCode||0;if(key==27)return close();if(key==13)return submit();if(e.which!=8&&e.which!=0&&((e.which<48||e.which>57)&&(e.which!=44&&e.which!=46)))
return false;}).keyup(function(e){if(e.which>40||e.which<37)compute($(this));}).blur(function(){if($(this).val().length==0||!$(this).val().match(/\d+/))
$(this).val(0);});$("#_ec_ok",calcForm).click(function(){submit();return false;});var pointerMenu=-1;var valutaMenu=$('<div id="_ec_vm">').css({position:"absolute"})
function displayMenu(selected){valutaMenu.empty();$.each(o.exchange,function(key,val){if($.inArray(key,o.filter)==-1)return;menuItem=$('<a>').attr("href","#").addClass("m").attr({title:key.toUpperCase()}).css({marginTop:"1px",padding:"2px 5px",display:"block"}).html(key.toUpperCase()).click(function(){o.valuta=key;$("#_ec_c_in").html(key.toUpperCase());compute(finp);closeMenu();return false;})
valutaMenu.append(menuItem);});dim=$("#_ec_c").position();valutaMenu.css({top:dim.top+$("#_ec_c").outerHeight(true),left:dim.left-1,width:$("#_ec_c").outerWidth()+2}).insertAfter($("#_ec_c")).fadeIn();}
function closeMenu(){valutaMenu.remove();tinp.focus();}
$("#_ec_c",calcForm).click(function(){displayMenu();return false;});if(o.source.length)finp.val(o.source.val().replace(/[^0-9,.]/g,""));compute(finp,calcForm);dim=getPageSize();darkSide.css({width:dim.document.width,height:dim.document.height}).appendTo($("body"));if(!$.browser.msie)calcForm.css({opacity:0});calcForm.appendTo($("body")).css({top:((dim.window.height-calcForm.height())/2*0.7)-25+dim.scroll.top,left:(dim.window.width-calcForm.width())/2}).show().css({zIndex:999});if(!$.browser.msie)calcForm.animate({opacity:1,top:"+=25"},300,"swing");finp.focus();return this;}});
