if( typeof(IDENTITEE) === 'undefined' ) {
	IDENTITEE = {};
}

/*
 * add_to_cart_form sets up the form used both on the lyrics.php and shop.php pages.
 *
 * @param {Function} swap_image is a function that contains the code to change
 * images on click events.  It is optional.
 *
 * @return an object with one public method reset()
 */

IDENTITEE.add_to_cart_form = function( swap_image ) {

	if( (swap_image === 'undefined' ) || ( typeof( swap_image) !== 'function') ) {
		swap_image = false;
	}

	var bindClicksToListItems = function(){
		$('#size-picker > a').click(function(){
			return false;
		});

		$('#size-picker li').click(function() {
		  //retrieves the class name and applies it to the top li of the drop down menu
		  //this will change the image on the top of them menu
		  $('li#size-picker').removeClass().addClass($(this).attr( 'class'));
		  $('input#size').val($(this).children('a').text());

		  //close the menu when a user clicks a size
		  $('ul.sf-menu li:first').hideSuperfishUl();
		  filterSizeOptions();
		  checkOutStock();
		  checkOutStock2();
		  return false;
		});
	},
	checkOutStock = function() {
		var size   = $('#size').val();
		var gender = $('#gender-select').val();
		var color  = $('#color input:checked').val();
		var lyricId= $('#lyric_id_field').val();

		if (lyricId == 2 && gender == 'M' && size == 'L' && color == 'B') {
			$('#add-to-cart-btn').hide();
			$('#out-of-stock').text('*Out of stock');
		} else if (lyricId == 2 && gender == 'M' && size == 'L' && color == 'W') {
			$('#add-to-cart-btn').hide();
			$('#out-of-stock').text('*Out of stock');		
		} else if (lyricId == 2 && gender == 'F' && size == 'XL' && color == 'B') {
			$('#add-to-cart-btn').hide();
			$('#out-of-stock').text('*Out of stock');
		} else if (lyricId == 2 && gender == 'F' && size == 'XL' && color == 'W') {
			$('#add-to-cart-btn').hide();
			$('#out-of-stock').text('*Out of stock');
		} else if (lyricId == 3 && gender == 'F' && size == 'XL' && color == 'B') {
			$('#add-to-cart-btn').hide();
			$('#out-of-stock').text('*Out of stock');
		} else if (lyricId == 1 && gender == 'F' && size == 'XL' && color == 'B') {
			$('#add-to-cart-btn').hide();
			$('#out-of-stock').text('*Out of stock');
		} else {
			$('#add-to-cart-btn').show();
			$('#out-of-stock').text('');
		}
	},
	checkOutStock2 = function() {
		var size   = $('#size').val();
		var gender = $('#gender-select').val();
		var color  = $("input:radio[name=color]:checked").val();
		
		if (gender == 'M' && size == 'L' && color == 'B') {
			$('#add-to-cart-btn-M-B-2').hide();
			$('#out-of-stock-M-B-2').text('*Out of stock');
		} else if (gender == 'M' && size == 'L' && color == 'W') {
			$('#add-to-cart-btn-M-W-2').hide();
			$('#out-of-stock-M-W-2').text('*Out of stock');
		} else if (gender == 'M' && size == 'S' && color == 'B') {
			$('#add-to-cart-btn-M-B-5').hide();
			$('#out-of-stock-M-B-5').text('*Out of stock');
		} else if (gender == 'F' && size == 'XL' && color == 'B') {
			$('#add-to-cart-btn-F-B-2').hide();
			$('#out-of-stock-F-B-2').text('*Out of stock');
			$('#add-to-cart-btn-F-B-1').hide();
			$('#out-of-stock-F-B-1').text('*Out of stock');
			$('#add-to-cart-btn-F-B-3').hide();
			$('#out-of-stock-F-B-3').text('*Out of stock');
		} else if (gender == 'F' && size == 'XL' && color == 'W') {
			$('#add-to-cart-btn-F-W-2').hide();
			$('#out-of-stock-F-W-2').text('*Out of stock');						
		} else {
			$('#add-to-cart-btn-M-B-2').show();
			$('#out-of-stock-M-B-2').text('');
			$('#add-to-cart-btn-F-B-2').show();
			$('#out-of-stock-F-B-2').text('');
			$('#add-to-cart-btn-F-W-2').show();
			$('#out-of-stock-F-W-2').text('');
			$('#add-to-cart-btn-M-W-2').show();
			$('#out-of-stock-M-W-2').text('');
			$('#add-to-cart-btn-M-B-5').show();
			$('#out-of-stock-M-B-5').text('');
			$('#add-to-cart-btn-F-B-3').show();
			$('#out-of-stock-F-B-3').text('');
			$('#add-to-cart-btn-F-B-1').show();
			$('#out-of-stock-F-B-1').text('');
		}
	},
	filterSizeOptions = function(){
		var men = '<li class="small"><a href="#">S</a></li><li class="medium"><a href="#">M</a></li><li class="large"><a href="#">L</a></li><li class="x-large"><a href="#">XL</a></li><li class="xx-large"><a href="#">XXL</a></li>',
			wom = '<li class="x-small"><a href="#">XS</a></li><li class="small"><a href="#">S</a></li><li class="medium"><a href="#">M</a></li><li class="large"><a href="#">L</a></li><li class="x-large"><a href="#">XL</a></li>',
			curr = ( $('#gender-select').val() == 'M') ? men : wom;
		$('#size-picker ul').html(curr);
		if (curr == wom && $('#size-picker').hasClass('xx-large') ) {
			$('#size-picker').removeClass('xx-large').addClass('x-large');
		}

		if (curr == men && $('#size-picker').hasClass('x-small') ) {
			$('#size-picker').removeClass('x-small').addClass('small');
		}

		bindClicksToListItems();

	},
	setup = function() {
		$(document).ready( function() {

			$('ul.sf-menu').superfish( {dropShadows: true, disableHI: false});

			$('#gender-select').change(function(){
				filterSizeOptions();
				if( swap_image ) {
					swap_image();
				}
				checkOutStock();
				checkOutStock2();
			});

			$('#size-button').click( function () {
				$('ul.sf-menu li:first').showSuperfishUl();
			});

			$('#colors label').click(function(){
				$(this).prev('input').attr('checked', true).click();
				$(this).addClass('selected');

				if($(this).siblings('label').hasClass('selected')){
				  $(this).siblings('label').removeClass('selected');
				}

				if( swap_image ) {
					swap_image();
				}
				checkOutStock();
				checkOutStock2();
				return false;
			});

			$('#add-to-cart').submit(function () {
				// yo D! need your help here
				if ('undefined' == typeof $('#colors label.selected').val()) {
				  alert('Please choose a color');
				  return false;
				}
				return true;
			});

			$('#black').click().addClass('selected');



			$('#size_chart_link').boxy({
				modal: true,
				title:'Size Chart',
				closeable:true
			});


			bindClicksToListItems();
			filterSizeOptions();
		});
	}();
	/*
	 * resetFrom sets the gender of the form to male and the color to black
	 *
	 * @param none
	 */

	var resetForm = function() {
			$('#colors label#black').click();
			$('#gender-select').val('M');
			filterSizeOptions();
	};


	return {
		reset: resetForm,
		checkStock: checkOutStock
	};
};