$(function() {
	
	$("form.update :input[name^='attributevalue']").change(function() {
		
		options = new Array();
		
		$(this).parents("form").find(":input[name^='attributevalue_']").each(function() { 
			options.push( $(this).val() ); 
		});
				
		// Go through each attribute in the array...
		for ( var i in attributes ) {
			
			// Should we update?
			var update;
			
			// We'll need this outside of the inner for loop.
			var update = false;
			
			for( var j in attributes[i].attributes ) {
				// If the current attributes attributes match the current option or it's value is "All", we're good.. if not, break out of loop...
				update = ( attributes[i].attributes[j] == "All" ) ? true : ( attributes[i].attributes[j] == options[j] ) ? true : false ;
				if ( !update ) { break; }
			}
			
			// If update is true, log it and break out of current loop...
			if ( update ) {
				current = parseFloat( $('#details table #price td').text().replace(/([^\d\.]+)/g, '') );
				$('#details table #price td').text(attributes[i].showPrice);
				$('#details table #sku td').text(attributes[i].sku);
				break; 
			}
			
			
		}
		
	});
	
		// This is the form validation!
    $("#AddToCart").submit(function(e) {
        
		var submit = true;
	
		// Go through each input and make sure there's something there...
		$(this).find(':input').each(function() {
			    
			input = $(this);
			
			tag = this.tagName.toLowerCase();
			tr =  input.parents('tr');
			
			classes = input.attr('class').split(' ');
			value = input.val();
			
			errors = new Array();
			    
		    $.each( classes, function( i, _class ) { 
			    if ( _class.match(/^(required|minimum|match|max)/) ) {
			        rule = _class.split('-')[0];
			        switch ( rule ) {
			            case 'required':
			                if( value == 0 || value.match(/^\s+$/) || !value ) {
			                    if ( tag == 'select' ) {
			                        errors.push('Choose ' + tr.attr('id').replace(/^Attribute/, ''));
			                    } else {
                                    errors.push("This field is required!");
			                    }
			                }
			            break;
			            case 'minimum':
			                minimum = _class.split('-')[1];
			                if ( parseInt(value) === 0 ) {
			                	errors.push("Confirm Amount");
			                } else if ( parseInt(value) < parseInt(minimum) ) {
			                    errors.push("The minimum order quantity is " + minimum);
			                }
			            break;
			            case 'match':
			                match = _class.split('-')[1];
			                if( $('#' + match).val() != value ) {
			                    errors.push("There was a mismatch!");
			                }
			            break;
			            case 'max':
			                max = _class.split('-')[1];
			                if ( parseInt(value) > parseInt(max) ) {
			                    errors.push("You may not order this many items.");
			                }
			            break;
			        }
			    }
			});
			
			tr.find('td ul.errors').remove();
			tr.removeClass('error');
			if( errors.length ) {
				tr.addClass('error');
			    input.after('<ul class="errors"></ul>');
    			    $.each( errors, function( i, error ) {
    			        input.next('ul.errors').append('<li>' + error + '</li>');
    			    });
			    submit = false;
			}
			        			
		});
		
		return submit;
		
	});
		
	// The "Extra Information" area..
	$('#extras #icons').tabs();
	
	$('#dialogs div.dialog').dialog({
		autoOpen: false, 
		modal: true, 
		width: 850, 
		height: 400,
		draggable: false,
		overlay: {
			opacity: 0.6,
			backgroundColor: '#000'
		}
	});
	
	$('div.ui-dialog-content').css({'overflow-y':'scroll','height':'353px'});
	
	$('#windows li a').click(function() {
		$('#' +  $(this).attr('href').split('#')[1] ).dialog('open');
		return false;
	});
	
});