var switchercount = 0;
var switcherelementcount = 0;
var switcherarray = new Array();

function loader()
{
	if(!parentExists())
{
	$(".switcher").each(setupSwitcher);
}
}

function parentExists()
{
 return (parent.location == window.location)? false : true;
}

function setupSwitcher()
{
	//setup id for this element
	switchercount++;
	switcherid = "switcher"+switchercount;
	$(this).attr("id", switcherid);
	switcherarray[switcherid] = new Array();

	//add a div for navigation
	$(this).html('<div class="switchernavigation"><ul></ul></div>'+$(this).html());

	//setup switcher panes
	switcherelementcount  = 0;
	$(this).children(".switcher-item").each(setupSwitcherItem);
	$(".nav-switcher").click(switchElement);
}

function setupSwitcherItem()
{
	//setup id for this pane
	switcherelementcount++;
	switcherid = "switcheritem"+switcherelementcount;
	$(this).attr("id", switcherid);
	
	switcherarray[$(this).parent().attr("id")][switcherelementcount] = switcherid;

	//add a control element to the switchernavigation
	title = $(this).children(".switcher-title").html();
	//$(this).children(".switcher-title").hide();
	$(this).parent().find('.switchernavigation ul').html($(this).parent().find('.switchernavigation ul').html() + '<li><a class="nav-switcher" id="nav-'+switcherid+'" href="#">'+title+'</a></li>');
	
	//hide this if it isn't the first
	if(switcherelementcount != 1)
	{
		$(this).hide();
	} else {
		$('#nav-'+switcherid).addClass('selectedoption');
	}
}

function switchElement()
{
	idarr = $(this).attr("id").split("nav-");
	for(i = 1; i < switcherarray[$(this).parents(".switcher").attr("id")].length; i++)
	{
		$("#"+switcherarray[$(this).parents(".switcher").attr("id")][i]).hide();
		$("#nav-"+switcherarray[$(this).parents(".switcher").attr("id")][i]).removeClass('selectedoption');
	}
	$(this).addClass('selectedoption');
	$("#"+idarr[1]).show();
	
	return false;
}

$(document).ready(loader);

