var _domain = "http://www.orangecinema.ch/";
var _wetterCityID = "06660000";

/* force to start loadXML */
window.onload=loadXML();


//==============================================
// handle xml loading
//==============================================
function loadXML()
{
// Values you provide
	var xmlRequest;
	var	requestParam = '';
    var feedURL = _domain + "iphone09/XML/prognose.xml";						// The feed to fetch
	
// XMLHttpRequest setup code
	if (typeof XMLHttpRequest != 'undefined') {
		xmlRequest = new XMLHttpRequest();
		requestParam = 'dom';
	}
    
    if (!xmlRequest) {
		try {
        		xmlRequest  = new ActiveXObject("Msxml2.XMLHTTP");
    	} 
    	catch(e) {
        	try {
            	xmlRequest  = new ActiveXObject("Microsoft.XMLHTTP");
        	} 
        	catch(e) {
            	xmlRequest  = null;
            }
        }
    }
    
    if( requestParam == 'dom' ) {
    	xmlRequest.onload = function() { xmlLoaded(xmlRequest); };
    }
    else {
   		// IE etc.
    	xmlRequest.onreadystatechange = function() { xmlLoaded(xmlRequest); };
    }
    	
    xmlRequest.open("GET", feedURL);
    xmlRequest.setRequestHeader("Cache-Control", "no-cache");
    xmlRequest.send(null);
}



// Called when an XMLHttpRequest loads a feed; works with the XMLHttpRequest setup snippet
function xmlLoaded( xmlRequest ) 
{

	
	
	if (xmlRequest.status == 200) {

// get the xml data
        var nodelist = xmlRequest.responseXML.getElementsByTagName( 'Location' );
        
       
// loop trought every element
        for(var nodeIndex= 0; nodeIndex<nodelist.length; nodeIndex++)
        {   
        
/* check the location id */
            var node    = nodelist[ nodeIndex ];
            var cityID  = node.getAttribute('id');
            

/* if id is equal, get other elements */
            if( _wetterCityID == cityID ) {
                
                var daylist = node.getElementsByTagName( 'Day' );

/* lop throught day elements */
                for( dayIndex= 0; dayIndex<daylist.length; dayIndex++)
                {  
                    var dayNode = daylist[ dayIndex ];
                    var dateXML = dayNode.getAttribute('val');

/* generate date object and build string with current date */
                    var currentDate = new Date();
                    var dateStringForDisplay = '';
/* get year */
                    year    = currentDate.getFullYear();
                    year    += ''; // convert to string
                    
/* get month formate XX */
                    month   = currentDate.getMonth()+1;
                    if( month < 10 ) {
                        month    += ''; // convert to string
                        month = '0'+month;
                    }
                    month    += '';
                    
/* get day formate XX */
                    day     = currentDate.getDate();
                    if( day < 10 ) {
                        day    += ''; // convert to string
                        day = '0'+day;
                    }
                    day    += '';
                    
/* build date string */
                    var dateString  = year + month + day;
                    
                    //alert(dateXML);

/* if XML date is equals current date, get data */
                    if( dateString == dateXML ) {

                 		var xmlRecord = new XML_RECORD( dayNode );
                 		
                 		var temperaturTextField = document.getElementById( "temperaturTextField" );
						temperaturTextField.innerHTML = xmlRecord.temperatur + "&deg;";
                 		
                 		var imageUrl = _domain + '/images/wetterImages/' + xmlRecord.symbol + '.jpg';
                 		
                 		//alert(imageUrl);
                 		
						var img = document.getElementById( 'canvasWetter' )
						img.innerHTML = '<img src="'+imageUrl+'" alt="" width="60" height="60" border="0">';
                    
/* fill our view with data */
                        
                    }
                }
            }
        }
	}
	
}



function XML_RECORD( node )
{   
    this.prognose_DE            = node.getElementsByTagName('TxtAft')[0].firstChild.nodeValue;
    this.prognose_FR            = node.getElementsByTagName('TxtAftfr')[0].firstChild.nodeValue;
    this.symbol                 = node.getElementsByTagName('SymbAft')[0].firstChild.nodeValue;
    this.temperatur             = node.getElementsByTagName('TempAft')[0].firstChild.nodeValue;
    this.windGeschwindigkeit    = node.getElementsByTagName('Force')[0].firstChild.nodeValue;
    this.windRichtung           = node.getElementsByTagName('Dir')[0].firstChild.nodeValue;

    return this;
}
