/*--Lightbox--*/

// Assign Lightbox functionality
function lightbox(content) {
	// Append divs to body
	$j('body').append('<div id="lightbox-background" style="filter:alpha(opacity=0)"></div>');
    $j('body').append('<div id="lightbox-window" style="filter:alpha(opacity=0)"><span class="title"><span class="close">Close X</span></span></div>');
	
	// Get content, dynamically resize window
	$j.getJSON('http://www.nesn.com/ajax/' + content + '.js', function(data) {
		if(data['img']) {
			$j('<img/>').attr('src', data['img']).one('load', function() {
				var imgW = this.width;
				var imgH = this.height;
				$j('#lightbox-window').css({
					'width' : imgW, 
					'height' : imgH + 20, 
					'margin-top' : -((imgH/2) + 14),
					'margin-right': -(imgW/2),
					'margin-bottom' : -(imgH/2),
					'margin-left': -(imgW/2)
				});
				$j('.title').css('width', (imgW-6));
			})
			.each(function() {
			   if(this.complete) {
				   $j(this).trigger('load');
			   }
			});	
			$j('#lightbox-window').append('<img src="' + data['img'] + '" />');
		} else {
			$j('#lightbox-window').append(data['vid']);
		}
	});
	
	// Fade in animations
    $j('#lightbox-background').animate({opacity: 0.5}, 500);
    setTimeout("$j('#lightbox-window').animate({opacity: 1.0}, 500)", 750);
	
	// Removal
    $j('.close').click(function() {
        $j('#lightbox-background').remove();
        $j('#lightbox-window').remove();
    });
    $j('#lightbox-background').click(function() {
        $j('#lightbox-background').remove();
        $j('#lightbox-window').remove();
    });
}

//Preload Ultimate Stadium Guide Images
$j(document).ready(function() {
	if($j('.ultimate-stadium-guide')) {
		(new Image()).src = 'http://cdn.nesn.com/img/ultimate-stadium-guide/green-monster.jpg';
		(new Image()).src = 'http://cdn.nesn.com/img/ultimate-stadium-guide/peskys-pole.jpg';
		(new Image()).src = 'http://cdn.nesn.com/img/ultimate-stadium-guide/right-field-grandstand2.jpg';
		(new Image()).src = 'http://cdn.nesn.com/img/ultimate-stadium-guide/right-field-grandstand1.jpg';
		(new Image()).src = 'http://cdn.nesn.com/img/ultimate-stadium-guide/field-box-first-base-line.jpg';
		(new Image()).src = 'http://cdn.nesn.com/img/ultimate-stadium-guide/yawkey-way.jpg';
		(new Image()).src = 'http://cdn.nesn.com/img/ultimate-stadium-guide/infield-standing-room.jpg';
		(new Image()).src = 'http://cdn.nesn.com/img/ultimate-stadium-guide/infield-grandstand-first-base.jpg';
		(new Image()).src = 'http://cdn.nesn.com/img/ultimate-stadium-guide/infield-box-seats.jpg';
		(new Image()).src = 'http://cdn.nesn.com/img/ultimate-stadium-guide/infield-grandstand-third-base.jpg';
		(new Image()).src = 'http://cdn.nesn.com/img/ultimate-stadium-guide/left-field-pavilion.jpg';
		(new Image()).src = 'http://cdn.nesn.com/img/ultimate-stadium-guide/the-300s.jpg';
		(new Image()).src = 'http://cdn.nesn.com/img/ultimate-stadium-guide/lower-300s.jpg';
		(new Image()).src = 'http://cdn.nesn.com/img/ultimate-stadium-guide/sideline-view.jpg';
	}
});

// Hijack Links
$j(document).ready(function() {
	if($j('.lightbox')[0]) {
		$j('.lightbox').click(function() {
			var content = $j(this).attr('id');
			lightbox(content);
			return false;
		});
	}
});

/*Vanity Show Page Videos*/

$j(document).ready(function() {
	if($j('#bruins-classics-video')[0]) {showThumbs('http://www.nesn.com/bruins-classics-video.xml');}					
	if($j('#bruins-faceoff-live-video')[0]) {showThumbs('http://www.nesn.com/bruins-faceoff-live-video.xml');}
	if($j('#bruins-overtime-live-video')[0]) {showThumbs('http://www.nesn.com/bruins-overtime-live-video.xml');}
	if($j('#charlie-moore-outdoors-video')[0]) {showThumbs('http://www.nesn.com/charlie-moore-outdoors-video.xml');}
	if($j('#friday-night-fenway-video')[0]) {showThumbs('http://www.nesn.com/friday-night-fenway-video.xml');}
	if($j('#nesn-daily-video')[0]) {showThumbs('http://www.nesn.com/nesn-daily-video.xml');}
	if($j('#nesns-comedy-all-stars-video')[0]) {showThumbs('http://www.nesn.com/nesns-comedy-all-stars-video.xml');}
	if($j('#pocket-money-video')[0]) {showThumbs('http://www.nesn.com/pocket-money-video.xml');}
	if($j('#red-sox-classics-video')[0]) {showThumbs('http://www.nesn.com/red-sox-classics-video.xml');}
	if($j('#red-sox-extra-innings-video')[0]) {showThumbs('http://www.nesn.com/red-sox-extra-innings-video.xml');}
	if($j('#red-sox-final-video')[0]) {showThumbs('http://www.nesn.com/red-sox-final-video.xml');}
	if($j('#red-sox-gameday-live-video')[0]) {showThumbs('http://www.nesn.com/red-sox-gameday-live-video.xml');}
	if($j('#red-sox-hot-stove-video')[0]) {showThumbs('http://www.nesn.com/red-sox-hot-stove-video.xml');}
	if($j('#red-sox-monster-monday-video')[0]) {showThumbs('http://www.nesn.com/red-sox-monster-monday-video.xml');}
	if($j('#sox-in-2-video')[0]) {showThumbs('http://www.nesn.com/sox-in-2-video.xml');}
	if($j('#the-big-bad-bruins-show-video')[0]) {showThumbs('http://www.nesn.com/the-big-bad-bruins-show-video.xml');}
	if($j('#the-bricks-video')[0]) {showThumbs('http://www.nesn.com/the-bricks-video.xml');}
	if($j('#the-instigators-video')[0]) {showThumbs('http://www.nesn.com/the-instigators-video.xml');}
	if($j('#the-remy-awards-video')[0]) {showThumbs('http://www.nesn.com/the-remy-awards-video.xml');}
	if($j('#ultimate-red-sox-show-video')[0]) {showThumbs('http://www.nesn.com/ultimate-red-sox-show-video.xml');}
});

/*Vanity Writer Page Articles*/

$j(document).ready(function() {
	if($j('#cole-wright-articles')[0]) {showThumbs('http://www.nesn.com/cole-wright.xml');}
	if($j('#daily-blend-articles')[0]) {showThumbs('http://www.nesn.com/daily-blend.xml');}
	if($j('#don-orsillo-articles')[0]) {showThumbs('http://www.nesn.com/don-orsillo.xml');}
	if($j('#douglas-flynn-articles')[0]) {showThumbs('http://www.nesn.com/douglas-flynn.xml');}
	if($j('#evans-clinchy-articles')[0]) {showThumbs('http://www.nesn.com/evans-clinchy.xml');}
	if($j('#heidi-watney-articles')[0]) {showThumbs('http://www.nesn.com/heidi-watney.xml');}
	if($j('#jack-edwards-articles')[0]) {showThumbs('http://www.nesn.com/jack-edwards.xml');}
	if($j('#jade-mccarthy-articles')[0]) {showThumbs('http://www.nesn.com/jade-mccarthy.xml');}
	if($j('#jeff-howe-articles')[0]) {showThumbs('http://www.nesn.com/jeff-howe.xml');}
	if($j('#jerry-remy-articles')[0]) {showThumbs('http://www.nesn.com/jerry-remy.xml');}
	if($j('#jim-rice-articles')[0]) {showThumbs('http://www.nesn.com/jim-rice.xml');}
	if($j('#john-chandler-articles')[0]) {showThumbs('http://www.nesn.com/john-chandler.xml');}
	if($j('#kathryn-tappen-articles')[0]) {showThumbs('http://www.nesn.com/kathryn-tappen.xml');}
	if($j('#naoko-funayama-articles')[0]) {showThumbs('http://www.nesn.com/naoko-funayama.xml');}
	if($j('#terry-francona-articles')[0]) {showThumbs('http://www.nesn.com/terry-francona.xml');}
	if($j('#tom-caron-articles')[0]) {showThumbs('http://www.nesn.com/tom-caron.xml');}
	if($j('#tony-lee-articles')[0]) {showThumbs('http://www.nesn.com/tony-lee.xml');}
});

/*Vanity Show Pages Video Thumbnails*/

function showThumbs(feed) {
	$j.ajax({
		type: 'GET',
		url: feed,
		dataType: 'xml',
		success: function(data) {
			$j(data).find('item').each(function() {
				var title = $j(this).find('title').text();
				var url = $j(this).find('link').text();
				var date = $j(this).find('pubDate').text();
				var day = (date.substr(5,2) * 1);
				var month = date.substr(8,3);
				var time = date.substr(17,5);
				time = showHourConvert(time);
				var image = $j(this).find('enclosure').attr('url');
				$j('#video-center ul').append('<li><a href="' + url + '" class="video-hover"><img src="' + image + '" alt="' + escapeChar(title) + '" /></a><br /><a href="' + url + '"><em>' + month + ' ' + day + ' | ' + time + '</em></a><br /><a href="' + url + '">' + escapeChar(title) + '&nbsp;&raquo;</a></li>');
			});
		}
	});
}

/*Vanity Show Page Tabs*/

$j(document).ready(function() {
	if($j('#tabs')[0]) {
		$j('#tabs').tabs({
			select: function(event, ui) {
				var url = $j.data(ui.tab, 'load.tabs');
				if(url) {
					location.href = 'http://www.nesn.com/tv-schedule-' + tvScheduleLink() + '.html';
					return false;
				}
				return true;
			}
		});
	}
});
