// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// Clear Form
$.fn.clearForm = function(){
      return this.each(function(){
            var type = this.type, tag = this.tagName.toLowerCase();
            if (tag == 'form')
                  return $(':input',this).clearForm();
            if (type == 'text' || type == 'password' || tag == 'textarea')
                  this.value = '';
            else if (type == 'checkbox' || type == 'radio')
                  this.checked = false;
            else if (tag == 'select')
                  this.selectedIndex = -1;
      });
};

// scroll to animation
// example usage: $('.scroll_to_here').scrollTo();

jQuery.fn.scrollTo = function(speed) {
  if(speed === undefined ){
    speed = 'slow';
  }
  $('html,body').animate({scrollTop: this.offset().top},speed);
};

function slideShowRotate(){
    var $active = $('.changeSlide li.ac');
    var $next = $active.next();

    if($active.hasClass('last')){
        $('#slide1').addClass('ac');
        $('.slideW div').hide();
        $('#slide1View').show();

    }else{
        $next.addClass('ac');
        $('.slideW div').hide();
        $('#' + $next.attr('id') + 'View').show();
    }
    $active.removeClass('ac');
}

$(function(){

    // Fade out flash messages
    //setTimeout(function() {
    //    $('#notice').fadeOut('slow').removeClass();
    //}, 5000);  Removed Sharon complained that it was too jerky.

    // Search bar focus
    $('#search').focus(function(){$(this).val('')});

    // Home page login
    $('#loginLink').click(function(event){
        event.preventDefault();
        $('#login').fadeIn('slow');
    });

    // Close button for pop in
    $('img#closeButton').click(function(){
        $('.closeButton').fadeOut('slow');
    });

    // Take Action
    $('.takeAction').click(function(){
        window.location = $(this).find('h3 a').attr('href');
        return false;
    });

    // Login Blog Page
    $('#loginBlogDialog').dialog({
        modal:true,
        autoOpen:false,
        title:"Member Login",
        width:380,
        height:335,
        buttons:{
            "Login": function() {
                $('#loginBlogDialog').submit();
            },
            "Close": function() {
                $('#loginBlog').clearForm();
                $(this).dialog("close");
            }
        }
    })
    $('#loginBlogLink').click(function(event){
        event.preventDefault();
        $('#loginBlogDialog').dialog('open');
    });

    // Override Submit - Blog show page
    $('#loginBlogDialog').submit(function(event){
        event.preventDefault();
        $.post($(this).attr('action'), $(this).serialize(), function(){
            window.location = window.location + '#addComment';
            window.location.reload();
        });
    });

    // Newsletter
    $('#newsletterDialog').dialog({
        modal:true,
        autoOpen:false,
        title:"Newsletter Subscribe",
        width:380,
        height:335,
        buttons:{
            "Subscribe": function() {
                $.post($(this).attr('action'), $(this).serialize(), function(res){
                    $('#validateTips').html(res);
                });
            },
            "Close": function() {
                $('#newsletterDialog').clearForm();
                $(this).dialog("close");
            }
        }
    });
    $('.newsletterLink').click(function(event){
        event.preventDefault();
        $('#newsletterDialog').dialog('open');
    });

    // Slide Show click
    $('.cursorEvent').addClass('cursorPointer');

    $('ul.changeSlide li').each(function(){
        $(this).click(function(event){
            event.preventDefault();
            $('ul.changeSlide li').removeClass('ac');
            $(this).addClass('ac');
            $('.slideW div').hide();
            $('#' + this.id + 'View').show();

        });
    });

    //IE6 PNG Fix
    //  if ($.browser.msie && $.browser.version == '6.0') {
    //    jqPngFix();
    //  }

    // Clear Forms
    $('.buttonResetForm').click(function(event){
        event.preventDefault();
        $(this).closest('form').clearForm();
    });

    // sets and toggles a default value for a text input
    $("input.defaultValue").each(function(){
        if(this.value === ""){
            this.value = $(this).attr("default")
        }
    });
    $("input.defaultValue").focus(function(event){
        if(this.value === $(this).attr("default")){
            this.value = ""
        }
    })
    $("input.defaultValue").blur(function(event){
        if($(this).attr("default") && this.value === ""){
            this.value = $(this).attr("default");
        }
    })

    // Update Billing Page - Update Button
    $("#updateBillingTotal").click(function(){
        var membership_amount = 0;
        if(parseInt($('p.billingDetails:first-child span.r').text().replace(/[^0-9.]/, '')) > 0){
            membership_amount = $('p.billingDetails:first-child span.r').text().replace('$', '');
        }

        var user_donation_amount = $('#user_donation_amount').val().replace(/[^0-9.]/, '');
        var total = parseInt(user_donation_amount) + parseInt(membership_amount);
        $("#billingTotal").html('$' + total);
    });

    // Submit paypal
    $('.paypal').click(function(){
      $('.paypal #paypal_form').submit(function(){
        $.get($(this).attr('action'));
        return false;
      });
    });


}); // End on ready

jQuery.ajaxSetup({
    'beforeSend': function(xhr) {
        xhr.setRequestHeader("Accept", "text/javascript")
    }
});

