/* SHADOWBOX INIT */
Shadowbox.init({
	language: "en",
	skipSetup: false,
	players: ["flv", "img", "swf", "qt", "wmp","iframe"]
});

var hpSelNewsItem;
var hpSelSpotItem;

$(function() {
	var pop = function() {
		$('#offsitelink').attr('href') = this.attr('href');
		$('#screen').css({ 'display': 'block', opacity: 0.7, 'width': $(document).width(), 'height': $(document).height() });
		$('#popupbox').css({ 'display': 'block' });
	}
	var unpop = function() {
		$('#popupbox').css({ 'display': 'none' });
		$('#screen').css({ 'display': 'none' });
	}

	$('#offsitelink').click(unpop);
	$('#backButton').click(unpop);
	$('a[offsite][offsite=1]').click(function(event) {
		event.preventDefault();
		var linkhref = $(this).attr('href');
		$('#offsitelink').attr('href', linkhref);
		$('#offsitelink').text(linkhref);
		var target = $(this).attr('target');
		if (target == "_blank") {
			$('#offsitelink').attr('target', target);
		}
		$('#screen').css({ 'display': 'block', opacity: 0.7, 'width': $(document).width(), 'height': $(document).height() });
		$('#popupbox').css({ 'display': 'block' });
	});

	/* SEARCH BOX */
	$('#hdRightShowSearch').click(function(event) {
		event.preventDefault();
		$('#hdRightSearchBox').css("display", "inline");
		$('#ctl00_cmssearchbox_txtWord').focus();
	});

	/* NEWS / SPOTLIGHT TOGGLING */
	var limitText = function(value, arg) {
		var value_arr = value.split(' ');
		if (arg < value_arr.length) {
			value = value_arr.slice(0, arg).join(' ');
		}
		return value;
	}

	var toggleNewsSpot = function(item) {
		var hpNewsSpotItemTxt = $(item).next('.hpNewsSpotItemTxt');
		var hpNewsSpotBtn = $(item).children('.hpNewsSpotBtn');
		var hpNewsSpotItemDesc = hpNewsSpotItemTxt.children('.hpNewsSpotItemDesc');

		hpNewsSpotItemTxt.toggleClass('invisible');
		hpNewsSpotBtn.toggleClass('hpNewsSpotBtnSel');

		// HANDLE TEASER IMAGE
		var img = $(item).parent().parent().children('.hpNewsSpotTeaserImg');
		img.attr('src', $(item).children('.hpNewsSpotImg').attr('value'));
		if ($(item).children('.hpNewsSpotImg').attr('value') != '') {
			$(item).parent().addClass('hpLowerMiddleTextNarrow');
			hpNewsSpotItemDesc.html(limitText(hpNewsSpotItemDesc.html(), 6));
			img.removeClass('invisible');
			$(item).parent().find('.hpNewsSpotTitle').each(function(i) {
				$(this).addClass('hpNewsSpotTitleNarrow');
			});
		} else {
			hpNewsSpotItemDesc.html(limitText(hpNewsSpotItemDesc.html(), 22));
			$(item).parent().removeClass('hpLowerMiddleTextNarrow');
			$(item).parent().find('.hpNewsSpotTitle').each(function(i) {
				$(this).removeClass('hpNewsSpotTitleNarrow');
			});
			img.addClass('invisible');
		}
	}

	hpSelNewsItem = $('#hpNewsItems').find('.hpNewsSpotItemHd')[0];
	hpSelSpotItem = $('#hpSpotItems').find('.hpNewsSpotItemHd')[0];
//	if (hpSelNewsItem != null || hpSelSpotItem != null) {
//		$('#main').css('backgroundColor', '#002f43');
//	}
	if (hpSelNewsItem != null) { toggleNewsSpot(hpSelNewsItem); }
	if (hpSelSpotItem != null) { toggleNewsSpot(hpSelSpotItem); }

	$('.hpNewsSpotItemHd').click(function() {
		var imgDiv = null;
		// NEWS OR SPOTLIGHT?
		if ($(this).parent().parent().attr('id') == 'hpNewsItems') { // NEWS
			if (hpSelNewsItem == this) { return; }
			toggleNewsSpot(hpSelNewsItem);
			hpSelNewsItem = this;
		} else { // SPOTLIGHT
			if (hpSelSpotItem == this) { return; }
			toggleNewsSpot(hpSelSpotItem);
			hpSelSpotItem = this;
		}

		// SHOW THE NEW ONE
		toggleNewsSpot(this);
	});
	/* END NEWS / SPOTLIGHT TOGGLING */
});

// THIS IS USED BY ASP.NET AJAX ON THE HOMEPAGE WITH THE UPDATE PANEL
function pageLoad(sender, args) {
	if (args.get_isPartialLoad()) {
		$('#ctl00_plcMain_homepageImageLink').click(function(event) {
			if ($('#ctl00_plcMain_homepageImageLink').attr('offsite') == "1") {
				event.preventDefault();
				var linkhref = $('#ctl00_plcMain_homepageImageLink').attr('href');
				$('#offsitelink').attr('href', linkhref);
				$('#offsitelink').text(linkhref);
				var target = $('#ctl00_plcMain_homepageImageLink').attr('target');
				if (target == "_blank") {
					$('#offsitelink').attr('target', target);
				}
				$('#screen').css({ 'display': 'block', opacity: 0.7, 'width': $(document).width(), 'height': $(document).height() });
				$('#popupbox').css({ 'display': 'block' });
			}
		});
	}
}

// USED FOR SHORTENING TEXT IN NEWS AND SPOTLIGHT
// NOT CURRENTLY BEING USED -- NEED TO OPTIMIZE