
var reletive_path = "";
var dev=false;

if(dev)
	reletive_path = "";
else
	reletive_path = "http://www.atlasct.com/helper/";


if (isset("helper_path"))
	reletive_path = helper_path;

function include_dom(script_filename) {
	var html_doc = document.getElementsByTagName('head').item(0);
	var js = document.createElement('script');
	js.setAttribute('language', 'javascript');
	js.setAttribute('type', 'text/javascript');
	js.setAttribute('src', script_filename);
	html_doc.appendChild(js);
	return false;
}

var included_files = new Array();
var included_css_files = new Array();

function include_once(_script_filename) {
	if(empty(_script_filename))
		return;

	var script_filename = "";

	if (strtolower(_script_filename.substring(0,4)) == "http")
		script_filename = _script_filename;
	else
		script_filename = reletive_path + _script_filename;
	if (!in_array(script_filename, included_files)) {
		included_files[included_files.length] = script_filename;
		include_dom(script_filename);
	}
}

function in_array(needle, haystack) {
	for (var i = 0; i < haystack.length; i++) {
		if (haystack[i] == needle) {
			return true;
		}
	}
	return false;

}
function include_css(_css_filename) {

	if(empty(_css_filename))
		return;
	if (in_array(css_filename, included_css_files)) {
		//included_files[included_files.length] = script_filename;
		//include_dom(script_filename);
		return;
	}
	var css_filename = "";
	if (strtolower(_css_filename.substring(0,4)) == "http")
		css_filename = _css_filename;
	else
		css_filename = reletive_path + _css_filename;

	included_css_files[included_css_files.length] = css_filename;

	var headID = document.getElementsByTagName("head")[0];
	var cssNode = document.createElement('link');
	cssNode.type = 'text/css';
	cssNode.rel = 'stylesheet';
	cssNode.href = css_filename;//'FireFox.css';
	cssNode.media = 'screen';
	headID.appendChild(cssNode);
}


function isset(varname)  {
	if(typeof( window[ varname ] ) != "undefined") return true;
	else return false;
}

function is_array(mixed_var){
	var key='';
	if(!mixed_var){
		return false;
	}
	this.php_js=this.php_js||{};
	this.php_js.ini=this.php_js.ini||{};
	if(typeof mixed_var==='object'){
		if(this.php_js.ini['phpjs.objectsAsArrays']&&((this.php_js.ini['phpjs.objectsAsArrays'].local_value.toLowerCase&&this.php_js.ini['phpjs.objectsAsArrays'].local_value.toLowerCase()==='off')||parseInt(this.php_js.ini['phpjs.objectsAsArrays'].local_value,10)===0)){
			return mixed_var.hasOwnProperty('length')&&!mixed_var.propertyIsEnumerable('length')&&mixed_var.constructor.name!=='String';
		}
		if(mixed_var.hasOwnProperty){
			for(key in mixed_var){
				if(false===mixed_var.hasOwnProperty(key)){
					return false;
				}
			}
		}
		return true;
	}
	return false;
}

function is_arr(obj){

	//alert(typeof(obj));	alert(obj.constructor);

	//string
	if(typeof(obj) == "string")
		return false;
	if(typeof(obj) == "function")
		return false;
	if(typeof(obj) != "object")
		return false;

	if(empty(obj))
		return false;
	if(empty(obj.length))
		return false;
	if(obj.length < 2)
		return false;
	else
		return true;

}

function empty(obj){
	//if (obj != null && typeof(obj) != "undefined")
	if (obj == null || obj == "" || typeof(obj) == "undefined")
		return true;
	else
		return false;
}


/**
 *
 *
 * try to eval(str) and check if empty
 *
 */



function ee(str){
	try{
		var obj = eval(str);
		if(obj === 0){
			//alert(str + eval(str));	alert("000000");
			return 0;
		}
		else
			return empty(obj);
	}
	catch(e){
		return true;
	}

}

/**
 *
 *
 *
 * if empty eval(str) returns def value
 * if not returns eval(str)
 *
 */
function eed(str,def){
	//alert(str + eval(str));
	if(ee(str))
		return def;
	else{
		return eval(str);
	}
}

function helper_map(){

	this.map = null


}


function gup( name )
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS , "i");
	//alert(regex);
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
};


function get_obj(obj_id){

	return document.getElementById(obj_id);

}
//document.write (document.location);


include_once("js/lib/utils.js");


include_once("js/on_load.js");
include_once("js/menus.js");
include_once("js/messages.js");
include_once("js/translate.js");
include_once("js/uri.js");
include_once("js/kml.js");
include_once("js/lib/Ajax.js");
include_once("js/lib/saXMLUtils.js");
//include_once("js/lib/mootools-1.2.3-core-yc.js");
include_once("js/address.js");
include_once("js/route.js");
include_once("js/poi.js");

//include_once("/dump.js");
//var scripts = document.getElementsByTagName('script');dmp(scripts,3);
//document.styleSheets


function utf8_encode ( argString ) {
	// http://kevin.vanzonneveld.net
	// +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: sowberry
	// +    tweaked by: Jack
	// +   bugfixed by: Onno Marsman
	// +   improved by: Yves Sucaet
	// +   bugfixed by: Onno Marsman
	// *     example 1: utf8_encode('Kevin van Zonneveld');
	// *     returns 1: 'Kevin van Zonneveld'

	var string = (argString+'').replace(/\r\n/g, "\n").replace(/\r/g, "\n");

	var utftext = "";
	var start, end;
	var stringl = 0;

	start = end = 0;
	stringl = string.length;
	for (var n = 0; n < stringl; n++) {
		var c1 = string.charCodeAt(n);
		var enc = null;

		if (c1 < 128) {
			end++;
		} else if((c1 > 127) && (c1 < 2048)) {
			enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
		} else {
			enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
		}
		if (enc !== null) {
			if (end > start) {
				utftext += string.substring(start, end);
			}
			utftext += enc;
			start = end = n+1;
		}
	}

	if (end > start) {
		utftext += string.substring(start, string.length);
	}

	return utftext;
}
function strtolower( str ) {
	// http://kevin.vanzonneveld.net
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Onno Marsman
	// *     example 1: strtolower('Kevin van Zonneveld');
	// *     returns 1: 'kevin van zonneveld'

	return (str+'').toLowerCase();
}


function getfileextension(filename)
{
	//var fileinput = document.getElementById("foo");
	// if(!fileinput ) return "";
	// var filename = fileinput.value;
	if( filename.length == 0 ) return "";
	var dot = filename.lastIndexOf(".");
	if( dot == -1 ) return "";
	var extension = filename.substr(dot+1,filename.length);
	return extension;
}




function _singleton_(){

	this.instance = null;
	this.getInstance = function() {
		if (this.instance == null) {
			this.instance = new _singleton_();
		}

		return this.instance;
	}

}

function helper(){


	this.lang = "he";

	this.map = null;

	this.route = null;
	/*
	this.is_empty = function(val){


		if ((val=="") || (val=="0") || (val==null) || (val=='undefined'))
		{
			return true;
		}
		else
		{
			return false;
		}
	}


	this.gup = function( name )
	{
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS , "i");
		//alert(regex);
		var results = regex.exec( window.location.href );
		if( results == null )
			return "";
		else
			return results[1];
	}
	*/



	this.set_map = function(map){
		this.map = map;
	}
	this.get_map = function(){
		return this.map;
	}

	this.set_route = function(route){
		this.route = route;
	}
	this.get_route = function(){
		return this.route;
	}


	this.map_error = function(message){
		alert(message);
	}


	this.writeMap = function(){

		document.write("<div><div><span id='map_wrapper'><span id='map_overlays'></span><div id='main_map'>");
		this.map.writeMap();
		document.write("</div><div id='messages_layer'></div></span></div><div id=map_rights></div></div><div id=debug></div>");



	}

	this.get_lang = function(){
		return translate.getInstance().get_lang();
	}
	this.set_lang = function(lang){
		return translate.getInstance().set_lang(lang);
	}

	this.translate = function(txt){
		
		return translate.getInstance().get(txt);

	}

	this.get_rights = function(){

		var map = this.get_map();
		var rights;
		var width = map.width;
		if(translate.getInst().isHebrew()){
			rights = '<table  class="center_" id=rights border="0" dir=rtl cellpadding="0" cellspacing="0" Width="'+width+'">'+
			'<tr>'+
			'<td width="10"></td>'+
			'<td id="tdLnk2" class="textD" height="21" width="64" style="display_:none;">&copy;&nbsp;<a href=\'http://www.gisrael.co.il\' class=\'links\' target="_blank" style="color:#5b8bd5">&#1502;&#1508;&#1492;</a> &#1489;&#1506;"&#1502;. </td>'+

			'<Td style="width:2px"></Td>'+
			'<td id="tdLnk1" class="textD" width="91" style="display_:none;">&copy;&nbsp;<a href="http://www.atlasct.co.il" id="lnkAtlas" class="links" target="_blank" style="color:#5b8bd5">&#8207;&#1488;&#1496;&#1500;&#1505;.&#1505;&#1497;.&#1496;&#1497;</a> &#1489;&#1506;"&#1502;. &nbsp; </td>'+
			'<td id="space" width="'+(width-370)+'" style="xdisplay:none"></td>'+

			'<Td style="width:2px"></Td>'+
			'<td align="left" class="textD" width="23"><a href="http://www.atlasct.co.il/legend/" onclick="return openTermsOfUseWindow(this, \'Legend\',220,480,\'\')" class="links" style="color:#5b8bd5">&#8207;&#1502;&#1511;&#1512;&#1488;</a></td>'+
			'<td style="width:10px" align="center" valign="middle" style="font-size:8px;vertical-align:middle;margin:auto;padding-top:5px;"><div style="width:1px;height:10px;background-color:black;margin-top:5px;"></div></Td>'+
			'<td align="right" style="xpadding-left:3px" class="textD" width="100"><a href="http://www.atlasct.co.il/TermsOfUse.html" id="lnkTermsOfUse" class="links" onClick="return openTermsOfUseWindow(this, \'TermsOfUse\',800,480,\'scrollbars=yes,\')" style="color:#5b8bd5">&#1514;&#1504;&#1488;&#1497; &#1513;&#1497;&#1502;&#1493;&#1513; - &#1502;&#1508;&#1493;&#1514;</a></td>'+
			'<td id="space" width="1" style="xdisplay:none"></td>'+


			'<td width="10"></td>'+
			'</tr>'+
			'</table>';
		}
		else{
			rights = '<table  class="center_" id=rights border="0" dir=ltr cellpadding="0" cellspacing="0" Width="'+width+'">'+
			'<tr>'+
			'<td width="10"></td>'+
			'<td id="tdLnk2" class="textD" height="21" width="64">&copy; <a href=\'http://www.gisrael.co.il/eng/\' class=\'links\' target="_blank" style="color:#5b8bd5">Mapa</a> Ltd.&nbsp </td>'+
			'<Td style="width:2px"></Td>'+
			'<td id="tdLnk1" class="textD" width="91">&copy; <a href="http://www.atlasct.com" id="lnkAtlas" class="links" target="_blank" style="color:#5b8bd5">AtlasCT</a> Ltd.&nbsp </td>'+
			'<td id="space" width="'+(width-370)+'" style="xdisplay:none"></td>'+
			'<Td style="width:2px"></Td>'+
			'<td align="right" class="textD" width="17" style="color:#5b8bd5;border:#000 0px solid;padding:0px;text-align:right!important;"><a href="http://www.atlasct.com/legend/" onclick="return openTermsOfUseWindow(this, \'Legend\',220,480,\'\')" class="linksss" style="color:#5b8bd5;border:#000 0px solid;float:right;margin:0px;">Legend</a></td>'+
			'<td style="width:10px" align="center" valign="middle" style="font-size:8px;" height="5px"><div style="width:1px;height:10px;background-color:black;margin-top:5px;"></div></Td>'+
			'<td align="left" style_="__xpadding-leftd:3px;border:#000 0px solid;" class="textD" width="120px" style="padding-left:0px!important;padding-left:6px;"><a href="http://www.atlasct.co.il/TermsOfUse.html" id="lnkTermsOfUse" class="links" onClick="return openTermsOfUseWindow(this, \'TermsOfUse\',800,480,\'scrollbars=yes,\')" style="color:#5b8bd5;">Maps - terms of use</a></td>'+
			'<td id="space" width="1" style="xdisplay:none"></td>'+
			'<td width="10"></td>'+
			'</tr>'+
			'</table>';
		}

		return rights;


	}

	//encodeURI(URI)
	//decodeURI(encodedURI)
	this.menus_start = false;
	
	this.menus = function(type,skin){
		/*
		var map_settings = null;
		if(isset("atlasmap"))
			map_settings = atlasmap;
		*/
		//alert(2);
		if(this.menus_start)
			return;
		this.menus_start = true;

		var x,y,sp,nav_type;
		var drag_x,drag_y;

		nav_type=eed('atlasmap["nav_bar"]["type"]',1)
		var uri_inst = uri.getInst();
		var map = this.get_map();
		var trans = translate.getInst();
		var isHeb = trans.isHebrew();

		var menu_inst = address_menu.getInst();
		var address_inst = address_menu.getInst();
		var route_menu_inst = route_menu.getInst();
		menu_inst.sla = true;

		if(isHeb){
			x = eed('atlasmap["nav_bar"]["x"]',0);
			y = eed('atlasmap["nav_bar"]["y"]',5);
			drag_x = eed('atlasmap["drag_bar"]["x"]',80);
			drag_y = eed('atlasmap["drag_bar"]["y"]',30);

		}
		else{
			x = eed('atlasmap["nav_bar"]["x"]',map.width-80);
			y = eed('atlasmap["nav_bar"]["y"]',5);
			drag_x = eed('atlasmap["drag_bar"]["x"]',map.width-140);
			drag_y = eed('atlasmap["drag_bar"]["y"]',30);
		}
		sp = new ScreenPoint(x,y);
		switch(nav_type+""){

			case "1":

				map.addAdvancedNavigationBar(sp);
				break
			case "2":map.addDefaultNavigationBar(sp);
				break
			default:
		alert(nav_type);

				break;
		}
		if(!ee('atlasmap["drag_bar"]')){
			map.addDragBar(new ScreenPoint(drag_x,drag_y));

		}


		var address_sla = false;
		if(isset("helpers_address_sla"))
			address_sla=helpers_address_sla;
		else{
			if (uri_inst.get("sla"))
				address_sla = true;
			else
				address_sla = false;
		}

		if(isset("atlasmap_menu_on_map"))
			menu_inst.on_map = atlasmap_menu_on_map;//true;
		menu_inst.collapse_button = menu_inst.on_map;
		if(isset("atlasmap_collapse_button"))
			menu_inst.collapse_button = atlasmap_collapse_button;//true;
		

		address_sla = true;

		menu_inst.sla = address_sla;
		if(isset("atlasmap_address"))
			address_inst.add_address_search(atlasmap_address);
		if(isset("atlasmap_route"))
			route_menu_inst.add_address_search(atlasmap_route);

		if(!empty(uri_inst.get("more")))
			menu_inst.add_menu_item_html("<div><img src='http://www.abmaps.co.il/Images/Top/trips_off_over.he.png' />"+
				"<img src='http://www.abmaps.co.il/Images/Top/maps_on.he.png' /></div>"+
				"<img src='http://www.abmaps.co.il/Images/Top/friends_off_over.he.png' /></div>");


	if(isset("helper_logo_location"))
		if(!empty(helper_logo_location))
			add_logo(helper_logo_location,helper_logo_pos_x,helper_logo_pos_y,helper_logo_link,helper_logo_title);


	if(isset("helper_target_icon"))
		if(!empty(helper_target_icon))
			home.getInst().change_icon(helper_target_icon);
	//var icon_url = "images/ajax-loader_circle.gif";
	get_obj("map_rights").innerHTML = help_inst.get_rights();
	if(!ee('atlasmap_poi'))
		poi.getInst().add(atlasmap_poi);
	//map.addAdvancedNavigationBar(new ScreenPoint(10,0));


	// skin: blue
	// types: basic addvanced



	}



}




/* singleton */
helper.instance = null;
helper.getInst = function() {
	if (helper.instance == null) {
		helper.instance = new helper();
	}

	return helper.instance;
}
helper.getInstance = helper.getInst;
/* singleton */

helper.start = function(__map){

	var width=700;
	var height=400;
	var map;

	//alert(menus.getInst().menu_width);

	if(isset("atlasmap")){

		if(!empty(atlasmap["width"])){
			width = parseInt(atlasmap["width"]);
		}

		if(!empty(atlasmap["height"])){
			height = parseInt(atlasmap["height"]);
		}
		if(!empty(atlasmap["menu_width"])){
			menus.getInst().menu_width = parseInt(atlasmap["menu_width"]);
		}
	}

	//alert(5);return;
	var help_inst = this.getInst();


	if(empty(__map)){
		if (isset("helper_map_width"))
			width = helper_map_width;
		if (isset("helper_map_height"))
			height = helper_map_height;
		map = new Map(width,height);
		help_inst.set_map(map);
	}
	else
		map = __map;

	var uri_css = uri.getInst().get("css")
	if(!empty(uri_css))
		include_css(reletive_path+uri_css);
	else
	{
		if(isset("helper_css_link"))
			include_css(helper_css_link);

		if(isset("atlasmap_css"))
			include_css(atlasmap_css);

	}


	include_css("css/gen.css");
	//	return;
	help_inst.writeMap();


				//alert(this.map)
	if(isset("atlasmap_target_icon"))
		if(!empty(atlasmap_target_icon)){
			if(!empty(atlasmap_target_icon["url"])){

				var x_offset = -(parseInt(atlasmap_target_icon["width"])/2);
				var y_offset = -(parseInt(atlasmap_target_icon["height"])/2);
				//alert(3);


				home.getInst().change_icon(atlasmap_target_icon["url"],x_offset,y_offset);

			}

		}

	//return;


	//help_inst
	//alert(this.getInst());
	var uri_inst = uri.getInst();

	/**/
	var lang = "eng";
		
		


	if (isset("atlasmap")){

		if(!empty(atlasmap["lang"])){
			lang = atlasmap["lang"];
		}
	}
	if (!empty(uri.getInst().get("lang")))
		lang = uri.getInst().get("lang");
	/**/
	

	var uri_lang = uri_inst.get("lang");



	if(empty(uri_lang))
		uri_lang = lang;//"heb";

	uri_lang = lang;//"heb";

	translate.getInstance().set_lang(uri_lang);
	this.getInstance().set_map(map);
	var route = this.getInst().get_route();
	if(empty(route)){
		route = new Route();
		this.getInst().set_route(route);
	}

	//alert(this.getInst().get_route());

	map.addListener(Flash.EVENT_FLASH_INITIALIZE,helper.onmapload,map);
	map.addListener(Map.EVENT_GEO_RESULT,helper.ongeocode,map);
	route.addListener(Route.EVENT_ROUTE_RESULT,helper.onroute,route);


	if(isset("helper_weather"))
		if(!empty(helper_weather)){
			map.addListener(Map.EVENT_MAP_SMOOTH_ZOOM,add_weather_js,map);
			map.addListener(Map.EVENT_MAP_DRAG_END,add_drag_weather,map);
		}

//map.addListener(Map.EVENT_GEO_RESULT,helper.onroute,map);
//map.addListener(Flash.EVENT_FLASH_INITIALIZE,helper.onmapload,map);

	
}

helper.ongeocode = function(map,geo){

	//menus.getInstance().ongeocode(map,geo);
	address_menu.getInstance().ongeocode(map,geo);
	//alert(3);

	return;
	on_load.getInstance().apply_uri();
	//menus.getInstance().apply_menus();
	//alert(6);
	return;
	var map = this.map;
	map.addListener(Flash.EVENT_FLASH_INITIALIZE,event,map);
}


helper.onmapload = function(event){
	help_inst = helper.getInst();
	var uri_inst = uri.getInst();

	on_load.getInstance().apply_uri();
	//menus.getInstance().apply_menus();
	//alert(6);
	if (!empty(uri_inst.get("kml")))
		kml.getInst().load(uri_inst.get("kml"));

	help_inst.menus(1,"blue");

	return;
	var map = this.map;
	map.addListener(Flash.EVENT_FLASH_INITIALIZE,event,map);
}
helper.onroute = function(route,r){
	route_menu.getInst().onroute(route);
}

helper.apply_uri = function(){

	}

//var key = "__qa_cache1_il__";
var key = "";//"__qa_cache1_il__";
//atlasmap_key
if (isset("atlasmap_key"))
	key = atlasmap_key;
//alert(key);

if (isset("helper_map_key"))
	key = helper_map_key;
if(!empty(gup("map_key")))
	key = gup("map_key");
//alert(key);

var lang,param_lang;

lang = eed("atlasmap['lang']","");

if(empty(lang))
	param_lang = "";
else
	param_lang = "&GuiLang="+lang;

//alert(eed("atlasmap['lang']",""));
document.write("<script type=\"text/javascript\" src=\"http://api.atlasct.co.il/sdk_v3_2/?key="+key+param_lang+"\"></script>");
//alert("text/javascript\" src=\"http://api.atlasct.co.il/sdk_v3_2/?key="+key+param_lang);
//helper.start();
