$(document).ready(function(){
	
	$('form.login_form input').focus(function(){
		if ($(this).val() == 'Username' || $(this).val() == 'Password') {
			$('form.login_form input').val('');	
		}
	});
	
	//magnify
	$('li.magnify a').click(function(){
		if ($('div#recipe_pic_new').hasClass('expanded')) {
			//contract
			$('div#recipe_pic_new').animate({height: '300px'});
			$('div#recipe_pic_new').removeClass('expanded')	;
			$('li.magnify a').html('View Full Size');
			$('li.magnify').removeClass('contract');
		} else {
			//expand	
			$('div#recipe_pic_new').animate({height: recipe_pic.height});
			$('div#recipe_pic_new').addClass('expanded');
			$('li.magnify').addClass('contract');
			$('li.magnify a').html('View Compact Size');
		}
		});
    
    //toggle home view
    $('a#toggle_home_view').click(function(){
        if ($('ul.all').is(':visible')) {
            $('ul.all').hide();
            $('ul.yours').show();
            $('em#your_all').html('Your');
        } else {
            $('ul.all').show();
            $('ul.yours').hide();            
            $('em#your_all').html('All');
        }
        });
    
    //homepage carousel
    if ($('ul#featured_slideshow').length) {
        $('ul#featured_slideshow').cycle({ 
            fx:     'fade', 
            random:  1 
        });      
    }
    
    //send message button
    $('ul#member_tools li.mail').click(function(){
        $('form#mail_form').submit();
        });
    
    //success notification timeout
    if ($('div#form_success').length) {
        var t = setTimeout("$('div#form_success').fadeOut('slow')",5000);
    }
    
    //change password toggle
    $('a#change_password_link').click(function(){
        $('div#change_password_container').toggle();
        });

    //external links, xhtml friendly
    $('a[rel=external]').click(function(){
        //open new window
        var myurl = $(this).attr('href');
        window.open(myurl, 'external');
        return false;
        });
	
	//delete comment
	$('a.delete_comment').click(function(){
		if (confirm('Really Delete?')) {
			var comment_id = $(this).parents('li').attr('id').replace('comment_','');
			$.ajax({
		   type: "POST",
		   url: "/ajax/delete_comment",
		   data: "id=" + comment_id,
		   success: function(msg){
		   	$('li#comment_' + comment_id).slideUp('slow');
		   }
		 });
		}
		});
	
	//toggle blogthis
	$('li.blog a').click(function(){
		$('div#blogthis').toggle();
		});

	//ban user button
	$('p.ban_user a').click(function(){
		if (confirm('Sure?')) {
		$.ajax({
		   type: "POST",
		   url: "/ajax/ban",
		   data: "id=" + $('input[name=member_id_ban]').val(),
		   success: function(msg){
		   window.location = '/people';
		   }
		 });
		}
		});

	//remove button
	$('li.remove a').click(function(){
		return confirm('Sure?')
		});
	
	//remove button
	$('li.approve a').click(function(){
		return confirm('Sure?')
		});
	
	//delete button
	$('input[name=delete]').click(function(){
		if (confirm('Really Delete?')) {
			$('form#delete_recipe').submit();
		}
		});

	//vote button
	$('a#vote_button_new').click(function(){
		$.ajax({
		   type: "POST",
		   url: "/ajax/vote",
		   data: "id=" + $('input[name=recipe_id]').val(),
		   success: function(msg){
		   	$('a#vote_button_new').fadeOut('slow');
		   	$('ul#recipe_tools').removeClass('vote');
		   }
		 });
		});
	
	//vote button
	$('a#vote_button').click(function(){
		$.ajax({
		   type: "POST",
		   url: "/ajax/vote",
		   data: "id=" + $('input[name=recipe_id]').val(),
		   success: function(msg){
		   	$('div#vote_container').slideUp('fast');
		   }
		 });
		});
	
	//trigger tag auto generate submit
	$('a#auto_generate_button').click(function(){
		var query = $('input[name=title]').val() + ' ' + getSpawContent('ingredients_data') + ' ' + getSpawContent('method_data');
		$('form#create_tags input').val(query);
        $('form#create_tags').submit();
        });

    //ajax form for create tags
    $('form#create_tags').ajaxForm(function(data) {
        if (data) {
            $('textarea[name=tags]').val(data);
            var ingredients = data.split(' ');
            var question = "What's your favorite way to cook with "+ingredients[Math.floor(Math.random()*ingredients.length)]+"?";
            $('span#question_topic').html(question);
            $('textarea[name=question]').val(question);
        }
        });

    //trigger add fan form submit
    $('li.follow a').click(function(){
        $('form#follow_form').submit();
        });

    //ajax form for add fan
    $('form#follow_form').ajaxForm(function(data) {
        if (data) {
            $('li.follow').removeClass('follow').addClass('follower');
            $('li.follower a').html('Following');
        }
        });

    //fade vote panel
    if ($('div#recipe_pic div.owner').length) {
        setTimeout("$('div#recipe_pic div.owner').fadeOut('fast')",2000);
        $('div#recipe_pic').mouseover(function(){
            $('div#recipe_pic div.owner').fadeIn('fast');
            });

        $('div#recipe_pic').mouseout(function(){
            setTimeout("$('div#recipe_pic div.owner').fadeOut('fast')",2000);
            });
            
    }

    //clear search field
    $('input[name=keywords]').click(function(){
        if ($(this).val() == 'Enter Keywords') {
            $(this).val('');
        }
        });

    //trigger view filter
    $('input[name=view_filter]').click(function(){
        $('form#view_filter_form input').val($(this).val());
        $('form#view_filter_form').submit();
        });

    //trigger view filter
    $('input[name=show_latest]').click(function(){
        if ($(this).attr('checked')) {
            $('form#show_latest_form input').val('true');
        } else {
            $('form#show_latest_form input').val('false');
        }
        $('form#show_latest_form').submit();
        });
    
});

function getSpawContent(editor_name) {
   return document.getElementById(editor_name + "_rEdit").contentDocument.body.innerHTML;
}