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

	epages.cartridges.de_epages.design.widget.Toolfontfamily $Revision: 1.20 $

*/
dojo.provide("epages.cartridges.de_epages.design.widget.Toolfontfamily");
dojo.require("epages.widget.LocalizedWidget");
dojo.require("epages.widget.Menu");
dojo.require("epages.event");
dojo.require("dijit._Container");

dojo.declare(
	"epages.cartridges.de_epages.design.widget.Toolfontfamily",
	[epages.widget.LocalizedWidget, dijit._Contained],
	{
		/**
		 * public properties
		 */
		label               : '',
		fontFamily          : '',
		fontType            : '', /* Headline, Paragraph, Link, ... */
		objectId            : '',

		/**
		 * private properties
		 */
		templateType        : "tablerow",
		defaultTemplateTyp  : "tablerow",
		_templateMapping:{
			'tablerow' : {templatePath: dojo.moduleUrl('epages.cartridges.de_epages.design.widget', 'templates/Toolfontfamily.html')},
			'xlicon'   : {templatePath: dojo.moduleUrl('epages.cartridges.de_epages.design.widget', 'templates/Toolfontfamilyxlicon.html')}
		},

		_lastSelected       : null,
		_menuWidget         : null,
		_popup              : null,
		_fonts: {
			'Arial'         : { fontfamily : "Arial, Verdana, Helvetica",                            webfont : '' },
			'Baumans'       : { fontfamily : "Baumans",                                              webfont : 'Baumans' },
			'ComingSoon'    : { fontfamily : "Coming Soon",                                          webfont : 'Coming+Soon' },
			'CourierNew'    : { fontfamily : "Courier New, Verdana, Helvetica",                      webfont : '' },
			'Cutive'        : { fontfamily : "Cutive",                                               webfont : 'Cutive' },
			'DroidSerif'    : { fontfamily : "Droid Serif",                                          webfont : 'Droid+Serif:400,700,400italic,700italic' },
			'ExpletusSans'  : { fontfamily : "Expletus Sans",                                        webfont : 'Expletus+Sans:400,700,400italic,700italic' },
			'Georgia'       : { fontfamily : "georgia, Times New Roman, arial, sans-serif",          webfont : '' },
			'Iceberg'       : { fontfamily : "Iceberg",                                              webfont : 'Iceberg' },
			'LucidaSans'    : { fontfamily : "Lucida Sans Unicode, Lucida Sans, Verdana, Helvetica", webfont : '' },
			'Marmelad'      : { fontfamily : "Marmelad",                                             webfont : 'Marmelad' },
			'Metamorphous'  : { fontfamily : "Metamorphous",                                         webfont : 'Metamorphous' },
			'NovaCut'       : { fontfamily : "Nova Cut",                                             webfont : 'Nova+Cut' },
			'RopaSans'      : { fontfamily : "Ropa Sans",                                            webfont : 'Ropa+Sans:400,400italic' },
			'Tahoma'        : { fontfamily : "Tahoma, Verdana, Helvetica",                           webfont : '' },
			'TimesNewRoman' : { fontfamily : "Times New Roman, Times",                               webfont : '' },
			'TrebuchetMS'   : { fontfamily : "Trebuchet MS, Verdana, Arial, Helvetica",              webfont : '' },
			'Verdana'       : { fontfamily : "Verdana, Arial, Helvetica",                            webfont : '' }
		},

		/**
		 * widget properties
		 */
		widgetType      : 'Toolfontfamily',
		templatePath    : dojo.moduleUrl('epages.cartridges.de_epages.design.widget' , 'templates/Toolfontfamily.html'),
		translationName : dojo.moduleUrl('epages.cartridges.de_epages.design.widget' , 'templates/translation'),
		imagePath       : epages.themeUrl('images'),

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

		postCreate  : function() {
			this.inherited("postCreate", arguments);
			this.fontSelectNode.value=this.fontFamily;
      if (!/Arial|Courier\sNew|georgia|Lucida\sSans\sUnicode|Tahoma|Times\sNew\sRoman|Trebuchet\sMS|Verdana/i.test(this.fontFamily)) {
        var newElement = document.createElement('option');
        newElement.innerHTML = this.fontFamily;
        this.fontSelectNode.insertBefore(newElement, this.fontSelectNode.firstChild);
        this.fontSelectNode.selectedIndex = 0;
      }
			if(this.clickNode) {
				this._menuWidget= new epages.widget.Menu({
				'class': 'FontfamilyChooser'
			});

				for(var i in this._fonts){
					this._addFontRow(i);
				}
				this._menuWidget.startup();
			}
		},

		_addFontRow: function(/*integer*/i) {
		// summary: add menu entry (link)
			var parentWidget=this;
			var entry= new dijit.MenuItem({
				label: i,
				iconClass: 'MinimizeIcon',
				onClick: function() {
					parentWidget.switchFont(i);
					dijit.popup.close(parentWidget._menuWidget);
				},
				id: 'font'+i
			});

			entry.containerNode.style.fontFamily= this._fonts[i].fontfamily;
			entry.containerNode.style.fontSize= '12px';
			this._menuWidget.addChild(entry);

			if(this.fontFamily == this._fonts[i].fontfamily) {
				this._lastSelected=entry.domNode;
				dojo.addClass(this._lastSelected, "Selected" );
			}

		},

		showFontMenu:	function()	{
		// summary: show menu
		// summary: open the popup to the side of the current menu item
			dijit.popup.open({
				parent:this.domNode,
				popup:this._menuWidget,
				orient: {'BL':'TL', 'BR':'TR', 'TL':'BL', 'TR':'BR'},
				around: this.domNode
			});
		},

		_onBlur: function () {
			dijit.popup.close(this._menuWidget);
		},

		switchFont: function(/*ObjectIndex*/fontIndex) {
			this.fontSelectNode.value=this._fonts[fontIndex].fontfamily;
			$('ContentWebfont').value = this._fonts[fontIndex].webfont;
			epages.event.fire(this.fontSelectNode,"change");
			if(this._lastSelected) {
				dojo.removeClass(this._lastSelected, "Selected" );
			}
			this._lastSelected=$$('font'+fontIndex).domNode;
			dojo.addClass(this._lastSelected, "Selected" );
		}
	});