$.fn.jdrop = function() {                  
    $('body').append('<div class="jDrop" style="display:none"></div>')
    var setWidth=$('.jDrop').css('width');
    if(setWidth=='auto'){var widthMode="auto"} else{var widthMode="css";setWidth=setWidth.replace('px','');}
    this.each(function(intIndex){               
        $(this).after('<div class="jDrop"><div class="jSel"><span class="jSelOp">'+
			$(this).children('option:selected').includeHTML()+'</span></div><div class="jDropBut"></div><div class="jOpDrop"></div></div>').hide();
        if(widthMode=="auto"){ var staticWidth = $(this).width()+25}
        else { var staticWidth = setWidth - $(".jDropBut:eq("+intIndex+")").width()}        
        $(".jDrop:eq("+intIndex+")").children('.jSel').css({width: staticWidth+'px'})                                                                                                                                        
    })
    $('.jSel, .jDropBut').click(function(){                            
        if( $('.jSel').index(this) != -1){var jDropIndex = $('.jSel').index(this)} else {var jDropIndex = $('.jDropBut').index(this)}        
        var jDropOptions='';                      
        $('select:eq('+jDropIndex+')').children('option').each(function(intIndex){
            jDropOptions=jDropOptions+'<div class="jOp">'+$(this).includeHTML()+'</div>';                              
        })
        $('.jDrop:eq('+jDropIndex+')').children('.jOpDrop').html(jDropOptions)
        $(".jDrop:eq("+jDropIndex+")").children('.jOpDrop').css({minWidth:$(".jDrop:eq("+jDropIndex+")").outerWidth()})

        var jDropTop = $(this).parent().offset().top-$(this).parent().offsetParent().offset().top;
        var dropDownTop = jDropTop + $(this).parent().height()-1;        
        var dropDownHeight=$('.jDrop:eq('+jDropIndex+') .jOpDrop').height();  
        var winHeight=$(window).height();                                      
        var docScrollTop=$(document).scrollTop();
        var dropDownYSpace=dropDownTop+dropDownHeight + $(this).parent().offsetParent().offset().top;
   
        var jDropLeft = $(this).parent().offset().left - $(this).parent().offsetParent().offset().left;
        var dropDownLeft=jDropLeft;
        var dropDownWidth=$('.jDrop:eq('+jDropIndex+') .jOpDrop').width();         
        var jDropWidth = $(this).parent().outerWidth();
        var winWidth=$(window).width();
        var docScrollLeft=$(document).scrollLeft();
        var dropDownXSpace=dropDownLeft+dropDownWidth+ $(this).parent().offsetParent().offset().left;                      
        
        if (dropDownYSpace>winHeight+docScrollTop){dropDownTop=jDropTop-dropDownHeight-1}                        
        if (dropDownXSpace>winWidth+docScrollLeft){dropDownLeft=docScrollLeft+winWidth-dropDownWidth-$(this).parent().offsetParent().offset().left-3;}
        $('.jDrop:eq('+jDropIndex+') .jOpDrop').css({top: dropDownTop, left: dropDownLeft })   

        $(this).parent().children('.jOpDrop').fadeIn(50,function(){            
            $(this).parent().hover(function(){},function(){$(this).children('.jOpDrop').fadeOut()}),
            $(this).children('.jOp').hover(function(){$(this).addClass('hover')}, function(){$(this).removeClass('hover')})
            $(this).children('.jOp').click(function(){
				var langimg = $("img", this).attr('class');
				window.location.href = public_root_path + "/index.php?lang=" + langimg.replace("ccc", "");
                $("select[name='"+ $(this).parent().parent().attr('title')+"']").val($(this).text())
                $(this).parent().parent().children('.jSel').children('.jSelOp').html($(this).html()).parent().parent().children('.jOpDrop').fadeOut()})
        })
          
    })
    $('.jSel').hover(function(){$(this).addClass('hover')},function(){$(this).removeClass('hover')})
    $('.jDropBut').hover(function(){$(this).addClass('hover')},function(){$(this).removeClass('hover')})        
                  
};
$.fn.includeHTML = function () {
	  var langimg = $(this).attr('class');
      var thisHTML = '<img alt="" class="ccc'+$(this).val()+'" src="'+public_root_path+'/_lang/'+ $(this).val() +'.'+ langimg.replace("c_", "") +'" />';
      if (thisHTML){
          return thisHTML + $(this).text() }
      else{
          return $(this).html()
      }
};