var connectionHandlerInstance;
var ziinga_auction_data_cache = {};
	
var connectionHandler = function () {
	
	var allowNewAjaxRequest = true;
	var isConnected = false;
	var isReconnecting = false;
	
	var ajaxTimer = null;
	
/*	private functions	*/
	var connectionMade = function (){
		isConnected = true;
		isReconnecting = false;
	};
	var connectionLost = function () { 
		isConnected = false;
	}
	
/*	public functions	*/
	var construct = function () {
		var playerVersion = swfobject.getFlashPlayerVersion();
		var interval = (playerVersion.major < 10 ? 1000 : 3000);
		
		loadAuctions();
		goAjax();
		ajaxTimer = window.self.setInterval(goAjax, interval);
	};

	var goAjax = function () {
		if (allowNewAjaxRequest) {
			allowNewAjaxRequest = false;
				
			$.getJSON('http://' + top.location.host + '/auctiondata.php?showServerTime', function (data) {
				allowNewAjaxRequest = true;				
				if (typeof data == 'object' && !$.isEmptyObject(data)) {
					ziinga_auction_data(data, true);
				}
			});
		}
	}		
	
	var goAjaxErrorDetected = function () {
		if (ajaxTimer === null) {
			allowNewAjaxRequest = true;
			goAjax();
			ajaxTimer = window.self.setInterval(goAjax, 2000);
		}
	}
	
	var loadAuctions = function () {
		auctionItemController.loadAuctions();
	}

	var reConnect = function () {
		
		if (isReconnecting === false) {
			
			var theFlash = $('#flashDiv')[0];
			if (theFlash !== undefined) {
				
				/*console.log(new Date() + ' : reconnecting...');*/
				
				theFlash.connect();
				isReconnecting = true;
			}
		}
	}
	
	var stopAjax = function () {
		if (ajaxTimer !== null) {
			allowNewAjaxRequest = false;
			
			window.self.clearInterval(ajaxTimer);
			ajaxTimer = null;
			delete ajaxTimer;
		}
	}
	
/*	publics	*/
	return {
		
		construct : construct,
		
		connectionMade : connectionMade,
		connectionLost : connectionLost,
		stopAjax : stopAjax,
		
		reConnect : reConnect,
		loadAuctions : loadAuctions,
		goAjaxErrorDetected : goAjaxErrorDetected
	};
}();
	

/*	DOM READY	*/
	$(document).ready(function () {
		ziinga_auction_data_cache.msg = getMessages();
		ziinga_auction_data_cache.countryCode = $('#country_param').text();
		
		ziinga_auction_data_cache.loggedOnUser = false;
		ziinga_auction_data_cache.getLoggedOnUser = function () {
			if (this.loggedOnUser === false) {
				this.loggedOnUser = $('.logged_on_user').text();	/*	now we only need to find that username once!	*/
				if (this.loggedOnUser.length === 0) {
					this.loggedOnUser = false;
				}
			}
			return this.loggedOnUser;
		}

		connectionHandlerInstance = connectionHandler;
		connectionHandlerInstance.construct();
	});
	
/*	CONNECT	*/	
	function ziinga_applet_initialized () {
		/*console.log(new Date() + ' : ziinga_applet_initialized');*/
	}

/*	MEDIUM SUCCESS	*/
	function ziinga_medium_success () {
		/*console.log(new Date() + ' : ziinga_medium_success');*/
	}

/*	LOGIN SUCCESS	*/
	function ziinga_login_success () {
		/*console.log(new Date() + ' : ziinga_login_success');*/
		
		connectionHandlerInstance.connectionMade();
	}
	
/*	MEDIUM CLOSED	*/	
/*	MEDIUM FAILED	*/	
/*	LOGIN FAILED	*/	
	function ziinga_connection_failure () {
		/*console.log(new Date() + ' : ziinga_connection_failure');*/
	}

	function ziinga_connection_failure_2 () {
		/*console.log(new Date() + ' : ziinga_connection_failure_2');*/
	}
	
	function ziinga_connection_failure_3 () {
		/*console.log(new Date() + ' : ziinga_connection_failure_3');*/
	}
	
/*	AUCTION DATA	*/
	function ziinga_auction_data (json, serverUsingAjax) {
		/*console.log(new Date() + ' : ziinga_auction_data');*/
		
		if (json === 'killAllClients') {
			auctionItemController.goToInactivity();
			
		} else {
		
			if (!serverUsingAjax) {
				var theString = json;
				if (theString.length > 0) {
					json = eval('(' + theString + ')');
				}
				
				connectionHandlerInstance.stopAjax();
			}
			auctionItemController.updateAuctions(json, serverUsingAjax);
		}
	}
	
/*	SERVER TIMESTAMP	*/
	function ziinga_server_timestamp (serverTimestampMillis) {
		var serverTimestamp = Math.ceil((parseInt(serverTimestampMillis) + 500) / 1000);
		auctionItemController.setServerTime(serverTimestamp - 2);
		/*
			serverTimestamp from applet is always 1 seconds higher than the data from auctiondata.php. No known reason for this...
		*/
	}
	
/*	ZIINGA_BIDS	*/	
	function ziinga_bids (noOfBids) {
		$(document).ready(function () {
			auctionItemController.theController.repladeNoOfBids(noOfBids);
		});
	}

