/*
 * This class handle events for slider on product page.
 * The slider has slide and change event. Slide event occurs when user select modules combination by slider sliding.
 * The Change event occurs when user select modules combination by clicking on some modules button or below it.
 * When user stop sliding or change module combination, then system sends selected data to Google analytics tool.
 */

var sliderPreviousValue = 0;

function initializeSlider(minValue){
	
	$("#slider").slider({
		value:2,
		min: minValue,
		max: 5,
		step: 1,
		slide: function(event, ui) {
			setSliderPreviousValue();
			handleOverlay(ui.value);
			handleOverlayedModules(ui.value);
			handleHiddenDescriptionList(ui.value);
			handleBulletinList(ui.value);
		},
		change: function(event, ui) {
			setHiddenSelectedModuleValue(ui.value);
			handleOverlay(ui.value);
			handleOverlayedModules(ui.value);
			handleHiddenDescriptionList(ui.value);
			handleBulletinList(ui.value);
		}
	});
	$("#slider").slider('option', 'value', parseInt($("#hdn_selectedModules").val()));
	$('#slider').bind('slidechange', function(event, ui) {
 		if(sliderPreviousValue != ui.value){
			pageTracker._trackEvent("Product overview", location.pathname, "Slide position " + ui.value);
		}
	});	
	handleOverlay($("#slider").slider("value"));
	handleOverlayedModules($("#slider").slider("value"));
	handleHiddenDescriptionList($("#slider").slider("value"));
	handleBulletinList($("#slider").slider("value"));
}

/**
 * Set selected module combination to hidden field with id 'hdn_selectedModules'.
 * @param index selected module index. Possible 2, 3, 4, 5
 */
function setHiddenSelectedModuleValue(index) {
	$("#hdn_selectedModules").val(index);
}

/**
 * Sets slider previous value to sliderPreviousValue variable.
 */  
function setSliderPreviousValue() {
	sliderPreviousValue = $("#hdn_selectedModules").val();
}
/**
 * Handle semi-transparent div over modules.
 * @param index selected module index. Possible 2, 3, 4, 5
 */
function handleOverlay(index) {
	$.each($(".product_image_wrapper"),function(i,v) {
		if(i + 1 <= index) {
			$(this).removeClass("opacity");
		} else {
			$(this).addClass("opacity");
		}
	});
	
	$.each($(".product_module_delimiter"),function(i,v) {
		if(i + 1 == index) {
			$(this).show();
		} else {
			$(this).hide();
		}
	});
}

/**
 * Handle modules description div 
 * @param index selected module index. Possible 2, 3, 4, 5
 */
function handleOverlayedModules(index) {
	$.each($(".descriptionWrapper"),function(i,v) {
		if(i + 1 <= index) {
			$(this).show();
		} else {
			$(this).hide();
		}
	});
}

/**
 * Handle Excluded modules list.
 * @param index selected module index. Possible 2, 3, 4, 5
 */
function handleHiddenDescriptionList(index) {
	$.each($(".hidden_modules"),function(i,v) {
		var moduleUrl = location.protocol + "//" + location.host + location.pathname + "?modules=" + (parseInt(i) + 1);
		if(i + 1 <= index) {
			$(this).hide();
		} else {
			if($("#hiddenModulesListTitle").is(":hidden")) {
				$("#hiddenModulesListTitle").show();
			}
			$(this).show();
			
			// Add name attribute to the link of Module Exclusion list
			$($(this).get(0).getElementsByTagName('a')).attr("name", "module_" + (i + 1));
		}
		if(index == $(".hidden_modules").size()) {
			$("#hiddenModulesListTitle").hide();
		}
	});
}

 /**
  * Handle bulletin list for selected module combination. 
  * @param index selected module index. Possible 2, 3, 4, 5
  */
function handleBulletinList(index) {
	$.each($(".bulletinListWrapper"),function(i,v) {
		if(i + 2 == index) {
			$(this).show();
		} else {
			$(this).hide();
		}
	});
}

/**
 * Handle click action on module button.
 */
function handleModuleButtonClick() {
	$("div.product_image_plus_sign_holder").click(function(){
		var moduleId = this.id;
		var moduleIndex = moduleId.substring(moduleId.lastIndexOf("_")+1);
		if(moduleIndex < 1) {
			return false;
		}
		sliderPreviousValue = $("#slider").slider('value'); 
		$("#slider").slider('value', moduleIndex);
	});
	
}
 
function ieHandleModuleButtonClick() {
	 $("div.product_image_image").click(function(){
		 var moduleId = $(this).parent().get(0).id;
		 var moduleIndex = moduleId.substring(moduleId.lastIndexOf("_")+1);
		 if(moduleIndex < 1) {
			 return false;
		 }
		 sliderPreviousValue = $("#slider").slider('value'); 
		 $("#slider").slider('value', moduleIndex);
	 });
}

function handleExcludedModulesLinkClick(){
	$(".hidden_modules a").click(function(){
		var moduleName = this.name;
		var moduleIndex = moduleName.substring(moduleName.lastIndexOf("_")+1);
		$("#slider").slider('value', moduleIndex);
	})
}

