function OnScreenAlertController(){
	var self = this; self.name = "OnScreenAlertController"; // MANDATORY

    this.api_connector = new ApiConnector();
    this.interval = 5000;
    this.timer = 0;
    this.send_id = false;
    this.url = "";
    this.active_alert = "";

    this.update = function(url,interval,send_id){
        if(typeof(url) != "undefined"){
            self.url = url;
        }
        if(typeof(interval) != "undefined"){
            self.interval = interval;
        }
        if(typeof(send_id) != "undefined"){
            self.send_id = send_id;
        }
        clearTimeout(self.timer);
        self.timer = setTimeout(self.alertCheckStatus,self.interval);
    };
    
    this.alertCheckStatus = function(){    
        clearTimeout(self.timer);
        self.timer = setTimeout(self.alertCheckStatus,self.interval);
        var url = self.url;
        if(url != ""){
            if(self.send_id){
                url += "/id/"+self.core_id;
            }
            self.api_connector.makeRequest("GET",url,function(response, status){
                try{
                    self.alertStatusResponse(JSON.parse(response),status);
                }catch(e){
                    self.alertStatusResponse(response,status);
                }
            });
        }        
    };
    
    this.alertStatusResponse = function(response,status,params){
        if(status == "success"){
            var xml = $($.parseXML(response));
            if(typeof(xml.length) != "undefined"){
                if(xml.length > 0){
                    var first_alert_type = xml.find("alerts").find('alert').attr('type');
                    var first_alert_url = xml.find("alerts").find('alert').attr('url');
                    if(first_alert_type == "screen"){
                        if(first_alert_url != self.active_alert){
                            self.active_alert = first_alert_url;
                            $(self).trigger("OnScreenAlertUpdate");
                        }
                    }else{
                        if(self.active_alert != ""){
                            self.active_alert = "";    
                            $(self).trigger("OnScreenAlertUpdate");
                        }
                    }
                }else{
                    if(self.active_alert != ""){
                        self.active_alert = "";    
                        $(self).trigger("OnScreenAlertUpdate");
                    }
                }
            }else{
                if(self.active_alert != ""){
                    self.active_alert = "";    
                    $(self).trigger("OnScreenAlertUpdate");
                }
            }      
        }else{
            if(self.active_alert != ""){
                self.active_alert = "";    
                $(self).trigger("OnScreenAlertUpdate");
            }
        }
    };
    
    this.stop = function(){
          clearTimeout(self.timer);
    };
    
    this.unload = function(){
        clearTimeout(self.timer);
        self.api_connector = null;  
    };
}	
OnScreenAlertController.prototype = new SystemController();
    
