//<!--
/** 
	©2006 e-gadget factory Javascript functions
**/
// setup path to image folder
if(path==null)var path = "";
var imgPath = path + 'resources/images/menu/';
// total number of items for each category, for preloading
var menuTotal = 12;
// name of current item selected in menu 
var currItemName = new String();
// reference to sb menu time out for when the user rolls out of a menu
var cat_arr = [ "intro", "prod", "about" ]; 
// minimum width of window
var minW = 808;
// is it Internet Explorer?
var EXPLORER = navigator.appName.substring( 0,9 ) == "Microsoft";
var AGENT = navigator.platform.toLowerCase();
var IS_MAC = ( AGENT.indexOf( "mac" ) != -1 ); 
/*
 * script to execute on page load
 */
function init( initObj ){
	// setup default page vars
	// setupVars( this, initObj );
	// check what user agent the user has
	checkPlatform();
	// init the newsfeed
	// showFirstNews();	
	// preload all rollover images
	// MM_getPreloadImages();
	// set on resize window event
	// window.onresize = checkWindowWidth;
	// check to see if page has parsed fully
	// onPageParse();
	preloadAllRollOverImages();
}
//
//loops all iamges and preloads rollover images
//
function preloadAllRollOverImages(){
	var ims = document.images;
	// ref to current image element
	var imRef;
	// loop all images in document
	for ( var i = 0; i < ims.length; i++ ){
		imRef = ims[ i ];
		// if the image has the swap image behaviour preload it's image over state
		if( String( imRef.onmouseover).indexOf( "img_swapSrc" ) != -1 ){
			// preload rollover image
			img_onLoad( imRef );
		}
	}
}
/*
 * sets up page variables based on object passed in
 * @note: object properties must match document vars
 */
 function setupVars( obj1, obj2 ){
	 // check object exists
	 if( obj1 != null && obj2 != null ){
		 // loop object properties
		for( var p in obj2 ){
			// set document property
			obj1[ p ] = obj2[ p ];
		}
	}
 }
 //
function checkPlatform(){
	if ( EXPLORER && IS_MAC ){ //
		alert("This website does not support Internet Explorer on the Mac platform.\n\nWe recommend you use an alternative browser such as Safari or Firefox. ")
	} 
}
//
var currSwapImg;
var standardImgPath = "resources/images/";
//
// rollover image swapper
//
function swapImage( el, newSrc ){
	if ( el != null ){
		currSwapImg = el;
		el.src = el.overImg.src;
	}else{
		currSwapImg.src = currSwapImg.oSrc;
	}
}
//
// preload rollover image
//
function preloadImg( el, imgName ){
	// if over src is not defined set it
	if ( el.oSrc == null ) el.oSrc = el.src;
	// preload over image
	el.overImg = new Image( );
	// set path of over image
	el.overImg.src = new String( standardImgPath + imgName );
}
//
/*
 * Returns the width of the browser window
 */
function getWinWidth(){
	return EXPLORER ? document.body.clientWidth : window.innerWidth;
}
// check thumbnails have loaded interval reference
var checkThumbsLoaded;
// 
function onPageParse(){
	checkThumbsLoaded = setInterval( "checkThumbsExist( checkThumbsLoaded )", 10 );
}
function thumbsLoaded(){
	return thumbsLoaded = document.getElementById( "thumbnails" );
}
function checkThumbsExist( interval ){
	// check thumbs have been created
	if ( thumbsLoaded() ){
		// check window width
		checkWindowWidth();
		clearInterval (interval );
	}
}

// whether the right column has been hidden or not
var rcHidden = false;
/*
 * Checks the width of the window
 */
function checkWindowWidth( ){
	// check thumbnails exist - if window is resized before page has fully loaded
	// if ( !thumbsLoaded() ) return;
	// hide or show the column based on width of window
	hideRightCol( getWinWidth() <= minW );
}
function hideIt(){
		/*setElementVisibility( [ document.getElementById("thumbnails") ],"none" );
		// 
		newsEl = document.getElementById("newsfeed");
		newsEl.setAttribute('id','newsfeed_narrow');
		//
		loginEl = document.getElementById("login");
		loginEl.setAttribute('id','login_narrow');
		//
		var cont = document.getElementById( 'content' );
		cont.setAttribute('id','content-full');*/
		var cont = document.getElementById( 'content' );
		cont.setAttribute('id','content-full');
}
function showGroup( cat_str, vis ){

	document[ cat_str ] = ( document[ cat_str ] == false ) ? true : false;
	var catVar = document[ cat_str ];
	// 
	var el;
	var counter = 0;

	while( document.getElementById( cat_str +"_"+ counter ) != null ){
		el = document.getElementById( cat_str +"_"+ counter );
		el.style.display = catVar ? EXPLORER ? "block" : vis : "none";
		counter++;
	}
	//
	counter = 0;
	// hide header text
	while( document.getElementById( cat_str +"_hdr_"+ counter ) != null ){
		el = document.getElementById( cat_str +"_hdr_"+ counter );
		if(el.oldTxt==null) el.oldTxt = el.innerHTML;
		el.innerHTML = catVar ? el.oldTxt : "";
		counter++;
	}
	//
	// change arrow Icon
	el = document.getElementById( cat_str +"_arrow" );
	el.src = standardImgPath + ( catVar ? "contract_arrow.png" : "expand_arrow.png" );
}
//
// @param vis should be set to "none" or "block"
//
function setElementVisibility( arr, vis ){
	var i = 0;
	for(i;i<arr.length;i++)arr[i].style.display = vis;
}
//
//
//
/*
 *	Preload rollover images
 */
function img_onLoad( el ){
	if( el.over_img == null ){
		var orig_src = new String( el.src );
		var over_src = new String( orig_src.substr( 0, orig_src.length-4 )+"_over.png" );
		// store ref to orig src
		el.orig_src = orig_src;
		// newe image object
		var over_img = new Image(); 
		// set src and preload
		over_img.src = over_src;
		// store ref to object
		el.over_img = over_img;
	}
}
//
//	swaps image srcs
//
function img_swapSrc( el ){
	if( el.over_img == null ) return;
	el.src = ( el.src ==  el.over_img.src ) ? el.orig_src : el.over_img.src;
}
//
function openWindow( id ){
	// var url = "";//"http://localhost/e-gadget%20factory/";
	id = ( id < 10 )? "0"+id  : id;
	var sW = screen.width/2;
	var sH = screen.height/2;
	// positioning
	var xpos = sW-(800/2);
	var ypos = sH-(600/2);
	//
	previewWin = window.open( path + 'e-gadget_preview.htm?'+ id,'latest','width=800,height=600,top=' + ypos + ', left=' + xpos + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no' );
	previewWin.focus();
}
 function openSample(url){
	var sW = screen.width/2;
	var sH = screen.height/2;
	// positioning
	var xpos = sW-(800/2);
	var ypos = sH-(600/2);
	//
	var sampleWin = window.open( url,'demo','width=800,height=600,top=' + ypos + ', left=' + xpos + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no' );
	sampleWin.focus();
 }
 function openImage(imagePath,name){
	var sW = screen.width/2;
	var sH = screen.height/2;
	// positioning
	var xpos = sW-(800/2);
	var ypos = sH-(600/2);
	// addaparams to page name
	var page = new String( 'image_viewer.php?path='+imagePath+'&name='+name );
	//
	var imageWin = window.open( page,'egadgetImage','width=800,height=600,top=' + ypos + ', left=' + xpos + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no' );
	imageWin.focus();
 }
 // store ref to window
var wkshpWin;
// ref t ointerval check for workshop window exisatnace
var winExistCheck;
// error text to display if workshop does not load
var errorTxt = 'The e-gadget workshop has been prevented from opening.\n\nPlease ensure you have any popup blocking software disabled.';
function openMaxWindow( url ){
	// get screen size
	var sW = screen.availWidth;
	var sH = screen.availHeight;
	// check after two seconds to see if window has been blocked
	winExistCheck = setInterval( 'checkWindowExists(wkshpWin,errorTxt)', 2000 );
	// launch workshop
	wkshpWin = window.open( url,'workshop','width=' + sW + ',height=' + sH + ',top=0, left=0,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=yes' );
	// force focus on new window
	wkshpWin.focus();
}
/*
 * checks if window exists
 */
function checkWindowExists(win,errorMsg){
	// if window does not exist as object
	if ( !win ){
		// display error message
		alert( errorMsg );
	}
	// clear check
	clearInterval( winExistCheck );
}
/*
*	Display large versions of random images
*
*/
// returns the imaage ID from the url path
function getImageID(){
	var imageID = location.search.substr(1).split("?");
	return imageID[ 0 ];
}
// returns the concatentated image path and the image ID
function getImage(  ){
	var image = '<a href="" onclick="window.close();"><img src="' + path + 'resources/images/e-gadget_thumbs/screenshots/_large'  + getImageID() + '.jpg" alt="e-gadget screenshot" title="e-gadget screenshot" width="800" height="600" /></a>';
	return image;	
}
// returns a full image tag to be written
function writeImageTag(){
	var imageTag = document.write( getImage() );
	return imageTag;
}
var newwindow;
function popup(url,w,h)
{
	newwindow=window.open(url,'name','height='+h+',width='+w);
	if (window.focus) {newwindow.focus()}
}
/*
 * Checks if a number key has been pressed
 */
function isNumber( keyCode ){
	// checks ASCII keycode is within the range of the numeric keypad
	return ( keyCode >= 48 && keyCode <= 57  );
}
/*
	Check if it is a letter
	a-z
	A-Z
*/
function isLetter( keyCode ){
	return ( keyCode >= 65 && keyCode <= 90 || keyCode >= 97 && keyCode <= 122 )
}
/*
function isNumericKey( keyCode ){
	alert( keyCode )
	return isKeypad( keyCode ) || isNumber( keyCode ); //  || 
}*/
/* 
	Check if a string is alpha numeric
*/
function isAlphaNumeric( string ){
	//
	var keyCode, check;
	// loop string and check is alphanumeric
	for( var i = 0; i < string.length; i++ ){
		// get next character
		keyCode = string.charAt( i ).charCodeAt( 0 );
		// check if number 
		check = isNumber( keyCode ) || isLetter( keyCode );
		// if it's not a number or a letter return false and do not continue checking
		if( !check ) break;
	}
	// returns true if correct
	return check;
}
//-->