

$(function()
{



	jQuery.extend({
	  httpData: function( xhr, type, s ) {

			if (xhr.getResponseHeader("mvn")) {
			
				var ct = xhr.getResponseHeader("content-type"),
					xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0,
					script = type == "script" || !type && ct && ct.indexOf("script") >= 0,
					json = type == "json" || !type && ct && ct.indexOf("json") >= 0,
					data = xml ? xhr.responseXML : xhr.responseText;

					if ( xml && data.documentElement.tagName == "parsererror" )
						throw "parsererror";

					// Allow a pre-filtering function to sanitize the response
					// s != null is checked to keep backwards compatibility
					if( s && s.dataFilter )
						data = s.dataFilter( data, type );

					// If the type is "script", eval it in global context
					if ( script )
						jQuery.globalEval( data );

					// Get the JavaScript object, if JSON is used.
					if ( json )
						data = eval("(" + data + ")");

					return data;
					
			} else {

				var ct = xhr.getResponseHeader("content-type"),
					xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0,
					data = xml ? xhr.responseXML : xhr.responseText;

				if ( xml && data.documentElement.tagName == "parsererror" )
					throw "parsererror";

				// Allow a pre-filtering function to sanitize the response
				// s != null is checked to keep backwards compatibility
				if( s && s.dataFilter )
					data = s.dataFilter( data, type );

				// The filter can actually parse the response
				if( typeof data === "string" ){

					// If the type is "script", eval it in global context
					if ( type == "script" )
						jQuery.globalEval( data );

					// Get the JavaScript object, if JSON is used.
					if ( type == "json" )
						data = window["eval"]("(" + data + ")");
				}

				return data;

			}
		}
	});









    /**
    * Business list
    */
    $('#subcategories').change(function()
    {
        var subcategory = $('#subcategories option:selected').val();
        //var business_type = $('#business_type').val();
        
        document.location.href = 
            document.location.pathname + 
            $.query.set('subcategory', subcategory);
    });
    
    
    $('img.overlay_trigger[rel]').overlay();
    $('a.overlay_trigger[rel]').overlay();
    
    /**
    * Blog comments
    */
    $('#blog input.reply').live('click', function()
    {
		var target = $('textarea#comment');

		var author = $(this).parent().parent().prev().find('span.author').html();
		var post = $(this).parent().parent().prev().find('div.comment').html();

		target.val(quote(author, post));
        $('#0').trigger('click');

        target.focus();
        return false;
    });
    
    $('#blog span.delete').click(function()
    {
        if (!confirm('Are you sure to delete this comment?')) {
            return false;
        }
        
        var row = $(this).parents('div.c-line');
        var id = row.attr('id');
        var query_string = $.query.get('user_id') 
            ? '?user_id=' + $.query.get('user_id') 
            : '';
        
        $.post('/user/blog/delete-comment.html' + query_string, 
            {
                id: id, 
                format: 'json'
            }, 
            function(response)
            {
                if (response.affected_rows > 0) {
                    row.slideUp('slow', function()
                    {
                        $(this).remove();
                    });
                }
            }
        , 'json');
    });


    /*
     * Chat room
     * 
     */
    $('#chat-room span.delete').click(function()
    {
        if (!confirm('Are you sure to delete this message?')) {
            return false;
        }

        var row = $(this).parents('div.c-line');
        var id = row.attr('id');
        var query_string = $.query.get('user_id')
            ? '?user_id=' + $.query.get('user_id')
            : '';

        $.post('user/chat-room/delete-message.html' + query_string,
            {
                id: id,
                format: 'json'
            },
            function(response)
            {
                if (response.affected_rows > 0) {
                    row.slideUp('slow', function()
                    {
                        $(this).remove();
                    });
                }
            }
        , 'json');
    });


    /*
     * Venue reviews
     *
     */
    $('#reviews span.delete').click(function()
    {
        if (!confirm('Are you sure to delete this review?')) {
            return false;
        }

        var row = $(this).parents('div.c-line');
        var id = row.attr('id');
        var query_string = $.query.get('user_id')
            ? '?user_id=' + $.query.get('user_id')
            : '';

        $.post('user/reviews/venue/delete.html' + query_string,
            {
                id: id,
                format: 'json'
            },
            function(response)
            {
                if (response.affected_rows > 0) {
                    row.slideUp('slow', function()
                    {
                        $(this).remove();
                    });
                }
            }
        , 'json');
    });


	/**
	 * Wall
	 */
	$('#wall input.reply').live('click', function()
    {
        var target = $('textarea#post');

		var author = $(this).parent().parent().prev().find('span.author').html();
		var post = $(this).parent().parent().prev().find('div.comment').html();
		
		target.val(quote(author, post));
        $('#0').trigger('click');

        target.focus();
        return false;
    });
	$('#wall span.delete').click(function()
    {
        if (!confirm('Are you sure to delete this post?')) {
            return false;
        }

        var row = $(this).parents('div.c-line');
        var id = row.attr('id');

        $.post('user/wall/delete-post.html', 
            {
                id: id,
                format: 'json'
            },
            function(response)
            {
                if (response.affected_rows > 0) {
                    row.slideUp('slow', function()
                    {
                        $(this).remove();
                    });
                }
            }
        , 'json');
    });
	
	$('#wall span.deletereply').click(function()
		    {
		        if (!confirm('Are you sure to delete this reply?')) {
		            return false;
		        }

		        var row = $(this).parents('div.c-line');
		        var id = row.attr('id');

		        $.post('user/wall/delete-reply.html',
		            {
		                id: id,
		                format: 'json'
		            },
		            function(response)
		            {
		                if (response.affected_rows > 0) {
		                    row.slideUp('slow', function()
		                    {
		                        $(this).remove();
		                    });
		                }
		            }
		        , 'json');
		    });	


   $('#venue_url_wrapper input').one('click', function()
   {
       $(this).select();
   });

   var flash_movie;
   $('img#video_picture').click(function(){
      flash_movie = $('#video_object').html();
   });

   $('div.close', 'div#video').click(function()
   {
	   $('#video_object').html(flash_movie);
   });
   
   $('#edit_photo_title_link').live('click', function(){
        $('#title_view_mode').hide();
        $('#title_edit_mode').show();
        return false;     
   });      
    
});


function quote(author, text)
{
	text = text
		.replace(/<blockquote>/gi, '[QUOTE]')
		.replace(/<\/blockquote>/gi, '[/QUOTE]')

		.replace(/<span class="author">/gi, '[AUTHOR]')
		.replace(/<\/span>/gi, '[/AUTHOR]')

		.replace(/<br>/gi, '\n')
		.replace(/\n\n/gi, '\n')
	;

	return '[QUOTE][AUTHOR]' + author + '[/AUTHOR]\n' + text + '[/QUOTE]\n';
}

