// JavaScript Document


var config = {};
//var cicode;
//var _map;
//var name;
//var	type;
//var	address;
//var	phone;
//var	postcode;
//var email;
var _key;
var _country;

function removeOverlay()
{
	swf = getFlashMovie( "my_flash" );
	swf.closeOverlay();
}


function closeThickbox()
{
	TB_remove();
	removeOverlay();
}


function recordSets()
{
	this.totalRecordCount = 0;
	this.records = new Array();
}

function distance()
{
	this.km = 0;
	this.miles = 0;
}

function point()
{
	this.lat = 0;
	this.lon = 0;
}

function error( message )
{
	this.message = message;	
}

function record()
{
	this.market = "";
	this.street = "";
	this.tradingTitle = "";
	this.geoQuality = "";
	this.town = "";
	this.brand = "";
	this.pc = "";
	this.email = "";
	this.addressLine2 = "";
	this.fax = "";
	this.addressLine3 = "";
	this.cicode = "";
	this.url = "";
	this.id = "";
	this.country = "";
	this.dealerName = "";
	this.bodyshop = "";
	this.clientID = "";
	this.partsOnly = "";
	this.phone = "";
	this.authorisedRepairer = "";
	this.primeSiteFlag = "";
	this.pagNumber = "";
	this.geoCodeSource = "";
	this.geoCodeDate ="";
	this.counter = 0;
	this.county = "";
	this.dealer = "";
	this.distance = new distance();
	this.point = new point();
}

var recordSetsFactory = {
	
	parseJson : function( jsonObj )
	{
		var key = "mm.clients." + _key;
		var country = "mm.clients." + _country;
		
		var rootObj = jsonObj.record_sets[ key ];
		var sets = new recordSets();

		sets.totalRecordCount = rootObj.total_record_count;
		if( rootObj.records )
		{
			for(var i = 0; i < rootObj.records.length; i++ )
			{
				sets.records.push(parseJsonRecord(rootObj.records[ i ] ) );
			}
		}

		return sets;
	}
}

function parseJsonRecord( jsonRecord )
{
	var rec = new record();

	rec.market = jsonRecord.market;
	rec.street = jsonRecord.street;
	rec.tradingTitle = jsonRecord.trading_title;
	rec.geoQuality = jsonRecord.GeoQuality;
	rec.town = jsonRecord.town;
	rec.brand = jsonRecord.brand;
	rec.postCode = jsonRecord.pc;
	rec.email = jsonRecord.email;
	rec.addressLine2 = jsonRecord.address_line2;
	rec.fax = jsonRecord.fax;
	rec.addressLine3 = jsonRecord.address_line3;
	rec.cicode = jsonRecord.cicode;
	rec.url = jsonRecord.url;
	rec.id = jsonRecord.id;
	rec.country = jsonRecord.country;
	rec.dealerName = jsonRecord.dealer_name;
	rec.bodyshop = jsonRecord.bodyshop;
	rec.clientID = jsonRecord.client_id;
	rec.partsOnly = jsonRecord.parts_only;
	rec.phone = jsonRecord.phone;
	rec.authorisedRepairer = jsonRecord.authorised_repairer;
	rec.primeSiteFlag = jsonRecord.prime_site_flag;
	rec.pagNumber = jsonRecord.pag_number;
	rec.geoCodeSource = jsonRecord.GeocodeSource;
	rec.geoCodeDate = jsonRecord.GeocodeDate;
	rec.counter = jsonRecord.counter;
	rec.county = jsonRecord.county;
	rec.dealer = jsonRecord.dealer;
	if( jsonRecord.distance ) rec.distance = parseDistance( jsonRecord.distance );
	rec.point = parsePoint( jsonRecord.point );

	return rec;
}

function parseDistance( jsonRecord )
{
	var dist = new distance();
	dist.km = jsonRecord.km;
	dist.miles = jsonRecord.miles;
	
	return dist;
}

function parsePoint( jsonRecord ) 
{
	var poi = new point();
	poi.lat = jsonRecord.lat;
	poi.lon = jsonRecord.lon;
}


function getDealer( cicode )
{
	_key = "jlr_test_api12";
	
	$.ajax({ 
    	//url: "http://clients.multimap.com/API/search/1.2/" + key + "/?fieldname_1=cicode&value_1=" + cicode + "&output=json",
		url: "http://clients.multimap.com/API/search/1.2/jlr_test_api12/?fieldname_1=cicode&value_1=" + cicode + "&output=json",
		type: "GET",
    	dataType: "jsonp",
    	success: function onSuccess(msg)
	 	{
			swf = getFlashMovie( "my_flash" );
			if( msg.errors && msg.errors.length )
			{
				swf.setDealer( new error( msg ) );
			}
			else
			{
       			var recordSets = recordSetsFactory.parseJson( msg );
				swf.setDealer( recordSets );
     		 }
		}
	});
}


function getDealers( key, params, country )
{
	_key = key;
	if( country ) _country = country;
	else _country = _key;
	
	$.ajax({
    	url: "http://clients.multimap.com/API/search/1.2/" + key + "/?" + params + "&output=json",
		type: "GET",
    	dataType: "jsonp",
    	success: function onSuccess(msg)
	 	{
			swf = getFlashMovie( "my_flash" );
		
			if( msg.errors && msg.errors.length )
			{
				swf.setDealers( new error( msg ) );
			}
			else
			{
       			 var recordSets = recordSetsFactory.parseJson(msg);
		
				swf.setDealers( recordSets );
     		 }
		}
	});
}


function getFlashMovie( movieName )
{
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[ movieName ] : document[ movieName ];
}

//TODO move this to window.js
function popup( url, width, height )
{
	var top= ( screen.height / 2 ) - ( height / 2 );
	var left= ( screen.width / 2 ) - ( width / 2 );
	newwindow=window.open( url ,"", "scrollbars=yes", " + height=" + height + ",width=" + width + ", top=" + top + ", left=" + left );
	if( !newwindow )
	{
		newwindow = window.open('','',  " + height=" + height + ",width=" + width + ", top=" + top + ", left=" + left );
		newwindow.location.href = url;
	}
}

function multiMap( cic, dealerName, dealerType, dealerAddress, dealerPostCode, dealerPhone, email, url, key )
{
	config.cicode = cic;
	config.name = dealerName;
	config.type = dealerType;
	config.address = dealerAddress;
	config.postcode = dealerPostCode;
	config.phone = dealerPhone;
	config.email = email;
	config.url = url;
	config.key = key;
	
	//local
	//var thickbox = TB_show( null, "multimap/loader_frame_" + key +  ".html?TB_iframe=true&amp;height=429&amp;width=804", false, this );
	//remote
	var thickbox = TB_show( null, "/multimap/loader_frame.html?key=" + key + "&amp;TB_iframe=true&amp;height=429&amp;width=804", false, this );
}

function registerMap( map )
{
	_map = map;
}
