(function($){
    
    //Function for dashboard mysites page
    $.fn.wpDashboard = function() {
        
        return this.each(function() {
        
            var dashboard = $(this);
            var speed = 500;
            //insert  border blocks
                //these can also just be inserted into the html instead
            dashboard.find(".dashboardBlocks > li").append('<div class="wb_top" ></div> <div class="wb_bottom"></div>');
            //set up click action for "show dashboard" link
            $("#dashboardCompact .dashboardFooter > a").click(function(){ 
                dashboard.css({height:"125px"});
                $("#dashboardCompact").animate({opacity:0},{queue:false, duration:speed}).slideUp(speed, function(){ $(this).css({display:"none"});});
                $("#dashboardFull").css({opacity:0}).animate({opacity:1},{queue:false, duration:speed}).slideDown(speed);
                return false; 
            });
            //set up click action for "hide dashboard" link
            $("#dashboardFull .dashboardFooter > a").click(function(){ 
                dashboard.css({height:"125px"});
                $("#dashboardFull").animate({opacity:0},{queue:false, duration:speed}).slideUp(speed, function(){$(this).css({display:"none"});});
                $("#dashboardCompact")
                    .css({opacity:0})
                    .animate({opacity:1},{queue:false, duration:speed})
                    .slideDown(speed, function(){ 
                        $("#dashboard").height("45px");  
                });
                
                return false; 
                
            });
        
        
        
         });
     };
     
     /* Set up and functions for mySites */
     
     $.fn.wpMySites = function() {
        
        return this.each(function() {
            
            mySitesContainer = $(this);
            //action when clicking a checkbox
            $("#mysiteListing tr td input:checkbox").click(function(){ this.checked==false?deactivateRow(this):activateRow(this); }); 
            //start with messages div hidden, include border boxes
            $("#messages").css({display:"none"}).append('<div class="wb_top" ></div> <div class="wb_bottom"></div>');
            //select all, select none
            $("#mysiteListing .selectAll").click(function(e){ $("#mySiteList input:checkbox").each(function(){ this.checked="checked"; activateRow(this); }); return false;});
            $("#mysiteListing .selectNone").click(function(e){ $("#mySiteList input:checkbox").each(function(){ this.checked=false; deactivateRow(this); }); return false;});

            $("#mysiteListing .selectAll").click(function(e){ $("#textAdTable input:checkbox").each(function(){ this.checked="checked"; activateRow(this); }); return false;});
            $("#mysiteListing .selectNone").click(function(e){ $("#textAdTable input:checkbox").each(function(){ this.checked=false; deactivateRow(this); }); return false;});

            //set up the draggable rows
            $("#mySiteList > tbody > tr").draggable({
                appendTo:"body",
                cursor:"pointer",
                cursorAt:{top:20,left:100},
                helper:function(){
                    //if it's an active row, grab all the rows
                    if($(this).hasClass("active")){
                        var inLength = $("#mySiteList input:checkbox:checked").length;
                        //this is the drag box that you see when you drag
                        return $('<div class="dragBox"><div class="back"></div><div class="front"><p><strong>'+inLength+' Site'+(inLength!=1?'s':'')+'</strong></p><p>Drag to a Group</p></div></div>');
                    }
                    //otherwise just grab the dragged row
                    else{
                        //this is the drag box that you see when you drag
                        return $('<div class="dragBox"><div class="back"></div><div class="front"><p><strong>1 Site</strong></p><p>Drag to a Group</p></div></div>');
                    }
                }
            });
            

        
            var activateRow = function(input){

                var row = $(input).parents("tr");
                
                $(row).addClass("active");
                
                
                
            
            };
            
            var deactivateRow = function(input){
                
                var row = $(input).parents("tr");
                $(row).removeClass("active");
            
            };
            
            //set up the droppable list elements
            $("#myGroupsList li.dropTarget").droppable({
                accept: "#mySiteList > tbody > tr",
                activeClass: 'droppable-active',
                hoverClass: 'droppable-hover',
                tolerance: 'pointer',
                drop: function(ev, ui) {
                    var dropEl = this;
                    
                    var dragEl = $(ui.draggable);
                    
                    if($(dragEl).hasClass("active")){
                        var data = $("#mysiteForm input:checkbox:checked[name='SiteID']").serialize();
                    }
                    else{

                        var data = "SiteID="+$(dragEl).find("input:checkbox[name='SiteID']").attr("value");
                    }
                    
                    
                    var group_id = $(this).find("a").attr("id");
                    group_id = group_id.substr(6);
                    data = data + "&group_id="+escape(group_id);
                    
                    //any additional data can be added to the end of this query string
                    
                    
                    var url = "/ajaxResponse/mysitesAjax.cfm?"+data;
                    $.getJSON(url, function(json){
                        //callback function uses the json data from the ajax to update
                        //the group count and insert the message.
                        //any element on the page can be updated this way, as needed. 
                        $(dropEl).find("span.count").text("("+json.curGroupCount+")");
                        $("#messages").html("<p>"+json.message+"</p>").show();
                        
                    });

                    

                    
                }
            });
            
            //just a line to activate already-checked rows, if there are any
            $("#mySiteList input:checkbox:checked").each( function(){ activateRow(this); });
        
        
        });
     
     
        
     };
     
     
     $.fn.wpBatchAdd = function() {
        
        return this.each(function() {
            
            var container = $(this);
            //drop arrow toggling
            $("#batchList .info .dropArrow").click(function(){ 
                $(this).parent().parent().toggleClass("expanded"); 
                return false; 
            });
            
            //activations for rows
            container.find("#domains a.selectAll").click(function(){ $("#domainList li input:checkbox").each(function(){ this.checked="checked"; activateRow(this); }); return false;});
            $("#domainList li label").click(function(){ 
                var inEl = $(this).find("input:checkbox")[0];
                inEl.checked==false?deactivateRow(inEl):activateRow(inEl);  
            });
            
                    
            
            
            
            var activateRow = function(inputEl){
                var row = $(inputEl).parent().parent();
                $(row).addClass("active");
            };
            var deactivateRow = function(inputEl){
                
                var row = $(inputEl).parent().parent();
                $(row).removeClass("active");
            
            };

        
        
        });
    }
    
    $.fn.wpHelp = function() {
        
        return this.each(function() {
            
            $(this).click(function(){
            
                $.get($(this).attr("href"),{},function(data){
                    $(data).modal();
                },"html");
                    
                
            
            
                return false;
            });

        
        
        });
    }
    
    $.fn.wpSortableTable = function(userOptions){
    
        return this.each(function() {
            var options = {};
            options = $.extend({}, userOptions, options);
            $(this).find("td").each(function(){
                $(this).css({width:$(this).width()+"px"});
            });
            var tbody = $(this);
            $(this).sortable({
                axis:"y",
                forceHelperSize:true,
                forcePlaceholderSize:true,
                placeholder:"placeHolder",
                helper:function(a,b,c){
                    var orig = b;
                    var help = b.clone();
                    help.children().each(function(i){
                        $(this).width(orig.children().eq(i).width());
                    });
                    return help;
                    
                },
                
                update:function(e,ui){
                    
                    $(this).find("tr:even").removeClass("alt");
                    $(this).find("tr:odd").addClass("alt");
                    var data = tbody.sortable("serialize");
                    for(param in options.postParams){
                        data = data + "&"+param+"="+options.postParams[param];
                    }
                    $.ajax({
                        type:"POST",
                        dataType: "json",
                        url: options.location,
                        data: data,
                        success: function(o){
                            $(".message").html(o.message);
                        },
                        error: function(o){
                            $(".message").html("There was an error: "+o.statusText);
                        }
                    });
                    
                    
                }
            
            
            
            });
            
            
            
        
        });
    
    
    };
    

    $.fn.wpSiteInfo = function(){
    
        return this.each(function(){
            var theLink = $(this);
            theLink.click(function(){
                theLink.addClass("loading");
                $(".siteInfoOverlay").remove();
                $.get(theLink.attr("href"), function(data){
                    var t = $('<div class="w"><a href="#" class="close">close</a><span class="nubbin"></span></div>').append(data);
                    $('<div class="siteInfoOverlay"></div>')
                        .append(t).css({top:(theLink.offset().top-200)+"px", left:(theLink.offset().left+19)+"px"})
                        .appendTo("body")
                        .find("a.close").click(function(){
                            $(this).parents(".siteInfoOverlay").remove();
                            theLink.removeClass("infoActive");
                            $(".siteInfoBackdrop").remove();
                            return false;
                        });
                    theLink.removeClass("loading").addClass("infoActive");
                    $('<div class="siteInfoBackdrop"></div>').appendTo("body");
                
                },"html");
                
                
                return false;
            });
        
        
        });
    };

    
    $(function(){
    
        $("a.checkArticles").live("click",function(e){
            e.preventDefault();
            $.post($(this).attr("href"),{keywords:escape($("#keywords").val())},function(data){
                $(data).modal();
            },"html");
            
        
        });
        

        $(".datefieldinput").datepicker();
    
        $(".gOptions").each(function(){
            var opt = $(this);
            $(this).parent().hover(function(){ opt.show(); },function(){opt.hide();});
            
            $(this).find("a").wpHelp();
                
        });
        
        
        $(".siteInfo").wpSiteInfo();
    
    });
    
})(jQuery);
