/**
 * @author Mike Buckley
 * @url http://openhouseconcept.com/
 */
(function($){
    $.fn.ohweather = function(options){    
        var pObj;
        
    	var defaults = {
    		language: "e",
    		imagePath: 'img/weather',
			forecast: {show: false, days: 7, header: false, shortSummary: false},
			loaderId: "weather-loader"
        };
    	
        var options = $.extend(defaults, options);
        
        var displayWeatherData = function(xml){
        	renderForecastDisplay(pObj, options, xml);
        };
        
        var displayLoader = function(req) {
        	pObj.append("<div style='margin: 20px 0 0 20px;' id='" + options.loaderId + "'>Loading</div>");
        };
        
        var removeLoader = function() {
        	jQuery("#" + options.loaderId).remove();
        };
        
        var handleError = function(XMLHttpRequest, textStatus, errorThrown) {
        	debug("error retrieving xml: " + textStatus);
        };
        
        return this.each(function(){
        	pObj = $(this);
        	$.ajax({
                type: "GET",
                url: "/xmlproxy.php?code=" + options.location.siteCode + "&section=" + options.location.province + "&lang=" + options.language,
                dataType: "xml",
                beforeSend: displayLoader,
                success: displayWeatherData,
                complete: removeLoader,
                error : handleError
            });
        });
    };
    
    // private functions
    function renderForecastDisplay(pObj, options, xml) {
    	if(options.forecast.show) {
    		if(options.forecast.header) {
    			pObj.append(createTagStr("h3", "Local Weather"));
    		}
    		pObj.append("<ul id='forecast-list' style='padding-bottom:99px;'>");
    		var forecastCnt = 0;
        	jQuery(xml).find("forecast").each(function(){
        		var forecastName = getElementAttr(jQuery(this), "period", "textForecastName");
        		if(forecastCnt < options.forecast.days) {
	        		var tempEle = jQuery(this).find("temperatures");
					$('#forecast-list').append("<li class='" + forecastCnt + "'></li>");
					$('#forecast-list li.' + forecastCnt).append(getIcon(getElementValue(jQuery(this).find("abbreviatedForecast"), "iconCode"), options));
	        		$('#forecast-list li.' + forecastCnt).append(createTagStr("span", forecastName, "day"));
	        		$('#forecast-list li.' + forecastCnt).append(createTagStr("span", getElementValue(tempEle, "temperature") + "&deg;" + getElementAttr(tempEle, "temperature", "units"), "temp"));
					forecastCnt++;
        		}
        	});
        	pObj.append("</ul>");
        	pObj.append("<a class='arrow' style='margin-left:35px;' href='/overview/calgary-facts/calgary-weather'>Learn about Calgary Climate and Weather</a>");
    	}
    	
    };
    
    function createTagStr(tag, content, cssClass) {
    	return '<' + tag + ' class="' + cssClass + '">' + content + '</' + tag + '>';
    };
    
    function getIcon(iconCode, options) {
    	var iconImg = document.createElement("img");
    	iconImg.setAttribute("src", options.imagePath + "/" + iconCode + ".png");
    	return iconImg;
    };
    
    function getElementValue(ele, name) {
    	return ele.find(name).text();
    };
    
    function getElementAttr(ele, name, attr) {
    	return ele.find(name).attr(attr);
    };
    
    function debug(msg) {
    	if (window.console && window.console.log) {
    		window.console.log('ohweather: ' + msg);
    	}
   };
})(jQuery);
