$(document).ready(function() {        
    $('.sharelink').click(function () { 
        //$(this).parent().siblings('.like-status').hide(); 
        $(this).parent().next('.share').slideDown(); 
        return false;
    });
    
    $('.hide-button').click(function() {
        $(this).parent().slideUp();
        return false;
    });
    
    $('.likelink').click(
        function () { 
            $link = $(this);
            $linkref = $(this).attr("href");
            $linkstatus = $(this).parent().siblings('.like-status');
            if ($link.html() == 'Unlike') {
                $linkstatus.hide();
                $link.html('Like');
            } else {
                $linkstatus.slideDown("fast");
                $link.html('Unlike');
            }
            $.getJSON($linkref, function(json){
                $('#' + json.id).html(json.rating);
            });
            return false;
        }
    );
    
    $('#more_category').toggle( 
        function () {
          $(this).prev().slideDown("fast"); 
          $(this).html('<img src="http://media.yumbaa.com/images/hide.png" /> Hide');
          return false;
        },
        function () {
          $(this).prev().slideUp("fast"); 
          $(this).html('<img src="http://media.yumbaa.com/images/show.png" /> More');
          return false;
        }                
    );    
    
    $('.delete').live("click", function(){
        $(this).parent().parent().slideUp();
        var dataString = 'id=' + $(this).attr('id'); 
        $.ajax({  
            type: "POST",  
            url: "/places/comment/ajax/delete/",  
            data: dataString,  
            dataType: "json",
            success: function(data, textStatus) {  
                
            },  
        });          
    });
    
    $('.remove').click(function(){
        $(this).parent().parent().parent().fadeOut("slow");
        var link = $(this).attr("href");            
        $.getJSON(link, function(json){});            
        return false;       
    });
    
    $('.wishlistlink').click(
        function () { 
            $(this).parent().hide();
            var link = $(this).attr("href");            
            $.getJSON(link, function(json){               
            });            
            return false;
        }
    );
    
    $('.commentlink').toggle(
        function () { 
            $dform = $(this).parent().parent().children('.form-comment'); 
            $dform.show();
            $dform.children('form').children('textarea').focus();
            return false;
        },
        
        function () { 
            $(this).parent().parent().children('.comments').children('.form-comment').hide(); 
            return false;
        }            
    );
    

    $(".dropdown img.flag").addClass("flagvisibility");

    $(".dropdown dt a").click(function() {
        $(".dropdown dd ul").toggle();
        return false;
    });
                
    $(".dropdown dd ul li a").click(function() {
        var text = $(this).html();
        $(".dropdown dt a span").html(text);
        $(".dropdown dd ul").hide();
        $("#result").html("Selected value is: " + getSelectedValue("sample"));
    });
                
    function getSelectedValue(id) {
        return $("#" + id).find("dt a span.value").html();
    }

    $(".dropdown dd ul").hide();


    $("#flagSwitcher").click(function() {
        $(".dropdown img.flag").toggleClass("flagvisibility");
    });


    
    $(".submit").click(function () { 
        var textarea = $(this).parent().children('textarea');
        var content = $(this).parent().children('textarea').val();
        var place = $(this).parent().children('input#place').val();
        var dataString = 'content=' + content + '&place=' + place; 
        var comment_list = $(this).parent().parent().prev();
        if (content == '') return false; 
        $.ajax({  
            type: "POST",  
            url: "/places/comment/ajax/add/",  
            data: dataString,  
            dataType: "json",
            success: function(data, textStatus) {  
                comment_list.append('<div class="comment"><strong><a href="/people/'+ data.user + '/">' + data.name + '</a></strong> ' + content + ' · <span>under 1 minute ago</span> · <span class="commands"><a href="#" class="delete" id="'+ data.comment +'">Delete</a></span></div>');
                textarea.val('');
            },  
        });  
        
        
        
        
        return false;
    });        
    
});

