jQuery(document).ready(function(){
	
	jQuery('#headernav li:last').css({'background':'none','paddingRight':'0'});
	
	
/*
	var arr = jQuery.makeArray(jQuery('#prod_state option'));
	
	_productType = jQuery("#prod_size option:first").val();
	
	if (_productType != 'item') {
	
	jQuery("#prod_state option:not(:first)").each(function () {
		var start = jQuery(this).val().split('_');
		if (start[0] != 'quarter') {
			jQuery(this).remove();
		}	

    });
    
    }

	
	jQuery('#prod_size').change(function() {
  		var option = this.options[this.selectedIndex];
  		var _selectedSize = jQuery(option).val();
  		
  		jQuery('#prod_state').find('option:first').attr('selected', 'selected').parent('select');
  		
  		var output = [];

    	jQuery.each(arr, function(i, val) {
		  output.push('<option value="'+ jQuery(val).attr('value') +'">'+ jQuery(val).text() +'</option>');
		});
		
		jQuery('#prod_state').html(output.join(''));

  		jQuery("#prod_state option:not(:first)").each(function () {
  			var check = jQuery(this).val().split('_');
  			if (check[0] != _selectedSize) {
  				jQuery(this).remove();
  			}
        });
	});
	*/
/*	
	jQuery('#prod_size').change(function() {
		var option = this.options[this.selectedIndex];
  		var _selectedSize = jQuery(option).val();
  		
  		switch(_selectedSize) {
  		
  			case 'quarter' :
  				jQuery('#prod_state_quarter').css({'display':'inline'});
  				jQuery('#prod_state_half').css({'display':'none'});
  				jQuery('#prod_state_full').css({'display':'none'});
  				break;
  			case 'half' :
  				jQuery('#prod_state_quarter').css({'display':'none'});
  				jQuery('#prod_state_half').css({'display':'inline'});
  				jQuery('#prod_state_full').css({'display':'none'});
  				break;
  			case 'full' :
  				jQuery('#prod_state_quarter').css({'display':'none'});
  				jQuery('#prod_state_half').css({'display':'none'});
  				jQuery('#prod_state_full').css({'display':'inline'});
  				break;
  			default :
  				jQuery('#prod_state_quarter').css({'display':'inline'});
  				jQuery('#prod_state_half').css({'display':'none'});
  				jQuery('#prod_state_full').css({'display':'none'});
  				break;
  		}
	
	});
*/	
/*
	jQuery('#productsform').submit(function() {
		var _showAlert = false;
  		var _selectedSize = jQuery('#prod_size').val();

		if (jQuery('#prod_state_'+_selectedSize).val()=='') {
			_showAlert = true;
		}

		if (_showAlert) {
  			alert('Please choose a Delivery State');
  			return false;
  		} else {
  			switch(_selectedSize) {
  				case 'quarter' :
  					jQuery('#prod_state_half').empty().remove();
  					jQuery('#prod_state_full').empty().remove();
  					break;
  				case 'half' :
  					jQuery('#prod_state_quarter').empty().remove();
  					jQuery('#prod_state_full').empty().remove();
  					break;
  				case 'full' :
  					jQuery('#prod_state_quarter').empty().remove();
  					jQuery('#prod_state_half').empty().remove();
  					break;
  			}
  			return true;
 		}
 		
  		return false;
	});
*/	

 	jQuery(".fancyvideo").click(function() {
		jQuery.fancybox({
			'padding'		: 10,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'overlayOpacity':	0.8,
			'overlayColor'	: '#000',
			'width'			: 536,
			'height'		: 360,
			'href'			: this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=jQuery1'),
			'type'			: 'swf'
		});

		return false;
	});
	
	jQuery("#register_new").bind("submit", setScreenName); 
	
	jQuery("#goto_payment").bind("submit", toggleSubmit); 
	
	setBillingInfo();
	
	jQuery("#prod_state").change(function() {
		if (jQuery("select#prod_state").val()!='') {
			jQuery("#addtocart_button").show();
		} else {
			jQuery("#addtocart_button").hide();
		}
	});
	
});

setScreenName = function() {
	_first = jQuery('#first_name').val();
	_last = jQuery('#last_name').val();
	_screenName = _first+' '+_last;
	jQuery('#screen_name').val(_screenName);
	
}

toggleSubmit = function() {
	jQuery('#mpayment').css({'display':'none'})
	jQuery('#wait').css({'display':'block'})
}


setBillingInfo = function() {

	
	_useBillingInfo = $("input:checked[name=use_billing_info]").val();
	
	if (_useBillingInfo) {
		jQuery('#shipping_first_name').val(jQuery('#first_name').val());
		jQuery('#shipping_first_name').attr('readonly', true);
		
		jQuery('#shipping_last_name').val(jQuery('#last_name').val());
		jQuery('#shipping_last_name').attr('readonly', true);
		
		jQuery('#shipping_address').val(jQuery('#address').val());
		jQuery('#shipping_address').attr('readonly', true);
		
		jQuery('#shipping_address2').val(jQuery('#address2').val());
		jQuery('#shipping_address2').attr('readonly', true);
		
		jQuery('#shipping_city').val(jQuery('#city').val());
		jQuery('#shipping_city').attr('readonly', true);
		
		if (jQuery('#state').val()!='') {
			_select = jQuery('#state').val();
			
			//jQuery("#shipping_state option:eq("+_select+")").attr("selected", "selected");
			
			jQuery("#shipping_state").val(_select);
			
			jQuery('#shipping_state').attr('readonly', true);
		}
		//jQuery('#shipping_state').val(jQuery('#state').val());
		
		
		jQuery('#shipping_zip').val(jQuery('#zip').val());
		jQuery('#shipping_zip').attr('readonly', true);
	} else {

		jQuery('#shipping_first_name').attr('readonly', false);
		jQuery('#shipping_last_name').attr('readonly', false);
		jQuery('#shipping_address').attr('readonly', false);
		jQuery('#shipping_address2').attr('readonly', false);
		jQuery('#shipping_city').attr('readonly', false);
		jQuery('#shipping_state').attr('readonly', false);
		jQuery('#shipping_zip').attr('readonly', false);
	}
	
}


imageClickChanger = function() {	
	clearTimeout(interval);
	var _gotoImage = jQuery(this).attr('rel');	
	jQuery("#slideClicker a").unbind('click', imageClickChanger);
	var _currentOffset = parseInt(jQuery('#headercontrol_currentoffset').text());
	jQuery('#headercontrol_ajaxreturn').load('/embeds/_ajax_header_image/'+_gotoImage, function() {
		jQuery('#slideContainer').prepend(jQuery('#headercontrol_ajaxreturn').html());
		jQuery("#slideContainer div").each(function(){	
			_thisDiv = $(this).attr('id');
			if ('slide_'+_gotoImage != _thisDiv) {
				jQuery('#'+_thisDiv).fadeOut('slow', function() {
					jQuery('#'+_thisDiv).remove();
				});
			}
		});
		updateCounts(_currentOffset,_gotoImage);
	  	jQuery("#slideClicker a").each(function(){
			if(jQuery(this).attr("rel")==_gotoImage) {
				jQuery(this).addClass('on');
			} else {
				jQuery(this).removeClass('on');
			}
		});	
		jQuery("#slideClicker a").bind('click', imageClickChanger);
	});
}

updateCounts = function(xCurrent,xNew) {
	doCleanUp();
	jQuery('#headercontrol_currentoffset').text(xNew);
}

doCleanUp = function() {
	jQuery('#headercontrol_ajaxreturn').html('');
}

preloadImage = function() {
	var _totalCount = parseInt(jQuery('#headercontrol_total').text());
	var _currentOffset = parseInt(jQuery('#headercontrol_currentoffset').text());
	var _tempcount = _currentOffset+1;
	if (_totalCount == _tempcount) {
		var _newOffset = 0;
	} else {
		var _newOffset = _currentOffset + 1;
	}
	jQuery('#headercontrol_ajaxreturn').load('/embeds/_ajax_header_image/'+_newOffset, function() {
  		jQuery('#slideContainer').prepend(jQuery('#headercontrol_ajaxreturn').html());
  		interval = setTimeout(function(){doImageRotation(_currentOffset,_newOffset)},4000);
	});
}

doImageRotation = function(xCurrent,xNew) {
	//jQuery("#slideClicker a").unbind('click', imageClickChanger);
	jQuery('#slide_'+xCurrent).fadeOut('slow', function() {
    	jQuery('#slide_'+xCurrent).remove();
    	updateCounts(xCurrent,xNew);
	  	//jQuery("#slideClicker a").each(function(){
		//	if(jQuery(this).attr("rel")==xNew) {
		//		jQuery(this).addClass('on');
		//	} else {
		//		jQuery(this).removeClass('on');
		//	}
		//});	
		//jQuery("#slideClicker a").bind('click', imageClickChanger);
		preloadImage();
  	});
}








