var ProductHomepageCollapsableElements  = {
	initialize: function() {
		$$("div#product-zone div.link-aggregation-row").each(function(elem){
			new ProductHomepageCollapsableGroup(elem);
		});
	}
};

var ChannelPageCollapsableElements  = {
	initialize: function() {
		$$("div#content-zone div.subcategory-nav").each(function(elem){
			new ProductHomepageCollapsableGroup(elem);
		});
	}
};

var ProductHomepageCollapsableGroup = Class.create();
ProductHomepageCollapsableGroup.prototype = {
	initialize: function(collapsableGroup) {
		if (!collapsableGroup) return;
		this.collapsableGroup = collapsableGroup;
		this.groupElements = this.collapsableGroup.getElementsBySelector("li.collapsed", "li.expanded");
		this.groupElement = false;
		this.groupElementsCount = this.groupElements.size();
		this.groupElementsCounter = 0;
		this.groupAllLink = this.collapsableGroup.getElementsBySelector("p.product-link-functions a").first();
		this.handleCollapsableGroup();
	},
	
	handleCollapsableGroup: function() {
		if (!this.groupAllLink) return;
		this.groupAllLink.observe('click',this._toggleListElements.bindAsEventListener(this));
		this.handleElements();
	},
				
	handleElements: function() {
		this.groupElements.each(function(elem) {
			elem=elem.down("A");
			this.groupElement = elem;
			elem.observe('click',this._toggleListElement.bindAsEventListener(this));
			
		}.bind(this));
	},
	
	_toggleListElements: function() {
		if (this.groupAllLink.hasClassName("collapsed")) {
			this.groupElements.invoke("addClassName","expanded");
			this.groupElements.invoke("removeClassName","collapsed");
			this._handleActionLink(true);
			this.groupElementsCounter = this.groupElementsCount;
			this.groupElements.each(function(elem) 
			{
				elem=elem.down("A");
				elem.addClassName("link-expanded").removeClassName("link");
			});
			
		} else {
			this.groupElements.invoke("addClassName","collapsed");
			this.groupElements.invoke("removeClassName","expanded");
			this._handleActionLink();
			this.groupElementsCounter = 0;
			this.groupElements.each(function(elem) 
			{
				elem=elem.down("A");
				elem.addClassName("link").removeClassName("link-expanded");
			});
		}
	},
	
	_handleActionLink: function(expandActionLink) {
		if (expandActionLink) {
			this.groupAllLink.addClassName("expanded").removeClassName("collapsed").update(COLLAPSE_ALL);
		} else {
			this.groupAllLink.addClassName("collapsed").removeClassName("expanded").update(EXPAND_ALL);
		}
	},
	
	_toggleListElement: function(event) {
		var elt = Event.element(event);
		var l = elt.up("LI");
		if (l.className=="") return;
		if (l.hasClassName("collapsed")) {
			l.addClassName("expanded").removeClassName("collapsed");
			elt.addClassName("link-expanded").removeClassName("link");
			this.groupElementsCounter ++;
		} else {
			l.addClassName("collapsed").removeClassName("expanded");
			elt.addClassName("link").removeClassName("link-expanded");
			this.groupElementsCounter --;
		}
		
		if (this.groupElementsCounter==0) this._handleActionLink();
		if (this.groupElementsCounter==this.groupElementsCount) this._handleActionLink(true);
	}
};

addDOMLoadEvent(ProductHomepageCollapsableElements.initialize);
addDOMLoadEvent(ChannelPageCollapsableElements.initialize);


function toggleDiv(divId) {
	if (document.getElementById(divId).style.display == 'none') {
		document.getElementById(divId).style.display = 'block';
	} else {
		document.getElementById(divId).style.display = 'none';
	}
}
	
function selectService(url) {
	new Ajax.Updater('service-content', url, {evalScripts: true});		
}

function submitServiceForm(sFormId)
{
		// fix for IE instead of $(sFormId).serialize(true)
		var serializedFormParameters = "?";
		for(var i = 0; i < $(sFormId).length; i++) {
			if( ($(sFormId).elements[i].type != "checkbox" || ($(sFormId).elements[i].type == "checkbox" && $(sFormId).elements[i].checked)) && ($(sFormId).elements[i].type != "radio" || ($(sFormId).elements[i].type == "radio" && $(sFormId).elements[i].checked))) {
				if(i>0) serializedFormParameters += "&";
				serializedFormParameters += $(sFormId).elements[i].name + "=" + $(sFormId).elements[i].value;
			}
		}
		serializedFormParameters = encodeURI(serializedFormParameters);

		new Ajax.Updater('service-content', document.getElementById(sFormId).action, {parameters: serializedFormParameters});
}

function submitSearchForm(formObj, basePath)
{
	if (!formObj) return false;
	var formSubmit = $(formObj);
	if (!formSubmit || !formSubmit.submit) return false;
	
	var searchTypeSelectionRadios = $('search-type-selection').elements["searchType"];
	var searchTypeSelection;
	for(var i = 0; i < searchTypeSelectionRadios.length; i++) 
	{
		if(searchTypeSelectionRadios[i].checked) 
		{
			searchTypeSelection = searchTypeSelectionRadios[i].value;
		}
	}	
	if(searchTypeSelection == 'searchPartNr')
	{
		formSubmit.action = basePath + "findProductTypeByName.html"
		document.cookie = "searchType=searchPartNr; path=/";
	}
	else if(searchTypeSelection == 'searchWebsite')
	{
		formSubmit.action = basePath + "search.html"
		document.cookie = "searchType=searchWebsite; path=/";
	}
	else if(searchTypeSelection == 'searchConfDocs')
	{
		formSubmit.action = basePath + "searchConfidential.html"
		document.cookie = "searchType=searchConfDocs; path=/";
	}
	
	formSubmit.submit();
}

function setWebsiteSearchCookie()
{
	document.cookie = "searchType=searchWebsite; path=/";
}

function submitProductForm(formId, actionValue) {
	var formVar = document.getElementById(formId);
	formVar.action = actionValue;
	formVar.submit();
}

function submitIfValid(select) {
	if (select.selectedIndex > 0) {
		if(select.value == '__createWS')
		{
			var sUrl = '/collaboration/tool/workspace/create';
			var oWin = new openWindow(sUrl, 380, 146, 0, 0, 0, 0, 'yes');
			oWin.open();
		}
		else if(select.value == '__createLib')
		{
			var sUrl = '/collaboration/library/library/create';
			var oWin = new openWindow(sUrl, 380, 146, 0, 0, 0, 0, 'yes');
			oWin.open();
		}
		else
		{
			select.form.submit();
		}
	}
}

function openProductTypeDocuments(productTypeId) {
	openDocuments('productTypeDocuments.html?productType=' + productTypeId);
}

function submitServiceFormIfValidSelection(select, formId) {
	if (select.selectedIndex > 0) {
		submitServiceForm(formId);
	}
}

function submitProductFormIfValid(input, formId) {

}

function submitProductFormIfValidSelection(selectFormId, formId, target) {
	if (document.getElementById(selectFormId).elements[0].selectedIndex > 0) {
		submitProductForm(formId, target);
	}
}

function submitTechnicalSpecifications(formId) {
	if (document.getElementById(formId).elements[0].value != '') {
		var splittedKey = document.getElementById(formId).elements[0].value.split('_');
		document.getElementById(formId).channel.value = splittedKey[0];
		if(splittedKey.length > 1)
		{
			var parentChannelRefID = '';
			for (var i = 1; i < splittedKey.length; i++)
			{
				parentChannelRefID += splittedKey[i];
				if(i < (splittedKey.length -1))
				{
					parentChannelRefID += '_';
				}
			}
			document.getElementById(formId).parentChannelRef.value = parentChannelRefID;
		}
		document.getElementById(formId).submit();
	}
}