using('evance.net.Request');
using('evance.net.RequestData');
using('ak.Slideshow');

if(!$defined(ak)){var ak = {}};

ak.Banner = evance.core.UIObject.extend({
    _className: 'Banner',
    _classOwner: 'ak.Banner',
    imageDir: '',
    proxy: null,
    slide: null,
	galleries: new Array(),
    galleryId: 0,

    initialize: function(){
        this.parent(arguments[0]);
        this._request = new evance.net.Request();
        this._request.method = 'POST';
        this._request.setMimeType = 'text/xml';
        this._request.setResponseFormat('xml');
        this._request.format = 'xml';
        this._request.addListener(this);
        this._requestData = new evance.net.RequestData();
        this._requestData.setAutoClear(true);
    },

    setup: function(proxy, galId){
        this._requestData.addItem('action', 'banners');
        this._requestData.addItem('galleryId', galId);
        if($defined(proxy)) this.proxy = proxy;
        this._request.post(this.proxy, this._requestData.serialise());
    },

    onHttpRequestResponse: function(e){
        if(this._getFileNames(e.response)){
            if($defined(this.slide))
                this.slide.loadImageSequence(this.galleries[this.galleryId]);
            else {
                this.slide = ak.Slideshow(this.id, this.galleries[this.galleryId],
                                         {
                                            imagesDir: this.imageDir,
                                            backgroundPosition: "left top"
                                         });
                var self = this;
                this._listeners.each(function(x){
                    self.slide.addListener(x);
                });
            }
            this.slide.resume();
            this.dispatchEvent('onHttpRequestResponse', e);
            //this.draw();
        } else {
            e.invalidResponse = true;
            this.dispatchEvent('onHttpRequestError', e);
        }
    },

    _getFileNames: function(xml){
        for(var a=0; a<xml.childNodes.length; a++){
            if(xml.childNodes[a].nodeType == 1){
                if(xml.childNodes[a].nodeName == 'gallery'){
                    var element = xml.childNodes[a];
                    if(element.attributes[0].name == 'id')
                        this.galleryId = element.attributes[0].value;
                    if($defined(this.galleries[this.galleryId]))
                        return true;
                    x = xml.childNodes[a];
                    var names = new Array();
                    for(var i=0; i<x.childNodes.length; i++){
                        element = x.childNodes[i];
                        if(element.nodeType == 1 && element.nodeName == 'banner'){
                            names.push(element.firstChild.nodeValue);
                        }
                    }
                    this.galleries[this.galleryId] = names;
                    return true;
                }
            }
        }
        return false;
    },

    onImageClick: function(e){
        this.slide.moveTo(e.imageId);
    },

    onGalleryChange: function(e){
        this.slide.loadImageSequence(this.galleries[e.galleryId]);
    },

    addGallery: function(g){
        //TODO
    },

    draw: function(gal){
		if(this.isPageLoaded && !this.isDrawn) {
			if(this.parentId) {
                this.parentElement = $(this.parentId);
				this.parent();
                this.element.className = 'banner';
                if(gal == true){
                    if($defined(this.galleries[this.galleryId])){
                        this.slide = ak.Slideshow(this.id, null,
                                                 {
                                                    imagesDir: this.imageDir,
                                                    backgroundPosition: "left top"
                                                 });
                        this._listeners.each(function(x){
                            this.slide.addListener(x);
                        });
                    } else {
                        evance.warn("Could not render slideshow. " + this._className + ".galleries was not defined.");
                    }
                }
            }
        }
    }
});    
