//jQuery.noConflict(); //Prototypeと共存するため

function deleteFromInterested(sessionToken,userId,tenantId){
    var answer = confirm("「気になる」を削除しますがよろしいですか？\n（削除したクリップは元に戻すことができません）")
    if (answer){
    jQuery.post("/service/delete_from_interested.php",
        {
        session_token:sessionToken,
        user_id: userId,
        tenant_id:tenantId
      },
        function(data){

        if(data == "true"){
          jQuery('#attendClip' + tenantId).slideUp('fast', function(){ $(this).remove(); });
        }else{
          alert(data);
        }
        }
    );
    }
    return false;
}

function deleteFromWent(sessionToken, userId, tenantId, clipId){
    var answer = confirm("「行った」を削除しますがよろしいですか？\n（削除したクリップは元に戻すことができません）");

    if (answer){
    jQuery.post("/service/delete_from_went.php",
        {
        session_token:sessionToken,
        user_id: userId,
        tenant_id:tenantId,
        clip_id:clipId
      },
        function(data){

        if(data == "true"){
          jQuery('#wentClip' + clipId).slideUp('fast', function(){ $(this).remove(); });
        }else{
          alert(data);
        }
        }
    );
    }
    return false;
}

/*
 * Went Function
 *  - Calling went.php
 */
function toggleEffectSlide(id){
  if($(id).style.display == "none"){
    Effect.SlideDown(id);
  }else{
    Effect.SlideUp(id);
  }
}

/*
 *  clipMap()
 *
 *  created: 2008/3/27
 *  author: Yasunori Takahashi
 *  email: yasunori.takahashi@alike.co.jp
 */
function clipMap(mapDivId,targets) {

  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById(mapDivId));
  doMap(map,targets);
  }
  return map;
}

/*
 *  Get Custom Icon
 */
function getCustomIcon(target){
  //
  // Create My Icon
  //
      var myIcon = new GIcon(G_DEFAULT_ICON);

  if(target.db_name == "bar"){
    target.division_id = 1;
  }else if(target.db_name == "restaurant"){
    target.division_id = 2;
  }else if(target.db_name == "beauty"){
    target.division_id = 4;
  }else if(target.db_name == "hotel"){
    target.division_id = 5;
  }else{
    //do nothing
  }

      if(target.division_id == 1){

        if(target.flag_clipped == "t"){
          if(target.flag_review == "t"){
              myIcon.image = "/images/googlemap/b0.png";
            }else if(target.flag_interested == "t"){
              myIcon.image = "/images/googlemap/b2.png";
            }else{
            myIcon.image = "/images/googlemap/b0.png";
            }
      }else{
          myIcon.image = "/images/googlemap/b-br.png";
      }
          myIcon.shadow = "/images/googlemap/shadow.png";
          myIcon.iconSize = new GSize(29, 41);
          myIcon.shadowSize = new GSize(50, 41);
      myIcon.iconAnchor = new GPoint(15,41);
      myIcon.imageMap = new Array(0,0,29,0,29,41,0,41);
          myIcon.infoWindowAnchor = new GPoint(29, 20);
          myIcon.infoShadowAnchor = new GPoint(29, 20);
       }
       if(target.division_id == 2){
      if(target.flag_clipped == "t"){
          if(target.flag_review == "t"){
              myIcon.image = "/images/googlemap/r0.png";
            }else if(target.flag_interested == "t"){
              myIcon.image = "/images/googlemap/r2.png";
            }else{
            myIcon.image = "/images/googlemap/r0.png";
            }
      }else{
        myIcon.image = "/images/googlemap/r-br.png";
      }
          myIcon.shadow = "/images/googlemap/shadow.png";
          myIcon.iconSize = new GSize(29, 41);
          myIcon.shadowSize = new GSize(50, 41);
      myIcon.iconAnchor = new GPoint(15,41);
        myIcon.imageMap = new Array(0,0,29,0,29,41,0,41);
       }
    if(target.division_id == 4){
      if(target.flag_clipped == "t"){
          if(target.flag_review == "t"){
              myIcon.image = "/images/googlemap/be0.png";
            }else if(target.flag_interested == "t"){
              myIcon.image = "/images/googlemap/be2.png";
            }else{
            myIcon.image = "/images/googlemap/be0.png";
            }
      }else{
        myIcon.image = "/images/googlemap/be-br.png";
      }
          myIcon.shadow = "/images/googlemap/shadow.png";
          myIcon.iconSize = new GSize(29, 41);
          myIcon.shadowSize = new GSize(50, 41);
      myIcon.iconAnchor = new GPoint(15,41);
          myIcon.imageMap = new Array(0,0,29,0,29,41,0,41);
       }
    if(target.division_id == 5){
      if(target.flag_clipped == "t"){
          if(target.flag_review == "t"){
              myIcon.image = "/images/googlemap/h0.png";
            }else if(target.flag_interested == "t"){
              myIcon.image = "/images/googlemap/h2.png";
            }else{
            myIcon.image = "/images/googlemap/h0.png";
            }
      }else{
        myIcon.image = "/images/googlemap/h-br.png";
      }
          myIcon.shadow = "/images/googlemap/shadow.png";
          myIcon.iconSize = new GSize(29, 41);
          myIcon.shadowSize = new GSize(50, 41);
      myIcon.iconAnchor = new GPoint(15,41);
          myIcon.imageMap = new Array(0,0,29,0,29,41,0,41);
       }
  return myIcon;
}

/*
 *
 */
function getDivisionName(divisionId){
  if(divisionId == "1"){
    return "bar";
  }else if(divisionId == "2"){
    return "restaurant";
  }else if(divisionId == "4"){
    return "beauty";
  }else if(divisionId == "5"){
    return "hotel";
  }
}

/*
 *Creates a marker at the given point
 */
function createMarker(map,latlng,targets,number) {

  var myIcon = getCustomIcon(targets.data[number-1]);

  // Set up our GMarkerOptions object
    markerOptions = { icon:myIcon };

      var marker = new GMarker(latlng,markerOptions);
      marker.value = number;

      //
      // markerにクリックイベントを設定する
      //
      GEvent.addListener(marker,"click", function() {
    if(targets.data[number-1].division_id > 0){
      dbName = getDivisionName(targets.data[number-1].division_id);
    }else{
      dbName = targets.data[number-1].db_name;
    }

    GDownloadUrl("/service/get_infowindow_contents.html?tenant_id=" + targets.data[number-1].tenant_id + "&user_id=" + userId , function(htmlContents, responseCode) {
      map.openInfoWindowHtml(latlng, htmlContents);
    });
      });

      return marker;
}

function createInfoWindowHtml(map,latlng,target){

  var myHtml = "";

  //
  //GET WENT FORM HTML
  //
  GDownloadUrl("/service/get_infowindow_contents.html?tenant_id=" + target.tenant_id + "&user_id=" + userId , function(htmlContents, responseCode) {
    myHtml = htmlContents;
    return myHtml;
  });
}

/**
 * マップ、「行った」登録maximize画面
 */
function maximizeWindow_went(tenantId,lat,lng){
  var latlng = new GLatLng(lat,lng);

  //
  //GET WENT FORM HTML
  //
  GDownloadUrl("/service/get_went_form.html?is_map=t&tenant_id=" + tenantId , function(htmlContents, responseCode) {
    mapObj.openInfoWindowHtml(latlng, null ,{maxContent:htmlContents,maxTitle:"<img src='/images/icon/add_went.png' style='width:16px;height=16px;'>「行った」でMyクリップに追加"});
    mapObj.getInfoWindow().maximize();
  });
}

/**
 * マップ、「気になる」登録maximize画面
 */
function maximizeWindow_interested(tenantId,lat,lng){
  var latlng = new GLatLng(lat,lng);

  //
  //GET INTERESTED FORM HTML
  //
  GDownloadUrl("/service/get_interested_form.html?is_map=t&tenant_id=" + tenantId, function(htmlContents, responseCode) {
    mapObj.openInfoWindowHtml(latlng,null,{maxContent:htmlContents,maxTitle:"<img src='/images/icon/add_interested.png' style='width:16px;height=16px;'>「気になる」でMyクリップに追加"});
    mapObj.getInfoWindow().maximize();
  });
}

/*
 *
 */
function doMap(map,targets){
  map.setCenter(new GLatLng(0,0),0);
  var bounds = new GLatLngBounds();
  map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
  map.addMapType(G_PHYSICAL_MAP);

  if(targets.data){
    var marker = null;
      for (var i = 0; i < targets.data.length; i++) {
      var latlng = new GLatLng(targets.data[i].lat,targets.data[i].lng);
      if(targets.data[i].lat != null && targets.data[i].lat != 0 && targets.data[i].lng != null && targets.data[i].lng != 0){
        //alert(latlng);
        bounds.extend(latlng);
      }
      marker = createMarker(map,latlng, targets,i + 1);
      map.addOverlay(marker);
      }
  }

  map.setZoom(map.getBoundsZoomLevel(bounds));
  map.setCenter(bounds.getCenter());
}

/**
 *
 */
function doMapSearchMap(map,targets,markers){
  map.setCenter(new GLatLng(0,0),0);
  var bounds = new GLatLngBounds();
  map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
  map.addMapType(G_PHYSICAL_MAP);

  if(targets.data){

    var marker = null;
      for (var i = 0; i < targets.data.length; i++) {
      var latlng = new GLatLng(targets.data[i].lat,targets.data[i].lng);
      if(targets.data[i].lat != null && targets.data[i].lat != 0 && targets.data[i].lng != null && targets.data[i].lng != 0){
        //alert(latlng);
        bounds.extend(latlng);
      }
      marker = createMarker(map,latlng, targets,i + 1);
      markers[targets.data[i].tenant_id] = marker;
      map.addOverlay(marker);
      }
  }

  map.setZoom(map.getBoundsZoomLevel(bounds));
  map.setCenter(bounds.getCenter());
}

/*
 * WHAT: Check/uncheck all checkboxes
 * WHERE: Bar's select sub form
 * PROPERTY: obj - form
 */
function chk_on(obj) {
    su = 2;
    lp = obj.count.value + su;
    for(i=su; i<lp; i++) {
        obj.elements[i].checked = true;
    }
}

/**
 *
 */
function chk_off(obj) {
    su = 2;
    lp = obj.count.value + su;
    for(i=su; i<lp; i++) {
        obj.elements[i].checked = false;
    }
}

/**
 * Function : dump()
 * Arguments: The data - array,hash(associative array),object
 *    The level - OPTIONAL
 * Returns  : The textual representation of the array.
 * This function was inspired by the print_r function of PHP.
 * This will accept some data as the argument and return a
 * text that will be a more readable version of the
 * array/hash/object that is given.
 */
function dump(arr,level) {
  var dumped_text = "";
  if(!level) level = 0;

  //The padding given at the beginning of the line.
  var level_padding = "";
  for(var j=0;j<level+1;j++) level_padding += "    ";

  if(typeof(arr) == 'object') { //Array/Hashes/Objects
  for(var item in arr) {
    var value = arr[item];

    if(typeof(value) == 'object') { //If it is an array,
    dumped_text += level_padding + "'" + item + "' ...\n";
    dumped_text += dump(value,level+1);
    } else {
    dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
    }
  }
  } else { //Stings/Chars/Numbers etc.
  dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
  }
  return dumped_text;
}

/**
 * getwidth - 全てのブラウザで、CSSのmax-widthが動くようにするために必要な、
 *            動的に画像の横幅を取得するスクリプト
 */
function getwidth(tagName,className)
{
  var retval = 0;
  list = document.getElementsByTagName(tagName);
  for (i = 0; i < list.length; i++) {
    if (list[i].className == className) {
      retval = list[i].width;
    }
  }
  return retval;
}

function getLink(arg_)
{
  var result = '';

  for (var i = 0; i < arg_.length; i += 2)
  {
    result = result + arg_.substr(i, 1);
  }

  return result;
}

