Number.prototype.isEven = function(){
    return (this % 2 == 0) ? true : false;
};

Number.prototype.toCash = function(){
	num = Math.ceil(this * 100) / 100;	
	return num.toFixed(2);
};

var addOnPricing = 0.0;
var regAddOnPricing = 0.0;

$(document).ready(function(){
	
	if ($('.item-select').length) {
			
		regAddOnPricing = parseFloat($('#regular-price span').text().split('$')[1]);
		
		$('.item-select').each(function(){
			
			$(this).unbind('focus');
			
			$(this).bind('focus', function(){
	            
				var oldValue = getValue($(this).attr('value'));				
				var price = parseFloat(getVar($('#base-price').attr('class'), 'price'));
				
				if (oldValue) {
					addOnPricing -= oldValue;
				}
				
				if (addOnPricing < 0) {
					addOnPricing = 0;
				}
								
				$(this).unbind('change');
				
				$(this).bind('change', function(event){
					
	            	var newValue = getValue($(this).find(':selected').attr('value'));
	            	
	            	var price = parseFloat(getVar($('#base-price').attr('class'), 'price'));
	            	    				
					if (newValue) { //if newValue is not zero
						addOnPricing += newValue;    					
					}
					
					if (addOnPricing < 0) {
						addOnPricing = 0;
					}
					
					$(this).blur();
					
					addOnPricing = parseFloat(addOnPricing);
					price += addOnPricing;
					price = price.toFixed(2);
					
					$('#our-price span').html('$' + price);
					
					if ($('#regular-price').length) {
						var regPrice = regAddOnPricing;
						regPrice += addOnPricing;
						regPrice = regPrice.toFixed(2);
						$('#regular-price span').text('$' + regPrice);
					}
					
				});		
				
			});
		
		});
	}
	
});

/**
 * getValue
 * specialized function for the candygram store / multi option update feature
 * pass this function the value of an option, and you will get the price that was attached to that option
 * 
 */

function getValue(optValue) {
	
	var value = optValue.split('(');
	
	if (value.length > 1) {
	
		value = value[1].replace('+$', '');
		value = value.replace(')', '');
		
		return parseFloat(value);
		
	} else {
		
		return 0.0;
		
	}
	
}

/**
 * getVar
 * introduces the ability to pass variables to javascript via the HTML
 * class attribute by searching through string of given classnames to
 * return your desired variable
 * @param classes
 *    a string of class names (preferably using the element.className
 *    prototype attribute)
 * @param variable
 *    the target variable you would like to return
 * @return the value of the desired variable, false if variable is not present
 *
 * @example
 *    the format:
 *    - the variable you would like to pass should be formatted the following way:
 *      - class="var_example*value"
 * @example
 *    the markup:
 *    <a id="page_1_link" class="links var_page*1" href="/add-module/1/1">page 1</a>
 *    the js:
 *    element = $('page_1_link');
 *    the function call:
 *    getVar(element.className, 'page');
 *    - the function would return 1 (the value of the page variable)
 */
function getVar(classes, variable) {
  var arrClasses = '';
  var varClass = false;
  
  arrClasses = classes.split(" ");
  
  if(arrClasses == '') {
    return false;
  }
  
  $(arrClasses).each(function (index, value) {
    if (value.indexOf('var_' + variable) != -1) {
      varClass = arrClasses[(index)];
      return false;
    }   
  });
  
  //Check to make sure varClass is defined
  if(!varClass) {
    return false;
  }
  
  //split the value by the variable name and dash to return the value, if no value is present return false
  //although you shouldn't be cluttering up the markup it's not necessary
  return !varClass.split(variable + '*')[1] ? false : varClass.split(variable + '*')[1];
  
}


$(document).ready(function(){
	
	$('.featured-tabs').unbind();
	
	$('.featured-tabs').bind('click', function(event){
		
		$('.featured-tabs').removeClass('active');
		
		$(this).addClass('active');
		
		$('.featured-tabs-contents').hide();
		
		if ($(this).attr('id') == 'featured-tab-1') {
			$('#featured-tab-1-contents').show();
		}
		
		if ($(this).attr('id') == 'featured-tab-2') {
			$('#featured-tab-2-contents').show();
		}
		
		if ($(this).attr('id') == 'featured-tab-3') {
			$('#featured-tab-3-contents').show();
		}
		
		if ($(this).attr('id') == 'featured-tab-4') {
			$('#featured-tab-4-contents').show();
		}
		
		if ($(this).attr('id') == 'featured-tab-5') {
			$('#featured-tab-5-contents').show();
		}
	});
	
});

$(document).ready(function(){
	
	$('.featured-tabs').unbind();
	
	$('.featured-tabs').bind('click', function(event){
		
		$('.featured-tabs').removeClass('active');
		
		$(this).addClass('active');
		
		$('.featured-tabs-contents').hide();
		
		if ($(this).attr('id') == 'featured-tab-1') {
			$('#featured-tab-1-contents').show();
		}
		
		if ($(this).attr('id') == 'featured-tab-2') {
			$('#featured-tab-2-contents').show();
		}
		
		if ($(this).attr('id') == 'featured-tab-3') {
			$('#featured-tab-3-contents').show();
		}
		
		if ($(this).attr('id') == 'featured-tab-4') {
			$('#featured-tab-4-contents').show();
		}
		
		if ($(this).attr('id') == 'featured-tab-5') {
			$('#featured-tab-5-contents').show();
		}
	});
	
});

$(document).ready(function(){
	
	$('#auto-close img').bind('click', function(event){
		
		$('#auto-add-wrapper').hide();
		
	});
	
});

$(document).ready(function(){
	
	$("#BlankFrame").load(function () {
	    $(".loadingText").remove();
	    $(".success").show();
	    $('#auto-add-wrapper').show();
	    //$('.accessory-image-wrapper').css({'text-align': 'left'});
	    //$('.accessory-image-wrapper').css({'text-align': 'center'});
	    //$('.accessory-image-wrapper').css({'position': 'relative'});
		iloadflag=1;
		iload('');
	});
	
	$('body').bind('click', function(event){
		
		if ($(event.target).hasClass('auto-add')) {		
			
		    $(".success").remove();
		    
			$(event.target).parents('form:first').attr('target', 'BlankFrame');
			
			$(event.target).after('<div class="loadingText">Adding Item to Cart...</div>');
			$(event.target).after('<div style="display: none;" class="success"><img src="http://lib.store.yahoo.net/lib/yhst-131057384930429/ey-item-added.jpg" alt="Item Added" /><br /><a href="http://order.store.yahoo.net/yhst-131057384930429/cgi-bin/wg-order?yhst-131057384930429">View Cart / Checkout</a></div>');
		}
		
	});
	
});
