var STR_PAD_LEFT = 1;
var STR_PAD_RIGHT = 2;
var STR_PAD_BOTH = 3;

function ImageLoadFailed() {
	window.event.srcElement.style.display = "None";
	window.event.srcElement.style.height = 0;
	window.event.srcElement.style.width = 0;
	window.event.srcElement.style.position = 0;
}
//function pausecomp(millis) 
//{
//var date = new Date();
//var curDate = null;
//
//do { curDate = new Date(); } 
//while(curDate-date < millis);
//}
 
function pad(str, len, pad, dir) {
	if (typeof(len) == "undefined") { var len = 0; }
	if (typeof(pad) == "undefined") { var pad = ' '; }
	if (typeof(dir) == "undefined") { var dir = STR_PAD_RIGHT; }
	if (len + 1 >= str.length) {
		switch (dir){
			case STR_PAD_LEFT:
				str = Array(len + 1 - str.length).join(pad) + str;
			break;
			case STR_PAD_BOTH:
				var right = Math.ceil((padlen = len - str.length) / 2);
				var left = padlen - right;
				str = Array(left+1).join(pad) + str + Array(right+1).join(pad);
			break;
			default:
				str = str + Array(len + 1 - str.length).join(pad);
			break;
		} // switch
	}
	return str;
}

/* 
 This file was generated by Dashcode.  
 You may edit this file to customize your widget or web page 
 according to the license.txt file included in the project.
 */

var listController = {
    // This object acts as a controller for the list UI.
    // It implements the dataSource methods for the list.
    
    numberOfRows: function() {
        // The List calls this dataSource method to find out how many rows should be in the list.
        return weatherMaps.length;
    },
    
    prepareRow: function(rowElement, rowIndex, templateElements) {
        // The List calls this dataSource method for every row.  templateElements contains references to all
        // elements inside the template that have an id. We use it to fill in the text of the rowTitle element.
        if (templateElements.rowTitle) {
            templateElements.rowTitle.innerText = weatherMaps[rowIndex].name;
        }
        //templateElements.listURL.innerText = weatherMaps[rowIndex].url;
        
        templateElements.images.innerHTML = "";
        
        
        
//      	templateElements.images.innerHTML += "<div style='-webkit-border-radius:10px; border:0px solid #000;'><img id='smallImage' src='"+weatherMaps[rowIndex].urls[0]+"' class='smallImage'/></div>";
					templateElements.images.innerHTML += "<div id='smallImage' class='smallImage' style='background:url("+weatherMaps[rowIndex].urls[0]+");'></div>";

      	
      	
//        for (item in weatherMaps[rowIndex].urls){
//        	templateElements.images.innerHTML += "<img id='smallImage' src='"+weatherMaps[rowIndex].urls[item]+"' height=80 class=smallImage/> ";
//        }
        
//        templateElements.smallImage.src = weatherMaps[rowIndex].url;
//        templateElements.smallImage.width = 80;
//        templateElements.smallImage.left = 210;

        // We also assign an onclick handler that will cause the browser to go to the detail page.
        var self = this;
        var handler = function() {
            var weatherMap = weatherMaps[rowIndex];
            detailController.setweatherMap(weatherMap);
            var browser = document.getElementById('browser').object;
            // The Browsers goForward method is used to make the browser push down to a new level.
            //Going back to previous levels is handled automatically.
            browser.goForward(document.getElementById('detailLevel'), weatherMap.name);
        };
        rowElement.onclick = handler;

    }
};

var detailController = {
    // This object acts as a controller for the detail UI.
    
    setweatherMap: function(weatherMap) {

        this._weatherMap = weatherMap;
        this._representedObject = weatherMap.name;
        // When the weatherMap is set, this controller also updates the DOM for the detail page appropriately.
        //As you customize the design for the detail page, you will want to extend this code to make sure
        //that the correct information is populated into the detail UI.
        var detailTitle = document.getElementById('detailTitle');
        detailTitle.innerHTML = this._weatherMap.name;
//        var detailURL = document.getElementById('detailURL');
//        detailURL.innerHTML = this._weatherMap.url;

				var fullImages=document.getElementById('fullimages');
        fullImages.innerHTML = "";
        if (weatherMap.name!="Rainfall Radar"){
	        for (item in this._weatherMap.urls){
	        	//fullImages.innerHTML += "<div id='fullImage' class='fullImage' style='background:url("+this._weatherMap.urls[item]+");'></div>";
	        	fullImages.innerHTML += "<div style='-webkit-border-radius:10px; border:0px solid #000;'><img id='fullImage' src='"+this._weatherMap.urls[item]+"' class='fullImage' style='-webkit-border-radius:10px; border:0px solid #000;'/></div>";
	        }
	      } else {
	      	//build dynamic urls
	      	//http://www.met.ie/weathermaps/radar/WEB_radar_200908101330.gif

        	//fullImages.innerHTML += "<img id='fullImage' src='"+this._weatherMap.urls[0]+"' width=300 class=image/>";
      		//fullImages.innerHTML += "<div id='fullImage' class='fullImage' style='background:url("+this._weatherMap.urls[0]+");''></div>";

	      	var current_date = new Date();
	      	var newDate = new Date(current_date);
	      	var durationInMinutes = 15;

	        for (i=0;i<(24*60);i+=15){
						newDate.setHours(parseInt(i/60));
						newDate.setMinutes(i%60);
						newDate.setSeconds(0);
		      	datevalue = newDate.getFullYear().toString() +
		      							pad((newDate.getMonth()+1).toString(),2,"0",STR_PAD_LEFT) +
		      							pad(newDate.getDate().toString(),2,"0",STR_PAD_LEFT) +
		      							pad(newDate.getHours().toString(),2,"0",STR_PAD_LEFT) +
		      							pad(newDate.getMinutes().toString(),2,"0",STR_PAD_LEFT);
	      	
	      		var imageURL = "http://www.met.ie/weathermaps/radar/WEB_radar_"+datevalue+".gif";
						var badImg = new Image();
						badImg.src = imageURL;
						//pausecomp(10);
        		//fullImages.innerHTML += imageURL+"<br>";
						if(badImg.naturalWidth==0||badImg.naturalHeight==0){
	        		fullImages.innerHTML += "";
	        	} else {
	        		fullImages.innerHTML += "<img class='fullImage' src='"+imageURL+"' OnError='ImageLoadFailed()'/><br/>\n";
	        		//fullImages.innerHTML += "<div id='fullImage' class='fullImage' style='background:url("+imageURL+");''></div>";
	        	}




	      	
//	        for (i=0;i<96;i++){
//						var roundedMinutes = Math.round(((newDate.getMinutes() - durationInMinutes ) % 60)/15,0)*15;
//						newDate.setMinutes(roundedMinutes);
//						newDate.setSeconds(0);
//		      	datevalue = newDate.getFullYear().toString() +
//		      							pad((newDate.getMonth()+1).toString(),2,"0",STR_PAD_LEFT) +
//		      							pad(newDate.getDate().toString(),2,"0",STR_PAD_LEFT) +
//		      							pad(newDate.getHours().toString(),2,"0",STR_PAD_LEFT) +
//		      							pad(newDate.getMinutes().toString(),2,"0",STR_PAD_LEFT);
//	      	
//	      		var imageURL = "http://www.met.ie/weathermaps/radar/WEB_radar_"+datevalue+".gif";
//						var badImg = new Image();
//						badImg.src = imageURL;
//						if(!badImg.width){
//	        		fullImages.innerHTML += "";
//	        	} else {
//	        		fullImages.innerHTML += "<img class='fullImage' src='"+imageURL+"'/>";
//	        		//fullImages.innerHTML += imageURL+"<br>";
//	        		//fullImages.innerHTML += "<div id='fullImage' class='fullImage' style='background:url("+imageURL+");''></div>";
//	        	}


	        }
      		fullImages.innerHTML +=  "<br>";
	      }



    }
    
};

//
// Function: load()
// Called by HTML body elements onload event when the web application is ready to start
//
function load()
{
    dashcode.setupParts();
}

// Sample data.  Some applications may have static data like this, but most will want to use information fetched remotely via XMLHttpRequest.
var weatherMaps = [
    { name: "Rainfall Radar",          urls: ["http://www.met.ie/weathermaps/Web_radar.gif"] }, 
    { name: "3 Hour Forecast",        urls: ["http://www.met.ie/weathermaps/3hr-rain.gif","http://www.met.ie/weathermaps/3hr-wind.gif","http://www.met.ie/weathermaps/3hr-wind-iso.gif"]      }, 
    { name: "Noon Today",             urls: ["http://www.met.ie/weathermaps/ISO_TODAY_ATL.png","http://www.met.ie/weathermaps/PREC_TODAY_ATL.png","http://www.met.ie/weathermaps/CLD_TODAY_ATL.png"]    }, 
    { name: "Noon Tomorrow",          urls: ["http://www.met.ie/weathermaps/ISO_TOMORROW_ATL.png","http://www.met.ie/weathermaps/PREC_TOMORROW_ATL.png","http://www.met.ie/weathermaps/CLD_TOMORROW_ATL.png"] }, 
    { name: "Satellite Ireland",      urls: ["http://www.met.ie/weathermaps/WEB_sat_ir_irl.jpg","http://www.met.ie/weathermaps/WEB_sat_vis_irl.jpg"]   }, 
    { name: "Satellite Europe",      urls: ["http://www.met.ie/weathermaps/WEB_sat_ir_eur.jpg","http://www.met.ie/weathermaps/WEB_sat_vis_eur.jpg"]   }, 
    { name: "Satellite Globe",      urls: ["http://www.met.ie/weathermaps/WEB_sat_ir_full.jpg","http://www.met.ie/weathermaps/WEB_sat_vis_full.jpg"]  }
];

