var W3CDOM = (document.createElement && document.getElementsByTagName);
var current_banner_position = 9999;
var banners_rotating = false;

function open_window_html(url, width, height, scroll, resize) {
    popup = window.open(url, "FADE_popup", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=" + scroll + ",resizable=" + resize + ",width=" + width + ",height=" + height);
    popup.focus();
};



function toggleDiv(a_obj, div_id, description)
{
    if (document.getElementById) {
        div_obj = document.getElementById(div_id);
        
        var div_class = div_obj.className;
        
        if (div_class.match("hide")) {
            div_class = div_class.replace(/hide/,"");
            div_obj.className = div_class + " show";
            a_obj.innerHTML = "[Hide " + description + "]";
        } else {
            div_class = div_class.replace(/show/,"");
            div_obj.className = div_class + " hide";
            a_obj.innerHTML = "[Show " + description + "]";
        }
    }
}


function switchDivs(div_to_show, div_to_hide)
{
    if (document.getElementById) {
        div_to_show = document.getElementById(div_to_show);
        div_to_hide = document.getElementById(div_to_hide);
        div_to_show.style.display = "";
        div_to_hide.style.display = "none";
    }
}

$(document).ready(function(){
    //image-rollover

    //preload images
    jQuery.preloadImages = function() {
        for(var i = 0; i<arguments.length; i++) {
        jQuery("<img>, <input>").attr("src", arguments[i]);
        }
    }
    
    
    $("img[src*='rollover'], input[src*='rollover']")
    
        .load(
            function(){
                var my_src = this.getAttribute('src');
                var extension = my_src.substr((my_src.length)-3);
                
                regex = new RegExp('-on.' + extension,"gi");
                my_src = my_src.replace(regex,"");
                
                regex = new RegExp('.' + extension,"gi");
                my_src = my_src.replace(regex,"");
                
                $.preloadImages("/images/"+my_src+"-on."+extension);
            }
        )
        
        //do the hover
        .hover(
            function(){
                var my_src = this.getAttribute('src');
                var extension = my_src.substr((my_src.length)-3);
                
                regex = new RegExp('-on.' + extension,"gi");
                my_src = my_src.replace(regex,"");
                
                regex = new RegExp('.' + extension,"gi");
                my_src = my_src.replace(regex,"");
                
                this.setAttribute("src",my_src+"-on."+extension);
                
            },
            function(){
                var my_src = this.getAttribute('src');
                var extension = my_src.substr((my_src.length)-3);
                
                regex = new RegExp('-on.' + extension,"gi");
                my_src = my_src.replace(regex,"");
                
                regex = new RegExp('.' + extension,"gi");
                my_src = my_src.replace(regex,"");
                
                this.setAttribute("src",my_src+"."+extension);
            
            }  
        );
    
    //preventing errors on the CMS where the superfish script is not included
    try {        
        $("ul.sf-menu").superfish({
            speed: 1
        });
    } catch(e) {}

    /*------------------------Newsletter archive code -- */
    // Newsletter archive show hide
    $(".archive_expander").addClass("hidden").next().hide();
    
    // This needed to open on the current year
    $(".archive_expander").toggle(
        function(){
            if(!$(this).hasClass("showing")) {
                $(this).addClass("showing").removeClass("hidden").next().show();
            } else {
                $(this).addClass("hidden").removeClass("showing").next().hide();
            }
        },
        function(){
            if(!$(this).hasClass("hidden")) {
                $(this).addClass("hidden").removeClass("showing").next().hide();
            } else {
                $(this).addClass("showing").removeClass("hidden").next().show();
            }
        }
    );
    
    // Reopen the archive if it is the current years archive
    var d = new Date();
    var curr_year = d.getFullYear();
    $(".archive_expander").each(function() {
        if($(this).attr("id") == curr_year) {
            $(this).addClass("showing").removeClass("hidden").next().show();
        }
    });
    /*-----------------end of archive code--------------------------------*/

    // Rotate event banners every 8 seconds - only fires once if there are no current event banners
    bannerInterval = setInterval("rotateBanner('1')", 8000);
    
    //sticky footer heights     
    $("#footer").css("height", "auto");
    var footer_height = $("#footer").height() + 2;
    $("#footer").css("margin-top", "-" + footer_height + "px");
    $("#content_wrapper").css("padding-bottom", footer_height + "px");

});

//background-image height
jQuery.bgDisplay = function(){
        
    var body_width = $("body").width();
    $("img.bg_fade_top, img.bg_fade_bottom").css("width", body_width);
    var body_height = $("#container").height();
    
    if(body_height > 1306){
        var butterfly_position = (body_height - 411) + "px";
        var bg_fade_bottom_position = (body_height - 781) + "px";
        $(".butterflies_bottom").css("top", butterfly_position).css("display", "block");
        $("img.bg_fade_bottom").css("top", bg_fade_bottom_position).css("display", "block");
    } else {
        $(".butterflies_bottom").css("display", "none");
        $("img.bg_fade_bottom").css("display", "none");
    }
}

$(window).resize(function(){
    $.bgDisplay();
});

$(window).load(function(){
    $.bgDisplay();
});
    

function calendarCall(date, community) {
    $.ajax({
        type: "GET",
        url: "/calendar-ajax/",
        data: "calendar=" + date + "&community=" + community,
        success: function(result){
            $("#calendar-holder").html(result);
        }
      });
    

    return false;
}


function rotateBanner(next_or_previous, cancel_interval) {
    
    var element = 0;
    
    // Get all banners    
    var all_banners = $("#event_calendar_images").children();
    var number_banners = all_banners.length - 1;
    
    if(number_banners > 0) {    
        
        if(!banners_rotating) {
            
            banners_rotating = true;  
            
            all_banners.each(function(i) {
                if($(this).hasClass("right_col_top_banner")) {
                    current_banner_position = i;
                }
            });
               
            if(current_banner_position != 9999) {
                all_banners.eq(current_banner_position).fadeOut("slow", function() {
                    if(next_or_previous) {
                        if((current_banner_position + 1) <= number_banners) {
                            element = current_banner_position + 1;                       
                        } else {
                            element = 0;
                        }                   
                    } else {
                        if((current_banner_position -1) >= 0) {
                            element = current_banner_position - 1;
                        } else {
                            element = number_banners;
                        }   
                    }   
                    
                    // Methods need to happen in this order otherwise the fade gets screwed
                    all_banners.eq(element).addClass("right_col_top_banner").hide().fadeIn("slow").removeClass("hide");
                    
                    $(this).removeClass("right_col_top_banner");
                    
                    // Reallow the user to click again
                    banners_rotating = false;                    
                });
                
                // If the user has clicked the previous or next buttons we'll clear the interval and restart it
                if(cancel_interval) {
                    // Clear the interval and then restart it after 8 seconds
                    clearInterval(bannerInterval);  
                    bannerInterval = setInterval("rotateBanner('1')", 8000);
                }
            } else {       
                clearInterval(bannerInterval);
            }
        }
    } else {
        clearInterval(bannerInterval);                
    }

}


function openVideo(guid) {
    var flashvars = {
      videoGuid: guid
    };
    var params = {
      menu: "false"
    };
    var attributes = {
      wmode: "opaque"
    };
    
    swfobject.embedSWF("/documents/video_player/video_player.swf", "flash-video-holder", "415", "390", "9.0.0", "#CCCCCC", flashvars, params, attributes);
    
    return false;
    
}

function openSteppingAhead() {
    var flashvars = {
    };
    var params = {
      menu: "false"
    };
    var attributes = {
      wmode: "window",
      id: "Captivate"
    };
    
    swfobject.embedSWF("/documents/stepping_ahead/preloader.swf", "stepping-ahead-holder", "850", "337", "10.0.0", "#CCCCCC", flashvars, params, attributes);
    
    return false;
    
}

