// <Class definitions>
var PushEvent = new Class({
	initialize: function(id) {
		this.attributes=new Hash();
		this.id=id;
	},
	
	
	set: function(attributeName, value) {
		this.attributes.set(attributeName,value);
	},
	
	
	add: function(attributeName, value) {
		var oldValue = this.attributes.get(attributeName);
		if ( $defined(oldValue) ) {
			var valueList;
			if ( $type(oldValue) == 'array' ) {
				valueList = oldValue;
			}
			else {
				valueList = new Array();
				valueList.push(oldValue);
				this.attributes.set(attributeName,valueList);
			}
			valueList.push(value);
		}
		else {
			this.attributes.set(attributeName,value);
		}
	},
	
	
	get: function(attributeName) {
		return this.attributes.get(attributeName);
	},
	
	
	getId: function() {
		return this.id;
	}
});
// </Class definitions>


// <Default Event Functions>
function replaceLiveValues(str, contentId, actualClipIndex, categoryId) {
	var result = str;
	if ( $defined(result) ) {
		var result = result.replace(/\[CONTENT\]/g, contentId);
		result = result.replace(/\[CLIP\]/g, actualClipIndex);
		result = result.replace(/\[CATEGORY\]/g, categoryId);
	}
	return result;
}

function pushUrlFrame(event, contentId, actualClipIndex, categoryId) {
	var url = replaceLiveValues(event.get('url'), contentId, actualClipIndex);
	var frame = event.get('frame');
	if ( url && frame ) {
		system.setFrameUrl(frame,url);
    }
}

function pushUrlTab(event, contentId, actualClipIndex, categoryId) {
	var url = replaceLiveValues(event.get('url'), contentId, actualClipIndex);
	var tab = event.get('tab');
	if ( !$defined(tab) ) {
		tab = event.get('frame');
	}
	if ( $defined(url) && $defined(tab) ) {
		system.setFrameUrl(tab,url);
    }
}

function pushUrlSwitchTab(event, contentId, actualClipIndex, categoryId) {
	var url = replaceLiveValues(event.get('url'), contentId, actualClipIndex);
	var tab = event.get('tab');
	if ( !$defined(tab) ) {
		tab = event.get('frame');
	}
	if ( $defined(url) && $defined(tab) ) {
		system.setFrameUrl(tab,url);
		if ( !system.freeNavigation ) {
			system.switchTab(tab);
		}
    }
}

function pushUrlForceSwitchTab(event, contentId, actualClipIndex, categoryId) {
	var url = replaceLiveValues(event.get('url'), contentId, actualClipIndex);
	var tab = event.get('tab');
	if ( !$defined(tab) ) {
		tab = event.get('frame');
	}
	if ( $defined(url) && $defined(tab) ) {
		system.setFrameUrl(tab,url);
		system.switchTab(tab);
    }
}

function pushUrlForceSwitchOtherTab(event, contentId, actualClipIndex, categoryId) {
	var url = replaceLiveValues(event.get('url'), contentId, actualClipIndex);
	var changeTab = event.get('changeTab');
	var switchTab = event.get('switchTab');
	if ( !$defined(changeTab) ) {
		changeTab = event.get('frame');
	}
	if ( $defined(url) && $defined(changeTab) && $defined(switchTab) ) {
		system.switchTab(switchTab);
		system.setFrameUrl(changeTab,url);
    }
}

function switchTab(event, contentId, actualClipIndex, categoryId) {
	var tab = event.get('tab');
	if ( $defined(tab) ) {
		if ( !system.freeNavigation ) {
			system.switchTab(tab);
		}
    }
}

function forceSwitchTab(event, contentId, actualClipIndex, categoryId) {
	var tab = event.get('tab');
	if ( $defined(tab) ) {
		system.switchTab(tab);
    }
}

function pushTitle(event, contentId, actualClipIndex, categoryId) {
	if ( system.currentContentId != contentId ) {
		system.reloadFrameUrl('title',['content'],[contentId]);
	}
}

function pushDownloadLink(event, contentId, actualClipIndex, categoryId) {
	if ( system.currentContentId != contentId ) {
		system.reloadFrameUrl('download',['content'],[contentId]);
	}
}

function pushImage(event, contentId, actualClipIndex, categoryId) {
	var div = event.get('div');
	var href = replaceLiveValues(event.get('href'), contentId, actualClipIndex);
	var target = event.get('target');
	var src = replaceLiveValues(event.get('src'), contentId, actualClipIndex);
	var width = event.get('width');
	var height = event.get('height');
	var alt = event.get('alt');
	var title = event.get('title');
	var border = event.get('border');
	
	var divObj = $(div);
	if ( $defined(src) && $defined(divObj) ) {
		var imageHtml = "";
		if ( $defined(href) ) {
			imageHtml += '<a href="' + href + '"';
			if ( $defined(target) ) {
			 	imageHtml += ' target="' + target + '">';
			}
		}
		imageHtml += '<img src="' + src + '"';
		if ( $defined(width) ) {
		 	imageHtml += ' width="' + width + '"';
		}
		if ( $defined(height) ) {
		 	imageHtml += ' height="' + height + '"';
		}
		if ( $defined(alt) ) {
		 	imageHtml += ' alt="' + alt + '"';
		}
		if ( $defined(title) ) {
		 	imageHtml += ' title="' + title + '"';
		}
		if ( $defined(border) ) {
		 	imageHtml += ' border="' + border + '"';
		}
		imageHtml += ' />';
		if (href) {
			imageHtml += '</a>';
		}
		$(divObj).setHTML(imageHtml);
	}
}

function jumpToPosition(event, contentId, actualClipIndex, categoryId) {
	var currentTime = system.player.getPosition()
	var startTime = event.get('startTime');
	var lastTime = event.get('endTime');
	var targetDiv = event.get('targetDiv');
	var targetTextDiv = event.get('targetTextDiv');
	var waitingInMS = event.get('waitingInMS');
	if ( (currentTime >= startTime) && (currentTime + waitingInMS <=lastTime) ){
		system.player.pause()
		var descriptionText = event.get('breakDescription');
		var skipLinkText = event.get('breakSkipLinkText');
		var pauseDiv = $(targetDiv);
		if ( $defined(pauseDiv) ) {
			pauseDiv.setStyles('visibility:visible; display:block;');
			var pauseDivText = $(targetTextDiv);
			if ( $defined(pauseDivText) ) {
				pauseDivText.innerHTML = descriptionText+'<br /><a href=\"javascript:void(0)\" onclick=\"skipBreak('+ lastTime +', \''+ targetDiv +'\')\">'+skipLinkText+'</a>';
				setTimeout('skipBreak('+ lastTime +', "'+ targetDiv +'")', waitingInMS);
			}
		}
	}
}

function skipBreak(endTime, targetDiv){
	system.player.setPosition(endTime);
	$(targetDiv).setStyles('visibility:hidden; display:none;');
	system.player.play();
}


function pushListTab(event, contentId, actualClipIndex, categoryId) {
	var pushTab = event.get('tab');
	var eventId = event.getId();
	
	if ( $defined(pushTab) && system.currentClipIndex != actualClipIndex ) {
		var pushUrls = event.get('url');
		if ( !$defined(system.currentPushUrlIndex.get(eventId)) ) {
			system.currentPushUrlIndex.set(eventId, 0);
		}
		
		var pushIndex = system.currentPushUrlIndex.get(eventId); 
		if ( $type(pushUrls) == 'array' && pushUrls.length > pushIndex ) {
			var pushUrl = pushUrls[pushIndex];
			if ( pushUrl ) {
				system.setFrameUrl(pushTab, pushUrl);
				system.switchTab(pushTab);
			}
			system.currentPushUrlIndex.set(eventId, (pushIndex >= pushUrls.length-1 ? 0 : pushIndex+1) );
		}
	}
}


// Make Default Events known to system
if ( $defined(system) ) {
	system.addEventHandler('pushTitle',self);
	system.addEventHandler('pushUrlTab',self);
	system.addEventHandler('pushUrlFrame',self);
	system.addEventHandler('pushUrlSwitchTab',self);
	system.addEventHandler('pushUrlForceSwitchTab',self);
	system.addEventHandler('pushUrlForceSwitchOtherTab',self);
	system.addEventHandler('pushImage',self);
	system.addEventHandler('pushListTab',self);
	system.addEventHandler('jumpToPosition',self);
	system.addEventHandler('switchTab',self);
	system.addEventHandler('forceSwitchTab',self);
	system.addEventHandler('pushDownloadLink',self);
//	system.addEventHandler('pushWebcastText',self);
//	system.addEventHandler('pushWebcastSlide',self);
	
	// make class accessible via system
	system.PushEvent = PushEvent;
}

