function page_lang(lang) {
    var ch = new CookieHandler();
    ch.setCookie("page_lang", lang);
	sett.language = lang;
}

function location_browse_toggle(a) {
    $("location_browse_container").descendants().length == 0 ? location_browse(a) : $("elem_location_browse").toggle();
}

function location_browse(args) {
    var a            = typeof(args) == "undefined" ? {} : args;
    var r_url        = "/index.php?page=elem_location_browse";
    var r_parameters = Object.toQueryString(a);
    
    new Ajax.Request(r_url, {
        method: "post",
        parameters: r_parameters,
        onSuccess: function(r) {
            $("location_browse_container").update(r.responseText);
        },
        onFailure: function() {
            setTimeout(function(){location_browse(a)}, 2000);
            a = null;
        }
    });
}

function change_location_complete(text, li) {
    $("location_full_name").update(li.innerHTML);
    $("autocomplete").value = "";
    $("autocomplete").blur();
    change_location_id({"location_id": li.id});
}

function change_location_browse(a) {
    if(a.adm1_name) {
        $("location_full_name").update("<strong>"+ a.location_name + "</strong>, " + a.adm1_name + ", " + a.country_name);
    }
    else {
        $("location_full_name").update("<strong>"+ a.location_name + "</strong>, " + a.country_name);
    }
    
    $("elem_location_browse").blindUp({ duration: 1.0 });
    change_location_id({"location_id": a.location_id});
}

function change_location_id(a) {
    if(a.location_id) {
        $("location_id").value = a.location_id;
    }
    
    sett.upd_vars();
    update_preview();
}

function change_unit_set() {
    var unit_set = get_radio("settings", "unit_set");
    
    switch(unit_set) {
        case "UNIT_SET_METRIC":
            set_radio("settings", "temp_u",  "UNIT_C");
            set_radio("settings", "winds_u", "UNIT_MS");
            set_radio("settings", "pres_u",  "UNIT_HPA");
            set_radio("settings", "prec_u",  "UNIT_MM");
            set_radio("settings", "vis_u",   "UNIT_KM");
            $("custom_units").hide();
            break;
        case "UNIT_SET_METRIC_KMH":
            set_radio("settings", "temp_u",  "UNIT_C");
            set_radio("settings", "winds_u", "UNIT_KMH");
            set_radio("settings", "pres_u",  "UNIT_HPA");
            set_radio("settings", "prec_u",  "UNIT_MM");
            set_radio("settings", "vis_u",   "UNIT_KM");
            $("custom_units").hide();
            break;
        case "UNIT_SET_IMPERIAL":
            set_radio("settings", "temp_u",  "UNIT_C");
            set_radio("settings", "winds_u", "UNIT_MPH");
            set_radio("settings", "pres_u",  "UNIT_IN");
            set_radio("settings", "prec_u",  "UNIT_IN");
            set_radio("settings", "vis_u",   "UNIT_MILES");
            $("custom_units").hide();
            break;
        case "UNIT_SET_US":
            set_radio("settings", "temp_u",  "UNIT_F");
            set_radio("settings", "winds_u", "UNIT_MPH");
            set_radio("settings", "pres_u",  "UNIT_IN");
            set_radio("settings", "prec_u",  "UNIT_IN");
            set_radio("settings", "vis_u",   "UNIT_MILES");
            $("custom_units").hide();
            break;
        case "UNIT_SET_CUSTOM":
            $("custom_units").show();
            break;
    }
}

function change_template(t) {
    $("template").value = t;
    $$(".templates .template_thumb").invoke("removeClassName", "template_thumb_active");
    $($("template").value).addClassName("template_thumb_active");
    
    if(t == "T_iGoogle") {
    	$("style_container").hide();
    	change_style("S_4");
    }
    else {
    	$("style_container").show();
    }
}

function change_style(s) {
    $("style").value = s;
    $$(".styles .template_thumb").invoke("removeClassName", "template_thumb_active");
    $($("style").value).addClassName("template_thumb_active");
    
    if(s == "S_4") {
        $("color_container").show();
    }
    else {
        $("color_container").hide();
    }
}

function change_color(c) {
    $("color").value = c;
    $$(".colors .bg").invoke("removeClassName", "bg_active");
    $($("color").value).addClassName("bg_active");
}

function update_iframe(id, val) {
	var iframe = document.getElementById(id);
	var doc = iframe.contentDocument;
	if (doc == undefined || doc == null) {
	    doc = iframe.contentWindow.document;
	}
	doc.open();
	doc.write(val);
	doc.close();
}

function update_preview(options) {
    var options_def = {
        "fade_duration"   : 0.5,
        "morph_duration"  : 0.5,
        "appear_duration" : 0.5,
        "sett"            : "{}"
    };
    if(typeof options == "undefined") {
        options = new Object();
    }
    for(var index in options_def) {
        if(typeof options[index] == "undefined") options[index] = options_def[index];
    }

    var r_url = "/index.php?page=elem_edit_iframe";
    var r_parameters = {"sett": Object.toJSON(sett)};
             
    new Ajax.Request(r_url, {
        method: "post",
        parameters: r_parameters,
        onSuccess: function(r) {
            var t = r.responseText.evalJSON();
            $("preview").fade({
                duration: options.fade_duration,
                afterFinish: function() {
                    $("preview_container").setStyle({visibility: "hidden"});
                    $("preview").replace(t.tag);
                    update_iframe("preview", t.content);
                    $("preview").hide();
                    $("preview_container").setStyle({visibility: "visible"});
                    
                    h = $("preview").getHeight() + "px";
                    $("preview_container").morph("height: " + h, {
                        duration: options.morph_duration,
                        afterFinish: function() {
                            $("preview").appear({ duration: options.appear_duration })
                        }
                    })
                }
            });
        },
        onFailure: function() {
            setTimeout(function(){update_preview(options)}, 2000);
        }
    });
}

function get_radio(form, name) {
    return Form.getInputs(form, "radio", name).find(function(radio){return radio.checked;}).value;
}

function set_radio(form, name, val) {
    Form.getInputs(form, "radio", name).find(function(radio){return radio.value==val}).checked = true;
}
