
/* run when page has loaded */
$(document).ready(function() {
	var offerRadioPrefix = "offer-choice",
		offerButtonPrefix = "offer-button";

	// toggle audio/video extra stuff
	$(".accordion .toggle").click(function() {
			if ($(this).next('.accordionContent').is(":hidden")) {
					$(this).addClass("expanded");
					$(this).parent().addClass("acc-expanded");
					$(this).next('.accordionContent').slideDown("fast");
			} else {
					$(this).removeClass("expanded");
					$(this).next('.accordionContent').slideUp("fast", function() {
							$(this).parent().removeClass("acc-expanded");
					});
			}
			return false;
	});

	// toggle catalog program sections
	$(".accordion .toggle a").click(function() {
			if ($(this).parent().parent().next().next().is(":hidden")) {
					$(".accordion .toggle a").removeClass("expanded");
					$(".accordion .toggle a").html("View Programs");
					$(this).html("Hide Programs");
					$(this).addClass("expanded");
					$(".accordion .accordionContent").slideUp("fast");
					$(this).parents(".toggle").nextAll(".accordionContent").slideDown("fast");
			} else {
					$(this).removeClass("expanded");
					$(this).html("View Programs");
					$(this).parents(".toggle").nextAll(".accordionContent").slideUp("fast");
			}
			return false;
	})

	// all catalog content marked with class "collapsed" will be closed
	$(".accordion .toggle a").each(function() {
		var collapse = false;

		// check for any sections that are marked to be collapsed
		$(this).parents(".accordion").find(".accordionContent").filter(".collapsed").each(function() {
			collapse = true;
		});

		// collapse
		if(collapse) $(this).click();
	});

	/* synchronize offer radio buttons with button "href"
	   - requires that radio button class begin with variable(offerRadioPrefix)
	   - requires that offer button class begins with variable(offerButtonPrefix) */
	$(":radio[name^='" + offerRadioPrefix + "']").click(function() {
		// get the specific identifier used to target the button that should be updated when this radio button is clicked
		var group = this.name.substring(offerRadioPrefix.length),
			re = /(.+)-\d+/,
			match = re.exec(group); // check for a numeric identifier that makes repeated sections unique
		
		// if we find a numeric identifier, then we should only preserve the non-numeric portion, 
		// so that we can match all instances of related buttons
		if(match) group = match[1];

		// ensure that all radio buttons with the same name are mirrored
		$("." + $(this).attr("class")).attr("checked", this.checked);

		// use the above identifier to only update the matching offer buttons on the page	
		$("a." + offerButtonPrefix + group).attr("href", this.value);
	});

	/* Open special links using popupWindow.js file */
	$("a.popup").first().each(function() {
		// dynamically load popupWindow script if we have an element that will use it
		$.getScript(
			"/cms/global/static/js/jquery.popupWindow.min.t8589170058924488439.js",
			function(){$("a.popup").popupWindow({centerScreen:1,windowName:"popup"});}
		);
	});

});

/*
* Writes the JWPlayer video player to the page using the optionally provided options parameter.
*/
function writeVideo(e,options) {
	var video = $(e).attr("video"),
		image = $(e).attr("image"),
		target_id = $(e).attr("target_id"),
		player = jwplayer(target_id),
		key_autostart = "autostart",
		key_backcolor = "backcolor",
		key_frontcolor = "frontcolor",
		key_width = "width",
		key_height = "height",
		playlist,
		currentVideo;

	// create default options object
	if(arguments[1] == undefined) options = {};

	// setting defaults
	options[key_autostart]	= (options[key_autostart] == undefined ? true : options[key_autostart]);
	options[key_backcolor]	= (options[key_backcolor] == undefined ? "000000" : options[key_backcolor]);
	options[key_frontcolor]	= (options[key_frontcolor] == undefined ? "ffffff" : options[key_frontcolor]);
	options[key_width]			= (options[key_width] == undefined ? 245 : options[key_width]);
	options[key_height]			= (options[key_height] == undefined ? 137 : options[key_height]);

	if(player) {
		// check to see if we have a player that has already been setup
		if(player.getState()) {
			playlist = player.getPlaylistItem();
			if(playlist) currentVideo = playlist.file;
		}

		// test if we are switching videos
		if(currentVideo != video) {
			// setup video player
			player.setup({
				autostart:options[key_autostart],
				backcolor:options[key_backcolor],
				flashplayer:"http://aws-cdn.hottopicmedia.com/global/static/media/jwplayer-5.8.swf",
				file:video,
				frontcolor:options[key_frontcolor],
				height:options[key_height],
				image:image,
				width:options[key_width]
			});
		}
		else {
			// just start the video, since we do not need to change it
			player.play(true);
		}

	}

	return false;
}
