(function ($) {
    $.fn.vAlign = function () {
        return this.each(function (i) {
            $(this).children().wrapAll('<div class="vertAlign" style="position:relative;"></div>');
            var div = $(this).children('div.vertAlign');
            var ph = $(this).innerHeight();
            var dh = div.height();
            var mh = (ph - dh) / 2;
            div.css('top', mh);
        });
    };
})(jQuery);

var CampQuality = new function () {

    var self = this;

    this.insertBullets = function () {
        // Replaces all lists and links with coloured bubbles
        var bullets = new Array("bubbleBlue", "bubbleGreen", "bubbleOrange", "bubblePink", "bubblePurple", "bubbleRed");
        $(".contentItem ul li,.sectionItem a,.searchItem a").each(function (i) {
            $(this).addClass(bullets[i % bullets.length]);
        });
    };

    this.replaceConnectors = function () {
        // Replaces last content bubble with no connector
        $(".contentBubble:last .contentBubbleBtm").addClass("noConnector");
        $(".contentBubble:first .contentBubbleTop").addClass("withConnector");
    };

    this.initRegionSelector = function () {
        $(".regionSelection .region-selector").mouseover(function () {
            $(".regionSelection UL").show();
        });
        $(".regionSelection .region-selector").mouseout(function () {
            $(".regionSelection UL").hide();
        });
    };

    this.HeroFlashOn = function () {
        var id = $("#mainHeroContainer").children().attr("id");
        self.GetFlashId().soundOn();
    };

    this.HeroFlashOff = function () {
        var id = $("#mainHeroContainer").children().attr("id");
        self.GetFlashId().soundOff();
    };

    this.GetFlashId = function () {
        var movieName = $("#flashId").attr("value") + "_swf";
        var isIE6 = navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.indexOf("MSIE 6") != -1 || navigator.appVersion.indexOf("MSIE 5") != -1);
        return (isIE6) ? window[movieName] : document[movieName];
    };

    this.initENews = function () {
        $("#sideBar .eNews").fancybox({ 'transitionIn': 'none', 'transitionOut': 'none', 'autoScale': false, 'type': 'iframe', 'width': 530, 'height': 375, 'scrolling': 'no' });
    };

    this.createDiagram = function() {
        $('.diagramContainer A').click(function() {
            $('.diagramContainer .layer').hide();
            $('.diagramContainer #' + $(this).attr('rel')).show();
            return false;
        });
        $('.diagramContainer .content').vAlign();
    };

};

$(document).ready(function () {
    CampQuality.insertBullets();
    CampQuality.replaceConnectors();
    CampQuality.initENews();
    CampQuality.initRegionSelector();
});
