var xmlHttp
var xml
var newClock
var newFace
var currClockFilename = ""
var clockData = "Hi";

function initClock()
{
	$('#img_c_clock').load(function ()
	{
		// handle data		
		if(xml != null)
		{					
			if($(xml).find('FaceFile').length > 0)
				$('#img_c_face').attr('src',$(xml).find('FaceFile').text());							

			if($(xml).find('X0').length > 0)
			{	    		
				//var cropRatio = parseFloat(100/(xmlHttp.responseXML.getElementsByTagName('X1')[0].firstChild.nodeValue - xmlHttp.responseXML.getElementsByTagName('X0')[0].firstChild.nodeValue))
				var cropRatio = parseFloat(100/($(xml).find('X1').text() - $(xml).find('X0').text()))
				var pic_width = 500;
				var pic_height = 500;
				if($('#img_c_clock').width() > $('#img_c_clock').height())
				{
				   pic_height = (pic_width/$('#img_c_clock').width()) * $('#img_c_clock').height();
				}
				else
				{
				   pic_width = (pic_height/$('#img_c_clock').height()) * $('#img_c_clock').width();
				}

				$('#img_c_face').css('width',(pic_width*cropRatio)+'px');		
				$('#img_c_face').css('height',(pic_height*cropRatio)+'px');
				$('#img_c_face').css('marginLeft',(-$(xml).find('X0').text()*cropRatio)+'px');
				$('#img_c_face').css('marginTop',(-$(xml).find('Y0').text()*cropRatio)+'px');
				
				$('#img_c_face').attr('src', $(xml).find('CurrClock').text());
			}	
			else if($(xml).find('FaceFile').length == 0)
			{
				document.getElementById("img_c_face").style.visibility = "visible"; 
				$('#img_c_face').attr('src','myClock.php?nothing='+$(xml).find('CurrTime').text());
			}
			
			$('#img_c_face').css('visibility','visible');		
		}
	});
}

function getClock()
{
	$('#workDiv').css('visibility','visible');
	
	d = new Date();

        m_h = d.getHours();
        m_m = d.getMinutes();
           
        //if(m_m < 10)
        //  m_m = "0"+m_m;
        
        //if(m_h == 0)
        //  m_h=12; 

	$.get("getclockdata2.php",{ h:m_h,m:m_m,sid:Math.random()}, function (data)
	{ 

		xml = data;
		$('#img_c_face').css({'width' : '100px', 'height' : '100px', 'marginLeft' : '0px' ,'marginTop' : '0px'});
	
		// Has a clock?		
		if($(xml).find('CurrClock').text().length > 0)
		{

			$('#img_c_clock').css('visibility','visible').attr('src',$(xml).find('CurrClock').text());			

			$('#cClockLabel').html('Current Clock: ');	

			var clockInfo = "";

			if($(xml).find('Name').length > 0)
				clockInfo = clockInfo + "Photographer: <a href="+$(xml).find('NameURL').text()+">"+$(xml).find('Name').text()+"</a><br/>";
			if($(xml).find('Title').length > 0)
                          clockInfo = clockInfo + "Title: "+$(xml).find('Title').text()+"<br/>";
			if($(xml).find('Location').length > 0)
			  clockInfo = clockInfo + "Location: "+$(xml).find('Location').text()+"<br/>";
			clockInfo = clockInfo + "Description: "+$(xml).find('CurrDesc').text()+"<br/>";
			if($(xml).find('URL').length > 0)
			clockInfo = clockInfo + "URL: <a href="+$(xml).find('URL').text()+">Click Me</a><br/>";

			$('#clockInfo').html(clockInfo);			
			// Is it a flickr cropped clock?
		}
		else
		{
			$('#img_c_clock').css('visibility','hidden').attr('src','');
			
			$('#img_c_face').css('visibility','visible').attr('src','myClock.php?nothing='+$(xml).find('CurrTime').text());
			$('#cClockLabel').html('Current Time: ');
			$('#clockInfo').html('');
		}

		newClockFilename = $(xml).find('NextClock').text();		
		if(newClockFilename != currClockFilename)
		{    	
			currClockFilename = newClockFilename;
			$('#clockLoadDiv').css('visibility','visible');			
			newClock = new Image();
			newFace = new Image();
			
			newClock.onload = function () {
				$('#clockLoadDiv').css('visibility','hidden');
			};
			newClock.src = "http://slackmaster.net/clockproject/midsize/"+$(xml).find('NextClock').text();
			newFace.src = "http://slackmaster.net/clockproject/clockfaces/"+$(xml).find('NextClock').text();
		}

		$('#cClock').html($(xml).find('CurrTime').text());
		$('#nClock').html($(xml).find('NextTime').text());
		$('#workDiv').css('visibility','hidden');

	});
	
	setTimeout("getClock()",60000 - d.getTime() % 60000)    
}


