jQuery(function() {
    /* Drop-down */
    jQuery('.show-dd').hover(function() {
        jQuery('.profile-nav .drop-down').slideUp('fast');
        var target = jQuery(this).children('.drop-down');
        try { clearTimeout(hover_intent_out); } catch(e) { };
        hover_intent = setTimeout(function() { target.slideDown('fast'); }, 200);
    }, function() {
        var target = jQuery(this).children('.drop-down');
        try { clearTimeout(hover_intent); } catch(e) {};
        hover_intent_out = setTimeout(function() { target.slideUp('fast'); }, 200);
    });

	/* input, textarea Hint */
	var value = '';
	jQuery('.layout-field').focus(function() {        
		if ((jQuery(this).attr('value') != '') && ! jQuery(this).hasClass('field-processed'))
		{
            value = jQuery(this).attr('value');
            jQuery(this).attr('value', '');
		}
        else if (jQuery(this).attr('value') == value)
        {
            jQuery(this).attr('value', '');
        }
        jQuery(this).addClass('field-processed');
	});
	jQuery('.layout-field').blur(function() {
		if (jQuery(this).attr('value') == '')
			jQuery(this).attr('value', value);
	});

    /* Tree Photos */
    function activate_deactivate()
    {        
        var current_photo = jQuery('#photo-controls span.current_photo');
        var total_photos = jQuery('#photo-controls span.total_photos');
        var control_prev = jQuery('#photo-controls a.prev_photo');
        var control_next = jQuery('#photo-controls a.next_photo');

        control_prev.removeClass('inactive');
        control_next.removeClass('inactive');
        
        (current_photo.text() == 1) ? control_prev.addClass('inactive') : '';
        (current_photo.text() == total_photos.text()) ? control_next.addClass('inactive') : '';
    }
    activate_deactivate();
    jQuery('#photo-controls a.prev_photo').click(function() {
        var current_photo = jQuery('.photo img.tree_photo:visible');
        var prev = jQuery(current_photo).prev('img.tree_photo');
        if (prev.size() != 0)
        {
            jQuery(current_photo).fadeOut(function() {
                prev.fadeIn();
                var photo_number = jQuery('#photo-controls span.current_photo').text();
                jQuery('#photo-controls span.current_photo').text(eval(photo_number)-1);
                
                activate_deactivate();
            });
        }       
        return false;
    });
    jQuery('#photo-controls a.next_photo').click(function() {        
        var current_photo = jQuery('.photo img.tree_photo:visible');
        var next = jQuery(current_photo).next('img.tree_photo');
        if (next.size() != 0)
        {
            jQuery(current_photo).fadeOut(function() {
                next.fadeIn();
                var photo_number = jQuery('#photo-controls span.current_photo').text();
                jQuery('#photo-controls span.current_photo').text(eval(photo_number)+1)

                activate_deactivate();
            });
        }        
        return false;
    });

    /* Filter */    
    jQuery('a.toggle-filter').click(function() {
        jQuery(this).parent('div').next('.form-wrapper').slideToggle();
        return false;
    });   
    jQuery('.filtered a').click(function() {
        var href = jQuery(this).attr('href');
        var parts = [];
        parts = href.split('/');        
        var offset = parts[parts.length-1];

        var current_attr = jQuery('form#filter').attr('action');
        jQuery('form#filter').attr('action', current_attr+'/'+offset);
        jQuery('form#filter').submit();
        return false;
    });

    /* Select All/None Checkboxes */
    jQuery('a.toggle').click(function() {
        if(jQuery('.toggle').is(':checked'))
        {
            jQuery('.single-toggle, .toggle').removeAttr('checked');
        }
        else
        {
            jQuery('.single-toggle, .toggle').attr('checked', 'checked');
        }
        return false;
    })
    jQuery('input.toggle').click(function() {
        if (jQuery(this).is(':checked'))
        {
            jQuery('.single-toggle').attr('checked', 'checked');
            jQuery('.toggle').attr('checked', 'checked');
        }
        else
        {
            jQuery('.single-toggle').removeAttr('checked');
            jQuery('.toggle').removeAttr('checked');
        }
    });
    jQuery('.single-toggle').click(function() {
        if (jQuery('.toggle').is(':checked'))
        {
            jQuery('.toggle').removeAttr('checked');
        }
        /* check '.toggle' checkboxes if all '.single-toggle' checkboxes are checked */
        var all_checked = 1;
        for (var j = 0; j < jQuery('.single-toggle').length; j++)
        {
            if ( ! jQuery('.single-toggle:eq('+j+')').is(':checked'))
            {
                all_checked = 0;
            }
        }
        if (all_checked == 1)
        {
            jQuery('.toggle').attr('checked', 'checked');
        }
    });

	/* Sign-Up & Member Search */
    if (jQuery('.type').is(':visible'))
    {
        jQuery('.type .choice').css('cursor', 'pointer');
        var current_id;
        if (jQuery('input[type="hidden"]').attr('id') == 'type')
            current_id = jQuery('input[type="hidden"]').val();
        else
            current_id = jQuery('.type .choice input:checked').attr('id');        
        var hide = '';
        if (current_id == 'private') hide = 'corporate'; else if (current_id == 'corporate') hide = 'private';
        jQuery('tr.'+hide).hide();
        jQuery('.type .choice').click(function() {
            jQuery('.type .choice input').removeAttr('checked');
            jQuery(this).children('input').attr('checked', 'checked');
            jQuery('tr.private, tr.corporate').hide();
            var show = jQuery(this).children('label').attr('for');
            jQuery('tr.'+show).show();
        });
    }
    /* Country & ZIP Code */
    var country = 'United States';
    if (jQuery('select#country option:selected').text() == country) jQuery('tr.optional').show(); else jQuery('tr.optional').hide();
    jQuery('select#country').change(function() {
        if (jQuery('select#country option:selected').text() == country) jQuery('tr.optional').show(); else jQuery('tr.optional').hide();
    });

	/* Captcha */
	jQuery('a#reload').click(function() {
		jQuery('#captcha').attr('src', './captcha/' + (new Date()).getTime());
		return false;
	});

    /* Edit Description */
    jQuery('#edit-dedication').livequery('click', function () {
        var old_dedication = jQuery('#dedication').html().replace(/<br\s*\/?>/mg, '');
        jQuery('#dedication').before('<textarea name="edit_dedication" id="dedication-field" class="layout-field">'+old_dedication+'</textarea>');
        jQuery('#edit-dedication').hide();
        jQuery('#editing-dedication').show();
        jQuery('#dedication').hide();

        return false;
    });
    jQuery('#cancel-dedication').livequery('click', function() {
        jQuery('#dedication').show();
        jQuery('#dedication-field').remove();
        jQuery('#edit-dedication').show();
        jQuery('#editing-dedication').hide();

        return false;
    });
    jQuery('#confirm-dedication').livequery('click', function() {
        var new_dedication = jQuery('#dedication-field').val();
        jQuery('#dedication-field').remove();
        jQuery('#dedication-processing').show();
        jQuery.post('./trees/dedication', { edit_dedication : new_dedication, tree_id : jQuery('#tree_id').text() }, function(data) {
            jQuery('#dedication-processing').hide();
            jQuery('#dedication').html(data).show();
        });
        jQuery('#editing-dedication').hide();
        jQuery('#edit-dedication').show();

        return false;
    });

    /* Market */
    jQuery('div.featured_product').click(function() {
        window.location = jQuery(this).attr('rel');
    });
    jQuery('#market input[type="text"]').focus(function() {
        this.select();
    });
    jQuery('#market a.more').livequery('click', function (){
        var prefix = jQuery(this);
        prefix.fadeOut('fast', function() {
            prefix.siblings('p.short_desc').slideUp('fast', function() {
                prefix.siblings('p.long_desc').slideDown('fast', function() {
                    prefix.siblings('a.less').fadeIn('fast');
                    prefix.parent('td').prev('td').children('a.img-more').addClass('img-less').removeClass('img-more');
                });
            });
        });

        return false;
    });
    jQuery('#market a.less').livequery('click', function (){
        var prefix = jQuery(this);
        prefix.fadeOut('fast', function() {
            prefix.siblings('p.long_desc').slideUp('fast', function() {
                prefix.siblings('p.short_desc').slideDown('fast', function() {
                    prefix.siblings('a.more').fadeIn('fast');
                    prefix.parent('td').prev('td').children('a.img-less').addClass('img-more').removeClass('img-less');
                });
            });
        });

        return false;
    });
    jQuery('#market a.img-more').livequery('click', function (){
        var initial = jQuery(this);
        var prefix = jQuery(this).parent('td').next('td').children('a.more');
        prefix.fadeOut('fast', function() {
            prefix.siblings('p.short_desc').slideUp('fast', function() {
                prefix.siblings('p.long_desc').slideDown('fast', function() {
                    prefix.siblings('a.less').fadeIn('fast');
                    initial.addClass('img-less').removeClass('img-more');
                });
            });
        });

        return false;
    });
    jQuery('#market a.img-less').livequery('click', function (){
        var initial = jQuery(this);
        var prefix = jQuery(this).parent('td').next('td').children('a.less');
        prefix.fadeOut('fast', function() {
            prefix.siblings('p.long_desc').slideUp('fast', function() {
                prefix.siblings('p.short_desc').slideDown('fast', function() {
                    prefix.siblings('a.more').fadeIn('fast');
                    initial.addClass('img-more').removeClass('img-less');
                });
            });
        });

        return false;
    });    
});