/*
 * Public JavaScripts for LVIInfoBase.
 */

if (jQuery.browser.msie)
{

	jQuery(document).ready(
		function () {
			jQuery('select').keydown(
				function () {
					document.ieNoSubmitSelect = true;
				}
			);
			
			jQuery('form button').mousedown(
				function () {
					document.ieNoSubmitSelect = false;
				}
			);
	
			jQuery('form').submit(
				function (event) {
					if (document.ieNoSubmitSelect)
						event.preventDefault();
				}
			);
	
		}
	
	);

}

function updateSelectElement(n, target, template, command) {
	// if changed from some value to "" dont do anything
	if (n.options[n.selectedIndex].value == '' && n.getAttribute('emptyListAll') == 'false')
		return;
	
	var url = n.getAttribute('url') + '?a=' + command + '&fragment=' + template + '&' + n.getAttribute('name') + '=' + n.options[n.selectedIndex].value;
	var el = jQuery(target);
	jQuery.jGrowl("Haetaan tietoja...");
	RenderingService.renderHtmlFragment(url, function (resp) {el.html(resp);});
}

function updateMultiSelectElement(n) {
	var selectedQuery = '';
	for (var i = 0; i < n.options.length; i++)
		if (n.options[ i ].selected)
			selectedQuery += '&'+n.getAttribute('name')+'=' + n.options[ i ].value;
	
	var url = n.getAttribute('url') + '?a=' + n.getAttribute('command') + '&fragment=' + n.getAttribute('template') + selectedQuery;
	var el = jQuery('#'+n.getAttribute('targetElement'));
	el.attr('disabled','disabled');
	//jQuery.jGrowl("Haetaan tietoja...");
	searchStarted();
	RenderingService.renderHtmlFragment(url, 
		function (resp) {
			el.html(resp).attr('disabled',false);
			searchEnded();
		}
	);	
}

function closeProductCard() {
	jQuery('select').show();
	jQuery('#overlaySearchIndicator').hide();
	jQuery('#productCardBoxi').hide();
}

function resetSelectElement(name) {
	jQuery('#'+name).html('');
}

function toggleProductCard(a) {
	var row = jQuery(a).parent().parent().parent();
	var toggle = jQuery(row).find('td:first').attr("toggleFragment");
	var track = (toggle == '2') ? true : false;
	var template = jQuery(row).attr('template'+toggle);
	var url = jQuery(row).attr('url') + '?a=getProduct&productId=' +  jQuery(row).attr('productId') + '&fragment=' + template;	
		
	RenderingService.renderHtmlFragment(url, function (resp) {
			renderProductCard(resp, true);
		}
	);
}

function toggleInfoProductCard(row, url) {
	var toggle = row.getElementsByTagName('td')[0].getAttribute("toggleFragment");
	var template = row.getAttribute('template'+toggle)
	url = url + '&fragment=' + template
	RenderingService.renderHtmlFragment(url, function (resp) {
		// OLD:
		// jQuery(row).html(resp);
		
		// NEW:
		var cardBox_ = jQuery("#productCardBoxi");
	
		if(!cardBox_ || !cardBox_.is(':visible')) {
			renderProductCard(resp, false);
		} else {
			closeProductCard();
		}
	});	
}

function renderProductCard(contents, track) {
	jQuery('select').hide();
	jQuery('#overlaySearchIndicator').show().height(document.body.offsetHeight);
	
	var posMode = jQuery.browser.msie ? "absolute" : "fixed";
	var posTop = jQuery.browser.msie ? "expression( ( foo = document.documentElement.scrollTop ) + 'px' )" : "0px";
	var cardBox = jQuery("#productCardBoxi");
	if (cardBox.size()) {
		cardBox.show();
	}
	else {
		cardBox = jQuery('<div id="productCardBoxi" style="position:'+posMode+';top:'+posTop+';left:0px;z-index:10000;width:100%;"></div>');
		
		var productCardContainer = jQuery('#productCardContainer');
		
		if(productCardContainer && productCardContainer.length > 0) {		
			cardBox.appendTo(productCardContainer);
		} else {
			cardBox.appendTo(document.body);
		}
	}		
	cardBox.html('<div style="background-color:#fff;width:800px;margin:100px auto 0px auto;padding:10px;">'+contents+'</div>');
	
	if(track === true) {
		trackerProductCardOpened(track, contents)
	}
}

var storedSelectOption = null;
var storeLock = false;

/*
 * Used in 'yleishaku'. Triggered by selecting one select element. Updates all other
 * select elements which do not have a selected value by making a query to remote rendering
 * service. The query is built from all select elements which do have a selected value.
 * 
 * Other case is when changing already selected element to another value.
 * Then update all select elements with lower hierarcy with new values.
 */
function filterSelectElements(originatingElement) {	
	storeLock = true;
	//jQuery.jGrowl("Haetaan tietoja...");

	// create query from select elements which do have a value selected
	// and the value is not '' (=Kaikki).
	var baseurl = originatingElement.getAttribute('url') + '?a=listElementsFiltered';
	var selectedCustomer = $('providerSelect').options[$('providerSelect').selectedIndex].value; 
	var selectedGroup = $('groupSelect').options[$('groupSelect').selectedIndex].value;
	var selectedClass = $('classSelect').options[$('classSelect').selectedIndex].value;
	var selectedProductName = $('nameSelect').options[$('nameSelect').selectedIndex].value;
	var selectedBrand = $('brandSelect').options[$('brandSelect').selectedIndex].value;
	var selectedSeries = $('seriesSelect').options[$('seriesSelect').selectedIndex].value;
	var url = null;
	
	// make a remote rendering request for all select elements 
	url = baseurl;
	url += '&groupId='+selectedGroup;
	url += '&classId='+selectedClass;
	url += '&productNameId='+selectedProductName;
	url += '&brandId='+selectedBrand;
	url += '&seriesId='+selectedSeries;

	$('providerSelect').setAttribute("disabled", "disabled");
	searchStarted();
	RenderingService.renderHtmlFragment(
		url + '&fragment=LVIInfoBase/fragments/suppliersOptions.vm&elementType=customer', 
		function (resp) {
			jQuery('#providerSelect').html(resp).attr('disabled',false);
			// restore selected value
			for (var i = 0; i<$('providerSelect').options.length; i++) {
				if ($('providerSelect').options[i].value == selectedCustomer) {
						$('providerSelect').selectedIndex = i;
				}
			}
			// if only one option (kaikki) disable the selection
			if ($('providerSelect').options.length <= 1) {
				$('providerSelect').setAttribute("disabled", "disabled");
			}
			searchEnded();
		}
	);
	
	url = baseurl;
	url += '&customerId='+selectedCustomer;
	url += '&classId='+selectedClass;
	url += '&productNameId='+selectedProductName;
	url += '&brandId='+selectedBrand;
	url += '&seriesId='+selectedSeries;

	$('groupSelect').setAttribute("disabled", "disabled");
	searchStarted();
	RenderingService.renderHtmlFragment(
		url + '&fragment=LVIInfoBase/fragments/productGroupsOptions.vm&elementType=productGroup', 
		function (resp) {
			jQuery('#groupSelect').html(resp).attr('disabled',false);
			// restore selected value
			for (var i = 0; i<$('groupSelect').options.length; i++) {
				if ($('groupSelect').options[i].value == selectedGroup) {
					$('groupSelect').selectedIndex = i;
				}
			}
			// if only one option (kaikki) disable the selection
			if ($('groupSelect').options.length <= 1) {
				$('groupSelect').setAttribute("disabled", "disabled");
			}
			searchEnded();
		}
	);

	url = baseurl;
	url += '&customerId='+selectedCustomer;
	url += '&groupId='+selectedGroup;
	url += '&productNameId='+selectedProductName;
	url += '&brandId='+selectedBrand;
	url += '&seriesId='+selectedSeries;

	$('classSelect').setAttribute("disabled", "disabled");
	searchStarted();
	RenderingService.renderHtmlFragment(
		url + '&fragment=LVIInfoBase/fragments/productClassesOptions.vm&elementType=productClass', 
		function (resp) {
			jQuery('#classSelect').html(resp).attr('disabled',false);
			// restore selected value
			for (var i = 0; i<$('classSelect').options.length; i++) {
				if ($('classSelect').options[i].value == selectedClass) {
					$('classSelect').selectedIndex = i;
				}
			}
			// if only one option (kaikki) disable the selection
			if ($('classSelect').options.length <= 1) {
				$('classSelect').setAttribute("disabled", "disabled");
			}
			searchEnded();
		}
	);

	url = baseurl;
	url += '&customerId='+selectedCustomer;
	url += '&groupId='+selectedGroup;
	url += '&classId='+selectedClass;
	url += '&brandId='+selectedBrand;
	url += '&seriesId='+selectedSeries;

	$('nameSelect').setAttribute("disabled", "disabled");
	searchStarted();
	RenderingService.renderHtmlFragment(
		url + '&fragment=LVIInfoBase/fragments/productNamesOptions.vm&elementType=productName', 
		function (resp) {
			jQuery('#nameSelect').html(resp).attr('disabled',false);
			// restore selected value
			for (var i = 0; i<$('nameSelect').options.length; i++) {
				if ($('nameSelect').options[i].value == selectedProductName) {
					$('nameSelect').selectedIndex = i;
				}
			}
			// if only one option (kaikki) disable the selection
			if ($('nameSelect').options.length <= 1) {
				$('nameSelect').setAttribute("disabled", "disabled");
			}
			searchEnded();
		}
	);

	url = baseurl;
	url += '&customerId='+selectedCustomer;
	url += '&groupId='+selectedGroup;
	url += '&classId='+selectedClass;
	url += '&productNameId='+selectedProductName;
	url += '&seriesId='+selectedSeries;

	$('brandSelect').setAttribute("disabled", "disabled");
	searchStarted();
	RenderingService.renderHtmlFragment(
		url + '&fragment=LVIInfoBase/fragments/brandsOptions.vm&elementType=productBrand', 
		function (resp) {
			jQuery('#brandSelect').html(resp).attr('disabled',false);
			// restore selected value
			for (var i = 0; i<$('brandSelect').options.length; i++) {
				if ($('brandSelect').options[i].value == selectedBrand) {
					$('brandSelect').selectedIndex = i;
				}
			}
			// if only one option (kaikki) disable the selection
			if ($('brandSelect').options.length <= 1) {
				$('brandSelect').setAttribute("disabled", "disabled");
			}
			searchEnded();
		}
	);

	url = baseurl;
	url += '&customerId='+selectedCustomer;
	url += '&groupId='+selectedGroup;
	url += '&classId='+selectedClass;
	url += '&brandId='+selectedBrand;
	url += '&productNameId='+selectedProductName;

	$('seriesSelect').setAttribute("disabled", "disabled");
	searchStarted();
	RenderingService.renderHtmlFragment(
		url + '&fragment=LVIInfoBase/fragments/seriesOptions.vm&elementType=productSeries', 
		function (resp) {
			jQuery('#seriesSelect').html(resp).attr('disabled',false);
			// restore selected value
			for (var i = 0; i<$('seriesSelect').options.length; i++) {
				if ($('seriesSelect').options[i].value == selectedSeries) {
					$('seriesSelect').selectedIndex = i;
				}
			}
			// if only one option (kaikki) disable the selection
			if ($('seriesSelect').options.length <= 1) {
				$('seriesSelect').setAttribute("disabled", "disabled");
			}
			searchEnded();
		}
	);

	storeLock = false;
	storeCurrentOption(originatingElement);	
}

function updateProviderSelect(originatingElement) {
	var selectedQuery = originatingElement.getAttribute('url') + '?a=listSuppliersFiltered&productGroups=&productClasses=&productNames=';
	for (var i = 0; i < $('groupSelect').options.length; i++)
		if ($('groupSelect').options[ i ].selected)
			selectedQuery += '&productGroups=' + $('groupSelect').options[ i ].value;
	for (var i = 0; i < $('classSelect').options.length; i++)
		if ($('classSelect').options[ i ].selected)
			selectedQuery += '&productClasses=' + $('classSelect').options[ i ].value;
	for (var i = 0; i < $('nameSelect').options.length; i++)
		if ($('nameSelect').options[ i ].selected)
			selectedQuery += '&productNames=' + $('nameSelect').options[ i ].value;
	selectedQuery += '&fragment=LVIInfoBase/fragments/suppliersOptions.vm';
	RenderingService.renderHtmlFragment(
		selectedQuery,
		function (resp) {
			var selectedVal = $('providerSelect').options[$('providerSelect').selectedIndex].value;
			jQuery('#providerSelect').html(resp).attr('disabled',false);
			// if only one option (kaikki) disable the selection
			if ($('providerSelect').options.length <= 1) {
				$('providerSelect').setAttribute("disabled", "disabled");
			} else {
				for (var i = 0; i < $('providerSelect').options.length; i++) {
					if ($('providerSelect').options[ i ].value == selectedVal)
						$('providerSelect').options[ i ].selected = 'selected';
				}
			}
		}
	);
}

function storeCurrentOption(originatingElement) {
	if (!storeLock) {
		storedSelectOption = originatingElement.options[originatingElement.selectedIndex];
	}
}

var ongoingSearches = 0;

function searchStarted() {
	ongoingSearches++;
	if (ongoingSearches > 0) {
		// Show overlay search indicator
		$('overlaySearchIndicator').show();
	}
}

function searchEnded() {
	ongoingSearches--;
	if (ongoingSearches <= 0) { 
		// Hide overlay search indicator
		$('overlaySearchIndicator').hide();	
		if (jQuery.browser.msie) document.ieNoSubmitSelect = false;	
	}
}

