/*Copyright for the functions drawLine(x1, y1, x2, y2,t) and setPixel (x, y, w, h). 
Source: http://www.codeproject.com/KB/scripting/javascriptlinedrawing.aspx*/
/**
 * Copyright (C) 2002-2005
 * W3L GmbH
 * Technologiezentrum Ruhr
 * Universitätsstraße 142
 * D-44799 Bochum
 * 
 * Author: Dipl.Ing. Doga Arinir
 * E-Mail: doga.arinir@w3l.de
 *
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the author or the company be held liable 
 * for any damages arising from the use of this software. EXPECT BUGS!
 * 
 * You may use this software in compiled form in any way you desire PROVIDING it is
 * not sold for profit without the authors written permission, and providing that this
 * notice and the authors name is included. If the source code in this file is used in 
 * any commercial application then acknowledgement must be made to the author of this file.
 */
 
 var hourHandLength=11;
 var minuteHandLength=16;
 var secHandLength=18;
 var xOffset = 22;
 var yOffset = 22;
 var hourHandColor="#55aaee";
 var minuteHandColor="#55aaee";
 var secHandColor="#336699";
 
 function startClock() 
    {
        var time = new Date();
        var hours = time.getHours();
        var minutes = time.getMinutes();
        var seconds = time.getSeconds();
        calculateAndDraw(hours,minutes,seconds);
        setTimeout("startClock()", 1000);
    }

        function calculateAndDraw(h,m,s)
        {
            clearTime();
            
            // Draw the hours hand.
            if(m>25 && m<=40)
            {
                h=h+0.5;
            }
            else if(m>40 && m<=59)
            {
                h=h+1;
            }
            x2=hourHandLength*Math.cos(((h-3)/6)*Math.PI);
            y2=hourHandLength*Math.sin(((h-3)/6)*Math.PI);
            x2=Math.round(x2);
            y2=Math.round(y2);
            drawLine(xOffset, yOffset,
                     xOffset + x2, yOffset + y2,
                     1, hourHandColor);

            // Draw the minutes hand.
            x2=minuteHandLength*Math.cos(((m-15)/30)*Math.PI);
            y2=minuteHandLength*Math.sin(((m-15)/30)*Math.PI);
            x2=Math.round(x2);
            y2=Math.round(y2);
            drawLine(xOffset, yOffset,
                     xOffset + x2, yOffset + y2,
                     1, minuteHandColor);

            // Draw the seconds hand.
            if(s==45)
                var a =2;
            x2=secHandLength*Math.cos(((s-15)/30)*Math.PI);
            y2=secHandLength*Math.sin(((s-15)/30)*Math.PI);
            x2=Math.round(x2);
            y2=Math.round(y2);
            drawLine(xOffset, yOffset, 
                     xOffset + x2, yOffset + y2,
                     0, secHandColor);

        }
        var backbuffer="";
        function drawPixel()
        {
            //document.body.innerHTML=divPixel;
            document.getElementById("clock").innerHTML=backbuffer;
        }
        function clearTime()
        {
            document.getElementById("clock").innerHTML="";
            backbuffer="";
        }
        function setPixel (x, y, w, h,color)
        {
        	this.backbuffer += '<div style="position:absolute;left:'+x+'px;top:'+y+'px;width:'+w+'px;height:'+h+'px;background-color:'+color+';overflow:hidden;"></div>';
        }


        function drawLine(x1, y1, x2, y2,t,color)
        {
	        //Always draw from left to right. This makes the algorithm much easier...
	        if (x1 > x2)	{
		    var tmpx = x1; var tmpy = y1;
		    x1 = x2; y1 = y2;
		    x2 = tmpx; y2 = tmpy;
	    }
	
	        var dx = x2 - x1;	
	        var dy = y2 - y1; var sy = 1;	
	        if (dy < 0)	{
		    sy = -1;
		    dy = -dy;
	    }
	
	        dx = dx << 1; dy = dy << 1;
	        if (dy <= dx)
	    {	
		    var fraction = dy - (dx>>1);
		    var mx = x1;
		    while (x1 != x2)
		    {
			    x1++;
			    if (fraction >= 0)
			{
				setPixel(mx, y1,x1-mx+t,1+t,color);
				y1 += sy;
				mx = x1;
				fraction -= dx;
			}
			fraction += dy;
		}
		setPixel(mx,y1,x1-mx+t,1+t,color);
	} 
	else 
	{
		var fraction = dx - (dy>>1);
		var my = y1;
		if (sy > 0)
		{		
			while (y1 != y2)
			{
				y1++;
				if (fraction >= 0)
				{
					this.setPixel(x1++, my,1+t,y1-my+t,color);
					my = y1;
					fraction -= dy;
				}
				fraction += dx;
			}	
			setPixel(x1,my,1+t,y1-my+t,color);
		}
		else
		{
			while (y1 != y2)
			{
				y1--;
				if (fraction >= 0)
				{
					this.setPixel(x1++, y1,1+t,my-y1+t,color);
					my = y1;
					fraction -= dy;
				}
				fraction += dx;
			}	
			setPixel(x1,y1,1+t,my-y1+t,color);		
		}

	}
			drawPixel();
}

