
function init()
{
	var obj = new classMenu();
	obj.init();
}

var classMenu = new Class({

	_anchorList: null,
	_sublistList: null,
	
	init: function() {
		this._setVars();
		this._setEvents();
		this._setPageIfHashIsSet();
	},
	
	_setVars: function() {
		this._anchorList = $$('#menu_list li.nav_item a ');
		this._sublistList = $$('#menu_list li.nav_item ul.child_root');
	},
	
	_setEvents: function() {
		this._setEventClick();
		this._setEventMenu();
	},
	
	_setIfActive: function() {
		this._sublistList.each(function(item,index){
			item.getElements('li a.active').each(function(subItem,subIndex) {
				subItem.fireEvent('click',subItem);
			});
		});
	},
	
	_setEventClick: function() { var $this = this;
		this._sublistList.each(function(item,index){
			item.getElements('li a[rel]').each(function(subItem,subIndex) {
				subItem.addEvent('click',function(event){
					new Event(event).stop();
					$this._setAnchorActiveStatus(subItem);
					ajaxResponseObj.getAjaxData( subItem.rel, true );
					$this._setWindowHash( subItem.href );
					$this._setSpriteAnchorActiveStatus( item, index );
				});
			});
		});
	},
	
	_setEventMenu: function() { var $this = this;
		this._anchorList.filter('.sprite').each(function(item,index) {
			item.addEvent('click',function(event){
				new Event(event).stop();
				if ( $this._sublistList[index].getElements('li').length ) {
					$this._setSpriteAnchorActiveStatus( item, index );
				} else {
					$this._setSpriteAnchorActiveStatus( item, index );
					ajaxResponseObj.getAjaxData( item.href, true );
					$this._setWindowHash( item.rel );
				}
			});
		});
	},
	
	_setSpriteAnchorActiveStatus: function( item, index ) {
		this._anchorList.filter('.sprite').getParent().removeClass('active');
		this._sublistList.setStyle('display','none');			
		item.parentNode.addClass('active');
		this._sublistList[index].setStyle('display','');
		
	},
	
	_setAnchorActiveStatus: function( item ) {
		this._anchorList.filter('.active').removeClass('active');
		item.addClass('active');
	},
	
	_setWindowHash: function( href ) {
		window.location.hash = this._getHashFromHref( href );
	},
	
	_setPageIfHashIsSet: function() {  var $this = this;
		var hash = this._getHashFromHref( window.location.hash );
		if ( !hash.length ) {
			this._setIfActive();
		} else {
		
			this._anchorList.each(function(item,index){
				if ( $this._getHashFromHref( item.href ) == hash ) { 
					item.fireEvent('click', item ); 
				}
			});
			
			this._anchorList.filter('.sprite').each(function(item,index){
				if ( $this._getHashFromHref( item.rel ) == hash ) { 
					item.fireEvent('click', item );
				}
			});
		}
	},
	
	_getHashFromHref: function( href ) {
		if ( href.indexOf('#') == -1 ) return '';
		return href.substr( href.indexOf('#'), href.length);
	}
	
});



var ajaxResponseUtilsClass = new Class({
	
	getAjaxData: function ( destPatch, showPreloader ) {
		this.jsonShowPreloader();
		this.jsonSetResponseHtml('');
		var json = new cmsAjaxResponse( destPatch+'&ajax=1', {loader: false }).doRequest();
	},
	
	insertDecodeHtml: function( html ) {
		$('contener').set( 'html', Base64.decode(html) );
		this.jsonHidePreloader();
	},
	
	jsonShowPreloader: function () {
		$('contener').addClass('json_loader');
	},
	
	jsonHidePreloader: function () {
		$('contener').removeClass('json_loader');
	},
	
	jsonSetResponseHtml: function ( html ) {
		$('contener').set( 'html', html );
		this.jsonHidePreloader();
	},
	
	jsonSetResponseText: function ( text ) {
		$('contener').text(text);
	}
	
});

var ajaxResponseObj = new ajaxResponseUtilsClass;

var cmsAjaxResponse = new Class({
	options:{
		loader: true,
		loaderId: '',
		loaderClassName:'loaderMedium',
		timeOut: 10000
	},
	initialize: function(url1, options){
		var $this = this;
		this.setOptions(options);
		this.request = new Request.JSON({
			url: url1,

		onComplete: function(jsonObj) {
			clearTimeout($this.requestTimer);
			$this.callFunction(jsonObj.response);
			$this.hideLoader();
		},
		onFailure: function(){
			clearTimeout($this.requestTimer);
			$this.hideLoader();
			$this.errorLoad();
		}
		
		});
	},
	doRequest: function(){
		this.request.get();
	},
	showLoader: function(){
		var $this = this;
		if(this.options.loader){
			this.loader = new Element('div', {'class':this.options.loaderClassName,'id':'loaderDiv'});
			if(this.options.loaderId!=''){
				this.loaderParent = $(this.options.loaderId);
				var dims = this.loaderParent.getSize();
				this.loader.setStyles({'width':dims.x,'height':dims.y});
				if($this.loaderParent.getStyles('position')!='relative') $this.loaderParent.setStyles({'position':'relative'}) 
				$this.loader.injectInside($this.loaderParent);
				return
			}
			else{
				$this.loaderParent = document.body;
				$this.loader.injectInside($this.loaderParent)
			}
		}	
	},
	hideLoader: function(){
		if (this.options.loader) {
			this.loader.remove();
		}
	},
	startTimer: function(){
		var $this = this;
		this.requestTimer = setTimeout(function() {
     		$this.request.fireEvent('onFailure', $this.request.transport);
    	 }, $this.options.timeOut); 
	},
	errorLoad: function(){
		var $this = this;
		//alert('error');
		clearTimeout($this.requestTimer);
	},
	callFunction: function(jsonObj){
		var $this = this;
		if (jsonObj.result == 0) { // error
			var wind = alert(jsonObj.message);
			return	
		}
		else {
			jsonObj.actions.each(function(item){
				switch (item.action) {
					case 'assign':
						$this.assign(item.id,item.attribute,item.value);
						break
					case 'append':
						$this.append(item.id,item.attribute,item.value);
						break
					case 'prepend':
						$this.prepend(item.id,item.attribute,item.value);
						break
					case 'replace':
						$this.replace(item.id,item.attribute,item.search, item.value);
						break
					case 'alert':
						$this.alertBox(item.title, item.message)
						break
					case 'insertAfter':
						$this.insertAfter(item.idAfter, item.id)
						break
					case 'insertBefore':
						$this.insertBefore(item.idBefore, item.id)
						break
					case 'addEvent':
						$this.addEv(item.id,item.event,item.value)
						break
					case 'removeEvent':
						$this.removeEv(item.id,item.event)
						break
					case 'script':
						$this.script(item.script)
						break
					case 'call':
						$this.call(item.funcName, item.args)
						break
					case 'removeById':
						$this.removeById(item.id);
						break
					case 'removeByClass':
						$this.removeByClass(item.className)
						break
					case 'create':
						$this.create(item.parent,item.tag,item.id,item.html,item.className,item.styles,item.events)
						break
					default:
						var wind = new cmsWindow(self, {title: 'error',content: ''});
						return
					}
				})
			return;
		}
	},
	assign: function(obj,attribute,value){
		var obj = $(obj);
		var $value = value;
		eval('obj.'+attribute+'=$value');
	},
	append: function(obj,attribute,value){
		var obj = $(obj);
		var $value = value;
		eval('obj.'+attribute+'=$value');
	},
	prepend: function(obj,attribute,value){
		var obj = $(obj);
		var oldVal = eval('obj.'+attribute);
		var newVal = value+oldVal;
		eval('obj.'+attribute+'=newVal');
	},
	replace: function(obj,attribute,search,value){
		var obj = $(obj);
		var oldVal = eval('obj.'+attribute).toString();
		var newVal = oldVal.replace(search,value);		
		eval('obj.'+attribute+'=newVal');
	},
	alertBox:	function(title, message){
		var wind = new cmsWindow(self,{title:title, content:message, loadMethod:'html',modal:true});
	},
	insertAfter: function(elAfter, el){
		$(el).injectAfter( elAfter );
	},
	insertBefore: function(elBefore, el){
		$(el).injectBefore( elBefore );
	},
	addEv: function(id,event,value){
		var $value = value;
		$(id).addEvent(event,function(){
			eval($value);
		})
	},
	removeEv: function(id, event){
		$(id).removeEvent(event)
	},
	script: function(script){
		eval(script);
	},
	call: function(funcName, args){
		eval(funcName+'('+args+')');
	},
	removeById: function(id){
		$(id).remove();
	},
	removeByClass: function(classname){
		$$(classname).each(function(item){
			item.remove();
		})
	},
	create: function(elParent,elTag,elId,elHtml,elClass,elStyles,elEvents){
		var newEl = new Element(elTag, {'styles':elStyles,'events':elEvents,'class':elClass,'id':elId});
		newEl.innerHTML = html;
		if(elParent){
			newEl.injectInside($(elParent));
			return
		}
		else {
			newEl.injectInside(document.body)
		}
	}
});
cmsAjaxResponse.implement(new Options);

function firePage(page){
	
	var url = Base64.decode(page);
	
	openPageFull(url);
}

function openPageFull(theUrl){
    var theWin ="'toolbar=0,location=1,directories=0,status=0,menubar=0,scrollbars=1,resizable=1'"
	littleWin = window.open(theUrl,'',theWin);
 	littleWin.focus();
}




