/*
	Copyright (c) 2006-2010, ePages GmbH
	All Rights Reserved.

	epages.cartridges.de_epages.design.widget.Toolbackgroundimage $Revision: 1.47 $

*/

dojo.provide('epages.cartridges.de_epages.design.widget.Toolbackgroundimage');
dojo.require('epages.widget.LocalizedWidget');
dojo.require('epages.string');
dojo.require('epages.lang.hitch');
dojo.require('epages.event');
epages.require('ep_shop_upload', 'epages.uploader');
dojo.require('epages.widget.Tooltip');
dojo.require("epages.widget.FormElement");
dojo['require']('epages.cartridges.de_epages.mediagallery.widget.Toolmediagallery');

dojo.declare(
	"epages.cartridges.de_epages.design.widget.Toolbackgroundimage",
	[epages.widget.LocalizedWidget],
	{
		/**
		 * public properties
		 */
		label               : "",
		fileName            : "",
		imageType           : "",
		imageSetHidden      : "",
		imagesetLeft        : "",
		imagesetRight       : "",
		imageTiling         : "",
		disableTiling       : "0",
		allowImageSetTiling : "0",
		disableImageSet     : "0",
		objectId            : "",
		'class'             : "",
		uploadAction      	: "UploadFiles",
		fileNameNode        : null,
		formElement					: 'NewFile',
		userConfirmation		: false,
		/**
		 * template properties
		 */
		templateType        : "tablerow",
		defaultTemplateType : "tablerow",
		templatePath       : dojo.moduleUrl('epages.cartridges.de_epages.design.widget' , 'templates/Toolbackgroundimage.html'),
		translationName    : dojo.moduleUrl('epages.cartridges.de_epages.design.widget' , 'templates/translation'),

		_templateMapping:{
			'tablerow' : {templatePath: dojo.moduleUrl('epages.cartridges.de_epages.design.widget', 'templates/Toolbackgroundimage.html')},
			'xlicon'   : {templatePath: dojo.moduleUrl('epages.cartridges.de_epages.design.widget', 'templates/NavbarPreviewLayout1.html')}
		},

		/**
		 * widget properties
		 */
		imagePath         : epages.themeUrl('images'),
		widgetsInTemplate : true,

		postMixInProperties: function(){
			this._templateDirectory = dojo.moduleUrl('epages.cartridges.de_epages.design.widget' , 'templates');
			this.inherited("postMixInProperties", arguments);
		},

		postCreate  : function() {
			this.inherited("postCreate", arguments);
			dojo.addClass(this.domNode,this['class']);

			if(this.objectId != '') {
				this.fileNameNode.name=this.objectId+":"+this.fileNameNode.name;
			}


			// pass callback function to toolMediaGalleryWidget
			this.toolMediaGalleryWidget.fileNameNode = this.fileNameNode;
			this.toolMediaGalleryWidget.onApply = dojo.hitch(this, function(pathFromSite) {
				this.setFileName(pathFromSite);
			});

			if(this.disableTiling=="1") {
				this.tilingAreaNode.style.display='none';
			}

			this.tilingCheckboxNode = new epages.widget.FormElement({}, this.tilingCheckboxNode).elementNode;

			// check tiling checkbox if necessary
			if(this.imageTiling == "1"){
				epages.event.fire(this.tilingCheckboxNode,"click");
			}

			// init events
			this._appendEvents();
			// init buttons
			this._showButtons();
		},

		/**
		 * public methods
		 */

		setFileName: function(pathFromSite) {
			this.fileNameNode.value = pathFromSite;
			epages.event.fire(this.fileNameNode, "change");
		},

		/**
		 * private methods
		 */

		_appendEvents: function() {
		// summary: append onclick and onchange events

			// add value handler
			this.connect(this.clearImageNode,      'onclick', '_clearImage');
			this.connect(this.clearImageSetNode,   'onclick', '_clearImageSet');
			this.connect(this.restoreImageSetNode, 'onclick', '_restoreImageSet');
			this.connect(this.tilingCheckboxNode,  'onclick', '_toggleImageTiling');

			this.connect(this.fileNameNode,        'onchange', '_showButtons');
			this.connect(this.imageHiddenNode,     'onchange', '_showButtons');
			this.connect(this.imageTilingNode,     'onchange', '_showButtons');
		},

		_clearImage: function(evt) {
		// summary: clears the value of "image file name" input and throws change event
			if(this.userConfirmation){
				var me = this;
				dojo.publish("uimessage/show", [
					this.translation.get("HeadLineDeleteFile"),
					this.translation.get("File") + ': ' + me.label,
					'Dialog',
					{
						titleBar: this.translation.get("TitleDeleteFile"),
						sizeClass:"Large",
						typeClass:"Confirmation",
						buttons: [{
							label: this.translation.get("Delete"),
							cssClass: 'Active',
							onclick: function() {
								this._modalDialogWidget.hide();
								me.fileNameNode.value="";
								epages.event.fire(me.fileNameNode, "change");
							}
						},
						{
							label: this.translation.get("Cancel"),
							onclick: function() {
								this._modalDialogWidget.hide();
							}
						}]
					}
				]);
			}
			else{
				this.fileNameNode.value="";
				epages.event.fire(this.fileNameNode, "change");
				/*
				if(this.imageHiddenNode.value=="1") {
					epages.event.fire(this.noUndoImageHiddenNode,"change");
				}
				*/
			}
		},

		_clearImageSet: function(evt) {
		// summary: set value of "imageHidden" input to "1" and throw change event
			this.imageHiddenNode.value="1";
			epages.event.fire(this.imageHiddenNode,"change");
		},

		_restoreImageSet: function(evt) {
		// summary: set value of "imageHidden" input to "0" and throw change events
			this.imageHiddenNode.value="0";
			epages.event.fire(this.imageHiddenNode,"change");
			epages.event.fire(this.imageSetNode,"change");
		},

		_showButtons: function() {
		// summary: decides which button is visible
			if(this.imageTilingNode.value=='1') {
				this.tilingCheckboxNode.checked=true;
			} else {
				this.tilingCheckboxNode.checked=false;
			}

			if(this.fileNameNode.value!="") {						// image uploaded
				this.clearImageNode.style.display="";
				this.clearImageSetNode.style.display="none";
				this.restoreImageSetNode.style.display="none";
				if(this.disableTiling!="1") {
					this.tilingAreaNode.style.display="";
				}
			} else if(this.imageHiddenNode.value!="1"){ // show image set
				this.clearImageNode.style.display="none";
				if(this.disableImageSet=="0") {
					this.clearImageSetNode.style.display="";
				}
				else {
					this.clearImageSetNode.style.display="none";
				}
				this.restoreImageSetNode.style.display="none";
				if(this.allowImageSetTiling=="0" || this.disableTiling=="1") {
					this.tilingAreaNode.style.display="none";
				} else {
					this.tilingAreaNode.style.display="";
				}
			} else {																		// no image/ image set hidden
				this.clearImageNode.style.display="none";
				this.clearImageSetNode.style.display="none";
				this.restoreImageSetNode.style.display="";
				this.tilingAreaNode.style.display="none";
			}
		},

		_toggleImageTiling: function(evt) {
		// summary: hidden input / checkbox handler set
		// description: Handles the value of the hidden input element which depends on the visible checkbox.
		//		Afterwards it fires an change event to trigger the display handler (_showButtons).
			if(this.tilingCheckboxNode.checked==true) {
				this.imageTilingNode.value=1;
			} else {
				this.imageTilingNode.value=0;
			}
			epages.event.fire(this.imageTilingNode,"change");
		}
});