/*
	---------------------
	** REQUIRES jQuery **
	---------------------
	
	-------------------------
	** REQUIRES flowplayer **
	-------------------------
	
	includes scripts to:
		+ add functionality to allow text boxes a default value that will be cleared on focus (ie. 'Search...')
		+ equalize the heights of elements matching the given selector rules

*/

var overridePresence = true;
/**
 *  This little script here will hijack the 'Presence' functions of Sharepoint to prevent
 *  public internet users from being prompted to install an ActiveX control if 
 *  they do not have name.dll on their machine...
 *
 **/
(function(bool) {
    if (bool) {
        window.ProcessImn = window.ProcessImnMarkers = window.EnsureIMNControl = function() { return false; };
    }
})(overridePresence);

/* End Presence Hijack */

var iomer = {
    searchDefault: 'Search...', // default value for search boxes if initialized

    equalizePanels: function(selector) {
        if (selector === '') return;

        var maxHeight = 0;
        $(selector).each(function() {
            var panelHeight = $(this).height();
            maxHeight = panelHeight > maxHeight ? panelHeight : maxHeight;
        });
        $(selector).height(maxHeight);
    },
    /*
    a very special case where i don't want it to resize the 
    elements if the largest is the one with class 'la_contentWrap'
    */
    equalizePanelsSpecial: function(selector) {
        if (selector === '') return;
        var maxHeight = 0;
        var resize = false;
        $(selector).each(function() {
            var panelHeight = $(this).height();
            if (panelHeight > maxHeight) {
                if ($(this).hasClass('la_contentWrap')) {
                    resize = false;
                } else {
                    resize = true;
                }
                maxHeight = panelHeight;
            }
        });
        if (resize) $(selector).height(maxHeight);
    },

    initSearchText: function(selector, defValue) {
        // example selectors to pass in: .searchText, .sf_searchText
        if (selector === '') return;
        defValue = defValue || iomer.searchDefault;

        $(selector).focus(function() {
            $(this).attr({ value: '' });
        })
    .blur(function() {
        if ($(this).attr('value') == '') {
            $(this).attr({ value: defValue });
        }
    })
    .attr({ value: defValue });
    },
    /*
	
		Functionality for the Lookup Results display.
    - expanding / contracting panels
	
	*/
    panelizeResults: function(selector) {
        // for each expandable li item...
        $(selector)
		.each(function(i) {
		    var $this = $(this);
		    // attach a click event to the anchor heading with in
		    $this.children('.rl_heading')
			.click(function(e) {
			    // stop the click event from happening
			    e.stopPropagation();
			    // toggle the visibility of the slide and change the expanded class
			    $this.children('.rl_slide')
				.each(function(i) {
				    var slide = $(this);
				    if (slide.is(":visible")) {
				        $this.removeClass('rl_expanded');
				        slide.slideUp(500);
				    } else {
				        slide.slideDown(500);
				        $this.addClass('rl_expanded');
				    }
				});
			});
		});
        return $(selector);
    },
    /*
	
		Used in conjunction with Flowplayer to create an overlayed FLV player, that is controlled
    by a custom webpart.
		
	*/
    initFlvPlayer: function(swfURL) {
        swfURL = swfURL || '';

        // create the overlay div and the flv player wrap
        $('body').append("<div class='overlay' id='overlay'><div id='flv_player'></div></div>");
        /* setup the flash flowplayer */
        $f('flv_player', swfURL + 'flowplayer.commercial-3.0.7.swf', {
            key: '$8358363807318e31f54',
            // logo
            logo: {
                url: '/_layouts/styles/iomer.SP.LAA.Internet/images/LAA/LAA_logo.png',
                fullscreenOnly: false,
                displayTime: 5
            },
            // context menu
            contextMenu: ['Legal Aid Alberta'],
            // plugins
            plugins: {
                // the default controlbar is called "controls". by tweaking this, you can modify the default controlbar 
                controls: {
                    // location of the plugin 
                    url: swfURL + 'flowplayer.controls-3.0.4.swf',
                    // display properties such as size, location and opacity 
                    opacity: 0.8,
                    // controlbar specific settings 
                    timeColor: '#eeeeee',
                    durationColor: '#aaaaaa',
                    all: false,
                    play: true,
                    volume: true,
                    scrubber: true,
                    fullscreen: true,
                    time: true,
                    autoHide: 'always'
                }
            }
        });
        // setup the overlay, and make sure the click event is not sent beyond that. */
        $('a.flowOpen[rel]')
		.overlay({
		    onLoad: function() { },
		    onClose: function() { $f('flv_player').unload(); }
		})
		.click(function(e) {
		    /* performing this here instead of onLoad to capture the href */
		    var fp = $f('flv_player');
		    fp.play({ url: this.href, autoPlay: false });
		    /* cancel propagation */
		    e.stopPropagation()
		});
    }
};