/**
* Javascript functions for TypeNavigator
*/

// opening window
var fontShopWin;
if (this.top.opener != undefined) {
	fontShopWin = this.top.opener;
} else {
	trace("fontshop window not available");
}


/**
* Submit the search form
*/
function submitSearchForm()
{
	document.searchForm.submit();
	top.top_left.turnRadar(true);
	return true;
}
 
/**
* Send Flash object a message
*/
function tellFlash(movieName, message, parameter)
{
	flash = getFlash(movieName);
	try {
		if (flash) {
			flash.SetVariable("jsMessage", message);	
			flash.SetVariable("jsParameter", parameter);
			flash.TCallLabel("/", "jsCall");
		}
	} catch (error) {
		trace(error);
	}
}
 
/**
* Get Flash object with given name / id
*/
function getFlash(name) {
	// IE and Netscape refer to the movie object differently.
	// This function returns the appropriate syntax depending on the browser.
	if (navigator.appName.indexOf ("Microsoft") !=-1) {
		return window[name];
	} else {
		return document[name];
	}
}

/**
* Turn the typenavigator logo (or stop it);
*/
function turnRadar(bool) {
	if (bool) {
		trace("turn radar");
		tellFlash("typenav_logo","go","");
	} else {
		tellFlash("typenav_logo","stop","");
	}
}

/**
* Stop the Radar
*/
function stopRadar() {
	if (top.frames.top_left) {
		try {
			top.frames.top_left.turnRadar(false);
		} catch(e) {
			trace(e);
		}
	}
	return true;
}

function findSimilar(GIFID) {
	if (top.frames.top_left) {
		top.frames.top_left.turnRadar(true);
	}
	window.location.href="search.asp?similar=1&GIFID="+GIFID;
	return false;
}

/**
* Open the font shop window and record stats about the buy button click
*/
function buy(shopURL, GIFID, buyButtonLocation, resultIndex)
{
	// log buy button click
	statURL = "/record_buy_click.asp?GIFID=" + GIFID;
	statURL = statURL + "&buyButtonLocation=" + buyButtonLocation;
	statURL = statURL + "&resultIndex=" + resultIndex;
	
	// try and use XMLHttpRequest
	httpRequestObject = createHttpRequestObject();
	if (httpRequestObject != null) {
		httpRequestObject.open('get', statURL);
		httpRequestObject.send(null);		
	} else {
		// if this doesn't work (old browser), use the hidden frame
		top.frames.hidden.location.href = statURL;
	}
	
	// open the shop url
	if (fontShopWin) {
		trace("font shop window found");
		if (fontShopWin.closed) {
			trace("but closed");
			fontShopWin = window.open(shopURL);
		} else if (window.focus) {
			trace("opening URL");
			fontShopWin.location.href = shopURL;
			fontShopWin.focus();
		}
	} else {
		trace("font shop window not found - opening new window");
		fontShopWin = window.open(shopURL);
	}
}


/**
* Reload the flash search form
*/
function reloadForm() {
	if (top.frames.left.document.location.href=="form.asp") {
		top.frames.left.document.location.reload();
	} else {
		top.frames.left.document.location.href="form.asp";
	} 
	top.frames.top_left.document.location.href="top.asp?search_type=1";
}

/**
* Basic AJAX stuff
*/

function createHttpRequestObject() {
    var reqObj = null;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer") {
        reqObj = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        reqObj = new XMLHttpRequest();
    }
    return reqObj;
}

function trace(s) {
	if (window.console) {
		window.console.log(s);
	}
}




