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

initialize_layout = function() {
	initialize_widths()
	initialize_category_filter()
}

initialize_widths = function() {
	win_width = $(window).width()
	$("#footer, #main_content").each(function() {
		pad = parseInt($(this).css("padding-left")) + parseInt($(this).css("padding-right"))
		$(this).width(win_width - pad)
	});
}

initialize_category_filter = function() {
  $('#main_nav_categories a').click(function() {
    if ($('#main_nav_categories').hasClass("hover")) {
      $("#cat_list").hide()
      $('#main_nav_categories').removeClass("hover")
    }
    else{
      $('#main_nav_categories').addClass("hover")
      $("#cat_list").show()
    }
  })

  return false;
}

toggle_side_box = function(link) {
  div = $("#find_" + link.id.replace("_link", ""))
  $link = $(link)
  div.toggle()
  if ($link.hasClass("expandable_open")) {
    $link.removeClass("expandable_open").addClass("expandable_closed")
  }
  else{
    $link.removeClass("expandable_closed").addClass("expandable_open")
  }
}

confirm_bid = function() {
	val = $("#auction_bid_max_bid").val()
	if (val.blank()) {
		alert("You must enter a bid")
	}
	else if (isNaN(parseFloat(val))) {
		alert("Please remove any extra symbols or characters from your bid like '$'. Only use numbers and '.'")
	}
	else if (parseFloat(val) < current_bid) {
		alert("Your bid must be higher that $" + current_bid)
	}
	else{
		$("#bid_confirm #confirm_bid").html("$" + val)
		$('#bid_submit_helper').click()
	}

}

confirm_buy_it_now = function() {
	val = $("#auction_bid_max_bid").val()
	
	$("#bid_confirm #confirm_bid").html("$" + val)
	$('#bid_submit_helper').click()

}

open_message = function(obj) {
	$panel = $(obj).siblings(".main_message_entry")
	$panel.toggle()
	$(obj).html($panel.is(":visible") ? "View Less" : "View More")
}

initialize_category_tree_select = function() {
	$(document).ready(function() {
    $("li.inner input:radio").click(function() {
      if (this.checked) {
        $(this).siblings("ul.outer").show()
				$(this).parents("li").siblings("li").children("ul.outer").hide()
      } 
    })
  })
}


initialize_sort_by_select = function() {
  $(document).ready(function() {
    $("#auction_filter_sort").change(function() {
      $.ajax({
        data: $.param({sort: $("#auction_filter_sort").val() }),
        success: function(request) {
          $('#content').html(request);
        },
        type: 'post',
        url: $("#auction_filter_sort").parents(".sort_form").attr("action")
      });
    })
  })
}

initialize_max_bid = function() {
  $(document).ready(function() {
    $("#your_bid").submit(function() {
      if (!confirmed) {
        return false;
      };
      if (confirmed) {
        form = this
        $.ajax({
          data: $.param($(this).serializeArray()) + '&authenticity_token=' + encodeURIComponent('IYSaPfKZVcaguWju/agDY3UdByujdsbWYbM2w+UxteU='),
          dataType: 'script',
          type: 'post',
          url: form.attr("action")
        });
      };
      return false;
    })
  })
  
}

initialize_new_2 = function() {
  $(document).ready(function() {
     $('#terms_link').attr("target", "_blank")
    CKEDITOR.replace( 'auction_description',
    {
      toolbar : 'Full',
      uiColor : '#9AB8F3'
    });
    $("#auction_location").focus(function() {
      if ($(this).hasClass("empty")) {
        $(this).removeClass("empty")
        $(this).val("")
      }
      else{
        
      }
    }).blur(function() {
      if ($(this).val() == "") {
        $(this).addClass("empty")
        $(this).val("Austin, TX")
      }
      
    })
    $("#auction_return_policy").change(function() {
      if ($("#auction_return_policy")[0].checked) {
        $("#auction_return_container").show()
      }
      else{
        $("#auction_return_container").hide()
      }
    })
    $("#auction_use_paypal").change(function() {
      if ($("#auction_use_paypal")[0].checked) {
        $("#auction_paypal_container").show()
      }
      else{
        $("#auction_paypal_container").hide()
      }
    })
  })
}