﻿
// establish a sIFR replacement object
var sIFRFont = {
  src: '/images/univers.swf'
}

sIFR.activate(sIFRFont);

// ektron carryovers for search
function parseQueryString() {
    var qs = window.location.search.substring(1).split("&");
	var aParams = new Array();
	
	for(var i=0; i<qs.length; i++) {
		parts = qs[i].split("=");
		aParams[parts[0]] = parts[1];
	}
	
	return aParams;
}

// JScript File
$(document).ready(
    function() {

        // attach event for executing search via enter key within text field
        $('#courtesy-nav input.search').bind('keypress',
            function(event) {
                checkKeycode(event);
            }
        );

        // attach event for executing search via click
        $('#ctl00_btnSearch').bind('click',
            function() {
                CallSearch();
                return false;
            }
         );

        // the 'music' element only occurs on the homepage - if it exists, run swf replacements
        if ($('#music').size() > 0) {
            swfobject.embedSWF('music.swf', 'music', '185', '226', '9.0.0');
            swfobject.embedSWF('video.swf', 'video', '185', '226', '9.0.0');
            swfobject.embedSWF('messaging.swf', 'messaging', '185', '226', '9.0.0');
        }

        // attach mouse events for rollover images
        $('img.rollover, .rollover img').not('.active').each(
	        function() {
	            // quick ref to image
	            var _this = $(this);

	            // preload each image
	            var src = _this.attr('src');
	            var ext = src.match(/.[\w]+$/);
	            var newSrc = src.replace(ext, '_on' + ext);
	            var img = new Image();
	            img.src = newSrc;

	            // attach mouse over/out events to image
	            _this.mouseover(
		            function() {
		                // replace .{ext} with _on.{ext}
		                var _this = $(this);
		                var src = _this.attr('src');
		                var ext = src.match(/.[\w]+$/);
		                var newSrc = src.replace(ext, '_on' + ext);
		                _this.attr('src', newSrc);
		            }
	             ).mouseout(
		            function() {
		                // replace ._on{ext} with .{ext}
		                var _this = $(this);
		                var src = _this.attr('src');
		                var ext = src.match(/.[\w]+$/);
		                var newSrc = src.replace('_on' + ext, ext);
		                _this.attr('src', newSrc);
		            }
	            );
	        }
	     );

        // START: TABS
        // if this a tabs content page, attach click events to the anchors
        // and initialize the display
        if ($('#tabs').size() > 0) {

            // give each PANEL div an id and then hide it
            $('#tabs div.panel').each(
			    function(index) {
			        $(this).hide().attr({ id: 'panel_' + index });
			    }
		    );

            var qTabs = $('#tabs-list a');
            qTabs.each(
			    function(index) {
			        $(this).attr({ id: 'tab_' + index });
			        if (qTabs.size() == (index + 1)) {
			            $(this).addClass('last');
			        }
			    }
		    );


            // now show the default tab and store a reference to it
            // assume the default is 0
            var defaultIndex = 0;

            // look for a 'tabIndex' parameter in the query string
            var myParams = parseQueryString();

            if (myParams['tabIndex']) {
                defaultIndex = new Number(myParams['tabIndex']) - 1;
            }

            var qPanel = $('#panel_' + defaultIndex);
            var qTab = $($('#tabs a').get(defaultIndex));

            qPanel.show();
            qTab.addClass('active');

            // create global references to these elements for showTab
            window.activePanel = qPanel;
            window.activeTab = qTab;

            $('#tabs-list a').click(
                function() {
                    showTab($(this).attr('id').replace('tab_', ''));
                    return false;
                }
		    );
        }

        window.showTab = function(index) {
            // hide previously active tabs
            window.activePanel.hide();
            window.activeTab.removeClass('active');

            // store references to this tab
            window.activePanel = $('#panel_' + index);
            window.activeTab = $('#tab_' + index);

            // show new tab
            window.activeTab.addClass('active');
            window.activePanel.show();
            return false;
        }
        // END: TABS


        // move press release data to target element
        if ($('#press-release-data').children().size() > 0) {
            var re_fixTitle = /^\d{4}\sPress\sRelease(s)?\s/;
            movePressData('press-release-data', 'feature-1 .body', '', re_fixTitle);
        }

        // move media alerts data to target element
        if ($('#media-alerts-data').children().size() > 0) {
            var re_fixTitle = /^\d{4}\sMedia\sAlert(s)?\s/;
            movePressData('media-alerts-data', 'feature-3', 'Media Alerts', re_fixTitle);
        }

        if ($('#press-release-data-home').children().size() > 0) {
            $($('.feature').get(2)).attr('id', 'feature-3');
            var re_fixTitle = /^\d{4}\sPress\sRelease(s)?\s/;
            movePressData('press-release-data-home', 'press', '', re_fixTitle);
        }

        if ($('#coverage-data-home').children().size() > 0) {
            var re_fixTitle = /^\d{4}\sPress\sRelease(s)?\s/;
            movePressData('coverage-data-home', 'coverage', '', re_fixTitle);
        }

        sIFR.replace(sIFRFont, {
            selector: '#intro h1',
            tuneHeight: '0px',
            css: [
				'.sIFR-root { color:#ffffff; margin: 0; padding: 0; }'
			],
            wmode: 'transparent'
        });

        sIFR.replace(sIFRFont, {
            selector: '#local-nav h1',
            css: [
				'.sIFR-root { font-size:20px; color:#666666; margin: 0; padding-left: 10px;}',
				'.sIFR-root #local-nav h1 a {text-decoration: none; color: #333333; }',
				'.sIFR-root #local-nav h1 a:hover {color: #333333; }'
			],
            wmode: 'transparent'
        });

        sIFR.replace(sIFRFont, {
            selector: '.drop-shadow .body h2 span',
            css: [
				'.sIFR-root { font-size:18px; color:#666666; }'
			],
            wmode: 'transparent'
        });

        // prees release content pages aren't represented in the Ektron menu system so
        // those content items  don't show up as active in the generated menu.
        // to address this without requiring Real to add each new PR item to the menu

    }
);




function movePressData(dataSource, destination, header, re_fixTitle) {
    // remove "200{n} Press Release(s)"
    var qSource = $('#' + dataSource);
    
    $('li a p', qSource).each(
        function() {
            var qThis = $(this);
            qThis.text( qThis.text().replace(re_fixTitle, '') );
        }
    );
    
    //10:54:21 PM
    var re_removeTimestamp = /\d{1,2}:\d{2}:\d{2}\s{1}[A|P]M/;
    $('li span.q-date', qSource).each(
        function() {
            var qThis = $(this);
            qThis.text( qThis.text().replace(re_removeTimestamp, '') );
        }
    );

    var prData = qSource.html();
    var prContainer = $('#' + destination);
    if( prContainer.size() == 0 ) {
        prContainer = $('.middle-column');
    }
	var elHeader = "";
	if( header != "" ) {
		elHeader = "<h2>" + header + "</h2>";
	}
    prContainer.html(elHeader + prData);
}




// force IE to look for cached background images
try { 
    document.execCommand("BackgroundImageCache", false, true); 
} catch(e) {} 


function checkKeycode(e) {
    var keycode = (window.event) ? e.keyCode : e.which;
    if (keycode==13) {
        CallSearch();
    }
}

function CallSearch(event) {
	if(!event) event = window.event;
	event.returnValue = false;
	event.cancel = true;
    var objSearchText = $('#courtesy-nav input.search');
    var jSubmit = $('#courtesy-nav :image');
	
	var searchText = objSearchText.val().replace(/>/gi, " >").replace(/</gi, "< ");
		searchText = searchText.replace('alert','').replace('confirm','');
    objSearchText.val( searchText );
	
    //This cookie is used for the backbutton to work in search on postback
    //This cookie must be cleared to prevent old search results from displayed
    document.cookie='postbackcookie=';
	
	var searchUrl = "/search.aspx?searchtext=" + searchText;// + "&folderid=0&searchfor=all&orderby=id&orderdirection=ascending"; 
    //var jForm = $('#aspnetForm');
	//jForm.attr('action', searchUrl);//.submit();
	window.location.href = searchUrl;
	/*
	var oForm = document.forms[0];
	oForm.action = searchUrl;
	jSubmit.focus();
	jSubmit.click();
	//oForm.submit();
	*/
	return false;
}
