﻿ 
 window.addEvent('domready', function() {
    ItemLoader =new LoadSystem()
    ContentHandler.initialize();
    CH=ContentHandler;
    });

var LoadSystem = new Class({
        options: {
            Fx1:{duration:600,transition:Fx.Transitions.Back.easeOut},
            Fx2:{duration:400,transition:Fx.Transitions.Expo.easeIn}
        
        },
        
        initialize:function() 
        {
         this.Element=new Element('div',{"class":"Loader"}).setStyle('opacity',0).injectInside(document.body)
         this.Element.Percentage=new Element('div').setStyle('width',60).injectInside(this.Element)
         this.Element.Fx = new Fx.Styles(this.Element,this.options.Fx2)
         this.Element.Percentage.Fx= new Fx.Styles(this.Element.Percentage,this.options.Fx1)
         this.Element.shown=false;
         this.Qeue=1;
         this.LQeue=1;
         this.max=140;         
        },
        
        newQeue:function(){
        if (!this.Element.shown)
            {
            this.Element.shown=true
            this.Element.Fx.stop();
            this.Element.Fx.start({'opacity':1})
            
            }
            this.Qeue++
            this.Element.Percentage.Fx.stop();
            this.Element.Percentage.Fx.start({'width':((this.LQeue/this.Qeue)*136)})
           

        },
        
        removeQeue:function(){
        if (this.Element.shown)
            {
            this.LQeue++
            this.Element.Percentage.Fx.stop();
            this.Element.Percentage.Fx.start({'width':((this.LQeue/this.Qeue)*136)})
            if (this.LQeue==this.Qeue)
                {
                this.LQeue=0
                this.Qeue=0;
                this.Element.Fx.stop();
                this.Element.Fx.start({'opacity':0})
                this.Element.shown=false
                }
            
            }
        }

});






function inlineProcess(parent)
{
var Arr=parent.getElementsByTagName("div");
    for (var i=0;i<Arr.length;i++)
        if (Arr[i].getAttribute('inti'))
            var New =function(x){eval(x)}.delay(10,this,Arr[i].getAttribute('inti'));
            
            
}

var ItemLoader
var CH
var ContentHandler =
{

initialize: function() {
    this.Object=new HistoryManager();
    this.Container={'Container0':$$('#LeftContext .maincontext')[0],'Container1':$('RightContext'),'Container2':$('LeftRightContext')};
    this.Object.addEvent('onHistoryChange',ContentHandler.CallBack);
    this.Object.fireEvent('onHistoryChange', [this.Object.getCurrentLocation()]);
    this.Controller=new Array();
    this.StyleSheet=new Array();
    inlineProcess(document.body);
},

Handel : function (Arg) 
{

    this.State=ContentHandler.Object.getCurrentLocation()
    this.Object.addState(Arg);
    this.Object.fireEvent('onHistoryChange', [this.Object.getCurrentLocation()]);   
},

CallBack : function(Arg)
{
    if (ContentHandler.State==Arg)
        return;
    new Json.Sync("CallBack/EventHandler.ashx?arg="+Arg+((ContentHandler.State==undefined)?'&bind=1':"&state="+ContentHandler.State)).send();
    
    ContentHandler.Update(Arg,0,0)
    ContentHandler.State=Arg;
    
    new Ajax("CallBack/EventHandler.ashx?arg="+Arg+"&idr=1&Cid=0", {
	method: 'get',
	evalResponse:true,
	onComplete: function() {ItemLoader.removeQeue()}
	}).request();
	ItemLoader.newQeue()
	
	
},

Update : function(Arg,Container,IsDubRes)
{

	new Ajax("CallBack/EventHandler.ashx?arg="+Arg+"&idr="+IsDubRes+"&Cid="+Container, {
		method: 'get',
		evalScripts:false,
		onSuccess : function(e){inlineProcess(ContentHandler.Container['Container'+Container]);ItemLoader.removeQeue()},
		onFailure: function() {alert('Check Network Status')},
		update:ContentHandler.Container['Container'+Container]

	}).request();
	ItemLoader.newQeue();
	
	

},
Execute : function(Arg,Container,IsDubRes)
{
	new Ajax("CallBack/EventHandler.ashx?arg="+Arg+"&idr="+IsDubRes+"&Cid="+Container, {
		method: 'get',
		evalResponse:true,
		onComplete: function() {ItemLoader.removeQeue()}
	}).request();
	ItemLoader.newQeue();

},
GetContent : function(Arg,Container,IsDubRes,OnComplete,data)
{
    new Json.Remote("CallBack/EventHandler.ashx?arg="+Arg+"&idr="+IsDubRes+"&Cid="+Container+"&", {
		onComplete: function(e) {OnComplete(e);ItemLoader.removeQeue()}
	}).send(data);
	ItemLoader.newQeue();
		


    
},

ModuleHandler: function(Module,Arg,IsDubRes,OnComplete,data)
{
    new Json.Remote("CallBack/ModuleHandler.ashx?arg="+Arg+"&idr="+IsDubRes+"&Module="+Module, {
		onComplete: function(e) {OnComplete(e);ItemLoader.removeQeue()}
	}).send(data);
	ItemLoader.newQeue();
	

    
},


LoadController : function (Arg,OnDone) 
{
    Arg=Arg.toLowerCase();
    for (i=0;i<this.Controller.length;i++)
        if (this.Controller[i]==Arg)
            {
            if(OnDone){OnDone();}
            return;
            }
            
   
 
        
	new RPCX("CallBack/ControllerHandler.ashx?arg="+Arg, {
	method: 'get',
	onFailure: function() {alert('Check Network Status')},
	onComplete: function() {this.Controller.push(Arg);if(OnDone){OnDone();};ItemLoader.removeQeue()}.bind(this,OnDone)
	}).request();
	ItemLoader.newQeue();
    
},
SyncEvent : function (Arg,OnDone) 
{
    Arg=Arg.toLowerCase();
    for (i=0;i<this.Controller.length;i++)
        if (this.Controller[i]==Arg)
            {
            if(OnDone){OnDone();}
            return;
            }
            
   
 
        
	new Ajax("CallBack/ControllerHandler.ashx?arg="+Arg, {
	method: 'get',
	evalResponse:true,
	onFailure: function() {alert('Check Network Status')},
	onComplete: function() {this.Controller.push(Arg);if(OnDone){OnDone();};ItemLoader.removeQeue()}.bind(this,OnDone)
	}).request();
	ItemLoader.newQeue();
    
},
SyncData : function(Arg,Container,IsDubRes,OnComplete,data)
{
    new Ajax("CallBack/EventHandler.ashx?arg="+Arg+"&idr="+IsDubRes+"&Cid="+Container+"&", {
		onComplete: function(e) {OnComplete(e)}
	}).send(Json.toString(data));
	
},

	
LoadStyle : function(Arg,OnDone,base,Arg2)
{
if (Arg2)
{
for (var property in Arg2)
   if(window[property])
        {
        Arg=Arg2[property]
        break;
        }
}
    Arg=Arg.toLowerCase();
    for (i=0;i<this.StyleSheet.length;i++)
        if (this.StyleSheet[i]==Arg)
            {
            if(OnDone){OnDone();}
            return;
            }
            
if (!base)	
    base=Arg
	        lpos=-1
            npos=0
            while (npos!=-1){
                lpos=npos
                npos=base.indexOf('/',lpos+1)
            }
            if (lpos>1)
	            base=base.substr(0,lpos+1)
            
	new Ajax(Arg, {
	method: 'get',
	evalResponse:false,
	onFailure: function() {alert('Check Network Status')},
	onComplete: function(e) {
            	
     re = /url\((.*?)\)/gim;
     e = e.replace(re, 'url('+base+"$1"+')');
	var styleElement = document.createElement("style")
	    styleElement.type = "text/css";
	    if (styleElement.styleSheet) {
	      styleElement.styleSheet.cssText = e;
	    } else {
	      styleElement.appendChild(document.createTextNode(e));
	    }
	    try{
	      document.getElementsByTagName("head")[0].appendChild(styleElement);
	    }catch(e){}
	    ItemLoader.removeQeue()
	    CH.StyleSheet.push(Arg)
	    if(OnDone){OnDone();}
	
	
	}
	}).request();
	
	ItemLoader.newQeue();
}

}



