var oMPC = {
  oM : null,
  oCMgr : null,
  oIMgr : null,
  
  mInit : function(){
    var oTmpM = document.getElementById('main_cemetery_map');
    if(oTmpM == null) return;
    oMPC.mInitMS();
    oMPC.oM = new google.maps.Map2(oTmpM, {size:new GSize(895,432)});
    oMPC.oM.setUIToDefault();
    oMPC.oM.setMapType(G_PHYSICAL_MAP);
    oMPC.oM.disableScrollWheelZoom();
    oMPC.oM.setCenter(new google.maps.LatLng(29.53522956294847, -23.5546875), 2);

    $('#main_map_view').bind('change', oMPC.mMapViewChange);
    $('#main_map_icon_view').bind('change', oMPC.mMapIconViewChange);

    oMPC.mMapViewChange();
    
    oMPC.CMgr = new MarkerManager(oMPC.oM, {trackMarkers:false});
    oMPC.IMgr = new MarkerManager(oMPC.oM, {trackMarkers:false});
    
    oMPC.mAddCemeteries();  
    oMPC.mAddIcons(); 
    
    oMPC.CMgr.refresh(); 
    oMPC.IMgr.refresh(); 
  },
  
  mMapViewChange : function(){
    var s = $('#main_map_view')[0].value;
    var aQ = s.split('_');
    if(aQ[0].trim() == 'world')
      oMPC.oM.setCenter(new google.maps.LatLng(29.53522956294847, -23.5546875), 2);
    else{
      oGCM.getLatLng(aQ[0].trim(), 
        function(point){
          if(point){
            oMPC.oM.setCenter(point, parseInt(aQ[1])); 
          }
          else oMPC.oM.setCenter(new google.maps.LatLng(29.53522956294847, -23.5546875), 2);
        }
        );
    }
  },
  
  mMapIconViewChange : function(){
    var s = $('#main_map_icon_view')[0].value;
    switch(s){
      case 'all' :
        oMPC.CMgr.show();
        oMPC.IMgr.show();
        break;
      case 'angels' :
        oMPC.CMgr.show();
        oMPC.IMgr.hide();
        break;
      case 'icons' :
        oMPC.CMgr.hide();
        oMPC.IMgr.show();
        break;
      default :
        oMPC.CMgr.show();
        oMPC.IMgr.show();
        break;
    }
  },
  
  mInitMS : function(){
    //oMPC.aCemeteryItems = $('.tributes_center_content_item');
    //oMPC.aClearItems = $('.closing_cemetery_item');
    
    Sys.Application.add_load(oMPC.mRenderCemeteries);
  },

  mAddCemeteries : function(){
    for(var i = 0; i < a_C_MarkerArray.length; i++){
      if(typeof(a_C_MarkerArray[i]) == 'undefined')
        continue;
      try{
        var mrkr = new GMarker(new google.maps.LatLng(parseFloat(a_C_MarkerArray[i][1]), parseFloat(a_C_MarkerArray[i][2])), {
            icon: oMPC.mGetCemeteryIcon(a_C_MarkerArray[i][6])
          });
        //oMPC.oM.addOverlay(mrkr);
        
        mrkr.CemeteryObject = a_C_MarkerArray[i];
        
        GEvent.addListener(mrkr, "click", function() {
	     var sBody = '<table class="mapinfowindow_table"><tbody>';
	     sBody += '<tr><td class="mapinfowindow_td"></td>';
	     sBody += '<td rowspan="3" class="mapinfowindow_imgtd" valign="top"><div class="angel_avatar_wrapper"><img src="' + this.CemeteryObject[5].substring(1) + '" class="item_avatar_image" title="' + this.CemeteryObject[3].replaceAll('"', '') +'"></img></div></td></tr>';
	     sBody += '<tr><td class="mapinfowindow_td">Megemlékezés róla:<br/>' + this.CemeteryObject[4] + ' (' + this.CemeteryObject[3] + ')</td></tr>';
	     sBody += '<tr><td class="mapinfowindow_td"><a href="' + sVCAppPath + '/' + sVCPage + '/' + this.CemeteryObject[0] + '">' + this.CemeteryObject[3] + ' adatlapja</td></tr>';
	     sBody += '</tbody></table>';

            this.openInfoWindowHtml(sBody);
          });
      oMPC.CMgr.addMarker(mrkr, 1);
                
      }catch(e){alert(e);}
    }
  },
  
    mAddIcons : function(){
    for(var i = 0; i < a_C_IconMarkerArray.length; i++){
      if(typeof(a_C_IconMarkerArray[i]) == 'undefined')
        continue;
      try{
        var mrkr = new GMarker(new google.maps.LatLng(parseFloat(a_C_IconMarkerArray[i][1]), parseFloat(a_C_IconMarkerArray[i][2])), {
            icon: oMPC.mGetCemeteryIcon(a_C_IconMarkerArray[i][6])
          });
        //oMPC.oM.addOverlay(mrkr);
        
        mrkr.CemeteryObject = a_C_IconMarkerArray[i];
        
        GEvent.addListener(mrkr, "click", function() {
        var sName = this.CemeteryObject[8];
        var sLink = '';
       if(this.CemeteryObject[8].toLowerCase() == 'anonymus'){
        sName = 'Vendég';
        }else{
        sLink = '<br/><a onclick="mGetEntry(\'' + sVCAppPath + '/' + sVCPage + '/getentry/' + this.CemeteryObject[10] + '\');" href="javascript:void(false);">' + sName + ' megemlékezése</a>';
       }

	     var sBody = '<table class="mapinfowindow_table"><tbody>';
	     sBody += '<tr><td class="mapinfowindow_td">Megemlékező: ' + sName + '<br/>Időpont: ' + this.CemeteryObject[9];
	     sBody += sLink + '</td>';
	     sBody += '<td rowspan="3" class="mapinfowindow_imgtd" valign="top"><div class="avatar_wrapper smallavtr"><img src="' + this.CemeteryObject[5] + '" class="user_avatar_image" title="' + this.CemeteryObject[8].replaceAll('"', '') +'"></div></td></tr>';
	     sBody += '<tr><td class="mapinfowindow_td">Megemlékezés róla:<br/>' + this.CemeteryObject[4] + ' (' + this.CemeteryObject[3] + ')</td></tr>';
	     sBody += '<tr><td class="mapinfowindow_td"><a href="' + sVCAppPath + '/' + sVCPage + '/' + this.CemeteryObject[0] + '">' + this.CemeteryObject[3] + ' adatlapja</td></tr>';
	     sBody += '</tbody></table>';

            this.openInfoWindowHtml(sBody);
          });
      oMPC.IMgr.addMarker(mrkr, 1);
      }catch(e){alert(e);}
    }
  },
  
  mRenderCemeteries : function(arg1, arg2, arg3){
    $('#main_loading_section').hide();
    /*if(oMPC.aCemeteryItems != null)
      oMPC.aCemeteryItems[0].fadeOut('fast');
    if(oMPC.aClearItems != null)
      oMPC.aClearItems.fadeOut('fast');
    oMPC.aCemeteryItems = $('.tributes_center_content_item');
    oMPC.aClearItems = $('.closing_cemetery_item');*/
  },
  
  mPageingCemeteries : function(oSender, arg){
    $('#main_loading_section').show();
    $('#' + sPagerArg)[0].value = arg;
    __doPostBack(sPagerTarget, '');
    return false;
  },
  
  mGetCemeteryIcon : function(isG){    
    var icon = new GIcon();
    var sIconRoot = "http://www.angyalweb.hu/Sites/VirtualCemetery/Images/MapIcons/";
    switch(isG){
      case 0 :
        icon.image = sIconRoot + "map_angel.png";
        icon.iconAnchor = new GPoint(31, 25);
        icon.infoWindowAnchor = new GPoint(31, 21);
        icon.iconSize = new GSize(62, 26);
        icon.shadow = sIconRoot + "map_angel_shadow.png";
        icon.shadowSize = new GSize(66, 31);
        break;
      case 1 :
        icon.image = sIconRoot + "map_angel_gold.png";
        icon.iconAnchor = new GPoint(31, 25);
        icon.infoWindowAnchor = new GPoint(31, 21);
        icon.iconSize = new GSize(62, 26);
        icon.shadow = sIconRoot + "map_angel_shadow.png";
        icon.shadowSize = new GSize(66, 31);
        break;
      case 3 :
        icon.image = sIconRoot + "map_candle_silver.png";
        icon.iconAnchor = new GPoint(17, 14);
        icon.infoWindowAnchor = new GPoint(16, 0);
        icon.iconSize = new GSize(29, 23);
        icon.shadow = sIconRoot + "map_candle_silver_shadow.png";
        icon.shadowSize = new GSize(29, 23);      
        break;
      case 4 :
        icon.image = sIconRoot + "map_candle_gold.png";
        icon.iconAnchor = new GPoint(17, 14);
        icon.infoWindowAnchor = new GPoint(16, 0);
        icon.iconSize = new GSize(29, 23);
        icon.shadow = sIconRoot + "map_candle_gold_shadow.png";
        icon.shadowSize = new GSize(29, 23);  
        break;
      case 5 :
        icon.image = sIconRoot + "map_flower.png";
        icon.iconAnchor = new GPoint(14, 30);
        icon.infoWindowAnchor = new GPoint(16, 0);
        icon.iconSize = new GSize(23, 43);
        icon.shadow = sIconRoot + "map_flower_shadow.png";
        icon.shadowSize = new GSize(23, 43); 
        break;
      case 6 :
        icon.image = sIconRoot + "map_flower_gold.png";
        icon.iconAnchor = new GPoint(14, 30);
        icon.infoWindowAnchor = new GPoint(16, 0);
        icon.iconSize = new GSize(23, 43);
        icon.shadow = sIconRoot + "map_flower_shadow.png";
        icon.shadowSize = new GSize(23, 43);
        break;
      case 7 :
        icon.image = sIconRoot + "map_tomb.png";
        icon.iconAnchor = new GPoint(14, 17);
        icon.infoWindowAnchor = new GPoint(16, 0);
        icon.iconSize = new GSize(26, 32);
        icon.shadow = sIconRoot + "map_tomb_shadow.png";
        icon.shadowSize = new GSize(26, 32);
        break;
      default :
        icon.image = sIconRoot + "map_candle_silver.png";
        icon.iconAnchor = new GPoint(17, 14);
        icon.infoWindowAnchor = new GPoint(16, 0);
        icon.iconSize = new GSize(29, 23);
        icon.shadow = sIconRoot + "map_candle_silver_shadow.png";
        icon.shadowSize = new GSize(29, 23);  
        break;
    }
    return icon;    
  }
};
$('document').ready(oMPC.mInit);

var oDPC = {
  oM : null,
  oTab : null,
  iSelectedTabIndex : 0,
  oSelectedMapIcon : null,
  oCemeteryMarker : null,
  oMmgr : null,
  
  mInit : function(){
    var oTmpM = document.getElementById('detail_cemetery_map');
    if(oTmpM == null) return;
    
    var aCollapser = $('.collapser');
    aCollapser.bind('click', oDPC.mCollapserClick);
    
    oDPC.oTab = $("#tabs").tabs();
    oDPC.oTab.tabs('select', iSelectedTabIndex);

    
    oDPC.oM = new google.maps.Map2(oTmpM, {size:new GSize(895,432)});
    oDPC.oM.setUIToDefault();
    oDPC.oM.setMapType(G_PHYSICAL_MAP);
    oDPC.oM.disableScrollWheelZoom();
    
    oDPC.oM.setCenter(new google.maps.LatLng(29.53522956294847, -23.5546875), 2);  
    $('#main_map_view').bind('change', oDPC.mMapViewChange);
    oDPC.mMapViewChange();

    oDPC.oMmgr = new MarkerManager(oDPC.oM, {trackMarkers:false});
    oDPC.mAddOwnIcons();
    oDPC.oMmgr.refresh();

    Sys.Application.add_load(oDPC.mMapCallBack);
    
    var im = $('.infomessage');
      if(im.length != 0)
        im.idle(10000).fadeOut('slow');
        
    oDPC.mInitStandaloneIcons();
    
  },
  
  mInitStandaloneIcons : function(){
    $('.map_items_section div').bind('click', oDPC.mSelectStandAloneIcon);
  },
  
  mSelectStandAloneIcon : function(){
    var cg = $(this).attr("cg");
    if(cg != '' && cg != 'undefined')
      mGetEntry(sVCAppPath + '/' + sVCPage + '/getentry/' + cg);
  },
  
  mMapCallBack : function(){
    /*$('.map_items_section div').bind('click', oDPC.mSelectMapIcon);
    oDPC.oSelectedMapIcon = $('.map_items_section div.selectedicon');
    $('#select_term').bind('change', oDPC.mSelectTermChange);
      
    var im = $('.infomessage');
      if(im.length != 0){
        im.idle(10000).fadeOut('slow');
        oDPC.oMmgr.show();
        oDPC.mSelectTermChange();
      }
      else{
        $('#pnlNewMapItem').fadeIn("fast");
        oDPC.mSelectTermChange();
      }*/
  },
  
  mGetLat : function(){return parseFloat(oDPC.oLat[0].value);},
  mGetLng : function(){return parseFloat(oDPC.oLng[0].value);},
  mSetLat : function(lat){oDPC.oLat[0].value = lat;},
  mSetLng : function(lng){oDPC.oLng[0].value = lng;},
  mSetIconType : function(typeId){oDPC.oIconType[0].value = typeId;},
  
  mMapViewChange : function(){
    var s = $('#main_map_view')[0].value;
    var aQ = s.split('_');
    if(aQ[0].trim() == 'world')
      oDPC.oM.setCenter(new google.maps.LatLng(29.53522956294847, -23.5546875), 2);
    else{
      oGCM.getLatLng(aQ[0].trim(), 
        function(point){
          if(point){
            oDPC.oM.setCenter(point, parseInt(aQ[1])); 
          }
          else oDPC.oM.setCenter(new google.maps.LatLng(29.53522956294847, -23.5546875), 2);
        }
        );
    }
  },
  
  mCollapserClick : function(arg){
    if(this.className.indexOf('minus') != -1){
      this.className = 'collapser plus';
      $('#cemetery_table_' + this.id).fadeOut("slow");
    }
    else{
      this.className = 'collapser minus';
      $('#cemetery_table_' + this.id).fadeIn("slow");
    }
  },
  
  mModerate : function(){
    return confirm(sConfirmModerateComment);
  },
  
  mAddEntry : function(){
    Shadowbox.open({
              player: 'iframe',
              title: '',
              content: sVCAppPath + '/' + sVCPage + '/newentry/' + sActCemeteryGuid,
              height: 550,
              width: 750,
              keysClose: ['c', 27]
    });
  },
  
  mPageRefresh : function(){
    location.reload(true);
  },
  
  mAddOwnIcons : function(){
    for(var i = 0; i < a_C_IconMarkerArray.length; i++){
      if(typeof(a_C_IconMarkerArray[i]) == 'undefined')
        continue;
      try{
        var mrkr = new GMarker(new google.maps.LatLng(parseFloat(a_C_IconMarkerArray[i][1]), parseFloat(a_C_IconMarkerArray[i][2])), {
            icon: oMPC.mGetCemeteryIcon(a_C_IconMarkerArray[i][6])
          });
        //oDPC.oM.addOverlay(mrkr);
        
        mrkr.CemeteryObject = a_C_IconMarkerArray[i];
        
        GEvent.addListener(mrkr, "click", function() {
          var sName = this.CemeteryObject[8];
          var sLink = '';
          if(this.CemeteryObject[8].toLowerCase() == 'anonymus'){
            sName = 'Vendég';
          }else{
            sLink = '<br/><a onclick="mGetEntry(\'' + sVCAppPath + '/' + sVCPage + '/getentry/' + this.CemeteryObject[10] + '\');" href="javascript:void(false);">' + sName + ' megemlékezése</a>';
          }

           var sBody = '<table class="mapinfowindow_table"><tbody>';
           sBody += '<tr><td class="mapinfowindow_td" valign="top">Megemlékező: ' + sName + '<br/>Időpont: ' + this.CemeteryObject[9];
           sBody += sLink + '</td>';
           sBody += '<td class="mapinfowindow_imgtd" valign="top"><div class="avatar_wrapper smallavtr"><img src="' + this.CemeteryObject[5] + '" class="user_avatar_image" title="' + this.CemeteryObject[8].replaceAll('"', '') +'"></div></td></tr>';
           sBody += '</tbody></table>';

            this.openInfoWindowHtml(sBody);
          });
       oDPC.oMmgr.addMarker(mrkr, 1);
      }catch(e){alert(e);}
    }
    for(var ii = 0; ii < a_C_AngelMarker.length; ii++){
      if(typeof(a_C_AngelMarker[ii]) == 'undefined')
        continue;
      try{
        var mrkr = new GMarker(new google.maps.LatLng(parseFloat(a_C_AngelMarker[ii][1]), parseFloat(a_C_AngelMarker[ii][2])), {
            icon: oMPC.mGetCemeteryIcon(a_C_AngelMarker[ii][6])
          });
        oDPC.oM.addOverlay(mrkr);
      }catch(e){}
    }
  }   
};
$('document').ready(oDPC.mInit);

$.fn.idle = function(time){
  var o = $(this);
  o.queue(function()
  {
     setTimeout(function()
     {
        o.dequeue();
     }, time);
  });
  return this;
}

oGCM = new GClientGeocoder();

function mGetEntry(sTarget){
    Shadowbox.open({
              player: 'iframe',
              title: '',
              content: sTarget,
              height: 550,
              width: 750,
              keysClose: ['c', 27]
    });
}