Action = new Class({
    Implements: Events,

    options: {        
        callBacks: null
    },

    initialize: function(options){
        window.addEvent('load', this.load.bind(this));
    },

    load : function (event){
    }

});

function createAction(obj)
{
    var o = new obj();
    if (!window.actions)
        window.actions = [];
    window.actions[o.options.name] = o;
}



ListActionBase = new Class({
    Extends : Action,

    options: {
        name : 'default',
        id : 'list_id',
        target_name : 'target_name'
    },

    initialize: function(options){
        this.parent(options);
        t = this;
        ["onSelect"].each( function (index){
            if (t[index])
                t.addEvent(index, t[index]);
        });
		this.data = {};
    },
    
    load : function (event)
    {
        this.parent(event);
        this.tblObj = $(this.options.id);
        this._attachMouse();
		/*var url = this.tblObj.getAttribute("url");
		if (!url)
			url = "?mod="+this.tblObj.getAttribute("mod")+"&act="+this.tblObj.getAttribute("act")+"&filter_"+this.tblObj.getAttribute("filter")+"=on";
        var req = new Request({url: url});
        req.addEvent("success", this._onXmlLoaded.bind(this));
        req.send("xml=1");
		*/
    },
	
	_onXmlLoaded : function (responseText, responseXml)
	{
		var parser = new XmlParser(responseXml);
		this.data = parser.parseVar("rows");
		this.pager = parser.parseVar("pagerInfo");
	},

    _attachMouse : function ()
    {
        for(var i= 2; i < this.tblObj.rows.length - 1; i++){
			if ($(this.tblObj.rows[i]).getAttribute("type") &&
				$(this.tblObj.rows[i]).getAttribute("type") == "pager")
					continue;
            $(this.tblObj.rows[i]).addEvent("mouseover", this._onRowMouseOver.bind(this));
            $(this.tblObj.rows[i]).addEvent("mouseout", this._onRowMouseOut.bind(this));
            $(this.tblObj.rows[i]).addEvent("click", this.select.bind(this));
        }
    },

    _onRowMouseOver : function (e)
    {
        e.target.parentNode.addClass("active");
    },

    _onRowMouseOut : function (e)
    {
        e.target.parentNode.removeClass("active");
    },

    select : function (event)
    {
        for(var i= 2; i < this.tblObj.rows.length - 1; i++)
            $(this.tblObj.rows[i]).removeClass("selected");
        $(event.target.parentNode).addClass("selected");
        var partId = event.target.parentNode.getAttribute("partid");
        this.fireEvent("onSelect", [partId]);
    },

    onSelect : function (rowId)
    {
        window.actions[this.options.target_name].showPart(rowId);
    }

});

XmlParser = new Class({

    initialize: function(xml){
        this.xml = xml;
    },

    _parseNode : function (node, res)
    {
        for(var i=0; i < node.childNodes.length; i++)
            if (node.childNodes[i].nodeType == 1)
                if (node.childNodes[i].childNodes[0].nodeType == 4){
                    if (node.childNodes[i].tagName == "item")
                        var key = node.childNodes[i].getAttribute("key");
                    else 
                        var key = node.childNodes[i].tagName;
                    res[key] = node.childNodes[i].childNodes[0].data;
                } else {
                    if (node.childNodes[i].tagName == "item")
                        var key = node.childNodes[i].getAttribute("key");
                    else 
                        var key = node.childNodes[i].tagName;
                    res[key] = {};
                    this._parseNode(node.childNodes[i], res[key]);
                }
    },

    parseVar : function (name){
        var res = {};
        if (Browser.Engine.trident){
            for(var i=0; i < this.xml.childNodes[1].childNodes.length; i++){
                if (this.xml.childNodes[1].childNodes[i].tagName == name){
                    var element = this.xml.childNodes[1].childNodes[i];
                    break;
                }
            }
        } else {
            var element  = this.xml.childNodes[0].getElements(name)[0];
        }
        this._parseNode(element, res);
        return res;
    }
});


TopSearchActionBase = new Class({
    Extends : Action,

	fillObj : function ()
	{
        this.productersObj = $("topsearch_producter_id");
        this.productersObj.addEvent("change", this.onProducterChange.bind(this));


        this.modelsObj = $("topsearch_model_id");
        this.yearsObj = $("topsearch_year");
        this.nomenclatureObj = $("topsearch_nomenclature_id");
	},

    load : function (event)
    {
        this.parent(event);
    	this.fillObj();    
    },

    onProducterChange : function (event)
    {
        while(this.modelsObj.options.length > 0)
            this.modelsObj.options[0].parentNode.removeChild(this.modelsObj.options[0]);

        while(this.yearsObj.options.length > 0)
            this.yearsObj.options[0].parentNode.removeChild(this.yearsObj.options[0]);

        this.nomenclatureObj.selectedIndex = 0;
        
        var producterId = parseInt(event.target.options[event.target.selectedIndex].value);
        var req = new Request({url: "/?mod=search&act=get_models&xml=1"});
        req.addEvent("success", this._onModelsLoaded.bind(this));
        req.send("id="+producterId);
    },

    _onModelsLoaded : function(responseText,  responseXml)
    {
        var parser = new XmlParser(responseXml);
        var data = parser.parseVar("models");
        for(var i in data){        
            var opt = document.createElement("option");
            opt.value = i;
            opt.innerHTML = data[i];
            this.modelsObj.appendChild(opt);
        }

        if (this.modelsObj.options.length > 0){
        // years
                var opt = document.createElement("option");
                opt.value = 0;
                opt.innerHTML = "";
                this.yearsObj.appendChild(opt);
            for(var i = 2008; i >= 1991 ; i--){
                var opt = document.createElement("option");
                opt.value = i;
                opt.innerHTML = i;
                this.yearsObj.appendChild(opt);
            }
        }
    }

});

ProgressControl = new Class({
	objClass : "ctrl_progress_main",

	initialize : function (object)
	{
		this.object = $(object);
		// create object
		this.obj = new Element("div",{
				"html" : "<img src='/pic/progress1.gif'/>",
				"class" : this.objClass,
				"styles" : {
					"display" : "none",
					"width" : "48px",
					"height" : "48px",
					"margin" : "auto",
					"margin-top" : (parseInt(this.object.getStyle("height")) - 11) / 2
				}
		});
		this.object.appendChild(this.obj);
		this.oldHeight = this.object.getStyle("height");
	},

	show : function ()
	{
		$each (this.object.childNodes, function (obj,index){ 
			obj = $(obj);
			if (obj.setStyle)
				if (obj.className == this.objClass)
					obj.setStyle("display","");
				else
					obj.setStyle("display","none");
		}.bind(this));
		this.object.setStyle("height",this.oldHeight);
		
	},

	hide : function ()
	{
		$each (this.object.childNodes, function (obj,index){ 
			obj = $(obj);
			if (obj.setStyle)
				if (obj.className == this.objClass)
					obj.setStyle("display","none");
				else
					obj.setStyle("display","");
		}.bind(this));
	}
	
});


