/* 
 * System JS File
 */

//sets up jQuery calls to work with 'respond_to'
jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})

// Display or hide form
function display_form(caller,response_id, nohide)
{
    //hides the small comment input field if available
    if (caller != null) {
        $(caller).hide();
    };

    //get insertion position
    anchor = $('#'+response_id);
    
    if (!nohide) {
        if (anchor.html().length > 150) {
            anchor.hide();
            anchor.html('');
            return(false);
        }
    }

    //add form html into container div
    $(anchor).append($('#form_template').clone().html());

    //set the response id
    $(anchor).find('#r').attr('value',response_id);

    //make sure textarea is selected
    $(anchor).find('textarea').focus();

    //ensure form is showing
    $(anchor).css('display', 'block');
};

// render the tag list checkboxes
function refresh_taglist(ids) {
    for(i = 0; i < ids.length;i ++) {
        $('.tag_space').append('hello');
    }
}


// Lets go!
$(document).ready(function() {
    
	$('#search-home form').submit(function() {
        if ( $('#main_search').val() == '' ) {
            return false;
        } else {
            return true;
        }
    });
	
	$('#search-box form').submit(function() {
		if ( $('#main_search').val() == '' ) {
			return false;
		} else {
			return true;
		}
	});
	
        
    // Deal with flash notices
    var flash_alert = $('.notice');
    if (flash_alert.length > 0) {
        flash_alert.show().animate({height: flash_alert.outerHeight()}, 200);
        window.setTimeout(function() {
            flash_alert.slideUp();
        }, 5000);
    }   

    // Show extended profile information
    $('#profile_extend').click(function() {
        $('#profile_extend_container').toggle();
    });

    // Show extended profile information
    $('#change_password_form').click(function() {
        $('#change_password').toggle();
    });
    
    // Add a topic to my list
    $('#xadd').live('click', function() {
        href = $(this).attr('href').replace('#', '');
        if (href) {
            $('#add_to_topic').load(href + '.js'); 
        } else {
            alert('There was an error adding this topic to your list. Try again later');
        }
            
        return false;
    });
    
    // Toggle photos
    $('.toggle_photos').live('click', function() {
        $('#attach_photos').toggle();
        return(false);
    });
    
    // Toggle videos
    $('.toggle_videos').live('click', function() {
        $('#attach_videos').toggle();
        return(false);
    }); 
    
    // Show an attachment
    $('.show_attachment').live('click', function() {
        parts = $(this).attr('rel').split('|');
        // alert(parts);
        if (parts.length == 3) {
            $.get('/ajax/show_attachment/' + parts[1] + '?type=' + parts[0] + '&url=' + parts[2], function(data) {
                $('#showit').html(data);
            });
        }
        return false;
    });
    
    // Follow someone
    $("[class*='follow']").live('click', function(){
        url = $(this).attr('href') + '?c=' + $(this).attr('class')
        $.get(url, function(data) {
            $( '#followed_' + url.replace('/account/follow/', '').replace(/\?.*$/, '')).html(data);
//          $('#followed_' + url.replace('/account/follow/', '').substring(indexOf('?'))).html(data);
        });
        
        return(false);
    });
    
    // Email someone
    $('.my-email').live('click', function() {
        url = $(this).attr('href');
        // Toggle the form
        if ($('#user_action').html().length == 0) {
            $.get(url, function(data) {
                $('#user_action').html(data);
                $('#message_subject').focus();
            });
        } else {
            $('#user_action').html('');
        }
        return false;
    });
    
    // Like a post or comment
    $('.like_this').live('click', function() {
        if (id = $(this).attr('href').replace('#', '')) {
            type = $(this).attr('rel');
            url = '/ajax/like/' + id + '?type=' + type
            $('#like_' + id).load(url);
        }
        
        return false;
    });
    
    // Flow over posts
    $('#profile_title').keyup(function() {
        //alert($('#share-title').val().length);
        if ($('#profile_title').val().length >= 72) {
            $('#user_detail').show();   
            $('#profile_content').focus();
        }
    }); 
    
    // Handle shared topics
    $('#shared_topics_form').submit(function() {
        id = $('#topic_id').val();
        $('#shared_topics').load('/ajax/shared_topics/' + id + '.html');
        $('#shared_topics_see_all').attr('href','/people/related?topic=' + id);
        return false;
    });
    
    // Handle confirmations on links
    $('.confirm').live('click', function() {
        msg = $(this).attr('rel') ? $(this).attr('rel') : 'Are you sure?';
        if (confirm(msg)) {
            return true;
        }
        return false;
    });
    
    // Show more content
    $('.gcontent').live('click', function() {
        $('.first_content').hide();
        $('.more_content').show();
        return false;
    });
    
    // Search for tags similar to a query string
    $('.tagsearch').keyup( function() {
        $.get('/ajax/tagsearch/?query=' + $(this).val(), function(data) {
            $('.taglist').html(data);
        });
    });

    // add a new tag
    $('.create_tag_btn').live('click', function() {
        var inpt = $('.tagsearch').val();
        if (inpt) {
            $.get('/ajax/create_tag/?title=' + inpt + '&permalink=' + $('#permalink').val() , function(data) {
                data = data.split(',');
                id = $.trim(data[1]);
                tag = $.trim(data[0]);
                if (id && tag) {
                    $('.tag_space').append('<li><input name="tags[]" type="checkbox" checked="checked" value="' + id + '" /> ' + tag + '</li>');
                }
                $('.taglist').html(' ');
                $('.tagsearch').val('');
                //refresh_taglist(ids);
                //$('.taglist').html(data);
            });
        }
        return false;
    });

    $('.clickable').click( function() {
        window.location = $(this).find('a').attr('href');
    })
});


