
function _playerAdd(anchor) {
    var url = anchor.href;
    var code = '<object type="application/x-shockwave-flash" data="http://roommate.jp/b/includes/player/musicplayer_f6.swf?song_url=' + url +'&amp;b_bgcolor=ffffff&amp;b_fgcolor=000000&amp;b_colors=000000,000000,ff0000,ff0000&buttons=http://roommate.jp/b/includes/player/load.swf,http://roommate.jp/b/includes/player/play.swf,http://roommate.jp/b/includes/player/stop.swf,http://roommate.jp/b/includes/player/error.swf" width="14" height="14">';
    var code = code + '<param name="movie" value="http://roommate.jp/b/includes/player/musicplayer.swf?song_url=' + url +'&amp;b_bgcolor=ffffff&amp;b_fgcolor=000000&amp;b_colors=000000,000000,ff0000,ff0000&amp;buttons=http://roommate.jp/b/includes/player/load.swf,http://roommate.jp/b/includes/player/play.swf,http://roommate.jp/b/includes/player/stop.swf,http://roommate.jp/b/includes/player/error.swf" />';
    var code = code + '</object>';
    anchor.parentNode.innerHTML = code +' '+ anchor.parentNode.innerHTML;
}

function deleteBookmark(ele, item){
    var link = $(ele).parents("li.link");
    if (link.children("span.confirm").length < 1) {
        var confirmDelete = "<span class='confirm'>本当にしますか? <a href=\"#\" onclick=\"deleteConfirmed(this, " + item + ", \'\'); return false;\">はい</a> - <a href=\"#\" onclick=\"deleteCancelled(this); return false;\">いいえ</a></span>";
        link.append(confirmDelete);
    }
}

function deleteCancelled(ele) {
    $(ele).parents('span.confirm').remove();
}

function deleteConfirmed(ele, item) {
    $.ajax({
        type: 'POST',
        url:  'http://roommate.jp/b/ajaxDelete.php',
        data: { 'id': item },
        success: function(msg) {
           $(ele).parents('li.xfolkentry').remove();
        }
    });
}

function htmlentities(text) {
    text = text.replace(/'/g, '&apos;');
    text = text.replace(/"/g, '&quot;');
    return text;
}

// TODO: Convert to jQuery
function isAvailable(input, response){
    var usernameField = document.getElementById("username");
    var username = usernameField.value;
    username = username.toLowerCase();
    username = $.trim(username);
    var availability = document.getElementById("availability");
    if (username != '') {
        usernameField.style.backgroundImage = 'url(http://roommate.jp/b/loading.gif)';
        if (response != '') {
            usernameField.style.backgroundImage = 'none';
            if (response == 'true') {
                availability.className = 'available';
                availability.innerHTML = '利用できます';
            } else {
                availability.className = 'not-available';
                availability.innerHTML = '利用できません';
            }
        } else {
            loadXMLDoc('http://roommate.jp/b/ajaxIsAvailable.php?username=' + username);
        }
    }
}

function useAddress(ele) {
    var address = ele.value;
    if (address != '') {
      if (address.indexOf(':') < 0) {
        address = 'http:\/\/' + address;
      }

      var canonicalizedAddress = getNormalizedPermalink(address);

      getTitle(address, null);

      if (canonicalizedAddress != address) {
        address = canonicalizedAddress;
        ele.value = canonicalizedAddress;
      }
    }
}

function getTitle(address) {
   var title = document.getElementById("titleField");
   title.style.backgroundImage = "url(http://roommate.jp/b/images/loading.gif)";
   $.ajax({
      type: 'GET',
      url:  'http://roommate.jp/b/ajaxGetTitle.php',
      data: { 'url': address },
      dataType: 'text',
      complete: function(obj, response) {
         title.style.backgroundImage = 'none';
      },
      success: function(response) {
         title.value = response;
      }
   });
}

var xmlhttp;
function loadXMLDoc(url) {
    // Native
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = processStateChange;
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    // ActiveX
    } else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        if (xmlhttp) {
            xmlhttp.onreadystatechange = processStateChange;
            xmlhttp.open("GET", url, true);
            xmlhttp.send();
        }
    }
}

function processStateChange() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        response = xmlhttp.responseXML.documentElement;
        method = response.getElementsByTagName('method')[0].firstChild.data;
        result = response.getElementsByTagName('result')[0].firstChild.data;
        eval(method + '(\'\', result)');
    }
}

function playerLoad() {
    var links         = $("a.taggedlink[@href$=.mp3]");
    var links_length  = links.length;
    for (var i = 0; i < links_length; i++) {
       _playerAdd(links[i]);
    }
}

// Onload events
jQuery(function($) {
    // Toggle the 'Add a Bookmark' form
    $("div#toolbar a.add").click(function() {
        $("div#add").slideToggle("slow");
    });

    // Toggle tag display
//    $("li.xfolkentry").click(function() {
//        $("li.tags", this).slideToggle("fast");
//    });

    // Display options on hover
    $("li.xfolkentry").hover(function() {
        $("a.block", this).css("display", "inline");
        $("a.delete", this).css("display", "inline");
        $("a.edit", this).css("display", "inline");
    },function() {
        $("a.block", this).hide();
        $("a.delete", this).hide();
        $("a.edit", this).hide();
        $("span.confirm", this).remove();
    });

    // Hover effect for Delete buttons
    $("a.delete").hover(function() {
        $("img", this).attr("src", "http://roommate.jp/b/images/delete_on.png");
    },function() {
        $("img", this).attr("src", "http://roommate.jp/b/images/delete.png");
    });

    // Hover effect for Edit buttons
    $("a.edit").hover(function() {
        $("img", this).attr("src", "http://roommate.jp/b/images/edit_on.png");
    },function() {
        $("img", this).attr("src", "http://roommate.jp/b/images/edit.png");
    });

    // Hover effect for Block buttons
    $("a.block").hover(function() {
        $("img", this).attr("src", "http://roommate.jp/b/images/block_on.png");
    },function() {
        $("img", this).attr("src", "http://roommate.jp/b/images/block.png");
    });

    // Edit click
    /*
    $("a.edit").click(function() {
        // Remove existing edit forms and show all hidden bookmarks
        $('li.edit').remove();
        $('li.xfolkentry').show();

        var li = $(this).parents('li.xfolkentry');
        li.before('<li class="xfolkentry edit"></li>');
        $('li.edit').hide();
        var ul          = $(this).parents('ul');
        var address     = htmlentities($(this).prev().attr('href'));
        var title       = htmlentities($(this).prev().text());
        var description = htmlentities(ul.children('li.description').text());
        var tags        = htmlentities(ul.children('li.tags').text());
        $('li.edit').load(
            "http://roommate.jp/b/templates/editform.tpl.php",
            {
                'address':      address,
                'title':        title,
                'description':  description,
                'tags':         tags,
                'status':       "2"
            },
            function() {
                li.hide();
                $('li.edit').show();
            }
        );
        return false;
    });
    */

    // Hide search label on focus
/*    $("input[@name=terms]").focus(function() {
        $('../label', this).hide();
    });*/

    // Tag completion
    /*
    $("#tags").keyup(function() {
        
    });
    */
});

function getNormalizedPermalink(url) {
   var address = document.getElementById("address");
   address.style.backgroundImage = "url(http://roommate.jp/b/images/loading.gif)";
   $.ajax({
      type: 'GET',
      url:  'http://roommate.jp/b/ajaxNormalizeUrl.php',
      data: { 'url': url },
      dataType: 'text',
      complete: function(obj, response) {
         address.style.backgroundImage = 'none';
      },
      success: function(response) {
         address.value = response;
      }
   });
}

