/* 
 * Front-end JQuery
 */

    /* -------------------------------------------------------------------------
     * SITE functions
     * -----------------------------------------------------------------------*/
     //tabs

     $(document).ready(function() {
         //---------------------------------------------------------------------
        //tabs
        $(function() {
            $(".tabs").tabs({ fx: { opacity: 'toggle' } });
        });

        //---------------------------------------------------------------------
        //	AJAX functionality
        $.ajaxSetup ({
                cache: false,
                dataType: "html"
            });

        var ajax_load = "<p>Loading...</p>";
        //---------------------------------------------------------------------
        //accordions TODO: use the first one with the following markup?

        $(".top-drop").livequery('click',function(){
            $(this).next().slideToggle('fast');
        });

        $(".bottom").livequery('click',function(){
            /* We need to check if there is a subsection list among the siblings.
             * If so, we need to have the Ajax fire.
             * If there are any subsections then show the list; if not, don't.*/
            var dl_child = $(this).prev("dl"); //subsection list
            
            if( $(dl_child).size() > 0 ){
                
                //we have a subsection list
                var dt_child = $(dl_child).find(".load_children");//the dt element with the ajax information
                var link =   $(dt_child).attr("id")+"?remote=1 #meta .children";//ajax link
                var dd_child = $(dt_child).next(".load_target")//the dd which will carry the ajax

               $(dt_child).removeClass("load_children");//this will stop the ajax functionality running more than once
               
               
                //load the ajax into the dd
                $(dd_child).load(link, function(responseText, textStatus, XMLHttpRequest){
                    if (textStatus == "success") {
                         // successful AJAX call
                         if($(dd_child).contents().size() == 0){
                         //it didn't load any subsections, so hide the dl from the user
                            $(dl_child).remove();
                        }else{
                            //it did
                            $(dl_child).removeClass("accessibility")
                            //$(dl_child).show();
                            //$(dt_child).addClass('top').addClass('open');
                        }
                    }
                    
                });
                //test to see if ajax loaded anything
                
                
            }

            //toggle all siblings with a class of '.hide'
            $(this).siblings('.hide').slideToggle('fast');
            $(this).toggleClass('open');
        });


        //presets

        //hide everything marked .closed        
        if ($('.closed').children().size() > 0) {
            $('.closed').children().not('h2,h3,h4,h5,h6').hide();
        }else{
            $('.closed').next().hide();
        }

        //just .top defaults to closed
        if ($(".top:not(.open,closed)").children().size() > 0) {
            $(".top:not(.open,closed)").children().not('h2,h3,h4,h5,h6').hide();
        }else{
            $(".top:not(.open,closed)").next().hide();
        }
        

        $(".top").livequery('click',function(){
                if ($(this).children().size() > 0) {
                    $(this).children().not('h2,h3,h4,h5,h6').slideToggle('fast');
                    $(this).removeClass("closed").toggleClass('open');
                } else {
                    $(this).next().slideToggle('fast');
                    $(this).removeClass("closed").toggleClass('open');
                }
        });
        
        //---------------------------------------------------------------------
        //floating #meta
//        var name = "#meta";
//        var menuYloc = null;
//
//        menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")))
//        $(window).scroll(function () {
//            var offset = menuYloc+$(document).scrollTop()+"px";
//            $(name).animate({top:offset},{duration:0,queue:false});
//        });

        if ($(".poll-results").length > 0 ) {
            animateResults();
        }

        function animateResults(){
        $(".poll-results div").each(function(){
            var width = $(this).width();
            animateWidth(this,width,"slow");
            });
        }

        //---------------------------------------------------------------------
        function animateWidth(obj,width,speed){
            $(obj).css({width: "0%"}).animate({ width: width}, speed);
        }
        //---------------------------------------------------------------------
         

        //---------------------------------------------------------------------
        });
    