/*
	(c) 2007 Navin Kumar <n@vain.in>
	If you want to use this, please send me a mail!
*/

//rotate image by an angle (anticlockwise) :p
function rotate(image,angle,cx,cy)
{
	var im=typeof(image)=="string"?document.getElementById(image):image;
	
	angle=(angle%360)*Math.PI/180;//in radians

	//replace image with pixels and pixels :p
	var ref = document.createElement("div");
	ref.id="rotated";
	ref.style.position="absolute";
	ref.style.top = im.offsetTop;
	ref.style.left = im.offsetLeft;
	ref.style.width = im.offsetWidth;
	
	im.offsetParent.appendChild(ref);
	
	im.style.visibility="hidden";

	var sr =im.src;
	var h = im.height;
	var w = im.width;
	cx =typeof cx=="undefined"? parseInt(w/2):cx;
	cy =typeof cy=="undefined"? parseInt(h/2):cy;

	var nx,ny,ang,hyp;
	for(var i=0;i<h;i++)
	{
		for(var j=0;j<w;j++){
			var d = document.createElement("div");
			d.style.position="absolute";
			d.style.overflow="hidden";
			nx=j;
			ny=i;
			ang=ny==0?angle:Math.atan(nx/ny)+angle;
			hyp=Math.sqrt(nx*nx + ny*ny);
			nx=Math.round(hyp*Math.sin(ang));
			ny=Math.round(hyp*Math.cos(ang));

			d.style.top = ny;
			d.style.left= nx;

			d.style.height = "1px";
			d.style.width = "1px";

			var ig=document.createElement("img");
			ig.src=sr;
			ig.style.position="relative";
			ig.style.top = -i;
			ig.style.left= -j;
			d.appendChild(ig);

			ref.appendChild(d);
		}
	}

}



//create a reflection of an image 
// fade%: 50%-- the image fades off at half the height;
// keep fade 0 for no fade
function reflect(image,fade,fadeStart)
{
	var im=typeof(image)=="string"?document.getElementById(image):image;

	if(document.getElementById("reflected" + "__" + im.id))
	{
		document.getElementById("reflected" + "__" + im.id).parentNode.removeChild(document.getElementById("reflected" + "__" + im.id));
	}
	
	//replace image with pixels and pixels :p
	var ref = document.createElement("div");
	ref.id="reflected" + "__" + im.id;
	ref.style.position="absolute";
	ref.style.top = im.offsetTop +im.height;
	ref.style.left = im.offsetLeft;
	ref.style.width = im.offsetWidth;
	
	im.offsetParent.appendChild(ref);

	var sr =im.src;
	var h = im.height;
	var w = im.width;
	fadeStart=fadeStart?fadeStart:100;

	var opa=fadeStart;
	for(var i=0;i<h;i++)
	{
		var d = document.createElement("div");
		d.style.position="absolute";
		d.style.overflow="hidden";

		d.style.top = i;
		d.style.left= 0;
		d.style.height = "1px";
		d.style.width = w;
		d.style.opacity=opa;
		d.style.filter = "alpha(opacity="+opa*100+")";
		if(fade){
			opa = fadeStart/100-(100/fade)*(i/h);
			if(opa<0) break;
		}
		var ig=document.createElement("img");
		ig.src=sr;
		ig.style.position="relative";
		ig.style.top = 1-h+i;
		ig.style.left= 0;
		
		d.appendChild(ig);

		ref.appendChild(d);
	}
}
