

var current;
var number_dropdowns = 2;
var SoundOn = true;
var wallpaper = false;
var reset_meaning_select = new Array();
function notes( readingId, noreload, my_site ){
	//open up new window for notes
		if (! my_site){
			my_site = "astrology";
		}
	var file = "/readextras/?file=notes&site=" + my_site + "&rID=" + readingId + "&r=1&noreload="+noreload;
	newWindow( file, 456, 380, 0, 0, 'ReadingNotes' );
}

function name( readingId, my_site ){
	if (! my_site){
		my_site = "astrology";
	}
	var file = "/readextras/?file=name&site=" + my_site + "&rID=" + readingId;
	newWindow( file, 420, 360, 0, 0, 'NameReading' );
}


function prev()
{
	var scroll_to = (arguments[0]) ? arguments[0] : 0 ;
	var selects = document.getElementsByTagName('select');
	for( i = 0; i < selects.length; i++ )
	{
		if( selects[i].id.indexOf( 'nav_' ) == 0 )
		{
			selects[i].options.selectedIndex -= 1;
			if(scroll_to == 0){
				showChapter( selects[i].value );
			} else {
				showChapterScroll(selects[i].value, scroll_to);
			}
			return;
		}
	}
}


function next()
{
	var scroll_to = (arguments[0]) ? arguments[0] : 0 ;
	var selects = document.getElementsByTagName('select');
	for( i = 0; i < selects.length; i++ )
	{
		if( selects[i].id.indexOf( 'nav_' ) == 0 )
		{
			selects[i].options.selectedIndex += 1;
			if(scroll_to == 0){
				showChapter( selects[i].value );
			} else {
				showChapterScroll(selects[i].value, scroll_to);
			}
			return;
		}
	}
}

function load_wallpaper_overlay(myId){
	bgCurrent = (arguments[1]) ? "overlay_" + arguments[1] : document.getElementById("report_content_body_introductionTransBG").className ;
	if ( document.getElementById(myOverlay + "TransBG") ){
		document.getElementById(myOverlay + "_outer").removeChild(document.getElementById(myOverlay + "TransBG"));	
	}
	jQuery("#" + myId).transBGdraw({'background':'transparent url(http://gfx.tarot.com/images/product/en_us/68200/backgrounds/1-px.jpg) repeat-y scroll 12px 0px','z-index':'0'});
	jQuery("#" + myId + "TransBG").addClass(bgCurrent);

}
function change_wallpaper_overlay(myId){
	bgCurrent = (arguments[1]) ? "overlay_" + arguments[1] : document.getElementById("report_content_body_introductionTransBG").className ;
	jQuery("#" + myId + "TransBG").removeClass();
	jQuery("#" + myId + "TransBG").addClass(bgCurrent);

}

function showChapter( id )
{
	var no_top = (arguments[1]) ? arguments[1] : false ;
	if( !current )
	{
		current = 'cintroduction';
	}

	document.getElementById( current ).className = 'report_hidden';
	document.getElementById( id ).className = 'report_shown';
	current = id;
	var selected = '';
	selects = document.getElementsByTagName('select');
	for( i = 0; i < selects.length; i++ )
	{
		if( selects[i].id.indexOf( 'nav_' ) == 0 )
		{
			for( j = 0; j < selects[i].options.length; j++ )
			{
				if( selects[i].options[j].value == id )
				{
					selects[i].options.selectedIndex = j;
					selected = j;
				}
			}
		}
	}
	if (! no_top){
		scroll(0,0);
	}
	if (typeof (reset_meaning_select[id.substring(1)]) != 'undefined' && reset_meaning_select[id.substring(1)]){
		meaning_type_reset('position_meaning', id);
	}
	if (wallpaper != false){
		myOverlay = wallpaper + "_" + current.substr(1);
		change_wallpaper_overlay(myOverlay);
	}
	updateButtons();
}

function summary( file )
{
	var filename = "/shared/text.php?file="+file+"&section=astrology/summary";
	newWindow(filename, 450, 400, 10, 10, 'AstrologySummary');
}

function getElementsByClassName(tag, class_name){
	var Elems = document.getElementsByTagName(tag);
	var ElemByClass = new Array();
	var y = 0;
	for (x=0; x < Elems.length;x++){
		if (Elems[x].className == class_name){
			ElemByClass[y] = Elems[x];
			y++;
		}
	}
	return ElemByClass;
}

function updateButtons()
{
	var selects = getElementsByClassName('select', 'nav_normal');
	if( selects[0].options.selectedIndex == 0 )
	{
		select_id_top = selects[0].id;
		key_top = select_id_top.substr( 4 );
		key_btm = '';
		if( number_dropdowns == 2 ){
			select_id_btm = selects[1].id;
			key_btm = select_id_btm.substr( 4 );
		} else {
			// Get key when no secon select/dropdown.
			var anchors = document.getElementsByTagName('A');
			var my_switch = false;
			for( i = 0; i < anchors.length; i++ ) {
				if (anchors[i].id.match('navPrev_')){
					if ( my_switch ){
						key_btm = anchors[i].id.substr(8);
						break;
					}
					if (anchors[i].id == 'navPrev_' + key_top){
						my_switch = true ;
					}
				}
			}
		}
		//document.getElementById( 'navPrev_top_' + key_top ).className = 'report_hidden';
		//document.getElementById( 'navPrev_btm_' + key_btm ).className = 'report_hidden';
		if ( document.getElementById( 'navPrev_' + key_top ) ){
			document.getElementById( 'navPrev_' + key_top ).className = 'report_hidden';
		}
		if ( document.getElementById( 'navPrev_' + key_btm ) ){
			document.getElementById( 'navPrev_' + key_btm ).className = 'report_hidden';
		}
		if( number_dropdowns == 1 ){
			document.getElementById( 'nav_' + key_top ).className = 'nav_compensate';
		}
	}
	else if ( selects[selects.length - number_dropdowns].options.selectedIndex ==
		selects[selects.length - number_dropdowns].options.length - 1 )
	{
		select_id_top = selects[selects.length - number_dropdowns].id;		
		key_top = select_id_top.substr( 4 );
		if ( number_dropdowns == 2){
			select_id_btm = selects[selects.length - 1].id;
			key_btm = select_id_btm.substr( 4 );
		} else {
			var anchors = document.getElementsByTagName('A');
			my_switch = false;
			for( i = 0; i < anchors.length; i++ ) {
				if (anchors[i].id.match('navPrev_')){
					if ( my_switch ){
						key_btm = anchors[i].id.substr(8);
						break;
					}
					if (anchors[i].id == 'navPrev_' + key_top){
						my_switch = true ;
					}
				}
			}
		}
		if( document.getElementById( 'navNext_top_' + key_top ) ){
			document.getElementById( 'navNext_top_' + key_top ).className = 'report_hidden';
		}
		if( document.getElementById( 'navNext_' + key_top ) ){
			document.getElementById( 'navNext_' + key_top ).className = 'report_hidden';
		}
		if( number_dropdowns == 1 ){
			document.getElementById( 'nav_' + key_top ).className = 'nav_compensate';
		}
		if ( number_dropdowns == 2){
			if( document.getElementById( 'navNext_btm_' + key_btm ) ){
				document.getElementById( 'navNext_btm_' + key_btm ).className = 'report_hidden';
			}
		}
		if( document.getElementById( 'navNext_' + key_btm ) ){
			document.getElementById( 'navNext_' + key_btm ).className = 'report_hidden';
		}
	}
	if (wallpaper != false){
		myOverlay = wallpaper + "_" + current.substr(1);
		if ( ! document.getElementById(myOverlay + "TransBG") ){
			load_wallpaper_overlay(myOverlay);
		}
	}
}

function email( readingId){
	var filename = "/readextras/?file=email&site=tarology&rID=" + readingId;
	newWindow( filename, 450, 515, 0, 0, 'EmailReading' );
}

function email_ups( readingId , prodID, site, report_or_reading ){
	if ( report_or_reading === undefined ) {
		 report_or_reading = 'report';
	}
	var filename = "/readextras/?file=email&site=" + site + "&rID=" + readingId + "&report_or_reading=" + report_or_reading + "&ups=1";
	if(prodID >0){
		filename = filename + '&id=' + prodID;
	}
	newWindow( filename, 450, 515, 0, 0, 'EmailReading' );
}

function email_ki( readingId ){
	var filename = "/readextras/?file=email&site=ki&rID=" + readingId;
	newWindow( filename, 450, 515, 0, 0, 'EmailReading' );
}

function email_ki_forecast( readingId ){
	var filename = "/readextras/?file=email&site=kiforecast&rID=" + readingId;
	newWindow( filename, 450, 515, 0, 0, 'EmailReading' );
}

function show( box )
{
	var selected = box.options.selectedIndex;
	var id = box.options[selected].value;
	var no_top = (arguments[1]) ? arguments[1] : false ;

	if( !current )
	{
		current = 'cintroduction';
	}

	document.getElementById( current ).className = 'report_hidden';
	document.getElementById( id ).className = 'report_shown';
	
	current = id;
	var selects = document.getElementsByTagName('select');
	for( i = 0; i < selects.length; i++ )
	{
		if( selects[i].id.indexOf( 'nav_' ) == 0 )
		{
			selects[i].options.selectedIndex = selected;
		}
	}
	if ( ! no_top){
		scroll(0,0);
	} else {
		window.scrollTo(0,no_top);
	}
	updateButtons();
	if (typeof (reset_meaning_select[id.substring(1)]) != 'undefined' && reset_meaning_select[id.substring(1)]){
		meaning_type_reset('position_meaning', id);
	}
}

function showInPageLink( id, inpagelink ){
	// Pass the chapter and the id of the thing to scroll to.
	showChapter( id, true );
	Hg = document.getElementById( inpagelink );
	x = 0;
	y = Hg.offsetTop;
	window.scrollTo(x,y);
}

function showChapterScroll( id, px_to_scroll ){
	// Pass the chapter and the id of the thing to scroll to.
	showChapter( id, true );
	x = 0;
	y = px_to_scroll;
	window.scrollTo(x,y);
}

function newSmWindow( file, w, h, t, l, winName ){
	if( !t ) var t = 10;
	if( !l ) var l = 10;
	if( !w ) var w = 350;
	if( !h ) var h = 300;
	if( !winName ) var winName = "newWindow";

	newWin = window.open( file, winName, 'scrollbars=1,resizable=1,top='+ t +',left='+ l +',height='+ h +',width='+ w);
	newWin.focus();
	if (newWin.opener == null) newWin.opener = self;
}

function glossary( file, jumper ){
	var filename = "/astrology/glossary.php?file="+file+"#"+jumper;
	newSmWindow( filename, 450, 420, 0, 0, 'Glossary' );
}

function playSound(soundid){
	var thisSound = document.getElementById(soundid);
	if (SoundOn){
		thisSound.Stop();
		SoundOn = false;
	} else {
		thisSound.Play();
		SoundOn = true;
	}
}

function preloadImg(src) {
	var dummy = new Image();
	dummy.src=src;
}

function aboutPopup(content_id){
	if(document.getElementById(content_id)){
		var myWindow = window.open("","about_popup","width=480,height=230,scrollbars=1,resizable=1")
		var html = "<html><head>\n";
		html += '<link rel="stylesheet" type="text/css" href="/css/report-popup.css" />\n';
		html += "</head>\n<body>";
		html += "<div class='" + content_id + "'>";
		html += document.getElementById(content_id).innerHTML;
		html += "</div></body></html>\n";

		myWindow.document.open("text/html")
		myWindow.document.write(html)
		myWindow.document.close()
	}
	return true;
}


