// cpsa.js
/*
includes scripts to:
+ operate main navigation
+ equalize size of panelBody's for main content on homepage.
*/
var CPSA_utils = {
    addMenuDynamics: function(selector) {
        $(selector).each(function() {
            var $this = $(this);
            var $dropdown = $this.find('.RadMenu_CPSA_Dropdown .rmRootGroup');

            $("<span class=\"fhover\"></span>").css("display", "none").prependTo($this);
            // Safari dislikes hide() for some reason
            $this
		    .bind("mouseenter", function() {
		        $(".fhover", $this).fadeIn("fast");
		        $dropdown.fadeIn('fast');

		        // add second level of flyout
		        $('.rmItem', $dropdown)
		        .filter(function() {
		            return $(this).children('.rmSlide').length != 0;
		        }).bind('mouseenter', function() {
		            $(this).find('.rmSlide .rmGroup').fadeIn('fast');
		        }).bind('mouseleave', function() {
		            $(this).find('.rmSlide .rmGroup').fadeOut('fast');
		        });
		    })
		    .bind("mouseleave", function() {
		        $(".fhover", $this).fadeOut("fast");
		        $dropdown.fadeOut('fast');
		    });
            // add class to items that can expand
            $('.rmItem', $this)
            .filter(function() {
                return $(this).children('.rmSlide').length != 0;
            })
            .children('a')
            .addClass('CPSA_expandable');
        });
    },

    equalizePanels: function(selector) {
        //	This little bit of script just make the 'recent announcements' and 'contact us' panels
        //	the same height since we can't quite do it with css for undefined heights.
        var maxHeight = 0;
        $(selector).each(function() {
            var panelHeight = $(this).height();
            maxHeight = panelHeight > maxHeight ? panelHeight : maxHeight;
        }).height(maxHeight);
        // End same sizing
    },

    searchTextInit: function() {
        $('.sf_searchText').focus(function() {
            $(this).attr({ value: '' });
        })
		.blur(function() {
		    if ($(this).attr('value') == '') {
		        $(this).attr({ value: 'Search...' });
		    }
		})
		.attr({ value: 'Search...' });
        /*
        $('.ps_searchText').focus(function() {
        $(this).attr({ value: '' });
        })
        .blur(function() {
        if ($(this).attr('value') == '') {
        $(this).attr({ value: "Find a physician..." });
        }
        })
        .attr({ value: "Find a physician..." });
        */
    },
    newWindowAnchorAdjust: function() {
        $('a[href^=http]').click(function(e) {
            var href = $(this).attr('href').toLowerCase();
            if (href.search(document.domain.toLowerCase()) < 0) {
                window.open($(this).attr('href'));
                e.preventDefault();
            }
        });

        $('a[href$=.ashx]').click(function(e) {
            window.open($(this).attr('href'));
            e.preventDefault();
        });
    }
};

CPSA_utils.rotatingImage = {
    intervalIndex: -1,
    initDone: false,
    libPath: '/Libraries/Homepage_Headers/',
    filenameRoot: 'homeImageStrip',
    ext: '.sflb.ashx',
    currentIndex: 0,
    numImages: 5,


    init: function() {
        this.intervalIndex = setInterval('CPSA_utils.rotatingImage.rotate()', 10000);
        this._buildUrl = function(index) {
            index = index || '1';
            return this.libPath + this.filenameRoot + index + this.ext;
        };
        /* preload the first image and fade it in */
        var bgRef = this._buildUrl(this.currentIndex + 1);
        var imgPreloader = new Image();
        imgPreloader.onload = function() { $('#rotatingPictureFrame').css({ display: 'none', backgroundImage: 'url(' + bgRef + ')' }).fadeIn('slow'); };
        imgPreloader.src = bgRef;
        /* add the holder div for the image transition feature */
        $("<div class=\"newImage\"></div>").css('display', 'none').prependTo($('#rotatingPictureFrame'));
        this.initDone = true;
    },
    rotate: function() {
        // choose image from set
        var selector = '#rotatingPictureFrame';

        if (!this.initDone) return;

        this.currentIndex = (this.currentIndex + 1) % this.numImages;
        // preload and fade in image
        var bgRef = this._buildUrl(this.currentIndex + 1);
        var imgPreloader = new Image();
        imgPreloader.onload = function() { $(selector + " .newImage").css({ backgroundImage: 'url(' + bgRef + ')' }).fadeIn("slow", CPSA_utils.rotatingImage.swap); };
        imgPreloader.src = bgRef;
    },
    swap: function() {
        $('#rotatingPictureFrame').css('backgroundImage', $('#rotatingPictureFrame .newImage').css('background-image'));
        $('#rotatingPictureFrame .newImage').css('display', 'none');
    }
};

CPSA_utils.flyoutMenu = {
    init: function(selector) {
        $(selector + ' > a').click(function(event) {
            event.preventDefault();
            $('.flyout', selector)
			.bind("mouseleave", function() {
			    $(this).fadeOut("fast");
			})
			.fadeIn("fast");
        });
    }
};


//Toggles to a new tab and sets the tab title style
function ShowTab(linkId, dataId)
{
    //show the associated tab and hide the others
    $('.tab-content').hide();
    $('#' + dataId).show();

    //set the selected tab class
    $('.physician-tabby > li').removeClass('tab-selected');
    $('#' + linkId).addClass('tab-selected');
}

//Ensures that the google map is prepared and centered correctly.  
//This is called to ensure the map is only prepared when it is shown, otherwise it has issues with layout.
var isMapPrepared = false;
function PrepareMap()
{
    if(!isMapPrepared)
    {
        //These js methods are genereated and registered in the code behind.
        RegisterGoogleMapsStartup();
        RegisterGoogleMarker();
        isMapPrepared = true;    
    }
}
