// Example:
// simplePreload( '01.gif', '02.gif' ); 
function simplePreload()
{ 
  var args = simplePreload.arguments;
  document.imageArray = new Array(args.length);
  for(var i=0; i<args.length; i++)
  {
    document.imageArray[i] = new Image;
    document.imageArray[i].src = args[i];
  }
}

function rollover (imgName) {
	if (arguments.length > 1 && arguments[1].length > 1) {
		imgSource = arguments[1];
		// look for image sub-node w/ arg 2 as src
		if (imgName.childNodes) {	// DOM
			c = imgName.childNodes;
		} else {	// IE
			c = imgName.children;
		}
		for (i=0; i < c.length; i++) {
			if (c[i].tagName == "IMG" && c[i].getAttribute("SRC").indexOf(imgSource) > -1) {
				s = c[i];
				
				break;
			}
		}
	} else {
		s = document.images[imgName];
	}
	if (s.src.indexOf("_over") > 0) {
		s.src = s.src.replace(/_over/,"");
	} else {
		if (s.src.indexOf(".jpg") > 0) {
			s.src = s.src.replace(/\.jpg/,"_over.jpg");
		}
		if (s.src.indexOf(".gif") > 0) {
			s.src = s.src.replace(/\.gif/,"_over.gif");
		}
		if (s.src.indexOf(".png") > 0) {
			s.src = s.src.replace(/\.png/,"_over.png");
		}
	}
}

function swap (imgName,swapTo) {
	document.images[imgName].src = swapTo;
	}
	
function show(divname) {
	document.getElementById(divname).style.display = "inline";
	}
	
function hide(divname) {
	document.getElementById(divname).style.display = "none";
	}
	
function toggle(divname) {
	$("#" + divname).slideToggle(300);
	a = "a[href*='" + divname + "']";
	if ($(a).text().indexOf("  (close this)") > -1) {
		txt = $(a).text().replace(/\s\s\(close this\)/,"");
	} else {
		txt = $(a).text() + "  (close this)";
	}
	$(a).text(txt);
}

function download(file) {
	document.music.submit2.value="save";
	document.music.filename.value=file;
	document.music.submit();
}
	
// apAudio / flash stuff below
	
var movie = "apAudio";

function getMovie(movieName) {	// returns proper object reference
  if (window.document[movieName])   {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)  {
    if (document.embeds && document.embeds[movieName]) {
      return document.embeds[movieName]; 
	}
  }  else { // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
    return document.getElementById(movieName);
  }
}

// Checks if movie is completely loaded.
// Returns true if yes, false if no.
function movieIsLoaded (theMovie) {
  // First make sure the movie's defined.
  if (typeof(theMovie) != "undefined") {
    // If it is, check how much of it is loaded.
    return theMovie.PercentLoaded() == 100;
  } else {
    // If the movie isn't defined, it's not loaded.
    return false;
  }
}

function playSong(title) {
	m = getMovie(movie);
  if (movieIsLoaded(m)) {
	m.StopPlay();
	m.SetVariable("songTitle",title);
	m.GotoFrame(4);
//	m.Play();
  }
}

function saveSong() {
	m = getMovie(movie);
	if (movieIsLoaded(m)) {
		m.GotoFrame(9);
	}
}

function apAudio_DoFSCommand(command,params) {
	alert(params);
}

function fixFlash() {
	theObjects = document.getElementsByTagName("object");
	for (var i = 0; i < theObjects.length; i++) {
		theObjects[i].outerHTML = theObjects[i].outerHTML;
	}
}

function open_info(file) {	// fills imgInfo div appropriately
// jquery version
// hide others first
	$(".info_box").hide();
//	$("#ap_audio").hide();
	$("#ap_audio").css("visibility", "hidden");
// fade in bg
	$("#info_bg").fadeIn("fast");
	$("#info_bg").fadeTo("fast", .9);
// fade in box
	$("#info_"+file).fadeIn("fast");
	
// show info
	if ($(document).scrollTop() < 40) {
		$("#info_"+file).css("top", "80px");
	} else {
		$("#info_"+file).css("top", ($(document).scrollTop() + 40) + "px");
	}
	$("#img_"+file).attr({ src: "../work/" + file + ".jpg" });
	$("#img_"+file).load(function() {
		// resize bg if necessary
		$("#info_bg").height($(document).height());
	});
}

function get_info(file) {	// for pages that load data using ajax
// reset info
	$(".info_close_bar span").html("");
	$("#text").html("");
// show watch
	$("#img_").attr({ src: "../images/watch.gif" });
// fade in bg
	//$("#ap_audio").hide();
	$("#ap_audio").css("visibility", "hidden");
	$("#info_bg").fadeIn("fast");
	$("#info_bg").fadeTo("fast", .9);
// fade in box
	$("#info_").fadeIn("fast");
	
// show info box
	if ($(document).scrollTop() < 40) {
		$("#info_").css("top", "80px");
	} else {
		$("#info_").css("top", ($(document).scrollTop() + 40) + "px");
	}
// get info
	$.get("../includes/get_info.php?file=" + file, function(data) { 
		eval(data);
		$("#img_").attr({ src: "../work/" + file + ".jpg" });
		$("#img_").load(function() {
			// resize bg if necessary
			$("#info_bg").height($(document).height());
		});
	});	
}

function close_info(file) {
	$("#info_"+file).fadeOut(300);
	$("#info_bg").fadeOut(500);
	//$("#ap_audio").show();
	$("#ap_audio").css("visibility", "visible");
}

function menu_more() {
// toggles work menu items
	$(".menu").each(function (i) {
		if (this.href.indexOf("work.php") > -1) {
			$(this).toggle();
		}
	 })
}

function bubble_in() {	// standard bubble fadein routine
// optional args: message, width
	if (arguments[0]) {
		message = arguments[0];
	} else {
		message = "view larger image and more info";
	}
	if (arguments[1]) {
		w = arguments[1];
	} else {
		w = 100;
	}
	$("#bubble").html(message);
	$("#bubble").width(w);
	$("#bubble").fadeIn(100);
	$("#bubble").fadeTo("fast", .8);
}