/*
var $head = $('head')
if ($head.find('#selectbox_css').length==0){
  $head.append('<link id="selectbox_css" type="text/css" rel="stylesheet" href="/fileadmin/templates/css/entega.selectbox.css" media="all"/>')
}
*/
Entega = window.Entega || {}
 
Entega.SelectBox = function(params) {

  var id = params.id+'_js_select';
  var ie = $.browser.msie;
  
  var className = params.className || 'custom_selectbox';
  var el = $('#'+params.id);
 
  //Remove identical Element from page
  if ($("div#"+ id).length>0) { 
    ("div#"+ id).remove()
    el.show();
  }  
  
  var width = params.width || el.width();
  var selectedText = (params.selectedText!=null) ? params.selectedText : '---';
  var onclick = params.onclick
  var deep = params.deep || 3;
  var styles = width ? 'width:'+width+'px' : '';
  var ulStyles = width ? 'width:'+(width-1)+'px' : 'width:100%';
  var content = "<div id='" + id + "' class='"+className+"' style='"+styles+"'><a class='noajax' id='a" + id + "' href='#'>"+selectedText+"</a><ul style='"+ulStyles+"'></ul></div>";
  
  if (ie) {
    var parent = findParent(el,deep);
    parent.append(content);
    var $thisContainer = $("div#"+ id);
    var elPos = el.offset();
    el.hide();
    $thisContainer.css('position','absolute');
    $thisContainer.css('left','0px');
    $thisContainer.css('top','0px');
    var pos = $thisContainer.offset();
    //Korrektur der Position
    $thisContainer.css('top',(elPos.top-pos.top)+'px');
    $thisContainer.css('left',(elPos.left-pos.left)+'px');

  }else {
    el.hide();
    el.after(content);
  } 
  
  function findParent(el,level){
    if (level==0) return el;
    
    var parent = el.parent()
    if (parent) return findParent(parent,level-1)
    else return el
  }
    
  var $anchor = $("#a"+id);
  $anchor.css('width',(width-parseInt($anchor.css('paddingLeft')))+'px');
    
	// CLICK ON TITLE
  $anchor[0].onclick=function(){
    $(this).next().slideToggle("fast");
    return false;
	};

	var els = el.children("option");   
  var container = $($("div#"+ id + " ul")[0])
  var options = {}
       
	$.each(els, function(i,n) {
		var $opt = $(n)
    text = ($opt.attr("rel") || '') + ' ' + $opt.text();
		var $li = $("<li rel='" + $opt.val() +"'>" + text + "</li>");
       
    if ($.browser.msie && $.browser.version<=6) { 
      $li.mouseover(function(){
        $(this).addClass('hover')
      });
      
      $li.mouseout(function(){
        $(this).removeClass('hover');
      });
    }
  
    $li[0].index = i
		options[i] = $opt[0]
    container.append($li)

		if ($opt.attr("selected")) {
			selectedText = text;
		  $("#a"+id).html(selectedText)
    }	
	});   
  
	// CLICK ON ELEMENT
	$("div#" + id + " ul li").click(function(){
    listName = $(this).parent().parent().attr('id');
		listName = listName.substr(0, listName.length - 10);
		
    $('[name=' + listName + ']').val($(this).attr("rel"));

    $(this).parent().parent().children().eq(0).html($(this).html());
		$("div#"+ id + " ul").slideUp("fast");
		
		var option = options[this.index]
		if (onclick) onclick(option)
	}); 
};

/*********** Create different select boxes ****************/
Entega.SelectBox.create = function(type){
  $(document).ready(function(){
  switch(type) {
    case 'energieberatung':
      return new Entega.SelectBox({
        id:'pageselector', 
        className:'custom_selectbox' ,
        onclick:function(option){
            if (option.value!='---') Rollos.getContent(option.value,rolloId,false);
        }
      });
      
    case 'kontaktlightbox':
      //if ($.browser.msie) return;
      return new Entega.SelectBox({
        id:'pageselector_lightbox', 
        className:'custom_selectbox lightbox' ,
        onclick:function(option){
            if (option.value!='---') Rollos.getContent(option.value,rolloId,false);
        }
      });  

    case 'usertype-selector':
      if (Entega.SelectBox.userTypeSelectBox) return Entega.SelectBox.userTypeSelectBox;
      Entega.SelectBox.userTypeSelectBox = new Entega.SelectBox({
        id:'usertype_selector',
        width: '189', 
        deep:4,
        className:'custom_selectbox usertype_selector',
        onclick:function(option){
            $(option).trigger('change'); //$(option).parents("form").submit();
        }
      });          
  }
});
}