/************************************************************************************************************
	Editable select
	Copyright (C) September 2005  DTHMLGoodies.com, Alf Magne Kalleland
	
	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.
	
	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
	
	Dhtmlgoodies.com., hereby disclaims all copyright interest in this script
	written by Alf Magne Kalleland.
	
	Alf Magne Kalleland, 2006
	Owner of DHTMLgoodies.com
		
	************************************************************************************************************/	


function populate_dd_search(data) {
    if (data.categories){
        var options = '';
        var fakebox = '';
        jQuery.each(data.categories, function(i,item){
            var selected = (item.selected) ? 'selected' : '';
            options += '<option value="' + decodeURI(unescape(item.v)) + '" ' + selected + '>' + decodeURI(unescape(item.n)) + '</option>';
            fakebox += decodeURI(unescape(item.n)) + '|';
        });
        jQuery("select#searchcategory").html(options);
        document.getElementById("searchcategory_text").setAttribute("selectBoxOptions", fakebox);
        createEditableSelect(dd_searchform.searchcategory_text);
    }
    if (data.vendors){
        var options = '';
        var fakebox = '';
        jQuery.each(data.vendors, function(i,item){
            var selected = (item.selected) ? 'selected' : '';
            options += '<option value="' + decodeURI(unescape(item.v)) + '" ' + selected + '>' + decodeURI(unescape(item.n)) + '</option>';
            fakebox += decodeURI(unescape(helper_str_replace('%26','&amp;',item.n))) + '|';
        });
        jQuery("select#searchvendor").html(options);
        document.getElementById("searchvendor_text").setAttribute("selectBoxOptions", fakebox);
        createEditableSelect(dd_searchform.searchvendor_text);
    }
    if (data.colors){
        var options = '';
        var fakebox = '';
        jQuery.each(data.colors, function(i,item){
            var selected = (item.selected) ? 'selected' : '';
            options += '<option value="' + decodeURI(unescape(item.v)) + '" ' + selected + '>' + decodeURI(unescape(item.n)) + '</option>';
            fakebox += decodeURI(unescape(item.n)) + '|';
        });
        jQuery("select#searchfarbe").html(options);
        document.getElementById("searchfarbe_text").setAttribute("selectBoxOptions", fakebox);
        createEditableSelect(dd_searchform.searchfarbe_text);
    }
    if (data.sizes){
        var options = '';
        var fakebox = '';
        jQuery.each(data.sizes, function(i,item){
            var selected = (item.selected) ? 'selected' : '';
            options += '<option value="' + decodeURI(unescape(item.v)) + '" ' + selected + '>' + decodeURI(unescape(item.n)) + '</option>';
            fakebox += decodeURI(unescape(item.n)) + '|';
        });
        jQuery("select#searchgroesse").html(options);
        document.getElementById("searchgroesse_text").setAttribute("selectBoxOptions", fakebox);
        createEditableSelect(dd_searchform.searchgroesse_text);
    }
/*if (data.prices){
     			var options = '';
     			var fakebox = '';
          $.each(data.prices, function(i,item){
          	var selected = (item.selected) ? 'selected' : '';
		        options += '<option value="' + decodeURI(unescape(item.v)) + '" ' + selected + '>' + decodeURI(unescape(item.n)) + '</option>';
		        fakebox += decodeURI(unescape(item.n)) + ';';
          });
      		$("select#searchpreis").html(options);
      		document.getElementById("searchpreis_text").setAttribute("selectBoxOptions", fakebox);
      		createEditableSelect(dd_searchform.searchpreis_text);
	}*/
}
	
$.ajaxSetup({
    contentType: "text/html; charset=ISO-8859-15"
});
        
        
        
        
// Path to arrow images
	
var selectBoxIds = 0;
var currentlyOpenedOptionBox = false;
var editableSelect_activeArrow = false;
	

	
function selectBox_switchImageUrl()
{
    if(this.src.indexOf(arrowImage)>=0){
        this.src = this.src.replace(arrowImage,arrowImageOver);
    }else{
        this.src = this.src.replace(arrowImageOver,arrowImage);
    }
}
	
function selectBox_showOptions(e)
{
    if(editableSelect_activeArrow && editableSelect_activeArrow!=this){
        editableSelect_activeArrow.src = arrowImage;
			
    }
    editableSelect_activeArrow = this;
		
    var numId = this.id.replace(/[^\d]/g,'');
    var optionDiv = document.getElementById('selectBoxOptions' + numId);
    if(optionDiv.style.display=='block'){
        optionDiv.style.display='none';
        if(navigator.userAgent.indexOf('MSIE')>=0)document.getElementById('selectBoxIframe' + numId).style.display='none';
        this.src = arrowImageOver;
    }else{
        optionDiv.style.display='block';
        if(navigator.userAgent.indexOf('MSIE')>=0)document.getElementById('selectBoxIframe' + numId).style.display='block';
        this.src = arrowImageDown;
        if(currentlyOpenedOptionBox && currentlyOpenedOptionBox!=optionDiv)currentlyOpenedOptionBox.style.display='none';
        currentlyOpenedOptionBox= optionDiv;
        if (!e) var e = window.event;
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();
        window.document.body.onclick = hideCurrentSelectboxOptions;
    }
}
	
function selectOptionValue(e)
{
    var parentNode = this.parentNode.parentNode;
    var textInput = parentNode.getElementsByTagName('INPUT')[0];
    textInput.value = this.innerHTML.replace('&amp;','&');
    e = e || window.event;
    var targ = e.target || e.srcElement;
    var selectindex = targ.caption;
    var selectboxId = textInput.name.replace('_text','');
    document.getElementById(selectboxId).options[selectindex].selected = true;
    this.parentNode.style.display='none';
		
    document.getElementById('arrowSelectBox' + parentNode.id.replace(/[^\d]/g,'')).src = arrowImageOver;
		
    if(navigator.userAgent.indexOf('MSIE')>=0)document.getElementById('selectBoxIframe' + parentNode.id.replace(/[^\d]/g,'')).style.display='none';
		
    textInput.focus();
    hideCurrentSelectboxOptions();
}
	
function hideCurrentSelectboxOptions()
{
    currentlyOpenedOptionBox.style.display='none';
    window.document.body.onclick = null;
}
	
	
var activeOption;
function highlightSelectBoxOption()
{
    if(this.style.backgroundColor=='#316AC5'){
        this.style.backgroundColor='';
        this.style.color='';
    }else{
        this.style.backgroundColor='#316AC5';
        this.style.color='#FFF';
    }
		
    if(activeOption){
        activeOption.style.backgroundColor='';
        activeOption.style.color='';
    }
    activeOption = this;
		
}
	
function createEditableSelect(dest)
{
    dest.className='selectBoxInput';
    var div = document.createElement('DIV');
    div.style.styleFloat = 'left';
    div.style.width = dest.offsetWidth + 16 + 'px';
    if(navigator.userAgent.indexOf('MSIE')>=0){
        div.style.width = dest.offsetWidth + 15 + 'px';
    }
    div.style.position = 'relative';
    div.id = 'selectBox' + selectBoxIds;
    var parent = dest.parentNode;
    parent.insertBefore(div,dest);
    div.appendChild(dest);
    div.className='selectBox';
    div.style.zIndex = 100 - selectBoxIds;

    var img = document.createElement('IMG');
    img.src = arrowImage;
    img.className = 'selectBoxArrow';
		
    img.onmouseover = selectBox_switchImageUrl;
    img.onmouseout = selectBox_switchImageUrl;
    img.onclick = selectBox_showOptions;
    img.id = 'arrowSelectBox' + selectBoxIds;
    img.onclick = selectBox_showOptions;

    div.appendChild(img);
		
    var optionDiv = document.createElement('DIV');
    optionDiv.id = 'selectBoxOptions' + selectBoxIds;
    optionDiv.className='selectBoxOptionContainer';
    //optionDiv.style.width = div.offsetWidth-2 + 'px';
    optionDiv.style.width = div.offsetWidth+70 + 'px';
    div.appendChild(optionDiv);
		
    if(navigator.userAgent.indexOf('MSIE')>=0){
        var iframe = document.createElement('<IFRAME src="about:blank" frameborder=0>');
        iframe.style.width = optionDiv.style.width;
        iframe.style.height = optionDiv.offsetHeight + 'px';
        iframe.style.display='none';
        iframe.style.visibility='hidden';
        iframe.id = 'selectBoxIframe' + selectBoxIds;
        div.appendChild(iframe);
    }
		
    if(dest.getAttribute('selectBoxOptions')){
        var options = dest.getAttribute('selectBoxOptions').split('|');
        var optionsTotalHeight = 0;
        var optionArray = new Array();
        for(var no=0;no<options.length;no++){
            var anOption = document.createElement('DIV');
            anOption.innerHTML = options[no];
            anOption.className='selectBoxAnOption';
            anOption.caption = no;
            anOption.onclick = selectOptionValue;
            //anOption.style.width = optionDiv.style.width.replace('px','') - 2 + 'px';
            anOption.onmouseover = highlightSelectBoxOption;
            optionDiv.appendChild(anOption);
            optionsTotalHeight = optionsTotalHeight + anOption.offsetHeight;
            optionArray.push(anOption);
        }
        if(optionsTotalHeight > optionDiv.offsetHeight){
            for(var no=0;no<optionArray.length;no++){
                optionArray[no].style.width = optionDiv.style.width.replace('px','') - 22 + 'px';
            }
        }
        optionDiv.style.display='none';
        optionDiv.style.visibility='visible';
    }
		
    selectBoxIds = selectBoxIds + 1;
}

function helper_str_replace(search, replace, subject) {
	return subject.split(search).join(replace);
}	

