/* jDrop 0.4.5 - Alistair MacDonald - director@liminal-vj.com */
var msStart=0;var msTime=0;var msEnd=0;var msDelay=0;
var dropDownTop=0;var dropDownLeft=0;
var clickedInnerInput=0;

$.fn.jdrop = function(initMode) {
    $('.jDrop').livequery(function(){ $(this).children('.jSel, .jDropBut').click(function(){ //msTime = new Date();msStart = msTime.getTime();

        clickedInnerInput=0;
        $(this).parent().children('.jSel, .jDropBut').addClass('dropHover');
        
        var jDropTop = $(this).parent().offset().top-$(this).parent().offsetParent().offset().top;
        var dropDownTop = jDropTop + $(this).parent().height()-1;
        var dropDownHeight=$(this).parent().children('.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=$(this).parent().children('.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}
        if (dropDownXSpace>winWidth+docScrollLeft){dropDownLeft=docScrollLeft+winWidth-dropDownWidth-$(this).parent().offsetParent().offset().left-3;}
        
        // Width FIX needed in some situations //
        $(this).parent().children('.jOpDrop').children('.jOp').css({width: $(this).parent().outerWidth() - ($(this).parent().children('.jDropBut').width()-2) });                          
        
        // Single Dimensional Shadow //
        $(this).parent().children('.jOpDrop').children('.jDropShadow').css({width: jDropWidth+2, left:'-1px'});                                                
        
        $(this).parent().children('.jOpDrop').css({top: dropDownTop, left: dropDownLeft }).fadeIn(1,function(){ //msTime = new Date();msEnd=msTime.getTime();msDelay=msEnd-msStart;console.log(msDelay);
              $(this).parent().hover(function(){},function(){$(this).children('.jOpDrop').fadeOut(1);$(this).children('.jSel, .jDropBut').removeClass('dropHover');  })
              $(this).children('.jOp').hover(function(){$(this).addClass('hover')}, function(){$(this).removeClass('hover')})                                                                                              
              $(this).children('.jOp').click(function(){                                    
                  if (clickedInnerInput==0){
                      var jClass = $(this).attr('class');
                      var jValIndex = $(this).attr('class').slice(jClass.indexOf('jVal')+4, jClass.indexOf('_'));
                      $(this).parent().parent().prev().val(   $(this).parent().parent().prev().children('option:eq('+jValIndex+')').val()   );                          

                      //$(this).parent().parent().prev().attr("val", "asdss");
                      //var myVar = $(this).parent().parent().prev().attr("val");
                      //alert(myVar);
                      
                      $(this).parent().parent().children('.jSel').children('.jSelOp').html($(this).html()).parent().parent().children('.jOpDrop').slideUp(1);
                                                                                                                                                                                                      
                      var updateVal = $(this).parent().parent().prev().children('option:eq('+jValIndex+')').val();

                      $(this).parent().parent().prev().attr( "value", updateVal );

                      $(this).parent().parent().children('.jSel, .jDropBut').removeClass('dropHover');
                      $(this).unbind('click');
                  }
              })
        });

    })
    })
        
    
    $('.jSel').livequery(function(){  $(this).hover(function(){$(this).toggleClass('hover')},function(){$(this).toggleClass('hover')}) });
    $('.jDropBut').livequery(function(){  $(this).hover(function(){$(this).toggleClass('hover')},function(){$(this).toggleClass('hover')}); });
    
    $('INPUT').keypress(function(){
        var thisName=$(this).attr('name');
        $('INPUT[name='+thisName+']').val( $(this).val() );
    });
    $('.jOp INPUT').click(function(){clickedInnerInput=1;})
};
$.fn.includeHTML = function () { var thisHTML = $(this).attr('html'); if (thisHTML){ return thisHTML } else { return $(this).html() } };