jQuery(document).ready(function(){
	// call easySlider for feature box
	jQuery("#slider").easySlider( {
		numeric: true,
		numericId: 'slider_controls',
		auto: true,
		continuous: true,
		pause: 5000
	});	
	
	// add .last to last child of various types
	jQuery(".nav-container li:last-child, #front_page_posts div.post_wrapper:last-child, #product_tabs li:last-child").addClass("last");

	//variable to hold input values
	var default_values = new Array();

	//clear input text fields
	jQuery("input[type=text], input[type=password], textarea").focus(function() {
	
		if (!default_values[this.id]) {
			default_values[this.id] = this.value;
		}
		if (this.value == default_values[this.id]) {
			this.value = '';
		}
	
		jQuery(this).blur(function() {
			if (this.value == '') {
				this.value = default_values[this.id];
			}
		});
	});

	//product page tabs
	jQuery("#product_tabs").tabs();
	
	//set object variables for various elements
	var addToCartButton = jQuery('#customize_and_purchase button.btn-cart');
	var widthAttribute = jQuery('#attribute528');
	var heightAttribute = jQuery('#attribute529');
//	var  = jQuery('select.product-custom-option');
	
	//disable button 
	addToCartButton.attr( 'disabled', 'disabled').css('opacity', .2);
	
	var heightInput;
	var widthInput;
	
	
	//calculator 
	jQuery('#calculate').click( function() {

		//get value from form
		heightInput = jQuery('#product_height').val();
		widthInput = jQuery('#product_width').val();
		/*
		console.log(heightInput);
		console.log(widthInput);
		*/
		//change string to integer
		heightInput = parseInt(heightInput);
		widthInput = parseInt(widthInput);
		
		//variables to check if value set
		var heightSet = 0;
		var widthSet = 0;
		
		//get product name
		var productName = jQuery('.product-name h1').html();
		
				
		
		//deal with min and max width variances
		
		var minWidth;
		var maxWidth;

		if(productName == 'Roller') {
			minWidth="355";
			maxWidth="2100";
		}
		
		else if(productName == 'Cellular') {
			minWidth="355";
			maxWidth="1800";
		}
		
		else {
			minWidth="355";
			maxWidth="2400";
		}
		
		
		//deal with 2 anomaly products
		if ( productName == 'Roller' || productName == 'Cellular' ){
		
			/*SET HEIGHT*/
			if ( heightInput < 400 ) {
				heightAttribute.val('');
				jQuery('#calculator_results').html('<h4>The height must be at least 400mm</h4>').fadeIn(1500).delay(3000).fadeOut('slow');
			heightSet = 0;
			}
			else if( 400 <= heightInput && heightInput <= 2100 ) {
				heightAttribute.val('24');
				heightSet = 1;
			}
			else if( 2100 < heightInput ) {
				heightAttribute.val('');
				jQuery('#calculator_results').html('<h4>The height must be below 2100mm</h4>').fadeIn(1500).delay(3000).fadeOut('slow');
				heightSet = 0;
			}
			
			/*SET WIDTH*/
			if ( widthInput < 355 ) {
				widthAttribute.val('');
				jQuery('#calculator_results').html('<h4>The width must be above ' + minWidth + 'mm</h4>').fadeIn(1500).delay(3000).fadeOut('slow');
				widthSet = 0;
			}
			else if( 355 < widthInput && widthInput <= 600 ) {
				//console.log('last');
				widthAttribute.val('10');
				widthSet = 1;
			}
			else if( 600 < widthInput && widthInput <= 750 ) {
			//console.log('last');
				widthAttribute.val('11');
				widthSet = 1;
			}
			else if( 750 < widthInput && widthInput <= 900 ) {
			//	console.log('last');
				widthAttribute.val('12');
				widthSet = 1;
			}
			else if( 900 < widthInput && widthInput <= 1050 ) {
			//	console.log('last');
				widthAttribute.val('13');
				widthSet = 1;
			}
			else if( 1050 < widthInput && widthInput <= 1200 ) {
			//console.log('last');
				widthAttribute.val('14');
				widthSet = 1;
			}
			else if( 1200 < widthInput && widthInput <= 1350 ) {
			//console.log('last');
				widthAttribute.val('15');
				widthSet = 1;
			}
			else if( 1350 < widthInput && widthInput <= 1500 ) {
			//console.log('last');
				widthAttribute.val('16');
				widthSet = 1;
			}
			else if( 1500 < widthInput && widthInput <= 1650 ) {
			//console.log('last');
				widthAttribute.val('17');
				widthSet = 1;
			}
			else if( 1650 < widthInput && widthInput <= 1800 ) {
			//console.log('last');
				widthAttribute.val('18');
				widthSet = 1;
			}
			else if( 1800 < widthInput && widthInput <= 1950 ) {
				if (productName == 'Cellular') {
					jQuery('#calculator_results').html('<h4>The width must be below ' + maxWidth + '</h4>').fadeIn(1500).delay(3000).fadeOut('slow');
					widthAttribute.val('');
					widthSet = 0;
				}
				else {
					widthAttribute.val('19');
					widthSet = 1;
				}
			}
			else if( 1950 < widthInput && widthInput <= 2100 ) {
				if (productName == 'Cellular') {
					jQuery('#calculator_results').html('<h4>The width must be below ' + maxWidth + '</h4>').fadeIn(1500).delay(3000).fadeOut('slow');
					widthAttribute.val('');
					widthSet = 0;
				}
				else {
					widthAttribute.val('20');
					widthSet = 1;
				}
			
			}
			else if( 2100 < widthInput ) {
				widthAttribute.val('');
				jQuery('#calculator_results').html('<h4>The width must be below ' + maxWidth + 'mm</h4>').fadeIn(1500).delay(3000).fadeOut('slow');
				widthSet = 0;
			}
		}
		
		else {
			/*SET HEIGHT*/
	
			if ( heightInput < 400 ) {
				heightAttribute.val('');
				jQuery('#calculator_results').html('<h4>The height must be at least 400mm</h4>').fadeIn(1500).delay(3000).fadeOut('slow');
				heightSet = 0;
			}
		
			else if( 400 <= heightInput && heightInput <= 1370 ) {
				heightAttribute.val('23');
				heightSet = 1;
			}
			else if( 1370 < heightInput && heightInput <= 2100 ) {
				heightAttribute.val('24');
				heightSet = 1;
			}
			else if( 2100 < heightInput ) {
			heightAttribute.val('');
			jQuery('#calculator_results').html('<h4>The height must be below 2100mm</h4>').fadeIn(1500).delay(3000).fadeOut('slow');
			heightSet = 0;
			}	

			/*SET WIDTH*/
	
			if ( widthInput < 355 ) {
				widthAttribute.val('');
				jQuery('#calculator_results').html('<h4>The width must be above ' + minWidth + '</h4>').fadeIn(1500).delay(3000).fadeOut('slow');
				widthSet = 0;
			}
			else if( 355 <= widthInput && widthInput <= 450 ) {
				//console.log('first');
				widthAttribute.val('9');
				widthSet = 1;
			}
			else if( 450 < widthInput && widthInput <= 600 ) {
				//console.log('last');
				widthAttribute.val('10');
				widthSet = 1;
			}
			else if( 600 < widthInput && widthInput <= 750 ) {
			//console.log('last');
				widthAttribute.val('11');
				widthSet = 1;
			}
			else if( 750 < widthInput && widthInput <= 900 ) {
			//	console.log('last');
				widthAttribute.val('12');
				widthSet = 1;
			}
			else if( 900 < widthInput && widthInput <= 1050 ) {
			//	console.log('last');
				widthAttribute.val('13');
				widthSet = 1;
			}
			else if( 1050 < widthInput && widthInput <= 1200 ) {
			//console.log('last');
				widthAttribute.val('14');
				widthSet = 1;
			}
			else if( 1200 < widthInput && widthInput <= 1350 ) {
			//console.log('last');
				widthAttribute.val('15');
				widthSet = 1;
			}
			else if( 1350 < widthInput && widthInput <= 1500 ) {
			//console.log('last');
				widthAttribute.val('16');
				widthSet = 1;
			}
			else if( 1500 < widthInput && widthInput <= 1650 ) {
			//console.log('last');
				widthAttribute.val('17');
				widthSet = 1;
			}
			else if( 1650 < widthInput && widthInput <= 1800 ) {
			//console.log('last');
				widthAttribute.val('18');
				widthSet = 1;
			}
			else if( 1800 < widthInput && widthInput <= 1950 ) {
			//console.log('last');
				widthAttribute.val('19');
				widthSet = 1;
			}
			else if( 1950 < widthInput && widthInput <= 2100 ) {
			//console.log('last');
				widthAttribute.val('20');
				widthSet = 1;
			}
			else if( 2100 < widthInput && widthInput <= 2250 ) {
			//console.log('last');
				widthAttribute.val('21');
				widthSet = 1;
			}
			else if( 2250 < widthInput && widthInput <= 2400 ) {
				widthAttribute.val('22');
				widthSet = 1;

			}
			else if( 2400 < widthInput ) {
				widthAttribute.val('');
				jQuery('#calculator_results').html('<h4>The width must be below ' + maxWidth + '</h4>').fadeIn(1500).delay(3000).fadeOut('slow');
				widthSet = 0;
			}
				
		
	
		
		}
		
		//both height and width are set
		if(heightSet == 1 && widthSet == 1 ) {
		
			spConfig.reloadPrice();
			addToCartButton.removeAttr( 'disabled').css('opacity', 1);
		
		//watch for changes 
		var $watchedDiv = jQuery("#SCPcustomOptionsDiv");
		var html = $watchedDiv.html();
		var checking = setInterval(function() {
			var newhtml = $watchedDiv.html();
			if (html != newhtml) {
				watcherCallback();
				html = newhtml;
			}
		},500);
		
		//function that sets inputs once they're created
		function watcherCallback() {
				
				var exactWidth = new Array(450, 600, 750, 900, 1050, 1200, 1350, 1500, 1650, 1800, 1950, 2100, 2250, 2400);
				
				if( heightInput in {'1370':'', '2100':''}) {
					jQuery('.label_cut_height select').attr('selectedIndex', 1);
					jQuery('.label_cut_height').hide();					
				}
				else{ 
					jQuery('.label_cut_height select').attr('selectedIndex', 2);
					jQuery('.label_cut_height').show();
				}
				
				if( widthInput in {'450':'', '600':'', '750':'', '900':'', '1050':'', '1200':'', '1350':'', '1500':'', '1650':'', '1800':'', '1950':'', '2100':'', '2250':'', '2400':''} ) {
					jQuery('.label_cut_width select').attr('selectedIndex', 1);
					
					jQuery('.label_cut_width').hide()	
				}
				else{ 
					jQuery('.label_cut_width select').attr('selectedIndex', 2);
					jQuery('.label_cut_width').show();
				}
		
				//console.log('chchchchanges');
				jQuery('input.actual_height').val(heightInput);
				jQuery('input.actual_width').val(widthInput);
				jQuery('input.catch').val('1');
				
				//clearInterval(checking); //would break loop
		}
		
			
		}
		else {
			//spConfig.reloadPrice();
			//disable the button if height or width unset
			addToCartButton.attr( 'disabled', 'disabled').css('opacity', .5);
		}

	});	
});