// Copyright (c) 2006 Pavel Korshunov (http://korshunov.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// VERSION 0.1

Object.inherit = function(destination, name, source) {
  var result;
  result = {};
//  result = destination;
  for (var property in destination) {
    result[property] = destination[property];
//    destination.__parent[name][property]= source[property];
  }

//  destination.__parent = source;
  if (result.__parent === undefined)  result.__parent = [];
  result.__parent[name] = destination;
  for (var property in source) {
    result[property] = source[property];
//    destination.__parent[name][property]= source[property];
  }
  return result;
}


var Gallery = Class.create();
Gallery.prototype = Object.inherit(Window.prototype, 'ClassWindow' , {
//    initialize:    function(){
//        this.Gallery_initialize.apply(this, arguments);
//    },

    initialize: function(){
        arguments[1] = Object.extend( Object.extend(this.options || {},{
            classNameGallery:   "dialog",
            title:              "TEST",
            titleGallery:       "",
            bottitleGallery:    "",
            next:               true,
            prev:               true,
            last:               true,
            first:              true,
            playAndPause:       true,
            width:              500,
            height:             400,
            urlGallery:         '',
            idimg:               0,
            idgallery:           0,
            addContentGallery:  "right",
            addAjaxParam:       "",
            intervalTimer:      1,
            showCenter:         false,
            autoPlay:			false

        }), arguments[1] || {});

//        this.Window_initialize.apply(this, arguments);
       this.__parent['ClassWindow'].initialize.apply(this, arguments);
//        this.__parent['ClassWindow'].initialize(arguments);
		this.slideShow = false;
		this.addParam = '';
        this.eventNext = this._showNext.bindAsEventListener(this);
        this.eventPrev = this._showPrev.bindAsEventListener(this);
        this.eventFirst = this._showFirst.bindAsEventListener(this);
        this.eventLast = this._showLast.bindAsEventListener(this);
        this.eventPlay = this._doPlay.bindAsEventListener(this);
        this.eventPause = this._doPause.bindAsEventListener(this);

        this._createGallery();
        this.gallerycontent = $(this.element.id + "_gallery_content");
        this.addHeight = 0;
        this.addWidth  = 0;

        var div = this._createHiddenDiv(this.options.classNameGallery + "_gallery_n");
		this.addHeight += Element.getDimensions(div).height;
		this.test = this.addHeight;
		div.parentNode.removeChild(div);

        var div = this._createHiddenDiv(this.options.classNameGallery + "_gallery_s");
		this.addHeight += Element.getDimensions(div).height;
		div.parentNode.removeChild(div);

        var div = this._createHiddenDiv(this.options.classNameGallery + "_gallery_bs");
		this.addHeight += Element.getDimensions(div).height;
		div.parentNode.removeChild(div);

        var div = this._createHiddenDiv(this.options.classNameGallery + "_gallery_w");
		this.addWidth += Element.getDimensions(div).width;
		div.parentNode.removeChild(div);

        var div = this._createHiddenDiv(this.options.classNameGallery + "_gallery_e");
		this.addWidth += Element.getDimensions(div).width;
		div.parentNode.removeChild(div);

    },
    getGalleryContent: function() {
    	return this.gallerycontent;
    },
    showItem: function(cmd) {
		if (this.myAjax && this.myAjax._complete === false ) return;
		var url = this.options.urlGallery;
		var pars = 'cmd='+ cmd +'&id=' + this.options.idimg + '&galleryid=' + this.options.idgallery + '&winid=' + this.element.id + this.options.addAjaxParam + this.addParam;

		this.myAjax = new Ajax.Request(
			url,
			{
				method: 'get',
				parameters: pars,
				onComplete: 	(function(originalRequest)
					{
						//put returned XML in the textarea
						this.getGalleryContent().innerHTML = originalRequest.responseText;
						this.options.idimg = $F(this.getId() + "_idimg");
						if ( this.slideShow ) this._goSlideShow();

					}).bind(this),
					onLoading: function(originalRequest)
					{
						//put returned XML in the textarea
						this.value2 = originalRequest;
					}

			});

    },
    showGallery: function( idgall, addParam ) {
    	if (this.options.showCenter)this.showCenter();
    		else this.show();
    	if ( this.options.idgallery !== idgall || addParam !== this.addParam) {
	    	this.options.idgallery = idgall;
	    	this.options.idimg = 0;
	    	if ( addParam )this.addParam = addParam;
	    	this.showItem('first');
	    	if (this.options.autoPlay ) this._doPlay();
	    }
    },
    _showNext: function(event) {
	    	this.showItem('next');
    },
    _showPrev: function(event) {
	    	this.showItem('prev');
    },
    _showFirst: function(event) {
	    	this.showItem('first');
    },
    _showLast: function(event) {
	    	this.showItem('last');
    },
    _goSlideShow: function() {
    	if ( this.slideShow )
        	this.timer = setTimeout(this._showNext.bind(this), this.options.intervalTimer * 1000);
    },
    _doPlay: function(event) {
    	$(id + "_play").hide();
    	$(id + "_pause").show();
    	this.slideShow = true;
    	this._goSlideShow();
    },
    _doPause: function(event) {
    	$(id + "_pause").hide();
    	$(id + "_play").show();
    	this.slideShow = false;
	    clearTimeout(this.timer);
    },
    _createGallery: function() {
        id = this.getId();
        className = this.options.classNameGallery;

        var firstDiv = this.options.first ? "<div class='"+ className +"_first' id='"+ id +"_first'></div>" : "";
        var prevDiv = this.options.prev ? "<div class='"+ className +"_prev' id='"+ id +"_prev'></div>" : "";
        var playDiv = this.options.playAndPause ? "<div class='"+ className +"_play' id='"+ id +"_play'></div>" : "";
        var pauseDiv = this.options.playAndPause ? "<div class='"+ className +"_pause' id='"+ id +"_pause'></div>" : "";
        var nextDiv = this.options.next ? "<div class='"+ className +"_next' id='"+ id +"_next'></div>" : "";
        var lastDiv = this.options.last ? "<div class='"+ className +"_last' id='"+ id +"_last'></div>" : "";

        this.getContent().innerHTML = "\
          <table id='"+ id +"_gallery' class='"+ className +"_gallery_table fullsize'>\
            <tr>\
              <td class='"+ className +"_gallery_nw'></td>\
              <td class='"+ className +"_gallery_n'><div id='"+ id +"_gallery_top' class='"+ className +"_gallery_title'>"+ this.options.titleGallery +"</div></td>\
              <td class='"+ className +"_gallery_ne'></td>\
            </tr>\
            <tr>\
              <td class='"+ className +"_gallery_w'></td>\
                <td id='"+ id +"_table_gallery_content' class='"+ className +"_table_gallery_content' valign='middle' align='center'><div id=\"" + id + "_gallery_content\" class=\"" +className + "_gallery_content fullsizeq\"></div></td>\
              <td class='"+ className +"_gallery_e'></td>\
            </tr>\
            <tr>\
              <td class='"+ className +"_gallery_sw'></td>\
              <td class='"+ className +"_gallery_s'><div id='"+ id +"_gallery_bottom' class='"+ className +"_gallery_btitle'>"+ this.options.bottitleGallery +"</div></td>\
              <td class='"+ className +"_gallery_se'></td>\
            </tr>\
            <tr>\
              <td class='"+ className +"_gallery_bsw'></td>\
                <td class='"+ className +"_gallery_bs' align='center'><div id='"+ id +"_button' class='"+ className +"_gallery_control'>" + firstDiv + prevDiv + playDiv + pauseDiv + nextDiv + lastDiv + "</div></td>\
                <td class='"+ className +"_gallery_bse'></td>\
            </tr>\
          </table>\
         ";
        if ( this.options.first ) {
            Event.observe($(id + "_first"), "click", this.eventFirst);
        }
        if ( this.options.prev ) {
            Event.observe($(id + "_prev"), "click", this.eventPrev);
        }
        if ( this.options.playAndPause ) {
        	$(id + "_pause").hide();
            Event.observe($(id + "_play"), "click", this.eventPlay);
            Event.observe($(id + "_pause"), "click", this.eventPause);
        }
        if ( this.options.next ) {
            Event.observe($(id + "_next"), "click", this.eventNext);
        }
        if ( this.options.last ) {
            Event.observe($(id + "_last"), "click", this.eventLast);
        }
        if ( this.options.addButtonEffect ) {
            if ( this.options.first ) {
                this.initButtonEffect(id, 'first');
            }
            if ( this.options.prev ) {
                this.initButtonEffect(id, 'prev');
            }
            if ( this.options.playAndPause ) {
                this.initButtonEffect(id, 'play');
                this.initButtonEffect(id, 'pause');

            }
            if ( this.options.next ) {
                this.initButtonEffect(id, 'next');
            }
            if ( this.options.last ) {
                this.initButtonEffect(id, 'last');
            }
        }
		if (this.width && this.height)
		  this.setSize(this.options.width, this.options.height);
    },
    hide: function() {
		this.__parent['ClassWindow'].hide.apply(this, arguments);
		this._doPause();
    },
	// Sets window size
	setSize: function(width, height) {
		this.__parent['ClassWindow'].setSize.apply(this, arguments);
		var gallery_content = $(this.element.id + '_gallery_content');
		if(gallery_content) {
			gallery_content.setStyle({height: height - this.addHeight + 'px'});
			gallery_content.setStyle({width: width  - this.addWidth+ 'px'});
		}
//		$$('#' + this.getId() + ' .fullsize').each(function(node){
//			var size = node.parentNode.getDimensions();
//			node.setStyle({width: size.width + 'px'});
//			node.setStyle({height: size.height + 'px'});
//		})

	}


});

