﻿function SunriseHourWidget() {
    var HtmlLeft = "<a href=\"http://sunrisehour.com\" target=\"_blank\" style=\"text-decoration: none\"><div style=\"width: %width%; height: %height%; background-image: url(http://sunrisehour.com/images/widget%background%.png); background-position: right bottom\"><div style=\"font-family: Arial,sans-serif; font-size: 20px; color: #ffffff; padding: 20px\">";
    var Html1 = "<div style=\"color: #ffffff;\">%title%</div>";
    var Html2 = "<div style=\"color: #ffffff;\">%location%</div><div style=\"font-size: small; margin-bottom: 1em; color: #ffffff\">%date%</div><table style=\"color: #ffffff; border-collapse: collapse; font-size: medium\"><tr><td>Sunrise</td><td>%sunrise%</td></tr><td>Sunset</td><td>%sunset%</td></tr><tr><td style=\"padding-right: 10px\">Day length</td><td>%daylength% hours</td></tr></table>";
    var HtmlRight = "</div></div></a>";

    this.SetDefaults = function(settings) {
        if (!settings)
            settings = { };
        
        if (!settings.location) 
            settings.location = { };
        if (!settings.location.text)
            settings.location.text = "";
        if (!settings.location.lat)
            settings.location.lat = "";
        if (!settings.location.lon)
            settings.location.lon = "";
        if (!settings.location.utc)
            settings.location.utc = "";
            
        if (!settings.format) 
            settings.format = { };
        if (!settings.format.title)
            settings.format.title = "";
        if (!settings.format.width) 
            settings.format.width = "250px";
        if (!settings.format.height)
            settings.format.height = "350px";
        if (!settings.format.background) 
            settings.format.background = "1";
        
        if (!settings.targetelement)
            settings.targetelement = "";
        if (!settings.simulation)
            settings.simulation = "";

        return settings;
    }

    this.Write = function(result,settings) {
        if (!result.location) return;
        var html = HtmlLeft;
        if (settings.format.title.length > 0) {
            html += '' + Html1 + Html2 + HtmlRight;
        } else {
            html += '' + Html2 + HtmlRight;
        }
        html = html.replace(/%title%/gi, settings.format.title);
        html = html.replace(/%width%/gi, settings.format.width);                        
        html = html.replace(/%height%/gi, settings.format.height);
        html = html.replace(/%background%/gi, settings.format.background);
        if (result.location.text.length > 0) {
            html = html.replace(/%location%/gi, result.location.text);
        } else {
            html = html.replace(/%location%/gi, "Lat: " + result.location.lat + " Lon: " + result.location.lon);
        }
        html = html.replace(/%date%/gi, result.datelocal);
        html = html.replace(/%sunrise%/gi, result.sunrise);
        html = html.replace(/%sunset%/gi, result.sunset);
        html = html.replace(/%daylength%/gi, result.daylength);
        
        if (settings.targetelement.length > 0) {
            var target = document.getElementById(settings.targetelement);
            if (target) {
                target.innerHTML = html;
            }
        } else { 
            document.write(html);                        
        }
    };

    this.Create = function(settings) {
        settings = this.SetDefaults(settings);
        
        var x = 0;
        while (window['SHWJSONP' + x + 'R'] && (x < 1000)) x++;
        window['SHWJSONP' + x + 'R'] = this;
        window['SHWJSONP' + x + 'S'] = settings;
        eval("window['SHWJSONP" + x + "F'] = function(r) { window['SHWJSONP" + x + "R'].Write(r, window['SHWJSONP" + x + "S']); try { document.getElementsByTagName('head')[0].removeChild(window['SHWJSONP" + x + "J']); } catch (err) { }; window['SHWJSONP" + x + "R'] = window['SHWJSONP" + x + "S'] = window['SHWJSONP" + x + "F'] = window['SHWJSONP" + x + "J'] = null; };");
                
        if (settings.simulation == "1") {
            eval("SHWJSONP" + x + "F({ location: { text: '" + settings.location.text + "', lat: '" +  settings.location.lat + "', lon: '" + settings.location.lon + "' }, datelocal: '21 Month 2015', sunrise: '7:30 AM', sunset: '8:20 PM', daylength: '10:23' })");
        } else {            
            this.JSONPQuery("SHWJSONP" + x + "J","http://sunrisehour.com/widget/query/?q=" + encodeURIComponent(settings.location.text) + "&lon=" + encodeURIComponent(settings.location.lon) + "&lat=" + encodeURIComponent(settings.location.lat) + "&utc=" + encodeURIComponent(settings.location.utc) + "&jsoncb=SHWJSONP" + x + "F");
        }
    }
    
    this.JSONPQuery = function(id, url) {
        window[id] = document.createElement("scr" + "ipt");
        window[id].type = "text/javascript";
        window[id].src = url;
        document.getElementsByTagName("head")[0].appendChild(window[id]);
    }
}
