/** 
 * Lifetype Timeline Widget
 * 
 * Copyright by Mark Wu (http://blog.markplace.net)
 */


Lifetype.UI.Timeline = function()
{
	this.id = "timeline";
}

Lifetype.UI.Timeline.prototype =
{
	timeline: null,
	currentDate: null,
	centerDate: null,
	eventSource: null,
	eventSourceManager: new Object()
}

Lifetype.UI.Timeline.prototype.init = function( timelineDivId ) {
	this.timelineDivId = timelineDivId;
	this.timelineDivObj = document.getElementById( timelineDivId );

	this.eventSource = new Timeline.DefaultEventSource();
	this.eventSourceManager['197001'] = 1;

    theme = Timeline.ClassicTheme.create();
    theme.event.bubble.width = 400;

	var myEventSource = this.eventSource;
	bandInfos = [
		Timeline.createBandInfo({
			eventSource:    myEventSource,
			width:          "85%", 
			intervalUnit:   Timeline.DateTime.WEEK, 
			intervalPixels: 100,
			theme:          theme
	    }),
	    Timeline.createBandInfo({
			eventSource:    myEventSource,
			width:          "15%", 
			intervalUnit:   Timeline.DateTime.MONTH, 
			intervalPixels: 200,
			overview:       true,
			theme:          theme
		})
	];
  
	bandInfos[1].syncWith = 0;
	bandInfos[1].highlight = true;
  
	this.timeline = Timeline.create( this.timelineDivObj, bandInfos );
            
	band = this.timeline.getBand(1);
	band.timeline = this;
	this.currentDate = band.getCenterVisibleDate();

	band.addOnScrollListener( function( band ) {
		me = band.timeline;
		me.centerDate = band.getCenterVisibleDate();
	});
  
	var now = new Date();
	dateString = Lifetype.UI.Timeline.toYearMonth(now);
	this.eventSourceManager[dateString] = 1;
	this.timeline.loadXML( timelineBaseUrl+"/index.php?op=getArticlesXML&Date="+dateString, function(xml, url) { myEventSource.loadXML(xml, url); } );

    YAHOO.util.Event.addListener( this.timelineDivId, 'mouseup', this.mouseUpEventHandler , this, true );
}

Lifetype.UI.Timeline.prototype.mouseUpEventHandler = function(e, obj) {
	if ( Math.abs( this.currentDate - this.centerDate ) > 86400*25*1000 ) {
		this.currentDate = this.centerDate;
		dateString = Lifetype.UI.Timeline.toYearMonth(new Date(this.currentDate));
		if( this.eventSourceManager[dateString] != 1 ) {
			this.eventSourceManager[dateString] = 1;
			var myEventSource = this.eventSource;
			this.timeline.loadXML( timelineBaseUrl+"/index.php?op=getArticlesXML&Date="+dateString, function(xml, url) { myEventSource.loadXML(xml, url); });
		}
	}
}
  
Lifetype.UI.Timeline.toYearMonth = function ( dateObj ) {
	year = dateObj.getFullYear();
	month = new String(dateObj.getMonth() + 101).substr(1, 2);

	return year+month;
}

function loadJavascript( scriptPath ){
	var script = document.createElement( 'script' );
	script.type = 'text/javascript';
	script.src = timelineBaseUrl+scriptPath;
	document.getElementsByTagName('head')[0].appendChild(script); 
} 

var tl = null;
var bd = null;
var bu = null;

YAHOO.util.Event.addListener( window, "load", function() {
	// Dynamic load Timeline
	loadJavascript('/plugins/timeline/js/timeline/timeline-api.js');
	// Initialize Timeline
	tl = new Lifetype.UI.Timeline();
	bd = new YAHOO.widget.Effects.BlindDown('blind', {delay: true, ghost: true});
	bu = new YAHOO.widget.Effects.BlindUp('blind', {delay: true, ghost: true});
	bd.onEffectComplete.subscribe( function() { 
		if( tl.timeline == null) {
			tl.init('my_timeline');
		}
		YAHOO.util.Dom.get('timeline_button').innerHTML = "Close Timeline";
	});
	bu.onEffectComplete.subscribe( function() { 
		YAHOO.util.Dom.get('timeline_button').innerHTML = "Watch Timeline";
	});
	YAHOO.util.Event.addListener( 'timeline_button', "click", function() {
		if( YAHOO.util.Dom.getStyle('blind','display') == 'none' ) {
			bd.animate();
		} else {
			bu.animate();
		}
	} );
} );
