/**
* Draws a bar chart according to given data.
* @param {Object} data a map of x-scale units and height of the bar (in percents). Example:
* data = {
*	1999: 20,
*	2000: 30,
*	2003: 10,
*	2004: 0
* };
* @param {Object} style sets the color and alfa of fill that is used to draw the bars (optional, default is udes if omitted)
* Properties are:
* 	{String} color  HEX code of the color to be used (e.g. FF00FF)
* 	{Number} alpha transparency of the bar 
*/
function drawBarChart(data, style) {
	thisMovie('timeslider').drawBarChart(data, style);
}

/**
* Clears the bar chart.
*/
function clearBarChart() {
	thisMovie('timeslider').clearBarChart();
}

/**
* Draws a line highlighting given period of time.
* @param {String} args.id id of the period (can be used to change/remove highlighting of particular period)
* @param {Number} args.start the start of the period
* @param {Number} args.end the end of the period
* @param {Object} args.style the style to be applied to the line (optional, the default one is used if omitted). 
* Style properties are:
* 	{String} color HEX code of the color to be used (e.g. FF00FF)
* 	{Number} alpha transparency of the line
* @param {Boolean} args.drawEnds if true the period ends are drawn
* @param {Object} args.endsStyle the style of the ends (optional, the default is the highlight style, but alpha is 100). 
*/
function highlightPeriod(args) {
	thisMovie('timeslider').highlightPeriod(args);
}

/**
* Clears a line highlighting for period with given id (or all periods, if id is not specified)
* @param {String} id id of the period 
*/
function clearPeriodHighlight(id) {
	thisMovie('timeslider').clearPeriodHighlight(undefined);
}

/**
 * This function is called by the TimeSlider component every time when its state is changed by user
 * @param {Number} current map slider value in years
 * @param {Number} start timerange low value in years (left slider)
 * @param {Number} end  timerange high value in years (right slider)
 */
function timebar_event(current, start, end) {
	// delivering information to the main control object
	//window.top.CONTROLLER.timebarEvent(current, start, end); 

	// parameter description :
	// current:Number  -> map slider value in years
	// start:Number -> timerange low value in years (left slider)
	// end:Number -> timerange high value in years (right slider)
	//HyperCities.debug("current="+current);
	//HyperCities.debug("start="+start);
	//HyperCities.debug("end="+end);

	HyperCities.timebar.timeChangeHandler(current, start, end);
}

/**
 * Sets TimesLider component values according to given parameters
 */
function timebar_set(current, start, end, active, scaleFloor, scaleCeiling, rescaleDuration) {	
	timeBarParameters = new Object();

	if (typeof(start) !== "undefined" && start !== null)
		timeBarParameters.startYear = Number(start); // timerange low value (left slider)

	if (typeof(end) !== "undefined" && end !== null)
		timeBarParameters.endYear = Number(end); // timerange high value (right slider)
	
	if (typeof(current) !== "undefined" && current !== null)
		timeBarParameters.currentYear = Number(current); // map slider value 
	
	if (typeof(active) === "undefined" || active === null || active.length < 1) {
		timeBarParameters.showActiveMarker = false;
	} else {
		timeBarParameters.activeYear = Number(active); // selected admin unit marker position ( years)
		timeBarParameters.showActiveMarker = true;
	}
	
	//timeBarParameters.scaleFloor = Number(0); // lowest possible value of scale range
	//timeBarParameters.scaleCeiling = Number(3000); // highest possible value of scale range
	//timeBarParameters.rescaleDuration = Number(1500); //  duration of timebar transition animation (milliseconds)
	if (typeof(scaleFloor) !== "undefined" && scaleFloor !== null)
		timeBarParameters.scaleFloor = Number(scaleFloor);

	if (typeof(scaleCeiling) !== "undefined" && scaleCeiling !== null)
		timeBarParameters.scaleCeiling = Number(scaleCeiling);
	
	if (typeof(rescaleDuration) !== "undefined" && rescaleDuration !== null)
		timeBarParameters.rescaleDuration = Number(rescaleDuration);
	
	thisMovie('timeslider').setTimeBar(timeBarParameters);
}

/**
 * This function returns the reference to the Flash object 
 */
function thisMovie(movieName) {

	var agent = navigator.userAgent.toLowerCase();
	
	if ( agent.indexOf( "msie" ) != -1 ) {
        return window[movieName];
    } else if ( agent.indexOf( "firefox" ) != -1 ) {
		// if integrating flash by calling AC_FL_RunContent, there will be 2 objects with id == flashId, the first one will be object, and the second - embed
		return document[movieName].item(1);
	} else if ( agent.indexOf( "safari" ) != -1 ) {
		// if integrating flash by calling AC_FL_RunContent, there will be 2 objects with id == flashId, the first one will be object, and the second - embed
		return document[movieName];
	}
}

/**
 * Getter for current timeBar values
 */
function timebar_get() {
   timeBarParams = thisMovie('timeslider').getTimeBarValues();
   //console.log("timeBarParams.startYear="+timeBarParams.startYear);
   //console.log("timeBarParams.endYear="+timeBarParams.endYear);
   return timeBarParams;

   timeBarParams.endYear; // -> timerange high value (right slider)
   timeBarParams.startYear; // -> timerange low value (left slider)
   timeBarParams.currentYear; //  -> map slider value
   timeBarParams.activeYear; // -> selected admin unit marker position ( years)
} 
