$(document).ready(function() {

	// -------------------------------------------------------------
	// Accordian Setup
	// -------------------------------------------------------------
	$(".accordian").accordion({
		autoHeight: false,
		header: 'h3'
	});

	$(".benefits .accordian .topLevel").accordion({
		autoHeight: false,
		header: 'h4'
	});

	$(".benefits .accordian .topLevel h4").each(function() { $(this).onMouseUp = $(this).blur(); });

	$('div.topLevel').bind('accordionchangestart', function(event, ui) {
		ui.newHeader.parent().toggleClass("currentOpen");
		ui.oldHeader.parent().toggleClass("currentOpen");
		$(".benefits .accordian .topLevel h4").css("width", "auto");
		setTimeout(function() {
			$(".benefits .accordian .topLevel h4").css("width", "100%");
			$(".benefits .accordian .topLevel .currentOpen h4").css("width", "50%");
		}, 50);
	});

	$('.accordian').bind('accordionchange', function(event, ui) {
		setTimeout(function() {
			ui.newHeader.next().find("div.benefitItem.currentOpen").removeClass("currentOpen").addClass("currentOpen");
		}, 10);
		$(".benefits .accordian .topLevel h4").css("width", "auto");
		setTimeout(function() {
			$(".benefits .accordian .topLevel h4").css("width", "100%");
			$(".benefits .accordian .topLevel .currentOpen h4").css("width", "50%");
		}, 50);
	});
	

	// -------------------------------------------------------------
	// Toggle Corporate Only Benefits
	// -------------------------------------------------------------

	$('p.ui-accordion-content-active').parent().toggleClass("currentOpen");

	$(".toggle .switch").toggle(
      function() {
      	$(this).find('img').attr({ src: '/images/benefits/switch_no.gif' });
      	$('.accordian .corpItem.currentOpen').parent().accordion('activate', 0);
      	//$('.accordian .corpItem').css({ "display": "none" });
      	$('.accordian .corpItem').hide();
      },
      function() {
      	$(this).find('img').attr({ src: '/images/benefits/switch_yes.gif' });
      	//$('.accordian .corpItem').css({ "display": "block" });
      	$('.accordian .corpItem').show();
      });


	// -------------------------------------------------------------
	// Add to video list
	// -------------------------------------------------------------

	var benefitIndex = 0;
	$('.benefitItem').each(function() {
		var i = "bID" + benefitIndex;
		var addBtn = "<img class=\"addToListBtn\" alt=\"add\" src=\"/images/benefits/addToVideo.gif\" width=\"91\" height=\"36\" /> \n";
		$(this).attr("id", i);
		$(this).prepend(addBtn);
		benefitIndex++;
	});


	$('.addToListBtn').click(function() {
		var strParentName = $(this).parent().find('h4').text();
		var vidPath = $(this).parent().find('h4').attr('vp');
		var strParentID = $(this).parent().attr('id');
		var strNewItem = "<li class=\"" + strParentID + "\" vp=\"" + vidPath + "\">" + strParentName + "</li>";

		var exists = false;
		$('.benefitList ul li').each(function() {
			if ($(this).hasClass(strParentID)) {
				exists = true;
			}
		});

		if (!exists) {
			$('.benefitList ul').append(strNewItem);
			plvid(vidPath);
		}

		if ($('.benefitList ul > *').length > 0) {
			$('.benefitList').css({ "display": "block" });
			$('.videoAreaDefault').css({ "display": "none" });
			$('.videoAreaInner').css({ "display": "block" });
		}

		$('.benefitList li:last').click(function() {
			$(this).remove();
			if ($('.benefitList ul > *').length == 0) {
				$('.benefitList').css({ "display": "none" });
				$('.videoAreaDefault').css({ "display": "block" });
				$('.videoAreaInner').css({ "display": "none" });
			}
		});
	});

	// -------------------------------------------------------------
	// Video Overview
	// -------------------------------------------------------------  


	if (FlashDetect.installed && FlashDetect.major >= 9) {

		//inserts flash
		var flashvars = {};
		var params = {};
		var attributes = {};
		params.allowScriptAccess = "always";
		params.wmode = "opaque";

		jQuery.get("videos.xml", function(data) {
			if (jQuery(data).find("videopath")[0] != null && jQuery(jQuery(data).find("videopath")[0]).text() != "") {
				flashvars.xmlpath = jQuery(jQuery(data).find("videopath")[0]).text();
				flashvars.divid = "meet1";
				swfobject.embedSWF("/videodata/swfs/videoplayer.swf", "meet1", "340", "192", "9.0.0", "", flashvars, params, attributes);
			}
		});
	}

});



// function to send content to preloader
function plvid(path) {
  var swf = document.getElementById("videoPreloader");
  var plPath = "/videodata/flvs/" + path + ".flv";
  //if (swf.flashPreload) {
  	swf.flashPreload(plPath);
  //}
}

