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

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

$.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 = '';
			this.disabled = false;
		}
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    else if (tag == 'select')
      this.selectedIndex = -1;
  });
};

(function($) {  
 // Returns whether or not a result set has results in it
 $.fn.outerHTML = function() {
   return $('<div>').append( this.eq(0).clone() ).html();
 };
})(jQuery);



$(function() {
  // commands go here
	$('.copyForm:first').find('span').html("");

	$("#product_category_name").autocomplete("/autocompletes/categorylist.js", {
		width: 215,
		selectFirst: true
	});
		
	$("#product_player_name").autocomplete("/autocompletes/playerlist.js", {
		width: 215,
		selectFirst: true
	});
	
	$("#product_team_name").autocomplete("/autocompletes/teamlist.js", {
		width: 215,
		selectFirst: true
	});
	
	$("#product_brand_name").autocomplete("/autocompletes/brandlist.js", {
		width: 215,
		selectFirst: true
	});
	
	$("#product_style_name").autocomplete("/autocompletes/stylelist.js", {
		width: 215,
		selectFirst: true
	});
		
	$("#product_condition_name").autocomplete("/autocompletes/conditionlist.js", {
		width: 215,
		selectFirst: true
	});
	
	$("#product_conditiondetail_name").autocomplete("/autocompletes/conditiondetaillist.js", {
		width: 215,
		selectFirst: true
	});
	
	$("#product_stiching_name").autocomplete("/autocompletes/stichinglist.js", {
		width: 215,
		selectFirst: true
	});
	
	$("input[id$='size_name']").livequery(function() {
		$(this).autocomplete("/autocompletes/sizelist.js", {width: 215, selectFirst: true	});
	});
		
	$("input[id$='gender_name']").livequery(function() {
		$(this).autocomplete("/autocompletes/genderlist.js", {width: 215, selectFirst: true	});
	});
	
	$("input[id$='color_name']").livequery(function() {
		$(this).autocomplete("/autocompletes/colorlist.js", {width: 215, selectFirst: true	});
	});
	
	$(".deletelink").live("click", function() {
		
		//get the id of the photo
		p = $(this).parent().attr("className");
		p = p.split(" ");
		r = "id=" + p[0];
		
		//call a rails route with this id
		$.ajax({
         type : "POST",
         url  : "/productphoto/photodeletetemp",
         data : r,
         success: $(this).parent().remove()
       });
		
		//remove the photo from the view
		
	})
	
	$(".updatelink").live("click", function() {
		
		//get the id of the photo
		p = $(this).parent().attr("className");
		p = p.split(" ");
		r = "id=" + p[0];
		
		//call a rails route with this id
		$.ajax({
         type : "POST",
         url  : "/productphoto/photodelete",
         data : r,
         success: $(this).parent().remove()
       });
		
		//remove the photo from the view
		
	})
	
	$(function() {
		$("#thumbnails").sortable({
			placeholder: 'ui-state-highlight'
		});
		$("#thumbnails").disableSelection();
	});
			
})
