/*
 *
 *  V.Kondratiev
 *  Collection Photo Album (palbum) AddOn
 *
 *
 */

var palbum = $.inherit(Module, {
	__constructor: function(moduleDiv) {
		this.__base(moduleDiv);
		this.type = "palbum";
		this.isResizing = true;
		this.keepAspectRatio = false;
		if( this.draghandle ) {
			this.draghandle.addHelpButton();
		}
		var tmp=this;
		this.options = "";

      $("<script>").attr("type","text/javascript").attr("src","/adm/zbl/js/lib/jquery.lightbox.js").appendTo("head");
      var l = $("<link>").attr("type","text/css").attr("rel","stylesheet").appendTo("head");
      l.attr("href","/adm/zbl/css/lib/jquery.lightbox.css");
	},

	// Function to be called only on published pages. Provides access to ajax functionality on published pages.
	initialize: function() {
		// Get the username and thumbnail count from the database,
		// save them to this object, then load the photos
		this.getOptionsFromDb( createRef( this,this.loadPhotos ) );
	},


	// Get the options for this module instance from the database,
	// and possibly perform a callback function afterward
	getOptionsFromDb: function(callbackFunc) {
		_this = this;
		this.ajaxPost('getOptions', {},
			function(data, textSuccess) {
				// Save these options and then possibly call a callback functions
				(callbackFunc || $.noop)(data);
			}
		);
	},


	applyTheme: function( opts ) {
		$('#pa_sett_tab', this.container).removeClass().addClass( opts.theme );
		$('#pa_header', this.container).removeClass().addClass( opts.theme );
		$('#pa_content', this.container).removeClass().addClass( opts.theme );
	},


	loadPhotos: function( opts ) {
      opts = opts.response;
      this.applyTheme( opts );
		var _this = this, holder = $('#pa_content', this.container), $header = $('#pa_header', this.container);
      ( opts.show_title == 'Y' ) ? $header.html( opts.title ) : $header.hide();
		holder.empty();
		this.ajaxPost('getCollection', {'collection_id':opts.collection_id},
			function( data, textSuccess ) {
            if( data.response == "null" ) {
               holder.html('<h2>Album contains no photos</h2>');
            }
            else {
               var k = 0;
               var photos = eval( "("+data.response+")" );
               $.each( photos, function( i, photo ) {
                  if( photo.type == "image" ) {
                     k++;
                     var image = $('<img>', {'src':photo.thumb,'width':74,'heigth':74, 'class':'pa_thumb'}).appendTo( holder );
                     $('<a>', {'class':'lightbox', 'rel':opts.collection_id, 'html':image, 'href':photo.image, 'title':photo.caption}).appendTo( holder );
                  }
                  if( i+2 > opts.thumb_number ) {
                     return false;
                  }
                  else {
                     return true;
                  }
               });
               if( k == 0 ) { // <-- There is no photos in collection
                  holder.html('<h2>Album contains no photos</h2>');
               }
               $('a.lightbox', holder).lightbox({
                  fileLoadingImage: '/adm/zbl/images/lightbox/loading.gif',
                  fileBottomNavCloseImage: '/adm/zbl/images/lightbox/closelabel.gif',
                  overlayOpacity : 0.7,
                  fitToScreen: true
               });
            }
			}
		);
	},


	// This is called as a callback from loadModule.
	// It should also be called directly by modules that overload loadModule and avoid calls to frame2 on init (like customHtml).
	loadModuleCallback: function(data, textStatus) {
		// Store the returned data so we can access it later on
		this.moduleData = data;

		//this.loadCollectionData();

		// We need to use innerHTML here because of the (highly probable)
		// chance that the html we get back has <script> in it.  jQuery 1.4
		// tries to be smart and not insert non-standard code.
		this.container[0].innerHTML = data.html;

		// Handle actions to be taken after the saving of this module's settings
		if (this.postSaveData) {
			this.handleModuleSaveResult(this.postSaveData);
			this.setOptionsDb( this.postSaveData.options );
			this.postSaveData = '';
		}

		// Load the retrieved photos into the module div after getting the options from the database
		this.getOptionsFromDb( createRef(this, this.loadPhotos) );

		this.addDragHandle(data);

		// If a module has declared itself resizing, make it so...
		if (this.isResizing) {
			this.addResizing();
		}
	}
});

