﻿//variables
var map = null;

var revoIcon = new GIcon();
//revoIcon.image = "/content/images/frontend/newtabs/map_konaklama.png";
revoIcon.iconSize = new GSize(27,34);
revoIcon.shadow = "/content/images/frontend/newtabs/shadow.png";
revoIcon.shadowSize = new GSize(35,42);
revoIcon.iconAnchor = new GPoint(8,33);
//revoIcon.transparent = "/content/images/frontend/newtabs/map_konaklama_t.png";
revoIcon.infoWindowAnchor = new GPoint(5,3);
//revoIcon.imageMap = [];
var markerOptions = { icon:revoIcon };

var catRegistry = new Array();
var ewindowStyle = new EStyle("", new GSize(184,101), "mapinfo", new GPoint(-85,0));
var ewindow = null;

//on dom ready
$().ready(function(){
    $("ul#tabs li[class!='cats'][class!='mekanekle'] img").mouseover(function(){tabMouseOver($(this));}).mouseout(function(){tabMouseOut($(this));}).click(function(){tabClick($(this));});
    $("ul#tabs li.mekanekle img").mouseover(function(){tabMouseOver($(this));}).mouseout(function(){tabMouseOut($(this));});
    $("ul#tabs li[class!='cats'][class!='mekanekle'] a").tooltip({showURL:false,track:false,delay:0,top:-22,left:-7})
    load();
    
    if(showMapInfo)
    {
        var mapBoxPos = $("#mapbox").position();
        $("#map-overlay").css({left:mapBoxPos.left+261,top:mapBoxPos.top+42}).show();
        $("#map-overlay-min").css({left:mapBoxPos.left+531,top:mapBoxPos.top+42});
        
        $("#minoverlay").click(function(){
            $("#map-overlay").hide();
            $("#map-overlay-min").show();
        });
        
        $("#maxoverlay").click(function(){
            $("#map-overlay-min").hide();
            $("#map-overlay").show();
        });
        
        $("#closeoverlay,#closeoverlay2").click(function(){
            $("#map-overlay-min").remove();
            $("#map-overlay").remove();
        });
    }
    else
    {
        $("#map-overlay-min").remove();
        $("#map-overlay").remove();
    }
});

//on document unload
$(document).unload(function(){
     GUnload();
})

//load google map
function load() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("mapcontainer"));
    map.removeMapType(G_HYBRID_MAP);
    map.enableScrollWheelZoom();
    var mapControl = new GMapTypeControl();
    var zoomControl = new GLargeMapControl();
    map.setCenter(new GLatLng(39, 35), 5);
    map.addControl(mapControl);
    map.addControl(zoomControl);
    ewindow = new EWindow(map, ewindowStyle); 
    //register events
    //dragend event
    GEvent.addListener(map, "dragend", function(){
        getLocations(catRegistry, null, 100, null);
    });
    
    GEvent.addListener(map, "dblclick", function(){
        getLocations(catRegistry, null, 100, null);
    });
    
    GEvent.addListener(map, "zoomend", function(){
        getLocations(catRegistry, null, 100, null);
    });
    
    getLocations(catRegistry,null,100, null);
    
  }
}

//tab hover function
function tabMouseOver(obj)
{   
    
    var id = parseInt(/[0-9]+/.exec(obj.attr("id")));
    
    if(catRegistry.indexOf(id) == -1)
    {        
        var regex = new RegExp(/^(.+)\/([^\/]+)\.(gif|png|jpg|jpeg)$/);   
        var path = regex.exec(obj.attr("src"));    
        obj.attr("src", path[1] + "/" + path[2] + "_h." + path[3]);
    }
}

//tab mouse out function
function tabMouseOut(obj)
{    
    var id = parseInt(/[0-9]+/.exec(obj.attr("id")));

    if(catRegistry.indexOf(id) == -1)
    {
        var regex = new RegExp(/^(.+)\/([^\/]+)_h\.(gif|png|jpg|jpeg)$/);   
        var path = regex.exec(obj.attr("src"));    
        obj.attr("src", path[1] + "/" + path[2] + "." + path[3]);
    }
}

//tab click function
function tabClick(obj)
{  
    var id = parseInt(/[0-9]+/.exec(obj.attr("id")));
    var inArray = catRegistry.indexOf(id);
    if(inArray == -1)
    {
        var regex = new RegExp(/^(.+)\/([^\/]+)_h\.(gif|png|jpg|jpeg)$/);   
        var path = regex.exec(obj.attr("src"));    
        obj.attr("src", path[1] + "/" + path[2] + "_s." + path[3]);
        catRegistry.push(id);               
    }
    else
    {        
        var regex = new RegExp(/^(.+)\/([^\/]+)_s\.(gif|png|jpg|jpeg)$/);   
        var path = regex.exec(obj.attr("src"));    
        obj.attr("src", path[1] + "/" + path[2] + "_h." + path[3]);
        catRegistry = jQuery.grep(catRegistry, function(n){return n!=id;});
    }
    
    $("#map-overlay-min").remove();
    $("#map-overlay").remove();
    
    getLocations(catRegistry, null, 10, null);
}

