/**
 * Utility Functions
 * Add/Remove a class name
 *
 */
function addClsName( el, clsName ) {
	var currentCls = el.className;
	el.className = currentCls + ' ' + clsName;
}
function removeClsName( el, clsName ) {
	var index = el.className.indexOf( clsName );	
	if ( index > -1 ) {
		el.className = el.className.substring( 0, index ) + ' ' + el.className.substring( index + clsName.length );
	}
}

/**
 * Function object extension
 * build a suitable event handler that forwards to the function
 */
Function.prototype.eventHandler = function( scope ) {
	var func = this;
	return function( event ) {
		func.call( scope, event );
	}
}

/**
 * Schedule Object
 * This will provide all the required functionality
 * for the 'schedule' object defined in HTML
 *
 */
var Schedule = function( contentId ) {

	this.contentEl = document.getElementById( contentId );
	this.programs = this.allPrograms();
	this.selectedProgram = null;

	// get an event handler that calls the desired class method
	var onClick = this.selectProgram.eventHandler( this );
		
	// assign the onClick handler to all programs
	for ( var i = 0; i < this.programs.length; i++ ) {
		this.programs[i].addEventListener( 'click', onClick, false );
	}		
	
};
Schedule.prototype = {

	// program guide data
	programData: [{
			title: 'Blue Crush',
			channel: '520 STARZ',
			airtime: 'Sun 8/17 1:05 pm - 3:00 pm',
			rating: 'Rated PG-13 (AC, AL, V)',
			description: 'Drama, Romance (2002) *****1/2 Kate Bosworth, Matthew Davis, Michelle Rodriguez (hottie!), Sanoe Lake, Nothing gets between Anne Marie and her board. Living in a beach shack with three roommates including her rebellious younger sister, she is up before dawn every morning to conquer the waves and count the days until the Pipe Masters surf competition. Having transplanted herself to Hawaii with no one\'s blessing but her own, Anne Marie finds all she needs in the adrenaline-charged surf scene ... until pro quarterback Matt Tollman comes along. Like it or not, Anne Marie starts losing her balance - and finding it - as she falls for Matt.'
		},{
			title: 'Super Mario Brothers',
			channel: '521 STARZF',
			airtime: 'Sun 8/17 12:30 pm - 2:15 pm',
			rating: 'Rated PG (V)',
			description: 'Action (1993) ***1/2 Bob Hoskins, John Leguizamo, Can you make a movie out of a video game? Thats the question that is answered by this film. Mario Mario and Luigi Mario, two hard working plumbers find themselves in an alternate universe where evolved dinosaurs live in medium hi-tech squalor. They find themselves the only hope to save the Earth from invasion.'
		},{
			title: 'The Musketeer',
			channel: '521 STARZF',
			airtime: 'Sun 8/17 2:15 pm - 4:00 pm',
			rating: 'Rated PG-13 (V)',
			description: 'Action (2001) **** Justin Chambers, Mena Suvari, In 17th century Paris, a dashing swordsman named D\'Artagnan finds himself at odds with the powerful forces taking over France. He sets out to avenge the murder of his parents and finds his country cleaved by chaos and civil unrest. His heart softens only for Francesca, a fiery peasant girl who claims D\'Artagnan\'s heart on sight.'
		},{
			title: 'Love Come Down',
			channel: '523 BLKSTRZ',
			airtime: 'Sun 8/17 12:30 pm - 2:10 pm',
			rating: 'Rated R (LSD)',
			description: 'Drama (2000) ***** Larenz Tate, Deborah Cox, Two brothers in their early 20s, one black, one white, each the other\'s keeper since their family was torn apart by a decade old tragedy. Neville is a comedian struggling with his comedy, and his brother, Matthew, is a boxer consumed with the pleasure and pain of his skin. Neville becomes enchanted with Niko, a beguiling young singer who becomes entangled with him on his journey of self-discovery. With the support of their long-time friend Julian and Sister Sarah, a nun with a past, Neville and Matthew come to understand love in all its forms.' 
		},{
			title: 'Undisputed',
			channel: '523 BLKSTRZ',
			airtime: 'Sun 8/17 2:10 pm - 3:45 pm',
			rating: 'Rated R (LV)',
			description: 'Action, Drama (2002) ***** Wesley Snipes, Ving Rhames, George "Ice Man" Chambers (Rhames) is a top ranked heavyweight boxer. However Chambers has his world turned upside down when he is accused of rape and sent to prison. Upon his arrival he hears talk about Monroe Hutchen (Snipes) who is the top ranked prison boxing champ 10 years running. Immediately there is bad blood with Chambers not wanting to be second to no one which leads to a lunch room fight between the men. Figuring it will be a good way to make money fellow convict Emmanuel \'Mendy\' Ripstein (Peter Falk) sets up a prison boxing match between the two men to decide who is the real UNDISPUTED champ. Michael Rooker plays a guard, Fisher Stevens, John Seda, and Master P co star.' 
		},{
			title: 'Kull the Conquerer',
			channel: '526 ENCORE',
			airtime: 'Sun 8/17 1:00 pm - 2:45 pm',
			rating: 'Rated PG-13 (AV)',
			description: 'Action (1997) **** Kevin Sorbo, Tia Carrere, A barbarian named Kull unexpectedly becomes a king after an old king (whom Kull has just killed in a battle) gives his crown to him. But direct heirs of a killed king, trying to topple Kull and regain the throne, bring an old witch-queen Akivasha back to life. Their plan backfires, however, as Akivasha is going to allow their lords - demons - to rule the kingdom. The only thing that can stop her now is a breath of the god Volka.' 
		},{
			title: 'Rambo: First Blood',
			channel: '526 ENCORE',
			airtime: 'Sun 8/17 2:45 pm - 4:30 pm',
			rating: 'Rated R (VL)',
			description: 'Action (1982) ******* Sylvester Stallone, Richard Crenna, John Rambo is a disoriented Vietnam Vet. He is hitchhiking from town to town to see friends from the war. A sheriff tries to make him leave town and when he refuses, arrests him for vagrancy. While in jail, a deputy takes delight in abusing him. Rambo escapes showing his old Vietnam fighting skills and takes to the woods as the sheriff and deputies try and find him in his element. Things get out of hand as Colonel Trautman Rambo\'s old commander appears to shed light on the situation.'
		},{
			title: 'Cinenews',
			channel: '527 ENCOREF',
			airtime: 'Sun 8/17 1:40 pm - 2:10 pm',
			rating: 'No Rating',
			description: 'Series (2003) David Doerre, Need you News? Now ya GOT IT!'
		},{
			title: '102 Dalmations',
			channel: '527 ENCOREF',
			airtime: 'Sun 8/17 2:10 pm - 4:00 pm',
			rating: 'Rated G',
			description: 'Family (2000) **** Glenn Close, After a spot of therapy Cruella De Vil is released from prison a changed woman. Devoted to dogs and good causes, she is delighted that Chloe, her parole officer, has a dalmatian family and connections with a dog charity. But the sound of Big Ben can reverse the treatment so it is only a matter of time before Ms De Vil is back to her incredibly ghastly ways, using her new-found connections with Chloe and friends.'
		},{
			title: 'Serendipity',
			channel: '528 LOVE',
			airtime: 'Sun 8/17 1:00 pm - 2:35 pm',
			rating: 'Rated PG',
			description: 'Romance (2001) ******1/2 John Cusack, Kate Beckinsale, Jonathan Trager and Sara Thomas met while shopping for gloves in New York. Though buying for their respective lovers, the magic was right and a night of Christmas shopping turned into romance. Jon wanted to explore things further but Sara wasn\'t sure their love was meant to be. They decided to test fate by splitting up and seeing if destiny brought them back together... Many years later, having lost each other that night, both are engaged to be married. Still, neither can shake the need to give fate one last chance to reunite them. Jon enlists the help of his best man to track down the girl he can\'t forget starting at the store where they met. Sara asks her new age musician fiance for a break before the wedding and, with her best friend in tow, flies from California to New York hoping destiny will bring ...'
		},{
			title: 'Return to the Blue Lagoon',
			channel: '528 LOVE',
			airtime: 'Sun 8/17 2:35 pm - 4:30 pm',
			rating: 'Rated PG-13',
			description: 'Romance (1991) **** Milla Jovovich, Brian Krause, Although the title rather suggests a sequel to The Blue Lagoon, this is rather a variation on the same theme- neither Christopher Atkins nor Brooke Shields returns - Emmeline is dead, but a new generation is shipwrecked again on a desert island. This time the two kids, Richard and Lilly, have no experience in the civilizes world at all, and have to discover everything on their own, but loves proves an indomitable instinct, even given a \'choice\' of one inhabitant per gender.'
		},{
			title: 'The Hard Man',
			channel: '529 WESTERN',
			airtime: 'Sun 8/17 1:15 pm - 2:35 pm',
			rating: 'Rated PG',
			description: 'Western (1957) ***** Guy Madison, Valerie French, Steve Burden is a former lawman released for bringing in too many wanted men dead. The aging Sheriff of El Solito wants Steve for his Deputy and Steve takes the job knowing the last outlaw he killed was framed in El Solito. Looking for the man that framed him it appears that Rice Martin who controls El Solito is his man and Steve is quickly in trouble when Martin sends a man to kill him.'
		},{
			title: 'Images of Indians: How Hollywood ...',
			channel: '529 WESTERN',
			airtime: 'Sun 8/17 2:35 pm - 3:00 pm',
			rating: 'Rated PG',
			description: 'Western, Documentary (2003) ****** Nicholas Schatzki, Hollywood\'s depiction of Native Americans in Western films is traced through interviews and archive footage.'
		},{
			title: 'Amateur',
			channel: '530 MYSTERY',
			airtime: 'Sun 8/17 1:30 pm - 3:00 pm',
			rating: 'Rated R (LVN)',
			description: 'Mystery (1994) ******* Isabelle Huppert, Martin Donovan, Isabelle is an ex-nun waiting for her special mission from God. In the meantime, she is making a living writing pornography. She meets Thomas, a sweet, confused amnesiac who cannot remember that he used to be a vicious pornographer, responsible for turning his young wife, Sofia, into the world\'s most notorious porn queen. Sofia\'s on the run, convinced she\'s killed him. Together, Isabelle and Thomas set out to discover his past, a past waiting to catch up with him.'
	}],
		
	// return an Array of all program cells
	allPrograms: function() {
		var guide = document.getElementById( 'guide' );
		var programs = Array();
		for ( var i = 0; i < guide.rows.length; i++ ) {
			var row = guide.rows.item(i);
			for ( var j = 0; j < row.cells.length; j++ ) {
				if ( row.cells.item(j).className == 'program' ) {
					programs.push( row.cells.item(j) );
				}
			}		
		}
		return programs;
	},
		
	// the mouse click handler, higlight the chosen program and display it's data
	selectProgram: function( event ) {
	
		var program = event.target;
		
		if ( this.selectedProgram != null ) {
			removeClsName( this.selectedProgram, 'selected-program' );
		}
						
		var data = this.programData[ program.id ];
		
		document.getElementById( 'program-title' ).innerHTML = data.title;
		document.getElementById( 'program-channel' ).innerHTML = data.channel;
		document.getElementById( 'program-airtime' ).innerHTML = data.airtime;
		document.getElementById( 'program-rating' ).innerHTML = data.rating;
		document.getElementById( 'program-description' ).innerHTML = data.description;
		
		addClsName( program, 'selected-program' );				
		this.selectedProgram = program;
		
	}
	
};

