/*
 * $Id: dropdown.js 234 2008-03-05 13:01:45Z benzman $
 */

/*
 * Neues Dropdown Objekt 
 */
var Dropdown = {
	dropdown_timeout: null,
	dropdown_active: null,
	dropdown_locked: false,
	
	dropdownLock: function() {
		this.dropdown_locked = true;
	},
	
	dropdownUnlock: function() {
		this.dropdown_locked = false;
	},
	
	dropdownClose: function() {
		if(this.dropdown_active != null) {
			this.dropdown_active.up().undoPositioned().setStyle({ overflow: 'hidden' });
			this.dropdown_active.remove();
			this.dropdown_active = null;
		}
		window.setTimeout("Dropdown.dropdownUnlock()", 50);
	},
	
	dropdownOpen: function(el, width, options, selectCallback, max_height, type, classname) {
		if (!this.dropdown_locked) {
			this.dropdownLock();
			if (this.dropdown_active != null) {
				this.dropdownClose();
			}
			if(!classname) {
				classname = 'selectbox';
			}
			var selectbox = new Element('div', { 'class': classname }).setStyle({ width: width+'px' });
			$(el).makePositioned().setStyle({ overflow: 'visible' }).appendChild(selectbox);
			selectbox.observe('mouseover', function(event){
				window.clearTimeout(Dropdown.dropdown_timeout);
			});
			selectbox.observe('mouseout', function(event){
				Dropdown.dropdown_timeout = window.setTimeout("Dropdown.dropdownClose()", 300);
			});
			if(type == null) {
				type = 'text';
			}
			for (i = 0; i < options.length; i++) {
				if(type == 'text') {
					var option_text = new Element('span', { 'class': 'text' }).update(options[i]["text"]);
					var option_value = new Element('span', { 'class': 'value' }).hide().update(options[i]["value"]);
					var option = new Element('a', { href: 'javascript:void(0)' }).insert(option_text).insert(option_value);
				} else if(type == 'color') {
					var option_text = new Element('span', { 'class': 'text' }).update(options[i]["text"]);
					var option_value = new Element('span', { 'class': 'value' }).hide().update(options[i]["value"]);
					if(options[i]["css_bg"] != '') {
						var option_color = new Element('div', { 'class': 'color' }).setStyle({ background: options[i]["css_bg"] });
					} else {
						var option_color = new Element('div', { 'class': 'color' }).setStyle({ backgroundColor: options[i]["color"] });
					}
					var option = new Element('a', { href: 'javascript:void(0)' }).insert(option_color).insert(option_text).insert(option_value);
				} else if(type == 'image') {
					var option_img = new Element('img', { 'class': 'img', alt: options[i]["text"], src: options[i]["src"] });
					var option_value = new Element('span', { 'class': 'value' }).hide().update(options[i]["value"]);
					var option_text = new Element('span', { 'class': 'text' }).hide().update(options[i]["text"]);
					var option = new Element('a', { href: 'javascript:void(0)' }).insert(option_img).insert(option_value).insert(option_text);
				}
				if(options[i]["title"]) {
					option.title = options[i]["title"];
				}
				selectbox.appendChild(option);
				option.observe('click', function(event){
					Dropdown.dropdownClose();
					var el = Event.element(event);
					if(!el.match('a')) {
						if(el.up().match('a')) {
							el = el.up();
						} else {
							el = el.up('a');
						}
					}
					var value = el.down('.value').firstChild.nodeValue;
					var text = el.down('.text').firstChild.nodeValue;
					selectCallback(value, text);
				});
			}
			
			if (max_height != null) {
				if (selectbox.getHeight() > max_height) {
					selectbox.setStyle({ height: max_height+'px', overflow: 'auto' });
				}
			}
			this.dropdown_active = selectbox;
		}
	}
}

/*
 * Dropdown-Men�: Variablen
 */
var select_timeout;
var selectbox_active;
var colorchooser_active;
var dropdown_locked = false;

function dropdownLock() {
	dropdown_locked = true;
}

function dropdownUnlock() {
	dropdown_locked = false;
}

/*
 * Dropdown-Men�: Funktionen zum �ffnen und schlie�en
 */
function dropdownOpen(el, width, options, selectCallback, max_height) {
	if (!dropdown_locked) {
		dropdownLock();
		if (selectbox_active != null) {
			dropdownClose(selectbox_active.up().id);
		}
		if (colorchooser_active != null) {
			colorchooserClose(colorchooser_active.up().id);
		}
		var selectbox = document.createElement('div');
		Element.extend(selectbox);
		selectbox.className = 'selectbox';
		selectbox.style.width = width + 'px';
		$(el).makePositioned().appendChild(selectbox);
		el.style.overflow = 'visible';
		selectbox.observe('mouseover', function(event){
			window.clearTimeout(select_timeout);
		});
		selectbox.observe('mouseout', function(event){
			select_timeout = window.setTimeout("dropdownClose('" + el.id + "')", 300);
		});
		for (i = 0; i < options.length; i++) {
			var option = document.createElement('a');
			Element.extend(option);
			option.href = 'javascript:void(0)';
			option.title = options[i]["value"];
			var text = document.createTextNode(options[i]["text"]);
			option.appendChild(text);
			selectbox.appendChild(option);
			option.observe('click', function(event){
				dropdownClose(el.id);
				selectCallback(event);
			});
		}
		if (max_height != null) {
			if (selectbox.getHeight() > max_height) {
				selectbox.style.height = max_height + 'px';
				selectbox.style.overflow = 'auto';
			}
		}
		selectbox_active = selectbox;
	}
}

function dropdownClose(id) {
	$($(id).undoPositioned().setStyle({ overflow: 'hidden' }).lastChild).remove();
	window.setTimeout("dropdownUnlock()", 50);
	selectbox_active = null;
}

function colorchooserOpen(el, width, options, selectCallback) {
	if (!dropdown_locked) {
		dropdownLock();
		if (selectbox_active != null) {
			dropdownClose(selectbox_active.up().id);
		}
		if (colorchooser_active != null) {
			colorchooserClose(colorchooser_active.up().id);
		}
		var selectbox = document.createElement('div');
		Element.extend(selectbox);
		selectbox.className = 'colorselectbox';
		selectbox.style.width = width + 'px';
		$(el).makePositioned().appendChild(selectbox);
		selectbox.observe('mouseover', function(event){
			window.clearTimeout(select_timeout);
		});
		selectbox.observe('mouseout', function(event){
			select_timeout = window.setTimeout("colorchooserClose('" + el.id + "')", 300);
		});
		for (i = 0; i < options.length; i++) {
			var option = document.createElement('a');
			Element.extend(option);
			option.href = 'javascript:void(0)';
			option.title = options[i]["value"];
			var colorbox = document.createElement('div');
			Element.extend(colorbox);
			colorbox.className = 'option';
			colorbox.style.backgroundColor = options[i]["color"];
			//var tdiv = document.createElement('div');
			//tdiv.className = 'tooltip_title';
			//tdiv.title = options[i]["name"];
			//option.appendChild(tdiv);
			option.appendChild(colorbox);
			option.appendChild(document.createTextNode(options[i]["name"]));
			selectbox.appendChild(option);
			option.observe('click', function(event){
				//hideTooltip(event);
				colorchooserClose(el.id);
				selectCallback(event);
			});
			//option.observe('mouseover', showTooltip);
			//option.observe('mouseout', hideTooltip);
		}
		colorchooser_active = selectbox;
	}
}

function colorchooserClose(id) {
	$($(id).undoPositioned().lastChild).remove();
	window.setTimeout("dropdownUnlock()", 50);
	colorchooser_active = null;
}


/*
 * Dropdown zun Auswaehlen der Motiv-Kategorie
 */
var dropdown_motiv_kategorie_options = null;
function dropdownMotivKategorieOpen(el) {
	if(dropdown_motiv_kategorie_options == null) {
		new Ajax.Request('ajax/get_mkategorien.php', {
			method: 'get',
			onSuccess: function(transport) {
				dropdown_motiv_kategorie_options = transport.responseText.evalJSON();
				Dropdown.dropdownOpen(el, 161, dropdown_motiv_kategorie_options, dropdownMotivKategorieSelect, 224, 'text');
			}
		});
	} else {
		Dropdown.dropdownOpen(el, 161, dropdown_motiv_kategorie_options, dropdownMotivKategorieSelect, 224, 'text');
	}
}
function dropdownMotivKategorieSelect(value, text) {
	$('select_motive_kategorie').lastChild.nodeValue = text;
	Konfigurator.selectAufdruckAuswahlMotiveKat(value);
}

/*
 * Dropdown zum Auswaehlen der Spruch-Kategorie
 */
var dropdown_spruch_kategorie_options = null;
function dropdownSpruchKategorieOpen(el) {
	if(dropdown_spruch_kategorie_options == null) {
		new Ajax.Request('ajax/get_skategorien.php', {
			method: 'get',
			onSuccess: function(transport) {
				dropdown_spruch_kategorie_options = transport.responseText.evalJSON();
				Dropdown.dropdownOpen(el, 161, dropdown_spruch_kategorie_options, dropdownSpruchKategorieSelect, 224, 'text');
			}
		});
	} else {
		Dropdown.dropdownOpen(el, 161, dropdown_spruch_kategorie_options, dropdownSpruchKategorieSelect, 224, 'text');
	}
}
function dropdownSpruchKategorieSelect(value, text) {
	$('select_sprueche_kategorie').lastChild.nodeValue = text;
	Konfigurator.selectAufdruckAuswahlSpruecheKat(value);
}

/*
 * Dropdown zum Auswaehlen der Schriftart einer Textzeile
 */
var dropdown_aufdruck_text_font_options = null;
function dropdownAufdruckTextFontOpen(el, num) {
	if(dropdown_aufdruck_text_font_options == null) {
		new Ajax.Request('ajax/get_schriftarten.php', {
			method: 'get',
			onSuccess: function(transport) {
				dropdown_aufdruck_text_font_options = transport.responseText.evalJSON();
				dropdownAufdruckTextFontOpen(el, num);
			}
		});
	} else {
		var options = new Array();
		for(i=0; i<dropdown_aufdruck_text_font_options.length; i++) {
			options.push({
				'value': num+'-'+dropdown_aufdruck_text_font_options[i]['schriftart_id'],
				'text': dropdown_aufdruck_text_font_options[i]['name'],
				'title': dropdown_aufdruck_text_font_options[i]['name'],
				'src': '/content/fonts/'+dropdown_aufdruck_text_font_options[i]['schriftart_id']+'_klein.gif'
			});
		}
		Dropdown.dropdownOpen(el, 218, options, dropdownAufdruckTextFontSelect, 224, 'image', 'imgselectbox');
	}
}
function dropdownAufdruckTextFontSelect(value, text) {
	var array = value.split('-');
	var num = array[0];
	var value = array[1];
	$('select_aufdruck_text_font'+num).down('div.text').firstChild.nodeValue = text;
	var params = {
		action: 'update',
		schicht: Konfigurator.produkt_config_ebene,
		seite: Konfigurator.produkt_config_seite,
		zeile: num,
		schriftart_id: value
	};
	Konfigurator.updateProduktConfigAufdruck(params);
}

/*
 * Dropdown zum Auswaehlen der Farbe einer Textzeile
 */
var colorchooser_folie_options = null;
function colorchooserAufdruckTextColorOpen(el, num) {
	if(colorchooser_folie_options == null) {
		new Ajax.Request('ajax/get_folien.php', {
			method: 'get',
			onSuccess: function(transport) {
				colorchooser_folie_options = transport.responseText.evalJSON();
				colorchooserAufdruckTextColorOpen(el, num);
			}
		});
	} else {
		var options = new Array();
		for(i=0; i<colorchooser_folie_options.length; i++) {
			options.push({
				'value': num+'-'+colorchooser_folie_options[i]['hex']+'-'+colorchooser_folie_options[i]['css_bg'],
				'color': '#'+colorchooser_folie_options[i]['hex'],
				'text': colorchooser_folie_options[i]['name'],
				'css_bg': colorchooser_folie_options[i]['css_bg']
			});
		}
		Dropdown.dropdownOpen(el, 201, options, colorchooserAufdruckTextColorSelect, null, 'color');
	}
}
function colorchooserAufdruckTextColorSelect(value, text) {
	var array = value.split('-');
	var num = array[0];
	var value = array[1];
	var css_bg = array[2];
	$('select_aufdruck_text_color'+num).style.background = css_bg;
	var params = {
		action: 'update',
		schicht: Konfigurator.produkt_config_ebene,
		seite: Konfigurator.produkt_config_seite,
		zeile: num,
		farbe: value
	};
	Konfigurator.updateProduktConfigAufdruck(params);
}

/*
 * Dropdown zum Auswaehlen der Schriftgroesse einer Textzeile
 */
function dropdownAufdruckTextSizeOpen(el, num) {
	var options = [
		{ 'value': num+'-10', 'text': '10' }, 
		{ 'value': num+'-12', 'text': '12' }, 
		{ 'value': num+'-14', 'text': '14' }, 
		{ 'value': num+'-16', 'text': '16' }, 
	//	{ 'value': num+'-17', 'text': '17' }, 
		{ 'value': num+'-18', 'text': '18' }, 
		{ 'value': num+'-20', 'text': '20' }, 
		{ 'value': num+'-24', 'text': '24' }, 
		{ 'value': num+'-28', 'text': '28' }, 
		{ 'value': num+'-32', 'text': '32' }
	];
	Dropdown.dropdownOpen(el, 40, options, dropdownAufdruckTextSizeSelect, null, 'text');
}
function dropdownAufdruckTextSizeSelect(value, text) {
	var array = value.split('-');
	var num = array[0];
	var value = array[1];
	$('select_aufdruck_text_size'+num).lastChild.nodeValue = text;
	var params = {
		action: 'update',
		schicht: Konfigurator.produkt_config_ebene,
		seite: Konfigurator.produkt_config_seite,
		zeile: num,
		groesse: value
	};
	Konfigurator.updateProduktConfigAufdruck(params);
}

/*
 * Dropdown zum Auswaehlen der Farbe eines Motivs
 */
function colorchooserAufdruckMotivColorOpen(el, num) {
	if(colorchooser_folie_options == null) {
		new Ajax.Request('ajax/get_folien.php', {
			method: 'get',
			onSuccess: function(transport) {
				colorchooser_folie_options = transport.responseText.evalJSON();
				colorchooserAufdruckMotivColorOpen(el, num);
			}
		});
	} else {
		var options = new Array();
		for(i=0; i<colorchooser_folie_options.length; i++) {
			options.push({
				'value': num+'-'+colorchooser_folie_options[i]['hex']+'-'+colorchooser_folie_options[i]['css_bg'],
				'color': '#'+colorchooser_folie_options[i]['hex'],
				'text': colorchooser_folie_options[i]['name'],
				'css_bg': colorchooser_folie_options[i]['css_bg']
			});
		}
		Dropdown.dropdownOpen(el, 201, options, colorchooserAufdruckMotivColorSelect, null, 'color');
	}
}
function colorchooserAufdruckMotivColorSelect(value, text) {
	var array = value.split('-');
	var num = array[0];
	var value = array[1];
	var css_bg = array[2];
	$('select_aufdruck_motiv_color'+num).down('div.colorselect').style.background = css_bg;
	$('select_aufdruck_motiv_color'+num).down('div.colorselect_text').update(text);
	var params = {
		action: 'update',
		seite: Konfigurator.produkt_config_seite,
		schicht: Konfigurator.produkt_config_ebene
	};
	params["farbe"+num] = value;
	Konfigurator.updateProduktConfigAufdruck(params);
}

/*
 * Dropdown zum Auswaehlen der Skalierung des Aufdrucks
 */
function dropdownAufdruckTransformSizeOpen(el) {
	var options = [
		{ 'value': 1.0, 'text': '100%'},
		{ 'value': 0.9, 'text': '90%'},
		{ 'value': 0.8, 'text': '80%'},
		{ 'value': 0.7, 'text': '70%'},
		{ 'value': 0.6, 'text': '60%'},
		{ 'value': 0.5, 'text': '50%'},
		{ 'value': 0.4, 'text': '40%'},
		{ 'value': 0.3, 'text': '30%'}
	//	{ 'value': 0.2, 'text': '20%'},
	//	{ 'value': 0.1, 'text': '10%'}
	];
	Dropdown.dropdownOpen(el, 60, options, dropdownAufdruckTransformSizeSelect, null, 'text');
}
function dropdownAufdruckTransformSizeSelect(value, text) {
	$('select_aufdruck_transform_size').lastChild.nodeValue = text;
	var params = {
		action: 'update',
		seite: Konfigurator.produkt_config_seite,
		schicht: Konfigurator.produkt_config_ebene,
		skalierung: value
	};
	Konfigurator.updateProduktConfigAufdruck(params);
}

/*
 * Dropdown zum Auswaehlen der Rotation eines Bildes
 */
function dropdownAufdruckBildDrehungOpen(el) {
	var options = [
		{ 'value':   0, 'text':   '0°' },
		{ 'value':  90, 'text':  '90°' },
		{ 'value': 180, 'text': '180°' },
		{ 'value': 270, 'text': '270°' }
	];
	Dropdown.dropdownOpen(el, 60, options, dropdownAufdruckBildDrehungSelect, null, 'text');
}
function dropdownAufdruckBildDrehungSelect(value, text) {
	$('select_aufdruck_bild_drehung').lastChild.nodeValue = text;
	var params = {
		action: 'update',
		seite: Konfigurator.produkt_config_seite,
		schicht: Konfigurator.produkt_config_ebene,
		drehung: value
	};
	Konfigurator.updateProduktConfigAufdruck(params);
}

/*
 * Dropdown zum Auswaehlen der Rotation des Aufdrucks
 */
function dropdownAufdruckTransformRotateOpen(el) {
	var options = [
		{ 'value': -135, 'text': '-135'},
		{ 'value': -90, 'text': '-90'},
		{ 'value': -80, 'text': '-80'},
		{ 'value': -70, 'text': '-70'},
		{ 'value': -60, 'text': '-60'},
		{ 'value': -50, 'text': '-50'},
		{ 'value': -45, 'text': '-45'},
		{ 'value': -40, 'text': '-40'},
		{ 'value': -30, 'text': '-30'},
		{ 'value': -20, 'text': '-20'},
		{ 'value': -10, 'text': '-10'},
		{ 'value': 0, 'text': '0'},
		{ 'value': 10, 'text': '10'},
		{ 'value': 20, 'text': '20'},
		{ 'value': 30, 'text': '30'},
		{ 'value': 40, 'text': '40'},
		{ 'value': 45, 'text': '45'},
		{ 'value': 50, 'text': '50'},
		{ 'value': 60, 'text': '60'},
		{ 'value': 70, 'text': '70'},
		{ 'value': 80, 'text': '80'},
		{ 'value': 90, 'text': '90'},
		{ 'value': 135, 'text': '135'},
		{ 'value': 180, 'text': '180'}
	];
	Dropdown.dropdownOpen(el, 60, options, dropdownAufdruckTransformRotateSelect, null, 'text');
}
function dropdownAufdruckTransformRotateSelect(value, text) {
	$('select_aufdruck_transform_rotate').lastChild.nodeValue = text;
	var params = {
		action: 'update',
		seite: Konfigurator.produkt_config_seite,
		schicht: Konfigurator.produkt_config_ebene,
		drehung: value
	};
	Konfigurator.updateProduktConfigAufdruck(params);
}

/*
 * Dropdown zum Auswaehlen der Anzahl, wieviel Stueck bestellt werden sollen
 */
function dropdownGroesseAnzahlOpen(el, num) {
	var options = [
		{ 'value': num+'-0', 'text': '0' },
		{ 'value': num+'-1', 'text': '1' },
		{ 'value': num+'-2', 'text': '2' },
		{ 'value': num+'-3', 'text': '3' },
		{ 'value': num+'-4', 'text': '4' },
		{ 'value': num+'-5', 'text': '5' },
		{ 'value': num+'-6', 'text': '6' },
		{ 'value': num+'-7', 'text': '7' },
		{ 'value': num+'-8', 'text': '8' },
		{ 'value': num+'-9', 'text': '9' },
		{ 'value': num+'-10', 'text': '10' },
		{ 'value': num+'-11', 'text': '>10' }
	];
	Dropdown.dropdownOpen(el, 49, options, dropdownGroesseAnzahlSelect, null, 'text');
}
function dropdownGroesseAnzahlSelect(value, text) {
	var array = value.split('-');
	var num = array[0];
	var value = array[1];
	$('select_produkt_groesse'+num).lastChild.nodeValue = text;
	$('input_produkt_groesse['+num+']').value = value;
	if(value == '11') {
		$('input_produkt_groesse['+num+']').show();
	} else {
		$('input_produkt_groesse['+num+']').hide();
	}
}

/*
 * Dropdown zum Auswaehlen der Anrede
 */
function dropdownAnredeOpen(el, num) {
	var options = [
		{ 'value': num+'-Herr', 'text': 'Herr' },
		{ 'value': num+'-Frau', 'text': 'Frau' }
	];
	Dropdown.dropdownOpen(el, 148, options, dropdownAnredeSelect, null, 'text');
}
function dropdownAnredeSelect(value, text) {
	var array = value.split('-');
	var num = array[0];
	var value = array[1];
	$('select_anrede_'+num).lastChild.nodeValue = text;
	$('select_anrede_'+num+'_hidden').value = value;
}

/*
 * Dropdown zum Auswaehlen des Lands
 */
var dropdown_land_options = null;
function dropdownLandOpen(el, num) {
	if(dropdown_land_options == null) {
		new Ajax.Request('ajax/get_laender.php', {
			method: 'get',
			onSuccess: function(transport) {
				dropdown_land_options = transport.responseText.evalJSON();
				dropdownLandOpen(el, num);
			}
		});
	} else {
		var options = new Array();
		for(i=0; i<dropdown_land_options.length; i++) {
			options.push({
				'value': num+'-'+dropdown_land_options[i]['value'],
				'text': dropdown_land_options[i]['text']
			});
		}
		Dropdown.dropdownOpen(el, 148, options, dropdownLandSelect, null, 'text');
	}
}
function dropdownLandSelect(value, text) {
	var array = value.split('-');
	var num = array[0];
	var value = array[1];
	$('select_land_'+num).lastChild.nodeValue = text;
	$('select_land_'+num+'_hidden').value = value;
}