/*
* Misc Functions
* @date: 12-20-05
* @author: Joseph 'Pcjoe' Florencio
*/

// Preload Image
function preloadImage( img )
{
  // Preload image into array of document object
  if(document.images)
  {
    if(!document.p)
    {
      document.p = new Array();
    }
    var length = document.p.length;
    document.p[length] = new Image;
    document.p[length].src = img;
  }
}

// Check all
function checkAll(field)
{	for (i = 0; i < field.length; i++)
		field[i].checked = true ;
}

// Uncheck all
function uncheckAll(field)
{	for (i = 0; i < field.length; i++)
		field[i].checked = false ;
}

// Invert checks
function checkInvert(field)
{	for(i = 0; i < field.length; i++)
		field[i].checked = !field[i].checked;
}

// Make image popup
function makeImagePopup(url, title, width, height)
{	var newWindow = window.open('', '', "menubar=0,resizable=0,status=no,width="+width+",height="+height);
	var tmp = newWindow.document;
	var titleSpan = '';
	if(title != "")
	{	titleSpan = "<span>" + title + "</span>\n";
	}
	var extraCSS = '';
	var MSIEidx = 0;
	// IE browser check, include extra css files
	if((MSIEidx = navigator.appVersion.indexOf('MSIE')) > 0)
	{	var semiPos = navigator.appVersion.indexOf(';', MSIEidx);
		var MSIEversion = Number(navigator.appVersion.substr(MSIEidx+5, semiPos-(MSIEidx+5)));
		if(MSIEversion >= 7.0)
			extraCSS = "<link rel=\"stylesheet\" href=\"ie7.css\" type=\"text/css\">\n";
		else
			extraCSS = "<link rel=\"stylesheet\" href=\"ie.css\" type=\"text/css\">\n";
	}
	var output = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n" +
		"<html>\n" +
		"<head>\n" +
		"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n" +
		"<meta http-equiv=\"imagetoolbar\" content=\"no\">\n" +
		"<link rel=\"stylesheet\" href=\"default.css\" type=\"text/css\">\n" +
		extraCSS +
		"<title>"+title+"</title>\n" +
		"</head>\n" +
		"<body bgColor='#000000' style='margin: 0px;' scroll='no'>\n" +
		"<center>\n" + 
		"<div class=\"image\" style=\"width:" + width + "px; height:" + height + "px;\">\n" +
		"<a href=\"javascript:window.close()\"><img src=\""+url+"\" border=\"0\" alt=\""+title+"\"></a>\n" +
		titleSpan +
		"</div>\n" +
		"</center>\n" +
		"</body>\n" +
		"</html>";
	tmp.write(output);
	tmp.close();
}

// Change CSS class
function changeCSSClass(object, classChange)
{	object.className = classChange;
}

// Make gallery popup (admin)
function makeGalleryPopup(save_id, form, gal_id, name, button)
{	var link = 'gallery_popup.php?save_id='+save_id+'&form='+form+'&galid='+gal_id+'&makename='+name+'&button='+button;
	var newWin = window.open(link, '_blank', 'menubar=0,resizable=1,scrollbars=1,status=no,width=840,height=500');
}

// Change anime focus tab
function changeTabFocusATab(listElem, boxElem)
{	// Make sure we're not reclicking the same tab
	if(oldListElem != listElem)
	{	// set old list element back to normal
		// Change back to regular class
		changeCSSClass(oldListElem, "animetabs");
		// Do same with box element
		oldBoxElem.style.display = "none";
		
		// set new as old
		oldListElem = listElem;
		oldBoxElem = document.getElementById(boxElem);
		
		// update new list element
		changeCSSClass(listElem, "animetabs_sel");
		oldBoxElem.style.display = "block";
	}
}

// Navigation array
navArray = new Array("main","anime","games","work","contact");
currentnav = "";
function changeNavToCurrent(nav)
{	// Reset the nav for previous current
	if(currentnav.length > 0)
		changeCSSClass(document.getElementById(currentnav),"");
	// See which one fits
	for( i = 0; i < navArray.length; i++ )
	{	// Matches
		if(nav.indexOf(navArray[i]) != -1)
		{	currentnav = navArray[i];
			break;
		}
		// Last possible attempt, set to first tab
		else if(i == (navArray.length-1))
		{	currentnav = navArray[0];
			break;
		}
	}
	// Set this one to current
	changeCSSClass(document.getElementById(currentnav),"current");
}