namespace( 'Trb.Cms' );


Trb.Cms.Part = function(id, name, page, partClass, url, svcUrl, editDisplayMode) {
	this._id = id;
	this._name = name;
	this._page = page;
	this._class = partClass;
	this._url = url;
	this._svcUrl = svcUrl;
	this._lang = null;
	this._editDisplayMode = YAHOO.lang.isString(editDisplayMode) ? editDisplayMode : 'fullscreen';
	Trb.Cms.Part.instances[this._id] = this;
};

Trb.Cms.Part.instances = {};

Trb.Cms.Part.getPart = function(id) {
	return Trb.Cms.Part.instances[id];
}


Trb.Cms.Part.prototype.getId = function() {
	return this._id;
}
Trb.Cms.Part.prototype.getName = function() {
	return this._name;
}
Trb.Cms.Part.prototype.getPage = function() {
	return this._page;
}
Trb.Cms.Part.prototype.getClass = function() {
	return this._class;
}
Trb.Cms.Part.prototype.getUrl = function() {
	return this._url;
}
Trb.Cms.Part.prototype.getFront = function() {
	return _c('Trb_Cms_Part_Front_' + this.getId());
}
Trb.Cms.Part.prototype.getEdit = function() {
	return _c('Trb_Cms_Part_Edit_' + this.getId());
}
Trb.Cms.Part.prototype.getLang = function() {
	return this._lang;
}
Trb.Cms.Part.prototype.setLang = function(lang) {
	this._lang = lang;
}




Trb.Cms.Part.prototype.hideEdit = function() {
	
	var iframe = _e(this.getId() + '_iframe');
	iframe.parentNode.removeChild(iframe);
	this.editMask.destroy();
	
	try {
		this.load();
	} 
	catch (ex) { }
}




Trb.Cms.Part.prototype.showEdit = function(el) {
	var iframe = document.createElement('iframe');
	
	
    iframe.width = (YAHOO.util.Dom.getViewportWidth() - 0) + 'px';
    iframe.height = (YAHOO.util.Dom.getViewportHeight() - 0) + 'px';
    iframe.style.left = '0px';
    iframe.style.top = '0px';		
    iframe.style.position = 'absolute';
    iframe.frameborder = "0";
//    iframe.style.zIndex = Trb.Util.getHighestZindex(9999)+2;
    iframe.style.zIndex = 9999;
    iframe.id = this.getId() + '_iframe';
    iframe.style.display = 'none';
    iframe.style.backgroundColor = 'white';
    
    document.body.insertBefore(iframe, document.body.firstChild);
    
    var _iframe = new YAHOO.util.Element(iframe.id);
    _iframe.setStyle('border', '0px');
    

    
    // position
    var iframe = _e(iframe.id);
	var docRegion = YAHOO.util.Dom.getClientRegion();
	var iframeRegion = Trb.Util.getRegion(iframe);
	
	docRegion.width = docRegion.right - docRegion.left;
	docRegion.height = docRegion.bottom - docRegion.top;
	
	if (YAHOO.lang.isObject(el) && this._editDisplayMode == 'inplace') {
		var targetReg = Trb.Util.getRegion(el);
	    iframe.width = 600 + 'px';
	    iframe.height = 230 + 'px';
	    iframe.style.left = targetReg.left + 'px';
	    iframe.style.top = targetReg.top + 'px';
	}
	else {
		iframe.style.left   = docRegion.left + "px";
		iframe.style.top   = docRegion.top + "px";
	    iframe.width = YAHOO.util.Dom.getViewportWidth() + 'px';
	    iframe.height = YAHOO.util.Dom.getViewportHeight() + 'px';
	}
	iframe.style.display='block';
//	setTimeout("_e('"+iframe.id+"').style.display='block'", 1);

	
    this.editMask = new Trb.Mask(iframe);
    this.editMask.setColor('Black');
    this.editMask.setOpacity(0.5);
//    this.editMask.onClick.subscribe(Trb.Cms.Part.handleEditMask, this);
    this.editMask.show();
    
    iframe.src = this.getUrl();
//    iframe.src = 'http://www.google.com';
//	setTimeout("_e('"+iframe.id+"').src='"+this.getUrl()+"';", 100);
}


//
//Trb.Cms.Part.prototype.showEdit = function(el) {
//	var cont = document.createElement('div');
//    cont.style.left = '0px';
//    cont.style.top = '0px';		
//    cont.style.position = 'absolute';
//    cont.style.zIndex = Trb.Util.getHighestZindex()+10;
////    cont.style.zIndex = 9999;
//    cont.id = this.getId() + '_cont';
//    cont.style.display = 'none';
//    cont.style.backgroundColor = 'white';
//    
//    document.body.insertBefore(cont, document.body.firstChild);
//    
//    // position
//    var cont = _e(cont.id);
//	var docRegion = YAHOO.util.Dom.getClientRegion();
//	var contRegion = Trb.Util.getRegion(cont);
//	
//	docRegion.width = docRegion.right - docRegion.left;
//	docRegion.height = docRegion.bottom - docRegion.top;
//	
//	if (YAHOO.lang.isObject(el) && this._editDisplayMode == 'inplace') {
//		var targetReg = Trb.Util.getRegion(el);
//	    cont.width = 600 + 'px';
//	    cont.height = 230 + 'px';
//	    cont.style.left = targetReg.left + 'px';
//	    cont.style.top = targetReg.top + 'px';
//	}
//	else {
//		cont.style.left   = docRegion.left + "px";
//		cont.style.top   = docRegion.top + "px";
//	    cont.width = YAHOO.util.Dom.getViewportWidth() + 'px';
//	    cont.height = YAHOO.util.Dom.getViewportHeight() + 'px';
//	}
//	cont.style.display='block';
//	
//    this.editMask = new Trb.Mask(cont);
//    this.editMask.setColor('Black');
//    this.editMask.setOpacity(0.5);
//    this.editMask.show();
//    
//    var iframe = document.createElement('iframe');
//    iframe.id = this.getId() + '_iframe';
//    iframe.style.width = '100%';
//    iframe.style.height = '100%';
//    iframe.frameborder = 0;
//    
//    cont.appendChild(iframe);
//    
//    
////    iframe.src = this.getUrl();
////    iframe.src = 'http://www.google.com';
//	setTimeout("_e('"+iframe.id+"').src='"+this.getUrl()+"';", 1000);
//}


Trb.Cms.Part.prototype.load = function() {
	
	var callback = {
		success:this.handleSuccess,
		failure:this.handleFailure,
		argument: { me:this }
	}
	
	var url = this._svcUrl;
	if (this._lang != null) {
		url += "?lang=" + this._lang;
	}
	
	_e( this.getFront().globalizeId('cont') ).innerHTML = '<img src="/trb/img/icons/indicator.gif"/>';
	
	var request = YAHOO.util.Connect.asyncRequest('GET', url , callback);
}


Trb.Cms.Part.prototype.handleSuccess = function(o){ 
	var me = o.argument.me;
	var data = YAHOO.lang.JSON.parse(o.responseText);
	_e( me.getFront().globalizeId('cont') ).innerHTML = data.html;
}

Trb.Cms.Part.prototype.handleFailure = function(o){
	_e( me.getFront().globalizeId('cont') ).innerHTML = 'error';
}




Trb.Cms.Part.handleEditMask = function(type, args, me) {
	me.hideEdit();
}



Trb.Cms.Part.prototype.replaceContent = function(html) {
	var o = YAHOO.lang.JSON.parse(html);
	this.getFront().getElement().innerHTML = o.html;
}








/********************************************************************************************
 *	Front Class
 */

// CLASS
Trb.Cms.Part.Front = function (parent, id) {
	Trb.Cms.Part.Front.superclass.constructor.call(this, parent, id);
};

// Form extends Component.  Must be done immediately after the Form constructor 
YAHOO.lang.extend(Trb.Cms.Part.Front, Trb.Web.Component);




/********************************************************************************************
 *	Edit Class
 */

// CLASS
Trb.Cms.Part.Edit = function (parent, id) {
	Trb.Cms.Part.Edit.superclass.constructor.call(this, parent, id);
};

// Form extends Component.  Must be done immediately after the Form constructor 
YAHOO.lang.extend(Trb.Cms.Part.Edit, Trb.Web.Component);