﻿function MyMarker(marker, map, unique, lat, lng, thumb, iconWidth, url, name, width, height, link, creator, rating) {

    this.marker = marker;
    this.lat = lat;
    this.lng = lng;
    this.map = map;
    this.unique = unique;
    this.thumb = thumb;
    this.url = url;
    this.name = name;
    this.width = width;
    this.height = height;
    this.link = link;
    this.creator = creator;
    this.rating = rating;
    this.index = 0;
    this.isAdded = false;
    this.visible = false;
    this.iconWidth = iconWidth;

    this.setIndex = setIndex;
    this.setAddedState = setAddedState;
    this.getAddedState = getAddedState;
    this.setVisibility = setVisibility;
    this.getVisibility = getVisibility;
    this.getIconWidth = getIconWidth;

    function setMarker(marker) {
        this.marker = marker;
    }

    //this.toString = toString;
}

function MyMarker2() {
    this.printPnt = printPnt;
}

function setIndex(ind) {
    with (this) index = ind;
}

function getAddedState() {
    with (this) return isAdded;
}

function getVisibility() {
    with (this) return visible;
}

function setVisibility(show) {
    with (this) visible = show;
}

function setAddedState(status) {
    with (this) isAdded = status;
}

function getIconWidth() {
    with (this) return iconWidth / 2;
}

/*function toString() {
with (this) alert(name);
}*/

var lvl1 = new Object();

var geocoder = null;
var map = null;
//var markerm = null;

var timer_id = 0;
var timer_id_iter_1 = 0;
var timer_id_iter_2 = 0;
var _markers_iter_1 = null;
var _markers_iter_2 = null;

function findAddress(address) {
    if (map == null)
        return;
        
    geocoder.getLatLng(address, function(point) {
        if (!point) {
            alert("address not found, please check spelling");
            //document.forms['searchtxt'].elements[0].value = address + " not found";
        } else {
            map.setCenter(point, 13);
        }
    }
      );
}

function findAddressEvt(e, address) {
    if (map == null)
        return true;
        
    var keyCode = (window.Event) ? e.which : e.keyCode;
    if (keyCode == 13) {
        geocoder.getLatLng(address, function(point) {
            if (!point) {
                alert("address not found, please check spelling");
                //document.forms['searchtxt'].elements[0].value = address + " not found";
            } else {
                map.setCenter(point, 13);
            }
        }
      );
        return false;
    }

    return true;
}

var _temp_id = "";
var _g_lat = 0;
var _g_lng = 0;
var _g_zoom = 0;
var _g_id = 0;

function initialize(_lat, _lng, _zoom, _id, _flg) {
    if (GBrowserIsCompatible()) {

        //alert(_lat + " " + _lng + " " + _zoom);

	if (map != null && _flg == 2) {
            
            map.panTo(new GLatLng(_lat, _lng));
            map.setZoom(_zoom);

            _temp_id = _id;
            setTimeout('GEvent.trigger(lvl1[_temp_id].marker, \"click\", lvl1[_temp_id])', 3000);  
        }

        
	if (map == null) {
            map = new GMap2(document.getElementById("canvas"));
            geocoder = new GClientGeocoder();
            markerm = new GMarkerManager(map);

            map.setMapType(G_SATELLITE_MAP);

            map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());
            map.enableScrollWheelZoom();

            map.setCenter(new GLatLng(_lat, _lng), _zoom);

            if(_flg==1)
		 LoadTours("./" + sguid + ".sync?poa=" + _id, 1);
	    else
		 LoadTours("./" + sguid + ".sync?poa=11", 1);

	    
	    _g_lat = _lat;
	    _g_lng = _lng;
	    _g_zoom = _zoom;
	    _g_id = _id;

	    setTimeout('initialize(_g_lat, _g_lng, _g_zoom, _g_id, 2)', 3000);  
        }        
	
	/*GDownloadUrl("./promoregions.xml", function(data1, responseCode1) {

            var _xml = GXml.parse(data1);
        var _regions = _xml.getElementsByTagName("region");

            var _rand = Math.floor(Math.random() * _regions.length);

            _lat = parseFloat(_regions[_rand].getAttribute("lat"));
        _lng = parseFloat(_regions[_rand].getAttribute("lng"));
        _zoom = parseInt(_regions[_rand].getAttribute("zoom"));
        //_name = _regions[_rand].getAttribute("name");

            map.setCenter(new GLatLng(_lat, _lng), _zoom);
        });*/

        //map.setCenter(new GLatLng(_lat, _lng), _zoom);        

        //LoadTours("./" + sguid + ".sync?poa=" + _id, 1);        

        //var rel_uri = "./" + sguid + ".sync?poa=" + _id;        
    }
}

function placeMarkers(markers) {

    var gmarkerslvl1 = [];
    var gmarkerslvl2 = [];
    var gmarkerslvl3 = [];
    var gmarkerslvl4 = [];
    
    if (timer_id_iter_1 != 0)
        clearTimeout(timer_id_iter_1);
    else
        clearTimeout(timer_id_iter_2);
        
    var bounds = new GLatLngBounds(new GLatLng(28.786918085420225, 33.6181640625), new GLatLng(35.22767235493586, 37.85888671875));
    
    for (var i = 0; i < markers.length; i++) {
        var latlng = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("long")));

        var thumbnail = markers[i].getAttribute("thumbnail");
        var tourURL = markers[i].getAttribute("url");
        var rating = markers[i].getAttribute("rating");
        var id = markers[i].getAttribute("id");
        var lat = markers[i].getAttribute("lat");
        var lng = markers[i].getAttribute("lng");
        var name = markers[i].getAttribute("desc");
        var width = markers[i].getAttribute("width");
        var height = markers[i].getAttribute("height");
        var link = markers[i].getAttribute("link");
        var creator = markers[i].getAttribute("creator");

        if ((tourURL.indexOf('index_4') == -1) && (tourURL.indexOf('index_5') == -1) && (tourURL.indexOf('index_16') == -1))
            continue;

        var markeroptions;
        var marker;
        var iconWidth;

        if (thumbnail != null) {
            var thumb = new GIcon();
            thumb.image = thumbnail;

            thumb.iconSize = new GSize(20, 20);
            thumb.iconAnchor = new GPoint(10, 10);
            thumb.infoWindowAnchor = new GPoint(10, 2);
            iconWidth = 20;

            markeroptions = { icon: thumb, title: name };
            marker = new GMarker(latlng, markeroptions);

            var mymarker = new MyMarker(marker, map, id, lat, lng, thumbnail, iconWidth, tourURL, name, width, height, link, creator, rating);
            GEvent.bind(marker, "click", mymarker, function() {

              var trackinfo;
                trackinfo = '/Click-View/' + this.id;
                pageTracker._trackPageview(trackinfo);

                var frmwidth;
                var frmheight;

                var td1width = 100;
                var td3width = frmwidth - 100;

                if ((this.width - this.height) == (this.width / 4)) // 4:3
                {
                    frmwidth = (this.width / 2) * 1.62;
                    frmheight = (this.height / 2) * 1.75;
                }
                else if ((this.width - this.height) == (this.width / 5)) // 5:4
                {
                    frmwidth = (this.width / 2) * 1.62;
                    frmheight = (this.height / 2) * 1.75;
                }
                else if ((this.width - this.height) == ((this.width / 16) * 7)) // 16:9
                {
                    frmwidth = (this.width / 2) * 1.62;
                    frmheight = (this.height / 2) * 1.75;
                }

                var innerhtml;
                //innerhtml = "&nbsp;&nbsp;&nbsp;<b><font color=green size='2' face='verdana'><a target='_top' href='./TourReview.aspx?tg=" + this.unique + "'>" + this.name + "</a></font></b><br/><iframe scrolling='no' frameborder='0' src='" + this.url + "' width='" + frmwidth + "' height='" + frmheight + "'></iframe><br/><table width='" + frmwidth + "' height='20'><tr><td width='" + frmwidth + "'><font size='2' face='verdana'>Created By <a href='" + this.link + "' target='_top'>" + this.creator + "</a></font></td></tr></table>";
                innerhtml = "&nbsp;&nbsp;&nbsp;<b><font color=green size='2' face='verdana'><a target='_top' href='./TourReview.aspx?tg=" + this.unique + "'>" + this.name + "</a></font></b><br/><iframe scrolling='no' frameborder='0' src='./playerfrm.aspx?w=" + this.width + "&h=" + this.height + "&s=0.75&m=0&c=" + this.url + "' width='" + frmwidth + "' height='" + frmheight + "'></iframe><br/><table width='" + frmwidth + "' height='20'><tr><td width='" + frmwidth + "'><font size='2' face='verdana'>Created By <a href='" + this.link + "' target='_top'>" + this.creator + "</a></font></td></tr></table>";
                this.marker.openInfoWindowHtml(innerhtml, frmwidth);
            });

            lvl1[id] = mymarker;

            if (bounds.containsLatLng(latlng) == false)
               gmarkerslvl1.push(marker);
            else if ((i % 4) == 0)
                gmarkerslvl1.push(marker);
            else if ((i % 4) == 1)
                gmarkerslvl2.push(marker);
            else if ((i % 4) == 2)
                gmarkerslvl3.push(marker);
            else if ((i % 4) == 3)
                gmarkerslvl4.push(marker);
        }
    }

    //map.setCenter(new GLatLng(_lat, _lng), _zoom);

    if (gmarkerslvl1.length > 0 || gmarkerslvl2.length > 0 || gmarkerslvl3.length > 0 || gmarkerslvl4.length > 0) {
        markerm.addMarkers(gmarkerslvl1, 0, 17);
        markerm.addMarkers(gmarkerslvl2, 3, 17);
        markerm.addMarkers(gmarkerslvl3, 7, 17);
        markerm.addMarkers(gmarkerslvl4, 10, 17);

        markerm.refresh();
    }
}

var _iter_flag = 0;
var _g_rel_uri;
function LoadTours(_rel_uri, mode) {

    _g_rel_uri = _rel_uri;
    if (mode == 1)
        _uri = _g_rel_uri + "&mode=1";
    else {
        _uri = _g_rel_uri + "&mode=0";

        if (timer_id != 0)
            clearTimeout(timer_id);
    }

    GDownloadUrl(_uri, function(data, responseCode) {
        var xml = GXml.parse(data);

        if (_iter_flag++ == 0) {
            _markers_iter_1 = xml.getElementsByTagName("site");
            timer_id_iter_1 = setTimeout('placeMarkers(_markers_iter_1)', 250);
            timer_id = setTimeout('LoadTours(_g_rel_uri, 0)', 500);            
        }
        else {
            _markers_iter_2 = xml.getElementsByTagName("site");
            timer_id_iter_2 = setTimeout('placeMarkers(_markers_iter_2)', 250);
        }

        /*if (mode == 1) {
            timer_id = setTimeout('LoadTours(_g_rel_uri, 0)', 500);            
        }*/
    });
}





