function Mobile_Plugin_Admin(){
    var MP_Data;
    this.init = function(_MP_Data){
        //Set the values back to the formular elements
        MP_Data = _MP_Data;

        this.setValuesBackToForm();
        this.createEvents();
        this.handleContents();
    }

    this.setValuesBackToForm = function(){
        $("MPResults_Limit").value = 10;

        for (var i in MP_Data){
            var MP_Value = MP_Data[i];
            var MP_Name = i;
            var MP_Element = $(MP_Name);
    
            if (MP_Element){
                if (MP_Element.nodeName.toLowerCase() == "select"){
                    var MP_Element_Childs = MP_Element.select("option");
                    MP_Element_Childs.each(function(MP_Option){
                        if (MP_Option.value == MP_Value) MP_Option.selected = true;
                    });
                }else if (MP_Element.nodeName.toLowerCase() == "input"){
                    if (MP_Element.type == "checkbox"){
                        if (MP_Value == "on") MP_Element.checked = true;
                    }else{
                        if (MP_Element.id == "MPResults_Limit") {
                            MP_Element.value = !MP_Value && typeof MP_Value !== 'number' || MP_Value !== "0"  ? 10 : MP_Value;

                        } else {
                            MP_Element.value = MP_Value;
                        }
                    }
                }
            }
        }
    }

    this.createEvents = function(){
        //Create the brand change event to load new models
        $("MPResults_Brand").observe("change", function(){
            this.loadModele($F($("MPResults_Brand")));
        }.bind(this));
    
        //Create the activation / deactivation event buttons
        $("MPResults_Status_Container").observe("click", function(){

            // show mobile affilinet link
            if ($$('#MPResults_Status_Container input')[0].checked) {
                //$('MPResults_Affilinet').up('p').show();

            } else {
                //$('MPResults_Affilinet').up('p').hide();
            }

            // show autoscout affilinet link
            //if ($$('#MPResults_Status_Container input')[1].checked) {
            //    $('MPAResults_Affilinet').up('p').show();

            //} else {
            //    $('MPAResults_Affilinet').up('p').hide();
            //}

            if ($$('#MPResults_Status_Container input')[0].checked == false) {
                $("MPResults_Container").hide();

            } else {
                $("MPResults_Container").show();
            }
        });
    
        $("MPAdsense_Status").observe("click", function(){
            $("MPAdsense_Container").toggle();
        });
    
        $("MPSearch_Status").observe("click", function(){
            $("MPSearch_Container").toggle();
        });
    }

    this.handleContents = function(){
        //We have a preselect brand, now we need to load the models
        if ($F("MPResults_Brand") != ""){
            this.loadModele($F("MPResults_Brand"));
        }

        //Show the containers, its active!
        if (MP_Data.MPResults_Status == "on"){
            $("MPResults_Container").show();
        }

        if (MP_Data.MPSearch_Status == "on"){
            $("MPSearch_Container").show();
        }

        if (MP_Data.MPAResults_Status == "on"){
            //$("MPResults_Container").show();
        }

        if (MP_Data.MPAdsense_Status == "on"){
            $("MPAdsense_Container").show();
        }

        // show mobile affilinet link
        if (MP_Data.MPResults_Status == "on") {
            //$('MPResults_Affilinet').up('p').show();

        } else {
            //$('MPResults_Affilinet').up('p').hide();
        }

        // show autoscout affilinet link
        if (MP_Data.MPAResults_Status == "on") {
            //$('MPAResults_Affilinet').up('p').show();
            $('MPAResults_Affilinet').up('p').hide();

        } else {
            $('MPAResults_Affilinet').up('p').hide();
        }
    }

    //Loads Models by a brand
    this.loadModele = function(MP_Name){
        new Ajax.Request(MOBILE_PLUGIN_WEB_ROOT + "/ajax/api.php?get_model=" + MP_Name, {
            method: 'get',
            onSuccess: function(transport) { 
                $("MPResults_Model").update(transport.responseText);
                $("MPResults_Model").disabled = false;

                if (MP_Data.MPResults_Model != ""){
                    $("MPResults_Model").select("option").each(function(MP_Option){
                        if (MP_Option.value == MP_Data.MPResults_Model) MP_Option.selected = true;
                    });
                }
            }
        });        
    }
}

function Mobile_Plugin_Post(){
    var infos = {};

    this.init = function(formTarget){
        infos.target = formTarget;
        this.createEvents();
    }

    this.createEvents = function(){
        infos.target.select('select[name="makeModelVariant1.makeId"]')[0].observe("change", this.loadModele.bind(this, false));
    }

    this.setBrand = function(brand, model){
        infos.target.select('select[name="makeModelVariant1.makeId"] option').each(function(option){
            var part = option.id.split("_");
            var brand_name = part[1];
            if (brand_name == brand){
                option.selected = true;
                this.loadModele(model);
            }
        }.bind(this));
    }
    //Loads Models by a brand
    this.loadModele = function(model){
        var makeElement = infos.target.select('select[name="makeModelVariant1.makeId"]')[0];
        var searchValue = $F(makeElement);

        var foundKey = 0;
        makeElement.select("option").each(function(option){
            if (option.value == searchValue) foundKey = option.id.replace("key_","");
        });

        new Ajax.Request(MOBILE_PLUGIN_WEB_ROOT + "/ajax/api.php?key=no&get_model=" + foundKey, {
            method: 'get',
            onSuccess: function(transport) { 
                var modelField = infos.target.select('select[name="makeModelVariant1.modelId"]')[0];
                modelField.update(transport.responseText);
                modelField.disabled = false;
                if (model != false){
                    modelField.select("option").each(function(option){
                        var part = option.id.split("_");
                        var model_name = part[1];
                        if (model_name == model) option.selected = true;
                    });
                }
            }.bind(this)
        });        
    }    
}
