System.Gadget.settingsUI = "settings.html";
System.Gadget.onSettingsClosed = settingsClosed;
System.Gadget.onDock = docked;
System.Gadget.onUndock = undocked;    
var txtcolor = "Color(255, 255, 255, 255)";
var font = "Calibri";
var fontsizeBIG   = 21;
var fontsizeSMALL = 12;
var confirmy_var = System.Gadget.Settings.read("confirmy");
if(confirmy_var==''){confirmy_var=false;System.Gadget.Settings.write("confirmy", confirmy_var);}
var userAgenty = System.Gadget.Settings.read("userAgenty");
if(userAgenty==''){userAgenty=false;System.Gadget.Settings.write("userAgenty", userAgenty);}
var win        = System.Gadget.Settings.read("win");
if(win=='')
{
   var userAgent = navigator.userAgent.toLowerCase();
   if(userAgent.indexOf("windows nt 6.0")>-1){var win="vista";}else{var win="win7";}
   System.Gadget.Settings.write("win", win);
}


function settingsClosed(p_event) {
go();  
    setTimeout("alarmy();",1000 * 1);
    //OK clicked?
    if (p_event.closeAction == p_event.Action.commit) {
        //yes, read settings here
    }
    
} 

function p(){
    go_once();
 
}
function update(run){   
    document.getElementById("bg").removeObjects();
    if(confirmy_var == true && run == 0){setTimeout("alarmy();",1000 * 1);}
    if(System.Gadget.docked == true) {docked();}else{undocked();}
    
/*
var txtAlign = bg.addTextObject(ssid_txt, "Calibri", 12, "black", 9,7);
var txtAlign = bg.addTextObject(Strength+"%", "Calibri", 24, "black", 9,24);
var txtAlign = bg.addTextObject(ssid_txt, "Calibri", 12, "white", 8,6);
var txtAlign = bg.addTextObject(Strength+"%", "Calibri", 24, "white", 8,23);
bg.addImageObject("img/"+img+".png", 75, 22);
*/
}
function small(){  
    document.getElementById("bg").removeObjects();      
    bg.addImageObject("img/w"+System.Gadget.Settings.read("styley")+"/"+System.Gadget.Settings.read("current_skycode")+".png", 5, 8);
    
                 
if(System.Gadget.Settings.read("ortname") == ''){checkconnection();}else{
// TXT \\ 
var txtAlign = bg.addTextObject(System.Gadget.Settings.read("current_temperature"), font, fontsizeBIG, txtcolor, 121,5);
txtAlign.align = 2; txtAlign.addShadow("black", 1, 100, 1, 1);
var  txtAlign = bg.addTextObject(System.Gadget.Settings.read("today_vorschau"), font, fontsizeSMALL, txtcolor, 120,29);
txtAlign.align = 2; txtAlign.addShadow("black", 1, 100, 1, 1);
var  txtAlign = bg.addTextObject(System.Gadget.Settings.read("shortort"), font, fontsizeSMALL, txtcolor, 119,43);
txtAlign.align = 2; txtAlign.addShadow("black", 1, 100, 1, 1);
}

}

function big(){
    document.getElementById("bg").removeObjects();
    bg.addImageObject("img/w"+System.Gadget.Settings.read("styley")+"/"+System.Gadget.Settings.read("current_skycode")+".png", 5, 8);   

if(System.Gadget.Settings.read("ortname") == ''){checkconnection();}else{
// TXT \\
var txtAlign = bg.addTextObject(System.Gadget.Settings.read("current_temperature"), font, fontsizeBIG, txtcolor, 121,5);
txtAlign.align = 2; txtAlign.addShadow("black", 1, 100, 1, 1);
var  txtAlign = bg.addTextObject(System.Gadget.Settings.read("today_vorschau"), font, fontsizeSMALL, txtcolor, 120,29);
txtAlign.align = 2; txtAlign.addShadow("black", 1, 100, 1, 1);
var  txtAlign = bg.addTextObject(System.Gadget.Settings.read("shortort"), font, fontsizeSMALL, txtcolor, 119,43);
txtAlign.align = 2; txtAlign.addShadow("black", 1, 100, 1, 1);


var n = System.Gadget.Settings.read("current_skytext");
n = n.replace(/Vereinzelte Schneefälle/g, "Leichter Schneefall");
n = n.replace(/Vereinzelte /g, "Leichte ");
if(n.indexOf("/") > -1){var s = n.split("/");n = s[0];}
                   
var txtAlign = bg.addTextObject(lang.skytext, font, fontsizeSMALL, txtcolor, 9,70); 
txtAlign.addShadow("black", 1, 100, 1, 1);  
var txtAlign = bg.addTextObject(n, font, fontsizeSMALL, txtcolor, 119,70);
txtAlign.align = 2; txtAlign.addShadow("black", 1, 100, 1, 1); 

var txtAlign = bg.addTextObject(lang.feelslike, font, fontsizeSMALL, txtcolor, 9,82);
txtAlign.addShadow("black", 1, 100, 1, 1);
var txtAlign = bg.addTextObject(System.Gadget.Settings.read("current_feelslike")+"°"+System.Gadget.Settings.read("grad"), font, fontsizeSMALL, txtcolor, 120,82);
txtAlign.align = 2; txtAlign.addShadow("black", 1, 100, 1, 1);

var txtAlign = bg.addTextObject(lang.humidity, font, fontsizeSMALL, txtcolor, 9,94);
txtAlign.addShadow("black", 1, 100, 1, 1);
var txtAlign = bg.addTextObject(System.Gadget.Settings.read("current_humidity")+"%", font, fontsizeSMALL, txtcolor, 121,94);
txtAlign.align = 2; txtAlign.addShadow("black", 1, 100, 1, 1);

var txtAlign = bg.addTextObject(lang.windspeed, font, fontsizeSMALL, txtcolor, 9,106);
txtAlign.addShadow("black", 1, 100, 1, 1);
var txtAlign = bg.addTextObject(System.Gadget.Settings.read("current_winddisplay"), font, fontsizeSMALL, txtcolor, 119,106);
txtAlign.align = 2; txtAlign.addShadow("black", 1, 100, 1, 1);

var ob = System.Gadget.Settings.read("current_observationtime");
if(ob.indexOf(":") > -1){var s = ob.split(":");ob = s[0]+":"+s[1]+" "+lang.oclock;}


var txtAlign = bg.addTextObject(lang.ob, font, fontsizeSMALL, txtcolor, 9,118);
txtAlign.addShadow("black", 1, 100, 1, 1);
var txtAlign = bg.addTextObject(ob, font, fontsizeSMALL, txtcolor, 119,118);
txtAlign.align = 2; txtAlign.addShadow("black", 1, 100, 1, 1);



// days
bg.addImageObject("img/m"+System.Gadget.Settings.read("styley")+"/"+System.Gadget.Settings.read("forecast1_skycodeday")+".png", 15, 146);
var txtAlign = bg.addTextObject(System.Gadget.Settings.read("forecast1_day"), font, fontsizeSMALL, txtcolor, 49,145);
txtAlign.addShadow("black", 1, 100, 1, 1);
var txtAlign = bg.addTextObject(System.Gadget.Settings.read("forecast1_low")+"° / "+System.Gadget.Settings.read("forecast1_high")+"°", font, fontsizeSMALL, txtcolor, 50,157);
txtAlign.addShadow("black", 1, 100, 1, 1);


bg.addImageObject("img/m"+System.Gadget.Settings.read("styley")+"/"+System.Gadget.Settings.read("forecast2_skycodeday")+".png", 15, 177);
var txtAlign = bg.addTextObject(System.Gadget.Settings.read("forecast2_day"), font, fontsizeSMALL, txtcolor, 49,176);
txtAlign.addShadow("black", 1, 100, 1, 1);
var txtAlign = bg.addTextObject(System.Gadget.Settings.read("forecast2_low")+"° / "+System.Gadget.Settings.read("forecast2_high")+"°", font, fontsizeSMALL, txtcolor, 50,188);
txtAlign.addShadow("black", 1, 100, 1, 1);


bg.addImageObject("img/m"+System.Gadget.Settings.read("styley")+"/"+System.Gadget.Settings.read("forecast3_skycodeday")+".png", 15, 208);
var txtAlign = bg.addTextObject(System.Gadget.Settings.read("forecast3_day"), font, fontsizeSMALL, txtcolor, 49,207);
txtAlign.addShadow("black", 1, 100, 1, 1);
var txtAlign = bg.addTextObject(System.Gadget.Settings.read("forecast3_low")+"° / "+System.Gadget.Settings.read("forecast3_high")+"°", font, fontsizeSMALL, txtcolor, 50,219);
txtAlign.addShadow("black", 1, 100, 1, 1);


bg.addImageObject("img/m"+System.Gadget.Settings.read("styley")+"/"+System.Gadget.Settings.read("forecast4_skycodeday")+".png", 15, 239);
var txtAlign = bg.addTextObject(System.Gadget.Settings.read("forecast4_day"), font, fontsizeSMALL, txtcolor, 49,238);
txtAlign.addShadow("black", 1, 100, 1, 1);
var txtAlign = bg.addTextObject(System.Gadget.Settings.read("forecast4_low")+"° / "+System.Gadget.Settings.read("forecast4_high")+"°", font, fontsizeSMALL, txtcolor, 50,250);
txtAlign.addShadow("black", 1, 100, 1, 1);


}


}




function go_once(){
   var sett1  = setTimeout("load('1');",1000 * 1);
   var sett2  = setTimeout("load('1');",1000 * 5);
   var sett3  = setTimeout("load('1');",1000 * 10);
   var sett4  = setTimeout("load('1');",1000 * 15);
   var sett5  = setTimeout("load('1');",1000 * 30);
   var sett6  = setTimeout("load('1');",1000 * 60);
   var sett7  = setTimeout("load('1');",1000 * 60 * 5);
   var sett8  = setTimeout("load('1');",1000 * 60 * 10);
   var sett9  = setTimeout("load('1');",1000 * 60 * 20); 
}
function go() {
   var sett10 = setTimeout("load('0');",1000 * 60 * 30);
   go_once();
}
function load(run) {


if(run==0){go();/*confirm('lade daten');*/}
var txtcolor = System.Gadget.Settings.read("txtcolor");
if(txtcolor==''){txtcolor="1";System.Gadget.Settings.write("txtcolor", txtcolor);}
var ort  = System.Gadget.Settings.read("ort");  if(ort==''){ort="wc:GMXX0185";System.Gadget.Settings.write("ort", ort);System.Gadget.Settings.write("ortname", "Frankfurt/Main, HE");System.Gadget.Settings.write("shortort", "Frankfurt");}
var grad = System.Gadget.Settings.read("grad"); if(grad==''){grad="C";System.Gadget.Settings.write("grad", grad);}
var map  = System.Gadget.Settings.read("map");  if(map==''){map="0";System.Gadget.Settings.write("map", map);}
var style= System.Gadget.Settings.read("style");if(style==''){style="b";System.Gadget.Settings.write("style", style);}
var styley=System.Gadget.Settings.read("styley");
if(styley==''){styley="1";System.Gadget.Settings.write("styley", styley);}
if(System.Gadget.docked == true) {docked();}else{undocked();}

    //document.getElementById("bg").removeObjects();
    //document.body.style.height = "67px";
    //var oBackground = document.getElementById("bg");
    //    oBackground.src = "url(img/bg-docked4_"+style+".png)";

var rssDoc2 = new ActiveXObject("MSXML2.DOMDocument.3.0");
rssDoc2.onreadystatechange = popData2;
rssDoc2.load("http://weather.service.msn.com/data.aspx?src=vista&wealocations="+System.Gadget.Settings.read("ort")+"&weadegreetype="+System.Gadget.Settings.read("grad")+"&culture=de-DE");



//alert("jo");        

function popData2(){  
if(rssDoc2.readyState!=4)return;
var rssItems2 = rssDoc2.selectNodes("/weatherdata/weather");  
for(var i=0;i<rssItems2.length;i++)
{ 
System.Gadget.Settings.write("timezone", rssItems2[i].selectSingleNode("./@timezone").text);
var stadt = rssItems2[i].selectSingleNode("./@weatherlocationname").text;
var alertvar = rssItems2[i].selectSingleNode("./@weatherlocationname").text;  


System.Gadget.Settings.write("current_temperature", rssItems2[i].selectSingleNode("./current/@temperature").text+"°"+grad);
System.Gadget.Settings.write("current_skycode", rssItems2[i].selectSingleNode("./current/@skycode").text);
System.Gadget.Settings.write("current_skytext", rssItems2[i].selectSingleNode("./current/@skytext").text);
System.Gadget.Settings.write("current_date", rssItems2[i].selectSingleNode("./current/@date").text);
System.Gadget.Settings.write("current_day", rssItems2[i].selectSingleNode("./current/@day").text);
System.Gadget.Settings.write("current_shortday", rssItems2[i].selectSingleNode("./current/@shortday").text);
System.Gadget.Settings.write("current_observationtime", rssItems2[i].selectSingleNode("./current/@observationtime").text);
System.Gadget.Settings.write("current_observationpoint", rssItems2[i].selectSingleNode("./current/@observationpoint").text);
System.Gadget.Settings.write("current_feelslike", rssItems2[i].selectSingleNode("./current/@feelslike").text);
System.Gadget.Settings.write("current_humidity", rssItems2[i].selectSingleNode("./current/@humidity").text);
System.Gadget.Settings.write("current_windspeed", rssItems2[i].selectSingleNode("./current/@windspeed").text);
System.Gadget.Settings.write("current_winddisplay", rssItems2[i].selectSingleNode("./current/@winddisplay").text);

System.Gadget.Settings.write("forecast0_low", rssItems2[i].selectSingleNode("./forecast[0]/@low").text);
System.Gadget.Settings.write("forecast0_high", rssItems2[i].selectSingleNode("./forecast[0]/@high").text);
System.Gadget.Settings.write("forecast0_skycodeday", rssItems2[i].selectSingleNode("./forecast[0]/@skycodeday").text);
System.Gadget.Settings.write("forecast0_skytextday", rssItems2[i].selectSingleNode("./forecast[0]/@skytextday").text);
System.Gadget.Settings.write("forecast0_date", rssItems2[i].selectSingleNode("./forecast[0]/@date").text);
System.Gadget.Settings.write("forecast0_day", rssItems2[i].selectSingleNode("./forecast[0]/@day").text);
System.Gadget.Settings.write("forecast0_shortday", rssItems2[i].selectSingleNode("./forecast[0]/@shortday").text);
System.Gadget.Settings.write("forecast0_precip", rssItems2[i].selectSingleNode("./forecast[0]/@precip").text);
var today_vorschau = System.Gadget.Settings.read("forecast0_low")+"° / "+System.Gadget.Settings.read("forecast0_high")+"°";
System.Gadget.Settings.write("today_vorschau", today_vorschau);

System.Gadget.Settings.write("forecast1_low", rssItems2[i].selectSingleNode("./forecast[1]/@low").text);
System.Gadget.Settings.write("forecast1_high", rssItems2[i].selectSingleNode("./forecast[1]/@high").text);
System.Gadget.Settings.write("forecast1_skycodeday", rssItems2[i].selectSingleNode("./forecast[1]/@skycodeday").text);
System.Gadget.Settings.write("forecast1_skytextday", rssItems2[i].selectSingleNode("./forecast[1]/@skytextday").text);
System.Gadget.Settings.write("forecast1_date", rssItems2[i].selectSingleNode("./forecast[1]/@date").text);
System.Gadget.Settings.write("forecast1_day", rssItems2[i].selectSingleNode("./forecast[1]/@day").text);
System.Gadget.Settings.write("forecast1_shortday", rssItems2[i].selectSingleNode("./forecast[1]/@shortday").text);
System.Gadget.Settings.write("forecast1_precip", rssItems2[i].selectSingleNode("./forecast[1]/@precip").text);

System.Gadget.Settings.write("forecast2_low", rssItems2[i].selectSingleNode("./forecast[2]/@low").text);
System.Gadget.Settings.write("forecast2_high", rssItems2[i].selectSingleNode("./forecast[2]/@high").text);
System.Gadget.Settings.write("forecast2_skycodeday", rssItems2[i].selectSingleNode("./forecast[2]/@skycodeday").text);
System.Gadget.Settings.write("forecast2_skytextday", rssItems2[i].selectSingleNode("./forecast[2]/@skytextday").text);
System.Gadget.Settings.write("forecast2_date", rssItems2[i].selectSingleNode("./forecast[2]/@date").text);
System.Gadget.Settings.write("forecast2_day", rssItems2[i].selectSingleNode("./forecast[2]/@day").text);
System.Gadget.Settings.write("forecast2_shortday", rssItems2[i].selectSingleNode("./forecast[2]/@shortday").text);
System.Gadget.Settings.write("forecast2_precip", rssItems2[i].selectSingleNode("./forecast[2]/@precip").text);

System.Gadget.Settings.write("forecast3_low", rssItems2[i].selectSingleNode("./forecast[3]/@low").text);
System.Gadget.Settings.write("forecast3_high", rssItems2[i].selectSingleNode("./forecast[3]/@high").text);
System.Gadget.Settings.write("forecast3_skycodeday", rssItems2[i].selectSingleNode("./forecast[3]/@skycodeday").text);
System.Gadget.Settings.write("forecast3_skytextday", rssItems2[i].selectSingleNode("./forecast[3]/@skytextday").text);
System.Gadget.Settings.write("forecast3_date", rssItems2[i].selectSingleNode("./forecast[3]/@date").text);
System.Gadget.Settings.write("forecast3_day", rssItems2[i].selectSingleNode("./forecast[3]/@day").text);
System.Gadget.Settings.write("forecast3_shortday", rssItems2[i].selectSingleNode("./forecast[3]/@shortday").text);
System.Gadget.Settings.write("forecast3_precip", rssItems2[i].selectSingleNode("./forecast[3]/@precip").text);

System.Gadget.Settings.write("forecast4_low", rssItems2[i].selectSingleNode("./forecast[4]/@low").text);
System.Gadget.Settings.write("forecast4_high", rssItems2[i].selectSingleNode("./forecast[4]/@high").text);
System.Gadget.Settings.write("forecast4_skycodeday", rssItems2[i].selectSingleNode("./forecast[4]/@skycodeday").text);
System.Gadget.Settings.write("forecast4_skytextday", rssItems2[i].selectSingleNode("./forecast[4]/@skytextday").text);
System.Gadget.Settings.write("forecast4_date", rssItems2[i].selectSingleNode("./forecast[4]/@date").text);
System.Gadget.Settings.write("forecast4_day", rssItems2[i].selectSingleNode("./forecast[4]/@day").text);
System.Gadget.Settings.write("forecast4_shortday", rssItems2[i].selectSingleNode("./forecast[4]/@shortday").text);
System.Gadget.Settings.write("forecast4_precip", rssItems2[i].selectSingleNode("./forecast[4]/@precip").text);


//alert(System.Gadget.Settings.read("current_temperature"));
}   
update(run);   
}




}
function checkconnection(){
document.getElementById("bg").removeObjects();
var  txtAlign = bg.addTextObject(lang.need, font, fontsizeSMALL, txtcolor, 65,25);
txtAlign.align = 1; txtAlign.addShadow("black", 1, 100, 1, 1);
var sett2  = setTimeout("load('1');",1000 * 2);
}

function docked() {

if(System.Gadget.Settings.read("userAgenty") == true)
{
var style=System.Gadget.Settings.read("style");
    document.body.style.height = "280px";
var oBackground = document.getElementById("bg");
    oBackground.src = "url(img/bg-undocked4_"+style+".png)";
big();
}
else
{
var style=System.Gadget.Settings.read("style");
    document.body.style.height = "67px";
var oBackground = document.getElementById("bg");

    oBackground.src = "url(img/bg-docked4_"+style+".png)";
small();
}
}


function undocked() {
var style=System.Gadget.Settings.read("style");
    document.body.style.height = "280px";
var oBackground = document.getElementById("bg");
    oBackground.src = "url(img/bg-undocked4_"+style+".png)";
big();
}

function showFlyout () {
System.Gadget.Flyout.file = "flyout.html";
hideFlyout();
System.Gadget.Flyout.show = true;
System.Gadget.Flyout.onShow = function() {
System.Gadget.Flyout.document.getElementById("iframe").src = flyoutHTML;
}
}

function hideFlyout () {
if (System.Gadget.Flyout.show) {
System.Gadget.Flyout.show = false;
}
}


function stripHTML(str){
	var tmp = str.replace(/(<.*['"])([^'"]*)(['"]>)/g,
	function(x, p1, p2, p3) { return  p1 + p3;} );
	return tmp.replace(/<\/?[^>]+>/gi, '');
}
function trim (str) {
    return str.replace(/[\n\r]/g, '').replace(/ +/g, ' ').replace(/^\s+/g, '').replace(/\s+$/g, '');
}    
function alarmy(){

 var alarmhttp = new ActiveXObject("Microsoft.XMLHTTP");
 alarmhttp.open("GET", "http://wetter.msn.com/bulletin.aspx?wealocations="+System.Gadget.Settings.read("ort")+"&mkt=de-de&setunit="+System.Gadget.Settings.read("grad"), false);
 alarmhttp.send();
 if(alarmhttp.ReadyState==4)
    {
		if(alarmhttp.responseText.indexOf("Derzeit liegen keine aktiven Warnungen oder gültigen Empfehlungen vor") == -1){
            var posStart = alarmhttp.responseText.indexOf("<div class=\"parent chrome5 promo single1\"><h2>");
		    var posEnd   = alarmhttp.responseText.indexOf("</h2><div class=\"child c1 first\">");
		    var alarmExtrakt = alarmhttp.responseText.slice(posStart, posEnd);
		    alarmExtrakt = stripHTML(alarmExtrakt);
        }else{var alarmExtrakt = "";}
        System.Gadget.Settings.write("alarm", alarmExtrakt);
	}
 else{}
   
    var al = System.Gadget.Settings.read("alarm");
    if(System.Gadget.Settings.read("confirmy") == true && System.Gadget.Settings.read("alarm") != ''){alert(al);}
    
}    