/** $Id: script.js 173 2008-01-18 22:32:34Z jaimonetti $ **/

function fixHover(parent, hoverClassName) {
    if ( !parent ) return;

    $(parent).immediateDescendants().each( function (kid) {
        Event.observe(kid, 'mouseover', function(e) { $(kid).addClassName(hoverClassName); });
        Event.observe(kid, 'mouseout', function(e) { $(kid).removeClassName(hoverClassName); });
    });
}

function submitSearch(event) {
    if ( event.type == 'submit' && event.charCode != Event.KEY_RETURN ) return false;

    if ( $('query') && $('query').value.length > 0 ) {
	var params = "site:" + window.location.host + " " + $('query').value;
	window.location = 'http://google.com/search?q=' + params;
    }

    Event.stop(event);

    return false;
}

// when a bb is moused over, update the bread crumbs to have a link to the bb project
function updateBreadCrumbs(bb) {
    // removed as of 1/2/2008
    return;
}

function transitionBB(event, el) {
    var bb = el || Event.element(event);

    while ( ! $(bb).hasClassName('billboard') ) {
	bb = $(bb.parentNode);
    }

    if ( bb.hasClassName('loaded') ) return;

    if ( $('billboards').currBB ) {
	$('billboards').currBB.removeClassName('loaded');
	$('billboards').currBB.removeClassName('bbHover');
    }

    $('billboards').currBB = $(bb);
    bb.addClassName('loaded');
    bb.addClassName('bbHover');

    var finish = function() {
	$('coContent').innerHTML = 'Loading...';

	if ( bb.coContent ) {
	    $('coContent').update(bb.coContent);
	    new Effect.Appear('coContent');
	}
	else {
	    new Ajax.Updater($('coContent'), '/clients.php', {
		parameters: {
		    id: bb.id
		},
		onComplete: function() {
		    bb.coContent = $('coContent').innerHTML;
		    var ccl = $(document.getElementsByClassName("clientcaselogo")[0]);

		    new Effect.Appear('coContent', {duration: .5});

		    var href = ccl.down().href;
		    if ( href ) {
			$(bb).gotoSite = function() {
			    window.location = href;
			};
		    }
		    else {
			$(bb).gotoSite = null;
		    }
		}
	    });
	}

	updateBreadCrumbs(bb);
    }

    new Effect.Fade('coContent', {
	afterFinish: finish,
	duration: .5
    });

    if ( event ) Event.stop(event);
}

function initPage() {
    fixHover($('globalLink'), 'over');
    fixHover($('wedone_right'), 'over');

    if ( $('searchIt') ) {
        $('searchIt').observe('click', submitSearch);
        $('mainSearchDiv').observe('keypress', submitSearch);
    }

    if ( $('billboards') ) {
	$('billboards').immediateDescendants().each(function(bb) {
	    bb.observe('mouseover', transitionBB);
	});

	$$('#billboards a').invoke('observe', 'click', function(ev) { Event.stop(event); return false; });

	transitionBB(null, $('wb'));
    }
}

Event.observe(window, 'load', initPage);
