var OvertimeGuaranteed = new Class({
	Implements: [Events, Options],
	/**
	 * Default Options
	 * 
	 * defaultBehaviour: If disabled you may customize how overtime guaranteed works
	 * moveDuration: The transition duration for sliding the scoreboard up and down the page
	 * scoreboardOffset: A value that can be used to tune the position of overtime guaranteed next to events
	 * swfPath: Path to scoreboard flash file
	 * XML: The xml to use for the scoreboards default state
	 * default: if set a paramater is thrown on the xml url that will get a different xml for the default view
	 * startXML: The xml to display as soon as the scoreboard loads. If left null the default will display
	 */
	options: {
		defaultBehaviour: true,
		moveDuration: 500,
		scoreboardOffset: -100,
		swfPath3d: '/swf/play/sports/scoreboard3d.swf?random='+Math.random(),
		swfPath2d: '/swf/play/sports/scoreboard2d.swf',
		XML: '/vegas/play/sports/overtime.jsp',
		defaultType: null,
		startXML: null
	},
	initialize: function(options){
		this.setOptions(options);
		
		// grab the cable element and create effect to resize
		this.cable = $('scoreboard-cables');
		this.cable.startHeight = this.cable.offsetHeight;
		this.cable.fx = new Fx.Tween(this.cable,{
			property: 'height',
			duration: this.options.moveDuration
		});
		
		// use a cookie to track users fidelity setting
		var temp = Cookie.read('fidelity');
		 if(temp  == 'low' || temp == null){
			var flashPath = this.options.swfPath2d;
			this.fidelity = 'low';
		}
		else{
			var flashPath = this.options.swfPath3d;
			this.fidelity = 'high';
		}
							
		// inject flash here
		this.swiff = new Swiff(flashPath, {
			id: 'otgflash',
			container: $('scoreboard'),
			width: 340,
			height:660,
			callBacks: {
				saveFavorite: this.clickSaveFavorite.bind(this),
				onLoad: this.flashLoad.bind(this),
				setFidelity: this.setFidelity.bind(this)
			}
		});
		
		this.attach();
	},
	attach: function(){
		// attach click event to body and look for clicks from play louder elements
		if(this.options.defaultBehaviour){
			$(document.body).addEvent('click', function(e){
				var target = $(e.target);
				if(target.hasClass('overtime-guaranteed') && !target.getParent().hasClass('highlight')){
					// do not allow a click if busy
					if(!this.updateDisabled){
						this.updateDisabled = true;
						this.detachFromElement();
						this.attachToElement(target.getParent());
						this.position();
						var xmlUrl = target.getElement('a').get('href');
						
						//make sure flash has loaded..Moz/Webkit won't load flash until its container is on screen. If it's not loaded then we'll get an error when we try to update the xml.
						if (this.flashLoaded) 
						this.update(xmlUrl);
						else this.options.startXML=xmlUrl;
					}
				}
			}.bind(this));
		}
	},
	/**
	 * attachToElement - Attach to an element so that scoreboard can be positioned next to it
	 * 
	 * param {Element}: The element to attach to
	 */
	attachToElement: function(element){
		if($defined(this.attached))
			this.attached.removeClass('highlight');
		this.attached = element;
	},
	/**
	 * detachFromElement - Detach from an element
	 */
	detachFromElement: function(){
		if($defined(this.attached))
			this.attached.removeClass('highlight');
		this.attached = null;
	},
	position: function(){
		var coords = this.attached.getCoordinates(this.cable.getParent());
		this.cable.fx.start(coords.top + this.options.scoreboardOffset).chain(function(){
			this.attached.addClass('highlight');
			this.updateDisabled = false;
		}.bind(this));
	},
	/**
	 * reset - Detach from elements and move to starting position at top of page
	 */
	reset: function(){
		if(this.attached != null){
			this.detachFromElement();
			this.cable.fx.start(this.cable.startHeight);
			// call to flash to reset to default
			this.update(this.options.XML);
		}
	},
	/**
	 * update - Update the scoreboard
	 * 
	 * param {Number}: The id of the event to update to
	 */
	update: function(xmlUrl){
		// append the default paramater if set
		if(this.options.defaultType != null){
			// sometimes we get urls with params already attached figure out what to do here
			if(xmlUrl.contains('?'))
				xmlUrl += '&default=' + this.options.defaultType;
			else
				xmlUrl += '?default=' + this.options.defaultType;
		}
		//call to flash with required id to display		
		this.swiff.toElement().updateXML(xmlUrl);		
	},
	clickSaveFavorite: function(url){
		this.saveUrl = url;
		if(!isLoggedIn)
			modalHandler.getLogin(); // the user is not yet logged in show the pop up window
		else
			this.saveFavorite(); // the user has already logged in save the favorite
	},
	saveFavorite: function(url){
		new Request.HTML({
			onSuccess: function(){
				this.swiff.toElement().onSaveFavorite();
			}.bind(this),
			onFailure: function(){
			}
		}).get(this.saveUrl);
	},
	setFidelity: function(value){
		// save the users fidelity setting in a cookie and refresh the page
		if(this.fidelity != value){
			Cookie.write('fidelity', value, {
				duration: 365,
				path: '/'
			});
			// delay window refresh sometimes it seems to happen before the cookie is saved.
			(function(){
				window.location.reload();
			}).delay(500);			
		};
	},
	flashLoaded:false,
	flashLoad: function(){		
		if(this.options.startXML != null)
			this.update(this.options.startXML); // update to the defined starting xml
		else
			this.update(this.options.XML); // update to the default state
		
		//flag for if flash has loaded
		this.flashLoaded=true;
			
		// show the cables because the flash is ready
		this.cable.setStyle('visibility', 'visible');
	}
});