var ie = document.all?true:false;
if (!ie){
    document.captureEvents(Event.MOUSEMOVE);
}
document.onmousemove = getMouseXY;
var posx = 0;
var posy = 0;
var clockShowing=false; 
var currentClock;
var currentClockInput;
var clockCount=0;

function setClock(clock_id){
    clockCount++;
    currentClockInput=clock_id;
    currentClock="clock_"+clock_id+clockCount;
}
function drawHourClock(){
    if(!clockShowing){
        var clock=document.createElement("div");
        clock.id=currentClock;
        clock.style.position="absolute";
        clock.style.top=posy-75;
        clock.style.left=posx-75;
        clock.style.width="150px";
        clock.style.height="150px";
        clock.style.background="url('img/clockFace.png')";
        clock.style.zIndex="100";
        clock.appendChild(closeButton(clock,""));
        document.body.appendChild(clock);
        var r=65;
        var xc=65;
        var yc=65;
        var t=-(1/2)*Math.PI;
        var h=12
        while(t<=(4/3)*Math.PI){
            drawHour(xc+r*Math.cos(t),yc+r*Math.sin(t),h.toString(),clock);
            t+=(1/6)*Math.PI;
            h=h==12?1:h+=1;
        }
        clockShowing=true;
    }
}
function drawHour(x,y,hour,clock){
    var h=document.createElement("div");
    h.id="hour_"+hour;
    h.style.position="absolute";
    h.style.top=y;
    h.style.left=x;
    h.style.width="20px";
    h.style.height="20px";
    h.style.lineHeight="20px";
    h.style.verticalAlign="middle";
    h.style.textAlign="center";
    h.style.fontFamily="verdana";
    h.style.fontSize="10px";
    h.innerHTML="<a href=\"javascript:;\" onClick=\"setHour(this.innerHTML);\">"+hour+"</a>";
    clock.appendChild(h);
}
function setHour(hour){
    //alert(hour+" "+currentClock+" "+currentClock.substring(0,currentClock.length-1));
    var current=document.getElementById(currentClockInput);
    current.value=hour;
    drawMinuteClock(current.value);
}
function drawMinuteClock(h){
    var clock=document.getElementById(currentClock)
    clock.innerHTML="";
    clock.appendChild(closeButton(clock,""));
    var current=document.createElement("div");
    current.id="current_time";
    current.style.position="absolute";
    current.style.top="50px";
    current.style.left="0px";
    current.style.width="100%";
    current.style.height="50px";
    current.style.lineHeight="50px";
    current.style.verticalAlign="middle";
    current.style.textAlign="center";
    current.style.fontFamily="verdana";
    current.style.fontSize="40px";
    current.innerHTML=h.toString()
    clock.appendChild(current);
    var r=65;
    var xc=65;
    var yc=65;
    var t=-(1/2)*Math.PI;
    var m=0;
    while(t<=(4/3)*Math.PI){
        drawMinute(xc+r*Math.cos(t),yc+r*Math.sin(t),m==0?"00":m==5?"05":m.toString(),clock);
        t+=(1/6)*Math.PI;
        m+=5;
    }
}
function drawMinute(x,y,min,clock){
    var m=document.createElement("div");
    m.id="min_"+min;
    m.style.position="absolute";
    m.style.top=y;
    m.style.left=x;
    m.style.width="20px";
    m.style.height="20px";
    m.style.lineHeight="20px";
    m.style.verticalAlign="middle";
    m.style.textAlign="center";
    m.style.fontFamily="verdana";
    m.style.fontSize="10px";
    m.innerHTML="<a href=\"javascript:;\" onClick=\"setMin(this.innerHTML);\">"+min+"</a>";
    clock.appendChild(m);
}
function setMin(min){
    var current=document.getElementById(currentClockInput);
    current.value+=":"+min;
    drawAMPMClock(current.value);
}
function drawAMPMClock(curr){
    var clock=document.getElementById(currentClock)
    var current=document.createElement("div");
    current.id="current_time";
    current.style.position="absolute";
    current.style.top="50px";
    current.style.left="0px";
    current.style.width="100%";
    current.style.height="50px";
    current.style.lineHeight="50px";
    current.style.verticalAlign="middle";
    current.style.textAlign="center";
    current.style.fontFamily="verdana";
    current.style.fontSize="40px";
    current.innerHTML=curr;
    clock.innerHTML="";
    clock.appendChild(closeButton(clock,""));
    clock.appendChild(current);
    drawAMPM("10px","AM",clock);
    drawAMPM("120px","PM",clock);
}
function drawAMPM(x,AMPM,clock){
    var ap=document.createElement("div");
    ap.style.position="absolute";
    ap.style.top=x;
    ap.style.left="25px";
    ap.style.width="100px";
    ap.style.height="20px";
    ap.style.lineHeight="20px";
    ap.style.verticalAlign="middle";
    ap.style.textAlign="center";
    ap.style.fontFamily="verdana";
    ap.style.fontSize="20px";
    ap.innerHTML="<a href=\"javascript:;\" onClick=\"setAMPM(this.innerHTML);\">"+AMPM+"</a>";
    clock.appendChild(ap);
}
function setAMPM(AMPM){
    var current=document.getElementById(currentClockInput);
    current.value+=" "+AMPM;
    document.getElementById(currentClock).style.display="none";
    clockShowing=false;
}
//function clearClock(){
//    clockShowing=false;
//    document.getElementById(currentClock).value="";
//}
function getMouseXY(e) {
    if (ie){
        posx = event.clientX + document.body.scrollLeft;
        posy = event.clientY + document.body.scrollTop;
    }else{
        posx = e.pageX;
        posy = e.pageY;
    }
    if (posx < 0){posx = 0;}
    if (posy < 0){posy = 0;}
    return true;
}
function closeButton(container,add_args){
    var closeButton=document.createElement("div");
    closeButton.id=container.id+"_close";
    closeButton.style.position="absolute";
    closeButton.style.top="10px";
    closeButton.style.right="10px";
    closeButton.style.width="10px";
    closeButton.style.height="10px";
    closeButton.style.lineHeight="10px";
    closeButton.style.fontSize="10px"
    closeButton.style.fontFamily="Verdana";
    closeButton.style.verticalAlign="middle";
    closeButton.style.textAlign="center";
    closeButton.style.color="#ddd";
    closeButton.style.background="#eee";
    closeButton.style.border="solid 1px #888";
    closeButton.innerHTML="<a href=\"javascript:;\" onClick=\"document.getElementById('"+container.id+"').style.display='none';clockShowing=false;"+add_args+"\">&times;</a>";
    return closeButton;
}
