/* 
 * Created by Aaron Ellinwood for Miles Media
 * Rebuilds an <option> block into a div based dropdown
 */
;jQuery.fn.magic_drop = function(op) {
                return this.each(function(){
                    var s = this.serial = md.o.length;
                    var o = $.extend({},md.defaults,op);
                    md.o[s] = md.op = o;
                    
                    
                    var isVisible = false;
                    var e = this; // for the children

                    var dname = $(e).attr('id')+'_drop';
                    var cname = $(e).attr('id')+'_ctrl';
                    
                    $('#'+dname).remove();
                    $('#'+cname).remove();
                    
                    if (o.dropList == ""){
                        $('body').append('<div class="drop" id="'+dname+'"><ul></ul></div>');
                        $(this).find('option').each(function(){
                            if (this.value){
                                $('#'+dname+" ul").append('<li><a href="'+o.linkPathA+this.value+o.linkPathB+'">'+$(this).html()+'</a></li>');
                            }
                            $(this).remove();
                        });
                    } else {
                        $('#'+o.dropList).appendTo('body').attr('id',dname);
                        
                        //$(this).remove();
                    }
                    $(this).after('<span class="control" id="'+cname+'"><a href="javascript:void(0);" >'+o.readyName+'</a></span>').remove();
                    var tx = $('#'+cname+" a").offset();
                    var bmOff = 8;
                    if (jQuery.boxModel) { bmOff = 8; }
                    var dtop = tx.top + bmOff + $('#'+cname+" a").height();

                    $('#'+dname).css({ 'display':'none', 'position' : 'absolute', 'top':dtop, 'left': tx.left, 'padding':'5px', 'height':o.height, 'width':o.width })
                        .bind('mouseover',function(){isVisible = true;})
                        .bind('mouseout',function(){
                            isVisible = false;
                            setTimeout(function(){
                                if (isVisible == false) {$('#'+dname).hide(); }
                            },1500
                            );
                        });
                    $('#'+cname+" a")
                        .bind('mouseover',function(){isVisible = true;})
                        .bind('click',function(e){
                            var tx = $('#'+cname+" a").offset();
                            var bmOff = 8;
                            if (jQuery.boxModel) { bmOff = 8; }
                            var dtop = tx.top + bmOff + $('#'+cname+" a").height();
                            if (o.orientation == 'up'){
                                dtop = tx.top - $('#'+dname).height() - bmOff;
                                
                            }
                            $('#'+dname).css({'display':'block','top':dtop, 'left': tx.left});
                            isVisible = true; })
                        .bind('mouseout', function(e){
                            isVisible = false;
                            setTimeout(function(){
                                if (isVisible == false) {$('#'+dname).hide(); }
                            },1500
                            );
                        })
                  });
            };
            var md = $.fn.magic_drop;
            md.o = [];
            md.op = {};
            md.defaults = {
                linkPathA   : "",
                linkPathB   : "",
                readyName   : 'test',
                height      : '200px',
                width       : '200px',
                dropList    : '',
                orientation : 'down',
                onInit      : function(){} // callback functions
            };
