var data = 99;
function scApp()
{
	this.products = Array();
	this.items = Array();
	this.tokens = new Array();
	this.url = '';
	this._data = '';
	this.params = '';
	this.enable = 0;
	this.loading = 1;
}

proccessBarRefresh = function(){
	if( $('sourcecache_proccess').innerHTML == 'Loading.'){
		$('sourcecache_proccess').innerHTML = 'Loading..';
	}else if( $('sourcecache_proccess').innerHTML == 'Loading..'){
		$('sourcecache_proccess').innerHTML = 'Loading...';
	}else if( $('sourcecache_proccess').innerHTML == 'Loading...'){
		$('sourcecache_proccess').innerHTML = 'Loading....';
	}else if( $('sourcecache_proccess').innerHTML == 'Loading...'){
		$('sourcecache_proccess').innerHTML = 'Loading.....';
	}else $('sourcecache_proccess').innerHTML = 'Loading.';
	
	if( src_app.loading ){
		setTimeout("proccessBarRefresh()",1000);
	}
}

scApp.prototype.proccessBar = function(){
	$j('body').append("<div class=\"sourcecache_proccess\" id=\"sourcecache_proccess\" style=\"position:fixed;top:0px;right:0px;color:red;width:100px;text-align:left;z-index:1000\">Loading.</div>");
	proccessBarRefresh();
}

scApp.prototype.proccessBarClose = function(){
	$('sourcecache_proccess').style.display = 'none';
}

scApp.prototype.getDataFromServer = function (){
	var parameters = 'product_ids=';
	for( var i=0;i<this.products.length;i++ ){
		parameters += this.products[i]+',';
	}
	
	parameters += '&tokens=';
	for( var i=0;i<this.tokens.length;i++ ){
		parameters += this.tokens[i]+',';
	}
	
	var myAjax = new Ajax.Request(
	this.url,
	{
		method: 'post',
		parameters : parameters,
		onException: function (xhr, e)
		{
			alert('Exception : ' + e);
		},
		onComplete: function (xhr){
			var data = base64decode(xhr.responseText);
			data = zip_inflate(data);
			data = utf8to16(data);
			if( data.isJSON() ){		
				src_app._data = data.evalJSON();
			}else{				
				src_app._data = data
			}
			src_app.dance();
		}

	});
}

scApp.prototype.run = function()
{	
	if( this.enable ){
		/*this.proccessBar();*/
		this.getDataFromServer();
	}
}

scApp.prototype.addProduct = function( product_id ){
	this.products[this.products.length] = product_id; 
}

scApp.prototype.addToken = function( product_id,token ){
	this.tokens[this.tokens.length] = token; 
}

scApp.prototype.addDancer = function( product_id,token ){
	if( this.enable ){
		this.items[this.items.length] = new dancer( product_id,token ); 
		this.addProduct(product_id);
		this.addToken(product_id,token);
	}
}

scApp.prototype.dance = function (){	
	try{
		for( var i=0;i<this.items.length;i++ ){
			this.items[i].dance( this._data );
		}
		
		var elts=document.getElementsByTagName('textarea'); 
		if( elts.length > 0 ){
			var match = /eval[0-9]+/;
			for(var i=0;i<elts.length;i++){ 
				if(match.test(elts[i].getAttribute("id"))){ 
					eval( elts[i].value );
				} 
			}
		}
		this.loading=0;
		/*this.proccessBarClose();*/
	}catch(e){}	
}

var src_app = new scApp;


