
var URL = function (url) {
	var parts = url.split('?');
	this.paramMap = new Array();
	this.baseUrl = parts[0];
	if (parts.length>=2) {
		var paramParts = parts[1].split('&');
		for (var i=0, l=paramParts.length; i<l; i++) {
			var tokens = paramParts[i].split('=');
			var key = tokens[0];
			var value = (tokens.length>=2)?tokens[1]:null;
			this.paramMap[key] = value;
		}
	}
}
URL.prototype.getParameter = function (paramKey) {
	return this.paramMap[paramKey] || null;
}
URL.prototype.getIntParameter = function (paramKey) {
	var val = this.getParameter(paramKey);
	var valInt = parseInt(val);
	return (valInt >= 0) ? valInt:-1; 
};
URL.prototype.setParameter = function (key, value) {
	this.paramMap[key] = new String(value);
};
URL.prototype.removeParameter = function (key) {
	this.paramMap[key] = null;
};
URL.prototype.getFullUrl = function () {
	var string = this.baseUrl;
	var paramTokens = new Array();
	for (key in this.paramMap) {
		if (typeof(this.paramMap[key])!='function') {
			paramTokens.push(key + "=" + this.paramMap[key]);
		}
	}
	return string + '?' + paramTokens.join('&');
};
/**
 * User Information Management Class
 */
POSITION_ACCESS_WAIT = 2000;
var Info = function () {
	this.cookieManager = new CookieManager({shelfLife:30});
	this.homeLocation = DEFAULT_LOCATION;
	this.accessUrl = new URL(location.href);
	
	{
		var tmpApiDomain = this.accessUrl.getParameter('domain');
		if (tmpApiDomain) API_DOMAIN = tmpApiDomain;
	}
	
	this.targetDate = new Date(); //Default:Now
	this.lastFetchedPosition = null;
	{
		var savedLatitude = this.cookieManager.getCookie('homeLatitude');
		var savedLongitude = this.cookieManager.getCookie('homeLongitude');
		if (savedLatitude && savedLongitude) {
			this.homeLocation = 
				{latitude: parseFloat(savedLatitude), longitude: parseFloat(savedLongitude)};
		} else {
			this.homeEmptyAction();
		}
	}
	this.currentPosition = new GLatLng(this.homeLocation.latitude, this.homeLocation.longitude);
};
Info.prototype = {
	homeEmptyAction: function () {
		Element.hide($('gotoHomeIcon'));
		Message.showMessage(Message.HOME_EMPTY);
	},
	getYear: function (){return this.targetDate.getFullYear()},
	getMonth: function (){return this.targetDate.getMonth() + 1},
	getDay: function (){return this.targetDate.getDate();},
	getHour: function (){return this.targetDate.getHours();},
	getMin: function (){return this.targetDate.getMinutes();},
	getSeconds: function (){return this.targetDate.getSeconds();},
	
	getGmtYear: function (){return this.getGmtTargetDate().getFullYear()},
	getGmtMonth: function (){return this.getGmtTargetDate().getMonth() + 1},
	getGmtDay: function (){return this.getGmtTargetDate().getDate();},
	getGmtHour: function (){return this.getGmtTargetDate().getHours();},
	getGmtMin: function (){return this.getGmtTargetDate().getMinutes();},
	getGmtSeconds: function (){return this.getGmtTargetDate().getSeconds();},
	
	getGmtTargetDate: function () {
		var gmtDate = new Date();
		gmtDate.setTime(this.targetDate.getTime() + this.targetDate.getTimezoneOffset() * 60 * 1000);
		return gmtDate;		
	},
	
	applyFirstPosition: function () {
		if (this.accessUrl.getParameter('latitude') && this.accessUrl.getParameter('longitude')) {
			SatMap.moveTo(
				parseFloat(this.accessUrl.getParameter('latitude')), 
				parseFloat(this.accessUrl.getParameter('longitude'))
			);
		} else {
			this.gotoHome();
		}
		this.registerPosition(map.getCenter());
	},
	next24h: function () {
		this.targetDate.setTime(this.targetDate.getTime() + 60 * 60 * 24 * 1000);
		urlInfo.refresh(false);	
	},
	prev24h: function () {
		this.targetDate.setTime(this.targetDate.getTime() - 60 * 60 * 24 * 1000);
		urlInfo.refresh(false);	
	},
	getLatitude: function (){},
	getLongitude: function (){},
	getSatCode: function (){},
	createUrl: function (){
		var url = location.href;
	},
	saveHome: function () {
		Element.show($('gotoHomeIcon'));
		var position = map.getCenter();
		this.homeLocation = {latitude:position.lat(), longitude:position.lng()};
		this._saveHomeToCookie();
		Message.showMessage(Message.HOME_SET);
	},
	_saveHomeToCookie: function () {
		this.cookieManager.setCookie("homeLatitude",new String(this.homeLocation.latitude));
		this.cookieManager.setCookie("homeLongitude",new String(this.homeLocation.longitude));
		
	},
	gotoHome: function () {
		SatMap.moveTo(this.homeLocation.latitude,this.homeLocation.longitude);
	},
	isMoveRequired: function (position) {
		if (this.currentPosition)
		return (!(this.currentPosition && position.equals(this.currentPosition)))
	},
	registerPosition:function (position, withoutReset) {
		this.currentPosition = position;
		wSatMap.setCenter(position);
		if (this.lastFetchedPosition !=null && !this.lastFetchedPosition.equals(position) && this.lastFetchedPosition.distanceFrom(position) > 50 * 1000) {
			this.lastStopTime = (new Date()).getTime();
			setTimeout(checkMovement, POSITION_ACCESS_WAIT);
		}
		if (!withoutReset)Player.reset();
	},
	getUrl : function () {
		var urlHere = new URL(this.accessUrl.baseUrl);
		urlHere.setParameter("latitude", this.currentPosition.lat());
		urlHere.setParameter("longitude", this.currentPosition.lng());
		var sat = SatMap.getSelectedSat();
		if (sat) {
			urlHere.setParameter("satCode",sat.code)
		}
		return urlHere.getFullUrl();
	},
	refresh: function (requireSatChange) {
		if (SatMap.getSelectedSat()) {
			this.lastFetchedPosition = this.currentPosition;
			Message.showMessage(Message.LOADING_SAT_SCHEDULE_START,[SatMap.getSelectedSat().name]);
			Util.loadJsonp(SatMap.getSelectedSat().getSatScheduleUrl());
			if (requireSatChange)
				Util.loadJsonp(SatMap.getSelectedSat().getCalendarUrl());
		}
		if (this.targetDate) {
			var since = urlInfo.targetDate;
			var until = new Date();
			until.setTime(since.getTime() + 24*60*60*1000);
			$('labelTargetDateSince').innerHTML = Util.formatDate(since,"MM/DD HH:mm");
			$('labelTargetDateUntil').innerHTML = Util.formatDate(until,"MM/DD HH:mm");
		}
	}
};

function checkMovement () {
	if (urlInfo.lastStopTime <= (new Date()).getTime()-POSITION_ACCESS_WAIT) {
		SatMap.refreshSat();
		urlInfo.refresh(true);
	}
}
