﻿function addEvent(obj,type,fn) {if (obj.addEventListener) {obj.addEventListener(type,fn,false);EventCache.add(obj,type,fn);} else if (obj.attachEvent) {obj["e"+type+fn]=fn;obj[type+fn]=function() {obj["e"+type+fn](window.event);};obj.attachEvent("on"+type,obj[type+fn]);EventCache.add(obj,type,fn);}else{obj["on"+type]=obj["e"+type+fn];}};var EventCache=function(){var listEvents=[];return {listEvents:listEvents,add:function(node,sEventName,fHandler){listEvents.push(arguments);},flush:function(){var i,item;for(i=listEvents.length-1;i>=0;i=i-1){item=listEvents[i];if(item[0].removeEventListener){item[0].removeEventListener(item[1],item[2],item[3]);};if(item[1].substring(0,2)!="on"){item[1]="on"+item[1];};if(item[0].detachEvent){item[0].detachEvent(item[1],item[2]);};item[0][item[1]]=null;};}};}();
addEvent(window,'load',form_temizle);
addEvent(window,'load',kodyarat);
function form_temizle() {$("form").reset();kodyarat();}
function $(v) {return(document.getElementById(v));}
function doviz() {
	var birim = document.getElementById("birim").selectedIndex;
	var birim2 = document.getElementById("birim2").selectedIndex;
	var para = document.getElementById("para").value.toString().replace(/,/g,'')
		if (document.getElementsByName("satisalis")[0].checked == true) {
			var cvr = para * ((doviz_kuru_satis[birim])/(doviz_kuru_satis[birim2]))/1;
		} else {
			var cvr = para * ((doviz_kuru_alis[birim])/(doviz_kuru_alis[birim2]))/1;
		}
	document.getElementById("para").value = numberFormat(para);
	if (!isNaN(cvr)) {document.getElementById("para2").value = numberFormat(cvr.toFixed(2));}
};
function numberFormat(nStr){
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1))
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    return x1 + x2;
};
function tersdoviz(){
	var birim = document.getElementById("birim");
	var birim2 = document.getElementById("birim2");
	tur = birim.selectedIndex;
	tur2 = birim2.selectedIndex; 
	birim.options[tur2].selected = true;
	birim2.options[tur].selected = true;
	doviz();
};
function textkutu(v) {$S(v.id.replace(/2/, "")).background='#'+v.value}
function stil_degisim(id,islem,deger){
	dv = deger.value;
	var mysheet=document.styleSheets[0];
	var myrules=mysheet.cssRules? mysheet.cssRules: mysheet.rules;
	if (islem == "cl") {if (dv.length == 3 || dv.length == 6) {for (i=0; i<myrules.length; i++){if(myrules[i].selectorText.toLowerCase()==id){myrules[i].style.color="#"+dv;}}textkutu(deger);}}
	if (islem == "w") {if (dv >= 120 && dv <=220) {for (i=0; i<myrules.length; i++){if(myrules[i].selectorText.toLowerCase()==id){myrules[i].style.width=dv+'px';}}}}
}

function tackutu(isaret) {
	$("birimleri_ters_cevir").style.display = isaret?"block":"none";
	kodyarat();
}

function kodyarat() {
		ters_buton = $("ters_buton").checked?"1":"0";
		kod = "<a name=\"doviz_hesaplama\" target=\"_blank\" href=\"http://www.serbestdoviz.com/\">"+kelime()+"</a>\n";
		kod += "<script type=\"text/javascript\" charset=\"iso-8859-9\" src=\"http://www.serbestdoviz.com/kutu/doviz_hesaplama.asp?fr="+$("fontrengi2").value+"&g="+$("genislik").value+"&b="+ters_buton+"\"></script>";
		$("kutukodu").value = kod;
}

function kelime() {
	var kelimeler=new Array("döviz çevirici","döviz");
	if ($("kutukodu").value == "") {rnd = Math.floor(Math.random()*kelimeler.length);}
	return kelimeler[rnd];
}

function kopyavalue(id) {
	var islem = "metin";
	var input = $(id);
	var text = input.value;
	input.select();
  if (window.clipboardData) {
    window.clipboardData.setData("Text",text);
  } else {
    var fc = 'flashclipboard';
    if(!$(fc)) {
      var d = document.createElement('div');
      d.id = fc;
      document.body.appendChild(d);
    }
    $(fc).innerHTML = '<embed src="_kopyala.swf" FlashVars="'+islem+'='+escape(text)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
  }
}

function palet(v,e) {
	if (!$("paletkodu")) {
	var div = document.createElement("div");
	div.id = "paletkodu";
	div.innerHTML = "<div id=\"rp\" style=\"top:0px;left:0px;z-index:20;display:none;\" onmousedown=\"HSVslide('drag','rp',event)\"> <div id=\"plugCUR\"></div><div id=\"plugHEX\" onmousedown=\"stop=0;setTimeout('stop=1',100);\"></div><div id=\"plugCLOSE\" onmousedown=\"toggle('rp')\">Tamam</div><br><div id=\"SV\" onmousedown=\"HSVslide('SVslide','rp',event)\"><div id=\"SVslide\" style=\"top:-4px;left:-4px;\"><br /></div></div><form id=\"H\" onmousedown=\"HSVslide('Hslide','rp',event)\"><div id=\"Hslide\" style=\"top:-7px;left:-8px;\"><br /></div><div id=\"Hmodel\"></div></form></div>";
	$$("body")[0].appendChild(div);
	}
	
	d = $S("rp");
	_offset = absPos($(v.id));
	d.left=_offset.x+24+'px'; d.top=_offset.y+'px';
	renkutusu = v.id;
	color=$(renkutusu+'2').value;
	toggle('rp');	
	updateH(color);
	loadSV();
}

function absPos (_object) {
	var r = {
		x : _object.offsetLeft,
		y : _object.offsetTop
	};
	if (_object.offsetParent) {
		var v = absPos(_object.offsetParent);
		r.x += v.x;
		r.y += v.y;
	}
	return r;
}

function mkColor(v){$S(renkutusu).background='#'+v;$(renkutusu+'2').value=v;$(renkutusu+'2').onkeyup();}

var standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body
function $(v) { return(document.getElementById(v)); }
function $$(v) { return(document.getElementsByTagName(v)); }
function $S(v) { return($(v).style); }
function browser(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function toggle(v) { $S(v).display=($S(v).display=='none'?'block':'none');if ($S(v).display=='none'){kodyarat();}}
function within(v,a,z) { return((v>=a && v<=z)?true:false); }
function XY(e,v) { var z=browser('msie')?Array(event.clientX+standardbody.scrollLeft,event.clientY+standardbody.scrollTop):Array(e.pageX,e.pageY); return(z[zero(v)]); }
function zero(v) { v=parseInt(v); return(!isNaN(v)?v:0); }
function zindex(d) { d.style.zIndex=zINDEX++; }

/* PLUGIN */

var maxValue={'h':'359','s':'100','v':'100'},HSV={0:359,1:100,2:100};
var SVHeight=165,wSV=162,wH=162,slideHSV={0:359,1:100,2:100},zINDEX=15,stop=1;

function HSVslide(d,o,e) {

	function tXY(e) { tY=XY(e,1)-top; tX=XY(e)-left; }
	function mkHSV(a,b,c) { return(Math.min(a,Math.max(0,Math.ceil((parseInt(c)/b)*a)))); }
	function ckHSV(a,b) { if(within(a,0,b)) return(a); else if(a>b) return(b); else if(a<0) return('-'+oo); }
	function drag(e) { if(!stop) {
	
		if(d=='SVslide') { tXY(e); ds.left=ckHSV(tX-oo,wSV)+'px'; ds.top=ckHSV(tY-oo,wSV)+'px';
		
			slideHSV[1]=mkHSV(100,wSV,ds.left); slideHSV[2]=100-mkHSV(100,wSV,ds.top); HSVupdate();
			
		}
		else if(d=='Hslide') {
		
			tXY(e); ds.top=(ckHSV(tY-oo,wH)-5)+'px'; slideHSV[0]=mkHSV(359,wH,ds.top);
 
			function commit() { var r='hsv',z={},j='';

				for(var i=0; i<=r.length-1; i++) { j=r.substr(i,1); z[i]=(j=='h')?maxValue[j]-mkHSV(maxValue[j],wH,ds.top):HSV[i]; }
				
				return(HSVupdate(hsv2hex(z)));

			}

			mkColor(commit()); $S('SV').backgroundColor='#'+hsv2hex(Array(HSV[0],100,100));
		
		}
		else if(d=='drag') { ds.left=XY(e)+oX-eX+'px'; ds.top=XY(e,1)+oY-eY+'px'; }
	}
	if (e && e.preventDefault)
		e.preventDefault()
	else
		return false
	}

	if(stop) { stop=''; var ds=$S(d!='drag'?d:o);

		if(d=='drag') { var oX=parseInt(ds.left), oY=parseInt(ds.top), eX=XY(e), eY=XY(e,1); zindex($(o)); }
		else { var left=($(o).offsetLeft+10), top=($(o).offsetTop+22), tX, tY, oo=(d=='Hslide')?2:4; if(d=='SVslide') slideHSV[0]=HSV[0]; }
		document.onmousemove=drag; document.onmouseup=function(){ stop=1; document.onmousemove=''; document.onmouseup=''; }; drag(e);

	}
}

function HSVupdate(v) { HSV=v?hex2hsv(v):Array(slideHSV[0],slideHSV[1],slideHSV[2]);
	if(!v) v=hsv2hex(Array(slideHSV[0],slideHSV[1],slideHSV[2]));
	mkColor(v); $('plugHEX').innerHTML=v; $('plugCUR').style.backgroundColor="#"+v; return(v);

}

function loadSV() { var z=''; for(var i=SVHeight; i>=0; i--) z+="<div style=\"BACKGROUND: #"+hsv2hex(Array(Math.round((359/SVHeight)*i),100,100))+";\"><br /><\/div>"; $('Hmodel').innerHTML=z;}

function updateH(v) { HSV=hex2hsv(v);
	HSVupdate(v);
	$S('SV').backgroundColor='#'+hsv2hex(Array(HSV[0],100,100)); 
	$S('SVslide').top=(parseInt(wSV-wSV*(HSV[2]/100))-4)+'px'; $S('SVslide').left=parseInt(wSV*(HSV[1]/100))+'px';
	$S('Hslide').top=(parseInt(wH*((maxValue['h']-HSV[0])/maxValue['h']))-7)+'px';
}

/* CONVERSIONS */

function toHex(v) { v=Math.round(Math.min(Math.max(0,v),255)); return("0123456789ABCDEF".charAt((v-v%16)/16)+"0123456789ABCDEF".charAt(v%16)); }
function hex2rgb(r) { return({0:parseInt(r.substr(0,2),16),1:parseInt(r.substr(2,2),16),2:parseInt(r.substr(4,2),16)}); }
function _rgb2hex(r) { return(toHex(r[0])+toHex(r[1])+toHex(r[2])); }
function hsv2hex(h) { return(_rgb2hex(hsv2rgb(h))); }	
function hex2hsv(v) { return(rgb2hsv(hex2rgb(v))); }

function rgb2hsv(r) { // easyrgb.com/math.php?MATH=M20#text20

	var max=Math.max(r[0],r[1],r[2]),delta=max-Math.min(r[0],r[1],r[2]),H,S,V;
	
	if(max!=0) { S=Math.round(delta/max*100);

		if(r[0]==max) H=(r[1]-r[2])/delta; else if(r[1]==max) H=2+(r[2]-r[0])/delta; else if(r[2]==max) H=4+(r[0]-r[1])/delta;

		var H=Math.min(Math.round(H*60),360); if(H<0) H+=360;

	}

	return({0:H?H:0,1:S?S:0,2:Math.round((max/255)*100)});

}

function hsv2rgb(r) { // easyrgb.com/math.php?MATH=M21#text21

	var R,B,G,S=r[1]/100,V=r[2]/100,H=r[0]/360;

	if(S>0) { if(H>=1) H=0;

		H=6*H; F=H-Math.floor(H);
		A=Math.round(255*V*(1.0-S));
		B=Math.round(255*V*(1.0-(S*F)));
		C=Math.round(255*V*(1.0-(S*(1.0-F))));
		V=Math.round(255*V); 

		switch(Math.floor(H)) {

			case 0: R=V; G=C; B=A; break;
			case 1: R=B; G=V; B=A; break;
			case 2: R=A; G=V; B=C; break;
			case 3: R=A; G=B; B=V; break;
			case 4: R=C; G=A; B=V; break;
			case 5: R=V; G=A; B=B; break;

		}

		return({0:R?R:0,1:G?G:0,2:B?B:0});

	}
	else return({0:(V=Math.round(V*255)),1:V,2:V});

}
