$(document).ready(function() {

	var swapMessage = '<span class="collection-message">Changes made to this collection will not take effect until you click Save Changes at the bottom of the screen.</span>';

	function changeRow($row) {
		$row.addClass('changed').children().css('background-color', '#eed2d2').children('.pcollections').css('background-color', '#eed2d2').find('.collection-details').prepend(swapMessage).find('.collection-message').parents('.pcollections').find('.collection-thumb').css('border', '1px solid #eed2d2');
	};

	// Find all of the buttons and run a for each statement to find the toal amount
	// of rows that are being displayed. This will be used in to see if a button is in
	// the first or last row. If a button is in the first or last row it has a disabled class
	// added. There is css that adds a display:hidden to button.disabled.
 	var buttons = $('.order-controls button');
	var weight_fields =  $('.order-controls input:hidden');
	var total_weight = 0;
	weight_fields.each(function () {
		++total_weight;
	});
	buttons.each(function (ind, ele) {
    $ele = $(ele);
  	weight = $(ele).parent('div').parent('.order-controls').find('input:hidden').attr('value');
		if (weight == 1) {
			$(ele).parent('div').parent('.order-controls').find('.move-up button').addClass('disabled');
		}else if  (weight == total_weight) {
			$(ele).parent('div').parent('.order-controls').find('.move-down button').addClass('disabled');
		}
  });

  $('.order-controls button').bind('click', function(e) {

    // Get the necessary information on this row.
    //var button = $(this);
    var weight = $(this).parent('div').parent('.order-controls').find('input:hidden').eq(0);
    var row = $(this).parent('div').parent('.order-controls').parents('tr');

    // Look up the right row to swap with, and swap visual positions.
    var swapRow;
    var swapWeight;

    // In Internet Explorer 6 and 7, the contents of the button element (which
    // should just be its label) are incorrectly returned in place of the "value"
    // attribute.  This is a bug in the IE engine.  Because there is apparently
    // no way around that, we have to check for both the value attribute ("up")
    // and the label ("Move up").  Users if Internet Explorer should be aware
    // that the continued use of IE is severely harming the Internet, and should
    // be ashamed of themselves for being part of it.
    switch ($(this).attr('value')) {
      case 'up':
      case 'Move up':
        swapRow = $(row.get(0)).prev('tr');
        if (swapRow[0] != row[0]) {
          row.remove().insertBefore(swapRow.get(0));
					if (!row.is('.changed')) {
						changeRow(row);
					}
        }
        break;
      case 'down':
      case 'Move down':
        swapRow = $(row.get(0)).next('tr');
        if (swapRow[0] != row[0]) {
          row.remove().insertAfter(swapRow.get(0));
					if (!row.is('.changed')) {
						changeRow(row);
					}
        }
        break;
    }

    // Now swap the weight values, which is what actually gets submitted.
    swapWeight = $(swapRow.get(0)).find('input:hidden');
    var temp = weight.val();
    weight.val(swapWeight.val());
    swapWeight.val(temp);
		
		// Run through the buttons again and add disabled classes to the new items
		// at the top and bottom of the page. Remove disabled classes on any item
		// that is not at the top  or bottom.
		buttons = $('.order-controls button');
		buttons.each(function (ind, ele) {
			$ele = $(ele);
			weight = $(ele).parent('div').parent('.order-controls').find('input:hidden').attr('value');
			if (weight == 1) {
				$(ele).parent('div').parent('.order-controls').find('.move-up button').addClass('disabled');
			}else if  (weight == total_weight) {
				$(ele).parent('div').parent('.order-controls').find('.move-down button').addClass('disabled');
			} else {
				$(ele).parent('div').parent('.order-controls').find('.move-down button, .move-up button').removeClass('disabled');
			}
		});

  });
	$('.collection-desc .form-textarea').keydown(function (e) {
		$this = $(this);
		keycode = new Array('40','37','38','39','9','16','36','35','33','34','116','12');
		row = $(this).parent('div').parent('.collection-desc').parents('tr');
		if($this.attr('class').indexOf('text-changed') < 0 && $.grep(keycode, function(element, i){return element == e.keyCode;}) <= 0) {
			$this.addClass('text-changed');
			changeRow(row);
		}
	})
});