var myDocRoot = 'keldan/';
var imgClock = new Image();
var clock_x_offset=0;
var clock_y_offset=0;
var clock_image_width=63;
var clock_image_height=63;
var clock_radius = 31.5;
var clock_correction = [0.05,0.145,0.25];
var clock_hour_correction=3.0;
var clock_minute_correction=15.0;
var clock_onehour_rad=0.5;
var clock_seconds_ticker=0;
var clock_months_is = ['janúar','febrúar','mars','apríl','maí','júní','júlí','ágúst','september','október','nóvember','desember'];
var clock_days_is = ['Sunnudagur','Mánudagur','Þriðjudagur','Miðvikudagur','Fimmtudagur','Föstudagur','Laugardagur'];
var clock_months_en = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var clock_days_en = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var clock_city_timezone = new Array();
clock_city_timezone['London'] = {offset:0,region:"Europe"};
clock_city_timezone['Tokyo'] = {offset:9,region:"Tokyo"};
clock_city_timezone['Stockholm'] = {offset:1,region:"Europe"};
clock_city_timezone['New York'] = {offset:-5,region:"NAmerica"};


function drawClock() {
	//clockSetDateText();
	
	imgClock.onload = function() {
		clockDrawAll();
	}
	imgClock.src = 'themes/keldanpremium/images/taskbar/black/clock.png';
	setInterval(clockDrawAll,1000);
}
/*
function drawClock() {
	clockSetDateText();
	var clockEl = document.getElementById('clock_canvas');
	
	clock_x_offset = 200;
	clock_y_offset = 0;
	
	clock_center_x = clock_x_offset+clock_radius;
	clock_center_y = clock_y_offset+clock_radius;
	
    var ctx = clockEl.getContext('2d');
    var imgClock = new Image();
    imgClock.onload = function(){
      ctx.drawImage(imgClock,clock_x_offset,clock_y_offset);
      
      ctx.translate(clock_x_offset+49.3,clock_y_offset+50.5);
    
      drawHourArm(ctx);
      drawMinuteArm(ctx);
    
      setInterval("clockDrawAll()",10000);
     
    }
    imgClock.src = '/themes/hfvtheme/images/analog-clock.png';
}
*/
function clockDrawAll(ctx) {
	clockSetDateText();
	//clock_x_offset = 200;
	//clock_y_offset = 0;
	var clockEl = document.getElementById('clock_canvas');
	clockEl.width=63;
	clockEl.height=63;
	var ctx = clockEl.getContext('2d');
	
	//ctx.clearRect(0,0,100,100);
	
	ctx.save();
	
	ctx.drawImage(imgClock,clock_x_offset,clock_y_offset,clock_image_width,clock_image_height);
	ctx.translate(clock_x_offset+clock_radius,clock_y_offset+clock_radius-0.5);
	var d = new Date();
	drawHourArm(ctx,d);
	drawMinuteArm(ctx,d);
	drawSecondsArm(ctx,d);
	ctx.restore();
    
}


function drawHourArm(/* 2d context */ ctx,date) {
	ctx.save();
	//At 21:00 correction is 0.25, i.e. 4.75
	//At 18:00 correction is 0.145, i.e. 3.145
	//At 15:00 correction is 0.05, i.e. 1.55
	var angle = calculateForHour(date);
	
    ctx.rotate(angle);
    
	ctx.lineWidth=1.0;
    ctx.strokeStyle='#a3a6a8';
    ctx.beginPath();
    ctx.moveTo(0,0);
    ctx.lineTo(1.0,0);
    ctx.lineTo(0,-15)
    ctx.lineTo(-1.0,0);
    ctx.lineTo(0,0);
    //ctx.lineTo(0,-15);
    
    
    ctx.fillStyle = '#a3a6a8';
    ctx.fill();
    
    ctx.stroke();
    ctx.restore();
}
function drawMinuteArm(/* 2d context */ ctx,date ) {
	ctx.save();
	var angle = calculateForMinutes(clockGetDate());
	
	//ctx.rotate(3.145);
	//At 21:00 correction is 0.225,i.e 4.725
	//At 18:00 correction is 0.145,i.e 3.145
	//At 15:00 correction is 0.05,i.e 1.55 
	//console.log('minutes index: '+minutes[d.getUTCMinutes()]+" minutes is "+d.getUTCMinutes());
	ctx.rotate(minutes[date.getUTCMinutes()]);
	
	ctx.lineWidth=0.75;
    ctx.strokeStyle='#a3a6a8';
    ctx.beginPath();

    //line one starts here
    ctx.moveTo(0,0);
    ctx.lineTo(0.75,0);
    ctx.lineTo(0,-20)
    ctx.lineTo(-0.75,0);
    ctx.lineTo(0,0);
    
    ctx.fillStyle = '#a3a6a8';
    ctx.fill();
    
    ctx.stroke();
    
    ctx.restore();
}
function drawSecondsArm(/* 2d context */ ctx,date ) {
	ctx.save();
	
	
	//ctx.rotate(3.15);
	
	ctx.rotate(seconds[date.getUTCSeconds()]);
	//ctx.rotate(seconds[date.getUTCSeconds()]+0.1);
	//ctx.rotate(seconds[date.getUTCSeconds()]-0.1);
	
	
	ctx.lineWidth=0.7;
    ctx.strokeStyle='#a3a6a8';
    ctx.beginPath();

    //line one starts here
    ctx.moveTo(0,0);
    ctx.lineTo(0,-28);
    
    ctx.fillStyle = '#a3a6a8';
    //ctx.fill();
    
    ctx.stroke();
    
    ctx.restore();
}
function clockGetDate() {
	 
	 var d = new Date(NTP.fixTime());
	 return d;
}
function calculateForHour(date) {
	var h = date.getUTCHours();
	if(h >= 12)
		h -= 12;
	//alert('h starts : '+h);
	
	var correction = clockCalculateCorrection(h,clock_hour_correction);
	
	h = h/2.0;
	//alert('h is '+h);
	return h+correction + ( (clock_onehour_rad*date.getUTCMinutes()) / 60 );
	
	
}
function calculateForMinutes(date) {
	var m = date.getUTCMinutes();
	var correction = 0;//clockCalculateCorrection(m,clock_minute_correction);
	m = m/10.0;
	return m + correction;
	
	
}
function calculateForSeconds(date) {
	var s = date.getUTCSeconds();
	var c = clockCalculateCorrection(s,clock_minute_correction);
	s = s/10.0;
	return s+c;
}

function clockCalculateCorrection(val,corr_period) {
	var corrIdx = (( val - (val % corr_period) ) / corr_period) -1 ; // -1
	if(corrIdx >= 0 && corrIdx <= 2) 
		return clock_correction[corrIdx];
	else
		return 0;
}


function syncWithServer() {

}
function clockSetDateText() {
	var preEl = document.getElementById('date-text-pre');
	if(preEl.firstChild)
		preEl.removeChild(preEl.firstChild);
	preEl.appendChild(document.createTextNode(keldan_lang=='is'?clockGetISDateString(clockGetDate()):
		clockGetENDateString(clockGetDate())));
}
function  clockGetENDateString(date) {
	var year = date.getUTCFullYear();
	var month = date.getUTCMonth();
	var dayOfMonth = date.getUTCDate();
	var day = date.getUTCDay();
	
	if(dayOfMonth==1 || dayOfMonth==21 || dayOfMonth==31)
		dayOfMonth += "st";
	else if(dayOfMonth==2 || dayOfMonth==22)
		dayOfMonth += "nd";
	else if(dayOfMonth==3 || dayOfMonth==23)
		dayOfMonth += "rd";
	else
		dayOfMonth += "th";
	var s = clock_days_en[day]+" "+clock_months_en[month]+" "+dayOfMonth+" "+year;
	return s;
}
function  clockGetISDateString(date) {
	var year = date.getUTCFullYear();
	var month = date.getUTCMonth();
	var dayOfMonth = date.getUTCDate();
	var day = date.getUTCDay();
	
	var s = clock_days_is[day]+"  "+dayOfMonth+". "+clock_months_is[month]+" "+year;
	return s;
	
	
}
/* Since we have seconds ticking its cheaper to lookup value for each second from a static array.
 * Its not as cool, and the other method was implemented, but cheaper.
*/
var seconds = [
               0.0,0.1,0.2,0.3,0.4,0.525,
               0.625,0.725,0.825,0.925,1.05,
               1.15,1.25,1.35,1.45,1.575,
               1.675,1.775,1.875,1.975,2.075,
               2.175,2.275,2.375,2.475,2.6,
               2.7,2.8,2.9,3.0,3.15,
               3.25,3.35,3.45,3.55,3.65,
               3.75,3.85,3.95,4.05,4.18,
               4.28,4.38,4.48,4.58,4.7,
               4.8,4.9,5.0,5.1,5.24,
               5.34,5.44,5.55,5.65,5.775,
               5.875,5.975,6.075,6.175,0.0
               ];

var minutes = [0.0,0.0,0.1,0.2,0.3,0.51,
               0.61,0.71,0.81,0.91,1.05,
               1.15,1.25,1.35,1.45,1.55,
               1.65,1.75,1.85,1.95,2.075,
               2.175,2.275,2.375,2.475,2.595,
               2.7,2.8,2.9,3.0,3.145,
               3.245,3.345,3.445,3.545,3.66,
               3.75,3.85,3.95,4.05,4.19,
               4.3,4.4,4.5,4.6,4.725,
               4.825,4.925,5.025,5.125,5.23,
               5.33,5.43,5.53,5.63,5.77,
               5.87,5.97,6.1,6.2,0,0									
               ]
