
var mplayer;

var apple = false;
if( (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) ) {
	var apple = true;
}

function playerReadyCallback(obj) {
	//alert('the videoplayer '+obj['id']+' has been instantiated');
	mplayer = document.getElementById(obj['id']);
	mplayer.addModelListener('STATE', 'eq_toggle');	// play OR pause
	//$("#player-container").prepend('<a href="javascript: toggle_player()">&laquo;</a>');
	$("#player-container a").show();
};


function play(idx) {
	// link on page clicked
	mplayer.sendEvent('ITEM', idx);
}

function pause() {
	mplayer.sendEvent('PLAY', false);
}

function eq_toggle(obj) {
	//alert(obj.newstate);
	if (obj.newstate == 'PLAYING') {
		$("#eq").attr('src', '/images/eq_playing.gif');
	} else {
		$("#eq").attr('src', '/images/eq_stopped.gif');
	}
}

function get_answer(from_id, name) {
	// extracts answers from right pane contents based on anchor name passed and div id
	// quotes are ? to allow for IE's funky internal html representation
	pattern = '<a[^>]+name="?' + name + '"?[^>]*>[^<]*</a>(?:(?!<a[^>]+name=).)*';
	pattern = new RegExp(pattern, "gi");
	
	ans = $('#' + from_id).html();
	ans = ans.replace(/\r|\n|\t/g, ' ');
	ans = ans.match(pattern);
	
	ans = ans[0].substr( ans[0].toLowerCase().indexOf('</a>') + 4 );
	
	return ans;
}


function init() {
	// center site (and intro, if exists) vertically
	m = Math.round( ($(window).height() - $("#outer").height()) / 2 );
	if (m < 10) { m = 10; }
	$("#outer, #intro-container").css('margin-top', m + 'px');
	
	// close player if open
	$("#floater").css('width', '67px');
	$("#player-container a").html('&laquo;');
	
	// place the player
	l = $("#menu").offset().left + $("#menu").width() - 89;
	
	
	// intro or not?
	if ( parseInt($("#intro").css('margin-top')) < 600 ) {
		l += 24;
	}
	t = $("#menu").offset().top;
	$("#floater").css("left", l + "px");
	$("#floater").css("top", t + "px");
}


function restore_content() {
	// fix ie/webkit bug where content iframe reference is lost when certain functions are run
	// seems to be an issue w/ event binding/handling
	window.name = 'content';
}


function show_right(id) {
	// used on about us, faq
	// intended for left pane / right pane interaction

	if ( ! $("a[href*='show_right(" + id + ")']").hasClass("selected") ) {
		// different heading
		$("a[href*='show_right'], a[href*='show_right'] > *").removeClass("selected");
		$("a[href*='show_right(" + id + ")'], a[href*='show_right(" + id + ")'] > *").addClass("selected");
		$("a[href*='show_right']").mouseout();
		$("div[id^='right_']:visible").fadeOut(200, function() {						
			$("#right_" + id).fadeIn(200);
		});
		if ( id==0 ) {
			// small logo
			$("#logo-small img").attr('src', 'images/logo-small_over.gif');
		} else {
			$("#logo-small img").attr('src', 'images/logo-small.gif');
		}
	}
	
}

function slide_under(id) {
	// used on clients and your-event pages
	// slides p under passed jQuery object
	if ( $("a[href*='slide_under(" + id + ")']").hasClass("selected") ) {
		// user clicked already open heading, close it
		$("a[href*='slide_under'], a[href*='slide_under'] > *").removeClass("selected");
		$("a[href*='slide_under']").mouseout();
		$("div[id^='right_']:visible").slideUp(150);
	} else {
		// different heading
		$("a[href*='slide_under'], a[href*='slide_under'] > *").removeClass("selected");
		$("a[href*='slide_under(" + id + ")'], a[href*='slide_under(" + id + ")'] > *").addClass("selected");
		$("a[href*='slide_under']").mouseout();
		$("div[id^='right_']:visible").slideUp(150, function() {						
			$("#right_" + id).slideDown(150);
		});
	}
	
}


function start_player() {
	$("#floater").width(67);
	flashvars = {
    	playlistfile: 'mp3/playlist.php',
      	autostart: 'true',
		dock: 'false', 
		playlist: 'right',
		playlistsize: '180',
		playerready: 'playerReadyCallback', 
		skin: 'includes/flash/skin/wtdj.xml'
	};

	params = {
		wmode: 'transparent', 
		allowfullscreen: 'false', 
		allowscriptaccess: 'always', 
		loop: 'false'
	};
	swfobject.embedSWF("includes/flash/player.swf", "player", "380", "20", "9", null, flashvars, params, { id: 'mplayer', name: 'mplayer' });

}


function toggle_player() {
	if ( $("#floater").width() == 67 ) {
		$("#floater").animate( { width: '290px', left: '-=199px', top: '-=23px' }, 300, function() {
			$("#player-container a").html('&raquo;');
		});
	} else {
		$("#floater").animate( { width: '67px', left: '+=199px', top: '+=23px' }, 300, function() {
			$("#player-container a").html('&laquo;');
		});
	}
}

$(document).ready( function() {
							
	// show contents asap - avoids white flash in webkit and probably ie
	parent.$('#content').show();
	
	// a contents (as in left column on about us)
	$(".left a").each( function() {
		if ( $(this).children().length > 0 ) {
			$(this).hover (
				function() {
					$(this).children(":not(.selected)").css('border-color', '#ff0000');
				},
				function() {
					$(this).children(":not(.selected)").css('border-color', '#ffffff');
				}
			);
		}
	});
	
	// make sure the content page is reflected in parent's menu bar
	if ( $("#page-content").length > 0 ) {
		// we are on a content page
		page = document.location.href;
		page = page.substr( page.lastIndexOf('/')+1 );
		if ( parent.$("#black").length > 0 ) {
			// intro present
			page = 'home.php';
		}

		if ( page != 'home.php' ) {
			parent.$("#menu a").each( function() {
				if ( $(this).attr('href') == page ) {
					if ( ! $(this).children("img").hasClass('selected') ) {
						$(this).children("img").addClass("selected");
						s = $(this).children("img").attr("src").replace(/\.gif/, '_over.gif');
						$(this).children("img").attr("src", s);
						restore_content();
					}
				} else {
					if ( $(this).children("img").hasClass('selected') ) {
						s = $(this).children("img").attr("src").replace(/_over\.gif/, '.gif');			
						$(this).children("img").attr("src", s);
						$(this).children("img").removeClass("selected");
						restore_content();
					}
				}
			});
			
			
		}
	}
			
});


