$(document).ready(function(){
	
	$('.diffuser-finish').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		showBody: " - ",
		fade: 250
	});	

//INITIATION

	var series = jQuery.url.segment(3)+'-';
	var hangingSystem;
	var hangingSystemDiv;
	var size;
	var lamping;
	var lampingPreview = [];
	var diffuser;
	var diffuserPreview;
	var finish;
	var finishPreview;
	var voltage;
	var voltageDiv;
	var oah;
	var oahDiv;
	var oahDivContent;
	var option = new Array();
	var optionPreview = new Array();
	var optionDiv;
	var step = 0;
	var complete=new Boolean(false);
	var oahbound = new Boolean(false);
	var sku;
	var optionSKU = [];
	
	$('.config-section').hide();
	
	$('#hanging').parent().show();
	
	$('#hanging-attribute').addClass('activeSlide');
	
	//GLOBAL FUNCTION TO CHECK IF ARRAY HAS A DEFINED VALUE
	function isDefined(x){
		if(x != null){
			return true;	
		}
		else{
			return false;
		}
	}
	
	//FUNCTION TO FIND INDEX OF ARRAY VALUE
	Array.prototype.findIndex = function(value){
	var ctr = "";
	for (var i=0; i < this.length; i++) {
	// use === to check for Matches. ie., identical (===), ;
	if (this[i] == value) {
	return i;
	}
	}
	return ctr;
	};

	//ALLOWS IE TO USE THE "SOME" ARRAY METHOD
	if (!Array.prototype.some){
	  Array.prototype.some = function(fun /*, thisp*/)
	  {
		var len = this.length;
		if (typeof fun != "function")
		  throw new TypeError();
	
		var thisp = arguments[1];
		for (var i = 0; i < len; i++)
		{
		  if (i in this &&
			  fun.call(thisp, this[i], i, this))
			return true;
		}
	
		return false;
	  };
	}
	
//OPTIONS
	function optionSet(){
		
		complete = true;
		
		$(optionDiv).parent().show();
		$(optionDiv + ' > li > a').addClass('active-section');
		$('#options-attribute').addClass('activeSlide');
		
		//SET ALL VIEWABLE CONTENT FOR OPTIONS
		function replaceContent(){
				
			$('#options-preview ul').empty();
			for (var u=0;u<optionPreview.length;u++){
				if(optionPreview[u] != null){
					$('#options-preview ul').append('<li>' + optionPreview[u] + '</li>');
				}
				else{}
			}
			//DEFINE OPTIONSKU
			var optionSKUCount = 0;
			for (var z=0;z<option.length;z++){
				if (option[z] != null){
					optionSKU[z] = option[z];
				}
				else {
					optionSKU[z] = null;
				}
			}
			//SET OPTION CODE TEXT
			$('#options-code').empty();
			$('#options-code').append(option.join(' - '));
//			for (var u=0;u<optionSKU.length;u++){
//				if(optionSKU[u] != null){
//					$('#options-code').append(optionSKU[u] + ' - ');
//				}
//				else if (u == optionSKU.length){
//					$('#options-code').append(optionSKU[u]);
//				}
//				else{}
//			}
			//SET HEIGHT OF CODE DIV IF OPTIONS ARE CHOSEN
			if (optionSKU.some(isDefined)){
				$('#code').addClass('options-selected');
			}
			else{
				$('#code').removeClass('options-selected');
			}
			
		}
		//CHECK IF LINKS ARE ALREADY BOUND
		if($(optionDiv).parent().hasClass('bound')){}
		//IF NOT, BIND BEHAVIORS
		else{
			$('.active-section:not(.unavailable)').each(function(i){
					$(optionDiv).parent().addClass('bound');
					$(this).click(function(){
						if ($(this).hasClass('active')){
							var optionValue = $(this).attr('rel');
							var optionIndex = option.findIndex(optionValue);
							option.splice(optionIndex, 1);
							optionPreview[i] = null;
							$(this).removeClass('active');
							replaceContent();
						}
							
						else{
							option.push($(this).attr('rel'));
							optionPreview[i] = $(this).text();
							$(this).addClass('active');
							replaceContent();
						}
					});
			});
			bound = true;
		}
		//else{
//			$('.active-section:not(.unavailable)').each(function(i){
//					$(optionDiv).parent().addClass('bound');
//					$(this).click(function(){
//						if ($(this).hasClass('active')){
//							var optionValue = $(this).attr('rel');
//							var optionIndex = option.findIndex(optionValue);
//							option.splice(optionIndex, 1);
//							optionPreview[i] = null;
//							$(this).removeClass('active');
//							replaceContent();
//						}
//							
//						else{
//							option[i] = $(this).attr('rel');
//							optionPreview[i] = $(this).text();
//							$(this).addClass('active');
//							replaceContent();
//						}
//					});
//			});
//			bound = true;
//		}
		
	}
	
//OAH
	function oahSet(){
		if (oahDivContent != '--'){
			$('#oah-attribute').addClass('activeSlide');
			$(oahDiv + ' > li > a').addClass('active-section');
			$(oahDiv).parent().show();
			if($('.minmax')){
				if(oahbound == true){}
				else{
					oahbound = true;
					$('.oah-submit').click(function(){
						var testoah;
						var oahmin = $(this).siblings('.min').text();
						oahmin = parseInt(oahmin);
						var oahmax = $(this).siblings('.max').text();
						oahmax = parseInt(oahmax);
						testoah = $(this).siblings('.minmax_input').val();
						testoah = parseInt(testoah);
						
						if(testoah < oahmin){
							alert('Please enter a value between '+oahmin+' and '+oahmax+'.')
						}
						else if(testoah > oahmax){
							alert('Please enter a value between '+oahmin+' and '+oahmax+'.')
						}
						else{
							oah = testoah;
							$('#oah-code').text(oah + ' -');
							$('#oah-preview').text(oah + '"');
							$('#oah-attribute').removeClass('activeSlide');
							$(this).parent().parent().hide();
							$(oahDiv + ' > li > a').removeClass ('active-section');
							step = step + 1;
							
							optionSet();
						}
					});
				}
			}
			$('.active-section').click(function(){
				oah = $(this).attr('rel');
				$('#oah-code').text(oah + ' -');
				$('#oah-preview').text(oah + '"');
				$('#oah-attribute').removeClass('activeSlide');
				$(this).parent().parent().parent().hide();
				$(oahDiv + ' > li > a').removeClass ('active-section');
				step = step + 1;
				
				optionSet();
			});
		}
		
		else {optionSet();}
	}
	
//VOLTAGE
	function voltageSet(){
		$('.active-section').click(function(){
			voltage = $(this).text();
			$('#voltage-code').text(voltage + ' -');
			$('#voltage-preview').text(voltage);
			$(voltageDiv + ' > ul > li > a').removeClass('active-section');
			$(this).parent().parent().parent().hide();
			$('#voltage-attribute').removeClass('activeSlide');
			step = step + 1;
			oahSet();
		});
	}
	
//FINISH
	function finishSet(){
		$('#finish .diffuser-finish').click(function(){
			finish = $(this).attr('id');
			finishPreview = $(this).attr('rel');
			$('#finish-code').text(finish + ' -');
			$('#finish-preview').text(finishPreview);
			$(this).parent().parent().parent().hide();
			$('#finish-attribute').removeClass('activeSlide');
			$('#voltage-attribute').addClass('activeSlide');
			$(voltageDiv).show();
			$(voltageDiv + ' > ul > li > a').addClass('active-section');
			step = step + 1;
			voltageSet();
		});
	}
	
//DIFFUSER
	function diffuserSet(){
		$('#diffuser .diffuser-finish').click(function(){
			diffuser = $(this).attr('id');
			diffuserPreview = $(this).attr('rel');
			$('#diffuser-code').text(diffuser + ' -');
			$('#diffuser-preview').text(diffuserPreview);
			$(this).parent().parent().parent().hide();
			$('#diffuser-attribute').removeClass('activeSlide');
			$('#finish-attribute').addClass('activeSlide');
			$('#finish').parent().show();
			step = step + 1;
			finishSet();
		});
	}
	
//SIZE AND LAMPING
	function sizelampingSet(){
		$('.active-section').click(function(){
			size = $(this).children('p.code').attr('rel');
			lamping = $(this).children('p.code').text();

            // replace / with - for use in html tags
            lamping = lamping.replace("/","_");

			lampingPreview = $(this).children('p.bulb-info').text();
			lampingPreview = lampingPreview.split('');
			//PARSE LAMPING PREVIEW
				var atIndex = lampingPreview.findIndex('@');
				lampingPreview.splice(atIndex,0,' ');
				atIndex = lampingPreview.findIndex('@') + 1;
				lampingPreview.splice(atIndex,0,' ');
			voltageDiv = '#' + hangingSystem + '_' + size + '_' + lamping;
			oahDiv = '#' + hangingSystem + '_' + size + '_oah';
			oahDivContent = $(oahDiv + ' > li > a').text();
			if (oahDivContent != '--'){
				$('#oah-code').show();
				$('#oah-preview').show();
				$('#oah-preview').prev().show();
				$('#oah-attribute').parent().show();
			}
			else {
				$('#oah-code').hide();
				$('#oah-preview').hide();
				$('#oah-preview').prev().hide();
				$('#oah-attribute').parent().hide();
			}
			optionDiv = '#options_' + hangingSystem + '_' + size + '_' + lamping;
			$('#size-preview').text(size + '"');
			$('#lamping-preview').text(lampingPreview.join(''));
			$('#size-code').text(size + ' -');

            // replace the - with / for the text to display
			$('#lamping-code').text(lamping.replace("_","/") + ' -');

			$(hangingSystemDiv + ' > ul > li > a').removeClass('active-section');
			$(this).parent().parent().parent().hide();
			$('#sizelamping-attribute').removeClass('activeSlide');
			$('#diffuser-attribute').addClass('activeSlide');
			$('#diffuser').parent().show();
			step = step + 1;
			diffuserSet();
		});
	}
	
//HANGING SYSTEM
	function hangingSet(){
		$('#hanging a.option').click(function(){
			hangingSystem =  $(this).children('.code').text();
			hangingSystemDiv =  '#size_' + $(this).children('.code').text();
			$('#hanging-code').text(hangingSystem + ' -');
			$('#hanging-preview').text(hangingSystem);
			$('.hanging-image').hide();
			$('.'+jQuery.url.segment(3)+'-'+hangingSystem).show();
			$(this).parent().parent().parent().hide();
			$('#hanging-attribute').removeClass('activeSlide');
			$('#sizelamping-attribute').addClass('activeSlide');
			$(hangingSystemDiv).show();
			$(hangingSystemDiv + ' > ul > li > a').addClass('active-section');
			step = step + 1;
			sizelampingSet();
		});
	}
	
//RESETS
	function resetAttributes (){
	
		$('#hanging-attribute').click(function(){
											   
			if (step > 0){
											   
				var r=confirm('Returning to Hanging System will reset all attributes after Hanging System. Proceed?')
				
				if (r==true){
					
					size = null;
					lamping = null;
					lampingPreview = [];
					diffuser = null;
					finish = null;
					voltage = null;
					size = null;
					oah = null;
					option = [];
					optionSKU = [];
					optionPreview = []
					
					$('#size-preview').text(' ');
					$('#lamping-preview').text(' ');
					$('#size-code').text(' -');
					$('#lamping-code').text(' -');
					$('#diffuser-code').text(' -');
					$('#diffuser-preview').text(' ');
					$('#finish-code').text(' -');
					$('#finish-preview').text(' ');
					$('#finish-code').text(' -');
					$('#finish-preview').text(' ');
					$('#voltage-code').text(' -');
					$('#voltage-preview').text(' ');
					$('#oah-code').text(' -');
					$('#oah-preview').text('');
					$('#options-code').empty();
					$('#options-preview ul').empty();
					
					$('.config-section').hide();
					
					$('#hanging').parent().show();
					
					$('.attribute').removeClass('activeSlide');
					
					$(this).addClass('activeSlide');
					
					$('.active').removeClass('active');
					
					$('.active-section').removeClass('active-section');
					
					step = 0;
					
					complete = false;
				}
				
				else{}
			}
			
			else{}
		
		});
		
		$('#sizelamping-attribute').click(function(){
												   
			if (step > 1){
		
				var r=confirm('Returning to Size/Lamping will reset all attributes after Size/Lamping. Proceed?')
				
				if (r==true){
					
					diffuser = null;
					finish = null;
					voltage = null;
					oah = null;
					option = [];
					optionSKU = [];
					optionPreview = [];
					
					$('#diffuser-code').text(' -');
					$('#diffuser-preview').text(' ');
					$('#finish-code').text(' -');
					$('#finish-preview').text(' ');
					$('#voltage-code').text(' -');
					$('#voltage-preview').text(' ');
					$('#oah-code').text(' -');
					$('#oah-preview').text('');
					$('#options-code').empty();
					$('#options-preview ul').empty();
					
					$('.config-section').hide();
					
					$(hangingSystemDiv).show();
					
					$('.attribute').removeClass('activeSlide');
					
					$(this).addClass('activeSlide');
					
					$('.active').removeClass('active');
					
					$('.active-section').removeClass('active-section');
					
					$(hangingSystemDiv + ' > ul > li > a').addClass('active-section');
					
					step = 1;
					
					complete = false;
					
					sizelampingSet();
				}
				
				else{}
			}
			
			else{}
		
		});
		
		$('#diffuser-attribute').click(function(){
												
			if (step > 2){
		
				var r=confirm('Returning to Diffuser will reset all attributes after Diffuser. Proceed?')
				
				if (r==true){
		
					finish = null;
					voltage = null;
					oah = null;
					option = [];
					optionSKU = [];
					optionPreview = [];
					
					$('#finish-code').text(' -');
					$('#finish-preview').text(' ');
					$('#voltage-code').text(' -');
					$('#voltage-preview').text(' ');
					$('#oah-code').text(' -');
					$('#oah-preview').text('');
					$('#options-code').empty();
					$('#options-preview ul').empty();
					
					$('.config-section').hide();
					
					$('#diffuser').parent().show();
					
					$('.attribute').removeClass('activeSlide');
					
					$(this).addClass('activeSlide');
					
					$('.active').removeClass('active');
					
					$('.active-section').removeClass('active-section');
					
					step = 2;
					
					complete = false;
					
					diffuserSet();
				}
				
				else{}
			}
			
			else{}
		
		});
		
		$('#finish-attribute').click(function(){
											  
			if (step > 3){
		
				var r=confirm('Returning to Finish will reset all attributes after Finish. Proceed?')
				
				if (r==true){
					
					voltage = null;
					oah = null;
					option = [];
					optionSKU = [];
					optionPreview = [];
					
					$('#voltage-code').text(' -');
					$('#voltage-preview').text(' ');
					$('#oah-code').text(' -');
					$('#oah-preview').text('');
					$('#options-code').empty();
					$('#options-preview ul').empty();
					
					$('.config-section').hide();
					
					$('#finish').parent().show();
					
					$('.attribute').removeClass('activeSlide');
					
					$(this).addClass('activeSlide');
					
					$('.active').removeClass('active');
					
					$('.active-section').removeClass('active-section');
					
					step = 3;
					
					complete = false;
					
					finishSet();
				}
				
				else{}
			}
			
			else{}
		
		});
		
		$('#voltage-attribute').click(function(){
											   
			if (step > 4){
	
				var r=confirm('Returning to Voltage will reset all attributes after Voltage. Proceed?')
				
				if (r==true){
		
					oah = null;
					option = [];
					optionSKU = [];
					optionPreview = [];
					
					$('#oah-code').text(' -');
					$('#oah-preview').text('');
					$('#options-code').empty();
					$('#options-preview ul').empty();
					
					$('.config-section').hide();
					
					$(voltageDiv).show();
					
					$('.attribute').removeClass('activeSlide');
					
					$(this).addClass('activeSlide');
					
					$('.active').removeClass('active');
					
					$('.active-section').removeClass('active-section');
					
					$(voltageDiv + ' > ul > li > a').addClass('active-section');
					
					step = 4;
					
					complete = false;
					
					voltageSet();
				}
				
				else{}
			}
			
			else{}
		
		});
		
		$('#oah-attribute').click(function(){
										   
			if (step > 5){
		
				var r=confirm('Returning to OAH will reset all attributes after OAH. Proceed?')
				
				if (r==true){
					
					$('#options-preview ul').empty();
					$('#options-code').empty();
		
					option = [];
					optionSKU = [];
					optionPreview = [];
					
					$('.config-section').hide();
					
					$(oahDiv).parent().show();
					
					$('.attribute').removeClass('activeSlide');
					
					$(this).addClass('activeSlide');
					
					$('.active').removeClass('active');
					
					$('.active-section').removeClass('active-section');
					
					step = 5;
					
					complete = false;
					
					oahSet();
		
				}
				
				else{}
			}
			
			else{}
		
		});
	}
	
//SET ALL SETTINGS IF THEY EXIST
	
	function returnSet(){
		var urlSKUSplit = urlSKU.split('-');
		
		hangingSystem = urlSKUSplit[1];
		hangingSystemDiv = '#size_' + hangingSystem;
		size = urlSKUSplit[2];
		lamping = urlSKUSplit[5];
		diffuser = urlSKUSplit[3];
		diffuserPreview = $('#diffuser').children('li').children('#' + diffuser).attr('rel');
		finish = urlSKUSplit[4];
		finishPreview = $('#finish').children('li').children('#' + finish).attr('rel');
		voltage = urlSKUSplit[6];
		voltageDiv = '#' + hangingSystem + '_' + size + '_' + lamping;
		oah = urlSKUSplit[7];
		oahDiv = '#' + hangingSystem + '_' + size + '_oah';
		oahDivContent = $(oahDiv + ' > li > a').text();
		option = urlSKUSplit.slice(8);
		optionDiv = '#options_' + hangingSystem + '_' + size + '_' + lamping;
		for (var g=0;g<option.length;g++){
			optionPreview[g] = $(optionDiv).children('li').children('.' + option[g]).text();
		}
		
		$('#hanging-code').text(hangingSystem + ' -');
		$('#size-code').text(size + ' -');
		$('#lamping-code').text(lamping + ' -');
		$('#diffuser-code').text(diffuser + ' -');
		$('#finish-code').text(finish + ' -');
		$('#voltage-code').text(voltage + ' -');
		$('#oah-code').text(oah + ' -');
		
		$('#hanging-preview').text(hangingSystem);
		$('#sizelamping-preview').text(size + ' ' + lamping);
		$('#diffuser-preview').text(diffuserPreview);
		$('#finish-preview').text(finishPreview);
		$('#voltage-preview').text(voltage);
		$('#oah-preview').text(oah);
		for (var u=0;u<optionPreview.length;u++){
			$('#options-preview ul').append('<li>' + optionPreview[u] + '</li>');
		}
		
		if (oahDivContent == '--'){
			$(oahDiv).parent().hide();
		}
		else {}
		
		$('.attribute').removeClass('activeSlide');
		$('#options-attribute').addClass('activeSlide');
		$('.config-section').hide();
		
		optionSet();
		
		step = 10;
		
		for (var c=0;c<option.length;c++){
			$(optionDiv + ' > li > a.' + option[c]).addClass('active');
		}
	}
	
//RUN ALL FUNCTIONS
	
	var urlSKU = jQuery.url.segment(4);
	
	if (urlSKU){
		returnSet();
		resetAttributes();
		hangingSet();
	}
	
	else {
		hangingSet();
		resetAttributes();	
	}
	
//WHEN FINISHED BUTTON IS CLICKED

	$('#finished').click(function(){
		
		if (complete == true){
			
			if (option.some(isDefined)){
				optionSKU = [];
				var optionSKUCount = 0;
				for (var z=0;z<option.length;z++){
					if (option[z] != null){
						optionSKU[optionSKUCount] = option[z];
						optionSKUCount = optionSKUCount + 1;
					}
					else {}
				}
				
				if (oahDivContent != '--'){
					sku = series + hangingSystem + '-' + size + '-' + diffuser + '-' + finish + '-' + lamping + '-' + voltage + '-' + oah + '-' + optionSKU.join('-');
				}
				else{
					sku = series + hangingSystem + "-" + size + "-" + diffuser + "-" + finish + "-" + lamping + "-" + voltage + "-" + optionSKU.join('-');
				}
			
				top.location='http://'+jQuery.url.attr("host")+'/site/product_configure_finished/'+jQuery.url.segment(2)+'/'+jQuery.url.segment(3)+"/" + sku;
			}
			else{
				if (oahDivContent != '--'){
					sku = series + hangingSystem + '-' + size + '-' + diffuser + '-' + finish + '-' + lamping + '-' + voltage + '-' + oah;
				}
				else{
					sku = series + hangingSystem + "-" + size + "-" + diffuser + "-" + finish + "-" + lamping + "-" + voltage;
				}
			
				top.location='http://'+jQuery.url.attr("host")+'/site/product_configure_finished/'+jQuery.url.segment(2)+'/'+jQuery.url.segment(3)+"/" + sku;
			}
		
		}
		
		else{
			alert('Please complete your configuration before continuing.');
		}	
	
	});
	
});
