function PackageActualStockraceBets(name) {
	if(name!=null){
		this.name=name;
	}else{
		this.name = 'actual_stockrace_bets_game1x2';	
	}
	
	this.baseIteration = 3;
	this.currentIteration = 1;
	this.success = function(res) {
		if (res!=null && res.length > 5 ) {
			jQuery('#actual_stockrace_bets').html(res);
		}
	},
	this.next = function() {
		this.currentIteration = this.currentIteration + this.baseIteration;
	}
}

function PackageCustomerbalance() {
	this.name = 'customerbalance';
	this.baseIteration = 2;
	this.currentIteration = 2;
	this.success = function(res) {
		if (res.length > 5) {
			jQuery('#customerbalancedetail').html(res);
		}
	},
	this.next = function() {
		this.currentIteration = this.currentIteration + this.baseIteration;
	}
}

function PackageJackPot() {
	this.name = 'jackpot';
	this.baseIteration = 1;
	this.currentIteration = 1;
	this.success = function(t) {
		if (t.length > 3) {
			jQuery('#jackpot-value').html(t);
		}
	},
	this.next = function() {
		this.currentIteration = this.currentIteration + this.baseIteration;
	}
}

function PackageRanklist() {
	this.name = 'ranklist';
	this.baseIteration = 4;
	this.currentIteration = 2;

	this.success = function(html) {
		if (html != null && html != undefined && html.length > 3) {
			jQuery('#rankList').html(html);
		}
	},

	this.next = function() {
		this.currentIteration = this.currentIteration + this.baseIteration;
	}
}

function PackageWiners() {
	this.name = 'winers';
	this.baseIteration = 9;
	this.currentIteration = 3;

	this.success = function(html) {
		if (html != null && html != undefined && html.length > 3) {
			jQuery('#winnerlist').html(html);
		}

	},

	this.next = function() {

		this.currentIteration = this.currentIteration + this.baseIteration;
	}
}

function PackageToptrades() {
	this.name = 'toptrades';
	this.baseIteration = 5;
	this.currentIteration = 2;

	this.success = function(html) {
		if (html != null && html != undefined && html.length > 3) {
			jQuery('#toptradeslist').html(html);
		}
	},

	this.next = function() {
		this.currentIteration = this.currentIteration + this.baseIteration;
	}
	
}
var __ajaxPackedRequest;
var __countDownInterval;
function AjaxRequestPackage(packageList, url)

{
	this.packageList = packageList;
	this.currentIteration = 1;
	this.url = url;
	
	this.addPackage = function(pack) {
		this.packageList.push(pack);
	},

	this.buildPackage = function() {
		var pack = new Array();
		for ( var p in this.packageList) {
			if (this.currentIteration == this.packageList[p].currentIteration) {
				pack.push(this.packageList[p]);
			}
		}
		return pack;
	},

	this.send = function(addPackageToRequest) 
	{
		if (this.currentIteration > 500000000) {

			this.currentIteration = 0;
			for ( var p in this.packageList) {
				this.packageList[p].currentIteration = 0;
			}
		}

		pack = this.buildPackage();

		if (pack.length <= 0) {
			return true;
		}

		var packNames = '';
		var canAddPackageToRequest=true;
		for ( var p in pack) 
		{
			packNames = packNames + pack[p].name + ',';
			if(addPackageToRequest!=null  && addPackageToRequest.name==pack[p].name){
				 canAddPackageToRequest==false;
			}			
		}
		
		if(addPackageToRequest!=null && canAddPackageToRequest==true){
			packNames = packNames + ','+addPackageToRequest.name; 
		}
		

		url = this.url + '/paskages/' + packNames;
		var _this = this;
		this.currentIteration++;
		jQuery.ajax( {
			type : 'get',
			url : url,
			dataType : 'json',

		success : function(t) {
			var result = eval(t);			
			for ( var p in pack) {
				pack[p].next();
				if(  result[pack[p].name]!=null && result[pack[p].name]!=undefined ){
					pack[p].success(result[pack[p].name]);
				}
			}
			
			if(__countDownInterval == null){
				countDown();
				__countDownInterval = intervalStart("countDown()",1000);
			}
		}
		});
	}
	
}

function countDown()  {		
	
	var now = new Date();
	var year = now.getFullYear();
	var month = now.getMonth();
	var day = now.getDate();		

	// get time of end_time event, probably after ajax request it won't take new
	// value:)
	
	if(document.getElementById('end-time') == null){
		return true;
	}
	var newtime = document.getElementById('end-time').getAttribute('value');
	
	if(newtime==null || newtime==''){
		return true;
	}
	
	var result = newtime.split(",");
	var h = result[0];
	var m = result[1];
	var s = result[2];
	var to = new Date(year, month, day, h, m, s);
	
	// to = new Date(newtime);
	var ile = to.getTime() - now.getTime();

	var godzin = Math.floor(ile / (1000 * 60 * 60));

	var minut = Math.floor(ile / (1000 * 60) - godzin * 60);
	var sekund = Math.floor(ile / 1000 - godzin * 60 * 60 - minut * 60);
	
	if (ile <= 0) {
		
		// document.getElementById("time").innerHTML = 'end';
		document.getElementById("countdown").innerHTML = "00:00:00";
		// stop interval
		clearInterval(__countDownInterval);
		__countDownInterval = null;
		
		/*
		// fetch new data, clear cache
		//fetchData();
		*/
		new AjaxRequestPackage(new PackageJackPot).send();
	} else {
		
		minut += '';
		sekund += '';
		godzin += '';
		if (godzin.length == 1) {
			godzin = '0' + godzin;
		}
		if (minut.length == 1) {
			minut = '0' + minut;
		}
		if (sekund.length == 1) {
			sekund = '0' + sekund;
		}
		
		document.getElementById("countdown").innerHTML = godzin + ":" + minut
				+ ":" + sekund;
	}
}


function intervalStart(action, interval) {
	return setInterval(action, interval);
}

function intervalStop(intervalReferer) {
	clearInterval(intervalReferer);
}