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

	epages.cartridges.de_epages.seo.widget.ToolSEOCheck

*/
dojo.provide("epages.cartridges.de_epages.seo.widget.ToolSEOCheck");
dojo.require("epages.widget.LocalizedWidget");
dojo.require("epages.html");
dojo.require('epages.io.translation');
dojo.require('epages.lang.sort');
dojo.require('epages.widget.FormElement');
dojo.require('epages.cartridges.de_epages.presentation.icon');

dojo.declare(
	"epages.cartridges.de_epages.seo.widget.ToolSEOCheck",
	[epages.widget.LocalizedWidget],
	{
		/**
		 * public properties
		 */
		uri           : '?',
		objectTreeId  : 'contentExplorerObjectTree',
		classStoreId  : 'contentClassStore',
		imageUrl      : epages.vars.StoreRoot + '/BO/icons',
		widgetsInTemplate  : true,
		disabled      : false,
		featureNotAllowed : false,

		/**
		 * private properties
		 */
		_classStore :   undefined,
		_currentClassId: undefined,					// current classid of selected element in tree
		_overloadClassId: undefined,				// overload currentClassId (to create a new page on different page) - temporary only
		_overloadObjectId : undefined, 			// overload current parent object - temporary only
		_imageClickConnect: undefined,
		_selectedItemNoFeature: false,
		_currentObjectNode : undefined,
		_gaugeDialog : undefined,

		/**
		 * widget properties
		 */
		templatePath:    dojo.moduleUrl('epages.cartridges.de_epages.seo.widget', 'templates/ToolSEOCheck.html'),
		translationName: dojo.moduleUrl('epages.cartridges.de_epages.seo.widget', 'templates/translation'),

		postCreate: function() {
			this.inherited("postCreate", arguments);
			dojo.subscribe(this.objectTreeId+'/select', this, '_onSelectObject');
			this._imageClickConnect = dojo.connect(this.imageNode, 'click', this, '_onClick');
			this._classStore = dojo.getObject(this.classStoreId);

			if(this.disabled || this.featureNotAllowed) {
				var gauge = this.gaugeNode;
				jQuery.ready( 'de_epages.seo.ui.seoGaugeWidget', function($){
					de_epages(gauge).seoUiSeoGaugeWidget('setDeactivated', true);
				});
			}
		},

		_updateGauge : function(val) {
				var gauge = this.gaugeNode;
				jQuery.ready( 'de_epages.seo.ui.seoGaugeWidget', function($){
					de_epages(gauge)
						.seoUiSeoGaugeWidget()
						.seoUiSeoGaugeWidget('setPercentage', val);
				});
		},

		_onSelectObject:function(node) {
			this._currentObjectNode = node;
			this._currentClassId = node.item.classId;
			this._overloadClassId = undefined;  // reset _overloadClassId
			this._overloadObjectId = undefined; // reset _overloadObjectId
			var classItem = this._classStore.fetchItemByIdentity(this._currentClassId);

			if(node.item.canDoSEOCheck && !this.featureNotAllowed) {
				this._updateGauge(node.item.seoQuality);

				dojo.removeClass(this.domNode,'DisabledButton');
				if (this._imageClickConnect === undefined) {
					this._imageClickConnect = dojo.connect(this.imageNode, 'click', this, '_onClick');
				}
				dojo.publish(this.id+"/enabled", []);
				this.disabled=false;
			} else {
				this._updateGauge(0);
				dojo.addClass(this.domNode,'DisabledButton');
				if (this._imageClickConnect != undefined) {
					dojo.disconnect(this._imageClickConnect);
					this._imageClickConnect = undefined;
				}
				dojo.publish(this.id+"/disabled", []);
				this.disabled=true;
			}

			if(this.featureNotAllowed) {
				if (this._imageClickConnect === undefined) {
					this._imageClickConnect = dojo.connect(this.imageNode, 'click', this, function() {
						location.href = '?ViewAction=MBO-ViewStatusResources&ObjectID=' + epages.vars.SiteID;
						return false;
					});
				}
			}

		},

		_onClick: function (/* event */ evt, /* object? */ opt) {
			var _self = this;
			jQuery.ready(['ep.ui.dialog', 'de_epages.seo.ui.seoCheckWidget'], function($){
				if(!_self._gaugeDialog) {
					_self._gaugeDialog = de_epages('<div class="SEOCheckDialog">')
						.uiDialog({
							autoOpen:	false,
							dialogClass: 'SEOCheckDialogLayer',
							buttons: {
								"Cockpit":  {
									text: _self.translate('OpenSEOCockpit'),
									click: function() {
										location.href = ep.config.baseUrl + '?ViewAction=MBO-ViewSEOCockpit'  + '&ObjectID=' + ep.config.siteId;
									}
								},
								"Close":	{
									text:	_self.translate('Close'),
									click: function()	{
										ep(this).uiDialog("close");
									}
								}
							}
						});

					_self._gaugeDialog.bind("SEOCheckNewQuality", function(event, newValue) {
						_self._updateGauge(newValue);
					});
				}
				var item = _self._currentObjectNode.item;

				_self._gaugeDialog.seoUiSeoCheckWidget({
					'caption': '<span class="ep-SEOGaugeWidgetTitle">' + _self.translate('SEOCheck') + '</span> <span class="LeftPadding" style="font-weight: normal">' + _self.translate('ThisPageQuality') + '<span>',
					'currentSEOQuality' : item.seoQuality || 0,
					'objectId' : item.objectId
				});

				var epdlg = ep(_self._gaugeDialog);
				epdlg.find('.ep-js').metaparse();
				epdlg.uiDialog('Instance').uiDialog.find('.ep-js').metaparse();
				epdlg.uiDialog('open');
			});
		}
	}
);