/*******************************************************************/

/**
 * TFE Locator Widget
 * must be loaded from loader.js
 * depends on local jquery libraries
 * depends on local file locator.php
 */



/**
 * class function
 * instantiates the client widget object API
 * @param	object	args	js object of two parameters
 					div		the id of the container to use for the locator form and results
 					style	'modal' or 'inline'
 * @return	void
 */
function locator(args){

	this.div 		= args.div;
	this.style 		= args.style;
	this.hostname 	= window.location.hostname.replace(/\./g,'|');
	this.css 		= (args.css != undefined) ? args.css : baseuri+'locator.css';
	this.brand 		= (args.brand != undefined) ? args.brand : '';

	//load locator css for 'inline' locator  - local or specified url
	if(this.css != 'none'){document.write("<link rel='stylesheet' href='"+this.css+"' type='text/css' media='screen' />");}

	//show the locator form
	this.show = function(){
		if(this.style == 'modal'){
			$.nyroModalManual({
				url: baseuri+'locator.php?style=modal&hostname='+this.hostname+'&css='+this.css+'&brand='+this.brand,
				width: 301, minWidth: 301,
				height: 300, minHeight: 300,
				modal: false
			});
		}else{
			proxi('buildform', {display:this.div});
			$('#'+this.div).css('display', '')
		}
	}

	//hide the locator form
	this.hide = function(){
		if(this.style == 'modal'){$.nyroModalRemove();}
		else{$('#'+this.div).css('display', 'none')}
	}


	//test junk
	this.test = function(){proxi('formtest1', {display:this.div});}
	this.test2 = function(){
		var ob = this; //this.toString().match(/function\s+(\w+) *\(/);
		var t = '';
		for(i in ob){t += i + ' : ' + ob[i] + '\n'}
		alert(t);
	}

}



/**
 * web service proxi function
 * jsonp style call to locator.php
 * @param	string	method	method/functionality name to be run on the server
 * @param	object	args	js object of arguments to pass to this function and to the server
 *							`display` may be set to the id attibute of a container that can take innerHTML
 *							`htm` may be set to true or false to indicate if a new container in the dom should be used for returned content
 * @return	void
 */
function proxi(method, args){
	args.method = method;
	args.hostname = window.location.hostname.replace(/\./g,'|');
	od = document.getElementById(args.display);
	if(od == null) od = false;
	htm = (args.htm != undefined) ? args.htm : false;

	$.ajax({url: baseuri+'locator.php',
			type: 'GET',
			dataType: 'jsonp',			//jsonp required for cross-domain ajax
			jsonp: 'jsoncallback',		//	call will always be asyncronous - function returns bfore ajax is done
			cache: false,
			async: false,
			data: args,
			success: function(data, status){
				if(data.error != undefined){showerror(data.error);}
				else{
					if(od) od.innerHTML = data.stdout;
					if(htm) newdiv(data.stdout);
				}
			}
	});
}



/**
 * create new dom container for content
 * adds a new div after the last div found in the document
 * @param	string	data	the innerHTML to add to the new div
 * @return	void
 */
function newdiv(data){
	s = document.createElement("div");
	s.setAttribute("id", "ams_div_"+$ams_divnum);
	x = document.getElementsByTagName("div");
	x = x[x.length - 1];
	x.appendChild(s);
	document.getElementById("ams_div_"+$ams_divnum).innerHTML = data;
	$ams_divnum ++;
}




/**
 * note: this function is not yet completed and should output errors elegantly
 * @param	string	error	the error output
 * @return	void
 */
function showerror(error){
	alert(error);
}





/********************************************************/
// testing code //

//example: call image function and create new image
function test(){
	s = document.createElement("img");
	s.setAttribute("src", "http://suds.sightdesign.com/_images/photos.php?filename=../photos/261.jpg");
	x = document.getElementsByTagName("div");
	x = x[x.length - 1];
	x.appendChild(s);
}



function processtest(f1, f2, f3){
	tfe_widget('formtest2', {display:'oopy', htm:false, f1:f1, f2:f2, f3:f3})
}


/*

			$.nyroModalManual({
				type: 'iframe',
				content: '<iframe src="'+baseuri+'locator.php?style=modal" frameborder="0" hspace="0" style="height:200px; width:300px; overflow:hidden"></iframe>',
				width:'200',
				height:'200'});

*/


