//--------------------------------------------------------Global---------------------------------------------------------
//--------------------------------------------------------Global---------------------------------------------------------

var YNET_HOST_DOMAIN = "ynet1.emap.co.il";
var YNET_INFO_URL = "http://go.ynet.co.il/long/content/xml/map_window.asp";

//document.write('<script language="javascript" type="text/javascript" src="http://' + YNET_HOST_DOMAIN + '/Ynet/javascript/suffix.js"></script>');
document.write('<script language="javascript" type="text/javascript" src="http://' + YNET_HOST_DOMAIN + '/emaplight/javascript/clientAPI.js"></script>');
//document.write('<script language="javascript" type="text/javascript" src="http://kerenc-xp/emaplight1/javascript/clientAPI.js"></script>');


//var YNET_HOST_DOMAIN = "http://kerenc-xp/";
//var YNET_HOST_DOMAIN = "http://www.keren.co.il/";
//var YNET_HOST_DOMAIN = "http://www4.emap.co.il/b";
//var YNET_HOST_DOMAIN = "ynet1.emap.co.il";
//var YNET_HOST_DOMAIN = "212.150.51.246";


var YNET_2003_SERVICE = "ynet2003";
var YNET_2006_SERVICE = "ynet2006";
var YNET_2009_SERVICE = "ynet2009";

var isIE;
var map;

if(navigator.userAgent.indexOf("Gecko") != -1){
    isIE = false;
}else{
    isIE = true;
}

function YnetAPI(containerID){
    if(YnetAPI.instance != null)
        return YnetAPI.instance;
    YnetAPI.instance = this;
    this.Type = "YnetAPI";
    this.Message = "";
    this.MessageIndex  = 0;
    EMAP_HOST_DOMAIN = "http://ynet1.emap.co.il/";
    this.my_eMapAPI=new emapAPI();
    this.CitiesOn = false;
    this.level = 0;
    this.width = 0;
    this.height = 0;
    this.idHTMLstart = YNET_INFO_URL + "?t=";
    this.idHTMLFields = "<IT>&Agas={citysta}&city={semel}&Nafa={NafaCode}&year=|{NafaName}|{CityName}|{agas}</IT>";

    if(containerID!=null){
        this.map = this.my_eMapAPI.AddElement("Map",containerID);
        this.map.LevelChangeListener = this.handleLevel;
        this.map.SetExtentListenerSensitivity(1);
        this.map.ExtentChangeListener = handleChangeExtent;
        this.map.CenterAt(207000,595500);
        this.map.SetActiveTool('MapPan','DragImage', 'hand');
        this.map.ShowToolBar();
        this.addMainCities();
        this.map.SetDblClickAction('doCenterAndZoom');
        this.map.SetClickAction('doIdentify|common_map_symbol|identify|' + this.idHTMLFields);
        this.map.DefaultListener = handleMessage;

        this.CitiesOn = true;
        //this.service = "ynet" + suffix;
        this.service = YNET_2003_SERVICE;

    }
}

YnetAPI.instance == null;
YnetAPI.prototype.handleLevel = function(x){
    api = YnetAPI.instance;
    if (x != api.level) {
        api.level = x;
        closeCallOut();
        if(x==0){
            api.map.CenterAt(207000,595500);
        }
        api.map.SetActiveTool('MapPan','DragImage', 'hand');
    }
    
    if(x==4)
    	api.map.SetActiveTool('MapPan','DragImage', 'hand');
    	
    if (x==0 && !api.CitiesOn) {
        api.map.ShowGraphics();
        api.CitiesOn = true;
//        api.map.SetClickAction("");        
    }
    else if (x!=0 && api.CitiesOn) {
        api.map.HideGraphics();
        api.CitiesOn = false;
//        api.map.SetClickAction('doIdentify|common_map_symbol|stat|' + api.idHTMLFields);
    }
    api.map.RefreshMap();
}

YnetAPI.prototype.ChangeStyle = function(name,level,w,h)
{
    //var service = "ynet" + suffix;
    if (name!=null) {
       if (name == '2003') {
            this.service = YNET_2003_SERVICE;
       }
       else if (name == '2006') {
            this.service = YNET_2006_SERVICE;
       }
       else {
            //this.service = "ynet" + suffix;
            this.service = YNET_2009_SERVICE;
       }
       closeCallOut();
    }

    this.map.ChangeStyle(this.service,true);

    if (level!=null) {
	       this.map.SetLevel(level);
	    }
	    if (w!=null && h !=null) {
	        this.width = w;
	        this.height = h;
	        this.map.ResizeMap(w,h);
	    }
    this.map.RefreshMap();
}

YnetAPI.prototype.RefreshMap = function()
{
    this.map.RefreshMap();
}

YnetAPI.prototype.addMainCities = function() {
    this.map.Features.SetIconsVDir("http://www4.emap.co.il/emaplight/images/");

    this.map.Features.Add('221400','631700',3000,'', this.idHTMLstart + '2&Agas={CITY_STA}&city=3000&Nafa={Nafa}&year=*ירושלים',null,'blank.gif');
	this.map.Features.Add('177653','665500',5000,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=5000&Nafa={Nafa}&year=*תל אביב יפו',null,'blank.gif');
	this.map.Features.Add('200200','746400',4000,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=4000&Nafa={Nafa}&year=*חיפה',null,'blank.gif');
	this.map.Features.Add('181600','652400',8300,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=8300&Nafa={Nafa}&year=*ראשון לציון',null,'blank.gif');
	this.map.Features.Add('166100','634400',70,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=70&Nafa={Nafa}&year=*אשדוד',null,'blank.gif');
	this.map.Features.Add('180900','573200',9000,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=9000&Nafa={Nafa}&year=*באר שבע',null,'blank.gif');
	this.map.Features.Add('186700','692800',7400,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=7400&Nafa={Nafa}&year=*נתניה',null,'blank.gif');
	this.map.Features.Add('186446','666000',6100,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=6100&Nafa={Nafa}&year=*בני ברק',null,'blank.gif');
	this.map.Features.Add('158700','619400',7100,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=7100&Nafa={Nafa}&year=*אשקלון',null,'blank.gif');
	this.map.Features.Add('192100','704600',6500,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=6500&Nafa={Nafa}&year=*חדרה',null,'blank.gif');
	this.map.Features.Add('228000','734000',7300,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=7300&Nafa={Nafa}&year=*נצרת',null,'blank.gif');
	this.map.Features.Add('202000','644800',1200,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=1200&Nafa={Nafa}&year=*מודיעין-מכבים-רעות',null,'blank.gif');
	this.map.Features.Add('209500','768000',9100,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=9100&Nafa={Nafa}&year=*נהריה',null,'blank.gif');
	this.map.Features.Add('194900','385300',2600,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=2600&Nafa={Nafa}&year=*אילת',null,'blank.gif');
	this.map.Features.Add('207000','758900',7600,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=7600&Nafa={Nafa}&year=*עכו',null,'blank.gif');
	this.map.Features.Add('214500','713600',2710,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=2710&Nafa={Nafa}&year=*אום אל-פחם',null,'blank.gif');
	this.map.Features.Add('250800','744200',6700,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=6700&Nafa={Nafa}&year=*טבריה',null,'blank.gif');
	this.map.Features.Add('203000','552700',2200,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=2200&Nafa={Nafa}&year=*דימונה',null,'blank.gif');
	this.map.Features.Add('253500','790500',2800,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=2800&Nafa={Nafa}&year=*קרית שמונה',null,'blank.gif');
	this.map.Features.Add('162100','603700',1031,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=1031&Nafa={Nafa}&year=*שדרות',null,'blank.gif');
	this.map.Features.Add('216300','667900',3570,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=3570&Nafa={Nafa}&year=*אריאל',null,'blank.gif');
	this.map.Features.Add('264700','766500',4100,'',this.idHTMLstart + '2&Agas={CITY_STA}&city=4100&Nafa={Nafa}&year=*קצרין',null,'blank.gif');

    this.map.ReturnIDs();
    this.map.ShowTitles();
    this.map.SetTipAction('onmouseover');
    this.map.FeaturesListener=showCallout;
}

YnetAPI.prototype.ZoomToXY = function(x, y, level)
{
    if (x != null && y != null) {
        this.map.CenterAt(x , y);
    }

    if (level != null) {
        this.map.SetLevel(level);
    }
    this.map.RefreshMap();
}

function showCallout(feature) {
    closeCallOut();
    api = YnetAPI.instance;
    var isCityMouseOver = (feature.id.substr(0,1) != "&");
    var identifyType;
	var featureArray;
    var header;
    if(!isCityMouseOver){
    	featureArray = feature.id.split('|');
    	switch(api.level.toString())
    	{
    	    case "0":
    	        identifyType = 1;
    	        header = featureArray[identifyType];
    	        break;
    	    case "1":
    	    case "2":
    	    case "3":
    	    	identifyType = 2;
    	    	header = featureArray[identifyType];
    	        break;
    	    
    	    case "4":
    	    	identifyType = 3;
    	    	if(featureArray[identifyType] == "<Null>")
			return;
    	    	header = featureArray[2] + "-אזור "  + featureArray[identifyType];
    	        break;
    	}

    }
    else
    {
    	identifyType = 2;
    	featureArray = feature.id.split('*');
    	header = featureArray[1];
    }

	if(header == "<Null>")
		return;

    mapRect = calcElementPosition(my_YnetAPI.map.Container.id);
    inMapPosY = feature.top ;
    inMapPosX = feature.left ;
    Ypos = "B";

    tipWidth = 187;
    tipHeight = 174;
    tipYmargin = 15;
    yConst = 4;
    xConst = 5;

    Yshift = tipYmargin + yConst;
    if (inMapPosY < (api.height/2)){
        Ypos = "T";
        Yshift = tipHeight - tipYmargin + yConst;;
    }
    Xpos = "R";

    Xshift = tipWidth - xConst;
    if (inMapPosX < (api.width/2)){
        Xpos = "L";
        Xshift = 0 - xConst + mapRect.left;

    }

    var tips = document.getElementById("callOuts" + Ypos + Xpos);
    //tips.innerHTML = header + tips.innerHTML;
    tips.style.top = feature.top + mapRect.top - tipHeight + Yshift;
    tips.style.left = feature.left + mapRect.left - Xshift;
    var tipsframe = document.getElementById("CallOutWindow" + Ypos + Xpos);
    var tipsheader = document.getElementById("header" + Ypos + Xpos);
    tipsheader.innerHTML = header;
    if (feature.id.length > 0) {
        if (!isCityMouseOver) {
            api = YnetAPI.instance;
            tipsframe.src = api.idHTMLstart + identifyType + featureArray[0];
        }
        else {
            if(api.level!=0)
                return;
            tipsframe.src = featureArray[0];
        }

        if (api.service == YNET_2006_SERVICE) {
            tipsframe.src += '6';
        }
        else if (api.service == YNET_2003_SERVICE) {
            tipsframe.src += '3';
        }
        else {
            tipsframe.src += '9';
        }

    }
    
    tips.style.zIndex = 1000
    tips.style.display = "block";
    //tipsRect = calcElementPosition("callOuts" + Ypos + Xpos);

	my_YnetAPI.map.Container.onmousemove = alert;

}

function closeCallOut(){
    var tips = document.getElementById("callOutsBL");
    tips.style.display = "none";
    tipsframe = document.getElementById("CallOutWindowBL");
    tipsframe.src = "http://ynet1.emap.co.il/ynet/blank.htm";
    var tips = document.getElementById("callOutsBR");
    tips.style.display = "none";
    tipsframe = document.getElementById("CallOutWindowBR");
    tipsframe.src = "http://ynet1.emap.co.il/ynet/blank.htm";
    var tips = document.getElementById("callOutsTL");
    tips.style.display = "none";
    tipsframe = document.getElementById("CallOutWindowTL");
    tipsframe.src = "http://ynet1.emap.co.il/ynet/blank.htm";
    var tips = document.getElementById("callOutsTR");
    tips.style.display = "none";
    tipsframe = document.getElementById("CallOutWindowTR");
    tipsframe.src = "http://ynet1.emap.co.il/ynet/blank.htm";
}

function handleChangeExtent() {
//    alert("changeExtent!");
    closeCallOut();
}
//--------------------------------------------------------Write Callout div---------------------------------------------------------
//--------------------------------------------------------Write Callout div---------------------------------------------------------
var callOutDivBL="";
    callOutDivBL += '<div dir=ltr id="callOutsBL" style="display:none;position:absolute;background-image:url(http://ynet1.emap.co.il/ynet/images/map_info_bl.gif);width:187;height:174;">';
    callOutDivBL += '<div dir=rtl style="position:absolute;top:5;left:19;width:165;font-family:arial;font-size:12px;font-weight:bold" align="right" style="margin:0 0 0 0;line-height:80%;" >&nbsp;&nbsp; <span id="headerBL" style="margin:0 0 0 0" > </span><img  src="images/close.gif" width="11" height="11" style="position:absolute;top:0;left:3" onclick="this.parentNode.parentNode.style.display=\'none\'" style="cursor:hand" /><hr color="#c5c5c5" align="center" size="1" style="margin:0 5 0 5"></div>'
    callOutDivBL += '<iframe scrolling="no"  id=CallOutWindowBL frameborder="0" width="165px" height="145px" style="position:relative;top:24;left:19;overflow:hidden" src="http://ynet1.emap.co.il/ynet/blank.htm" onmouseout="this.parentNode.style.display=\'none\'"></iframe>';
    callOutDivBL += '</div>';

    document.write(callOutDivBL);

var callOutDivBR="";
    callOutDivBR += '<div dir=ltr id="callOutsBR" style="display:none;position:absolute;background-image:url(http://ynet1.emap.co.il/ynet/images/map_info_BR.gif);width:187;height:174;">';
    callOutDivBR += '<div dir=rtl style="position:absolute;top:5;left:4;width:165;font-family:arial;font-size:12px;font-weight:bold" align="right" style="margin:0 0 0 0;line-height:80%;" >&nbsp;&nbsp; <span id="headerBR" style="margin:0 0 0 0" > </span><img  src="images/close.gif" width="11" height="11" style="position:absolute;top:0;left:3" onclick="this.parentNode.parentNode.style.display=\'none\'" style="cursor:hand" /><hr color="#c5c5c5" align="center" size="1" style="margin:0 5 0 5"></div>'
    callOutDivBR += '<iframe scrolling="no"  id=CallOutWindowBR frameborder="0" width="165px" height="145px" style="position:relative;top:24;left:4;overflow:hidden" src="http://ynet1.emap.co.il/ynet/blank.htm" onmouseout="this.parentNode.style.display=\'none\'"></iframe>';
    callOutDivBR += '</div>';


    document.write(callOutDivBR);

var callOutDivTL="";
    callOutDivTL += '<div dir=ltr id="callOutsTL" style="display:none;position:absolute;background-image:url(http://ynet1.emap.co.il/ynet/images/map_info_tl.gif);width:187;height:174;">';
    callOutDivTL += '<div dir=rtl style="position:absolute;top:5;left:19;width:165;font-family:arial;font-size:12px;font-weight:bold" align="right" style="margin:0 0 0 0;line-height:80%;" >&nbsp;&nbsp; <span id="headerTL" style="margin:0 0 0 0" > </span><img  src="images/close.gif" width="11" height="11" style="position:absolute;top:0;left:3" onclick="this.parentNode.parentNode.style.display=\'none\'" style="cursor:hand" /><hr color="#c5c5c5" align="center" size="1" style="margin:0 5 0 5"></div>'
    callOutDivTL += '<iframe scrolling="no"  id=CallOutWindowTL frameborder="0" width="165px" height="145px" style="position:relative;top:24;left:19;overflow:hidden" src="http://ynet1.emap.co.il/ynet/blank.htm" onmouseout="this.parentNode.style.display=\'none\'"></iframe>';
    callOutDivTL += '</div>';


    document.write(callOutDivTL);

var callOutDivTR="";
    callOutDivTR += '<div dir=ltr id="callOutsTR" style="display:none;position:absolute;background-image:url(http://ynet1.emap.co.il/ynet/images/map_info_tr.gif);width:187;height:174;">';
    callOutDivTR += '<div dir=rtl style="position:absolute;top:5;left:4;width:165;font-family:arial;font-size:12px;font-weight:bold" align="right" style="margin:0 0 0 0;line-height:80%;" >&nbsp;&nbsp; <span id="headerTR" style="margin:0 0 0 0" > </span><img  src="images/close.gif" width="11" height="11" style="position:absolute;top:0;left:3" onclick="this.parentNode.parentNode.style.display=\'none\'" style="cursor:hand" /><hr color="#c5c5c5" align="center" size="1" style="margin:0 5 0 5"></div>'
    callOutDivTR += '<iframe scrolling="no" id=CallOutWindowTR frameborder="0" width="165px" height="145px" style="position:relative;top:24;left:4;overflow:hidden" src="http://ynet1.emap.co.il/ynet/blank.htm" onmouseout="this.parentNode.style.display=\'none\'"></iframe>';
    callOutDivTR += '</div>';

    document.write(callOutDivTR);
    
function calcElementPosition(elementId) {
		//window.status = elementId;
		var element = document.getElementById(elementId);
		eLeft = 0;
	    eTop = 0;
	    var eLeftBorder, eTopBorder;
		//eWidth = parseInt(element.style.width);
		//eHeight = parseInt(element.style.height);
		eWidth = element.clientWidth;
		eHeight = element.clientHeight;
	    while(element != null) {
			eLeftBorder = 0;
			eTopBorder = 0;
	        eLeft += element.offsetLeft;
	        eTop += element.offsetTop;
	        if (element!=document.documentElement) {
	            eLeft -= element.scrollLeft;
	            eTop -= element.scrollTop;
	        }
	        if (element.style.borderWidth!="") {
				eLeftBorder = parseInt(element.style.borderWidth);
				eTopBorder = parseInt(element.style.borderWidth);
			} else if (element.style.borderLeftWidth!="") {
				eLeftBorder = parseInt(element.style.borderLeftWidth);
				eTopBorder = parseInt(element.style.borderTopWidth);
			}
			if (isNaN(eLeftBorder)) eLeftBorder = 0;
			if (isNaN(eTopBorder)) eTopBorder = 0;
			eLeft += eLeftBorder;
			eTop += eTopBorder;
	        element = element.offsetParent;
	     }
		 return new RectangleObject("dummy", eLeft, eTop, eWidth, eHeight);
	}


function RectangleObject(id, left, top, width, height) {
	this.left = left;
	this.top = top;
	this.width = width;
	this.height = height;
}

function handleMessage(m){
	var att=m.split("::");
	switch(att[0]){
		case "srv":
			YnetAPI.instance.ChangeStyle(att[1]);
			break;
		case "cntr":
			eval("YnetAPI.instance.ZoomToXY(" + att[1] + ")");
			break;
	}
}
