function clickable (clickableDiv){
	clickableDiv = $(clickableDiv);
	clickableDiv.click(function(){
		var href = $(this).find('a').attr('href');
		window.location.href = href;
	});
} 

function linkExpanderAndHover(listItemString) {

	  listItem = $(listItemString);

		listItem.mouseover(function(){
			$(this).css({
				'cursor' : 'pointer'
			});
			$(this).find('a').css({
				'text-decoration' : 'underline'
			});
			$(this).addClass('hovered');
		});

		listItem.mouseout(function(){
			$(this).find('a').css({
				'text-decoration' : 'none'
			});
			$(this).removeClass('hovered');
		});

		listItem.click(function() {
		    var href = $(this).find('a').attr('href');
		    if ($(this).find('a').attr('target') == '_blank') {
		        window.open(href);
		        return false;
		    }
		    window.location.href = href;
		});
}


function searchChanger (){
	$('#search-changer ul li a').click(function(){
		$('#search-changer li a.selected').removeClass('selected');
		$(this).addClass('selected');
	});
}


function mouseOvers (hoverItem, hoverValue, origValue) {

	hoverItem = $(hoverItem);

	if (hoverItem.length > 0) {
	    hoverItem.css('cursor', 'pointer');
		hoverItem.hover(
			function () {
				$(this).css({ 'background' : hoverValue }); }, 
				function () { $(this).css({ 'background' : origValue });
		});
	}

 }

function initSifr(){
	$('.sifr').sifr({
		path: 'assets/fonts/',
		font: 'bliss-pro-bold',
		fontSize: "24px"
	});
	$('.bar h3 a').sifr({
		path: 'assets/fonts/',
		font: 'bliss-pro-bold',
		fontSize: "44px"
	});
}

function setSearchKeywordField(){
	$('input.searchBox').attr({ value: 'Search the site...' }).focus(function(){
		if ($(this).val()=='Search the site...') {
				$(this).val("");
			}
		}).blur(function(){
			if ($(this).val()=="") {
				$(this).val('Search the site...');
			}
		});
}

function publicationsGridList(){
 	$('ul.publications li:eq(5)').addClass('last');
	$('ul.publications li:eq(11)').addClass('last');
}

function gridList (){
	var gridListItem = $('.grid ul li');

	//remove current css
	gridListItem.find('a:hover').css('text-decoration', 'none');

	//clickable
	gridListItem.click(function() {
	    var href = $(this).find('a').attr('href');
	    if ($(this).find('a').attr('target') == '_blank') {
	        window.open(href);
	        return false;
	    }
	    window.location.href = href;
    });

	//mouse css
	gridListItem.css('cursor', 'pointer');
	gridListItem.mouseover(function(){
		$(this).css({
			'background-image' : 'url(/assets/images/grid-li-bg-hover.gif)',
			'background-position' : '0px -1px'
		});
	});

	gridListItem.mouseout(function(){
		$(this).css({
			'background-image' : 'url(/assets/images/grid-li-bg.gif)',
			'background-position' : '0px 0px' 
		});
	});

}

function subNavigationHover(){
 
	//$('.outer-sub-nav ul li').hover(
	//	function () { $(this).addClass('hovered'); },
	//	function () { $(this).removeClass('hovered'); 
	//});
}

function rollOverShowInfo(){
	$('.large-grid .info p').hide();

	$('.large-grid ul li').each(function(){

		$(this).mouseover(function(){
			$(this).find('.info p').show();
		});

		$(this).mouseout(function(){
			$(this).find('.info p').hide();
		});
	});
}

function initTooltip(tooltipString){
	tooltipTrigger = $(tooltipString);
	tooltipTrigger.tooltip({ 

    bodyHandler: function() { 
        return $($(this).attr("href")).html(); 
    }, 
	 fixPNG: true,
	 fade: 250,
		top: -145, 
    left: 10,
	 
    showURL: false 
	});
}

function toggleChildLists() {
    if (removeTopLevelLinks == true) {
        
        var toggle = $('#content ul li a.toggle');
        toggle.removeClass('static');
        toggle.attr('href', '#');
        $('#content ul li ul').hide();

        toggle.each(function() {
            $(this).click(function() {
                $(this).siblings('ul').toggle('fast');
                $(this).toggleClass('open');
               
            })
        });

        toggle.bind('mouseover', function() { $(this).toggleClass('hover'); });
        toggle.bind('mouseout', function() { $(this).toggleClass('hover'); });

    }
    $('#content ul li ul li:last-child').css('border', 'none');    
}

$(function callOnPageID() {

    setSearchKeywordField();

    var page = $('body').attr('id');

    switch (page) {

        case 'home':
            initSifr();
            clickable('ul.items li');
            searchChanger();
            //linkExpanderAndHover('.col ul li');
            break;

        case 'video-grid':
            var contentClass = $('#content').attr('class');

            if (contentClass == 'large-grid') {
                rollOverShowInfo();
                linkExpanderAndHover('.large-grid ul li');
            }
            else {
                gridList();
            }
            break;

        case 'news':
            linkExpanderAndHover('.titled-lists ul li');
            subNavigationHover();
            break;

        case 'a-z':
            linkExpanderAndHover('ul.wide li');
            break;

        case 'publications':
            linkExpanderAndHover('ul.publications li');
            publicationsGridList();
            break;

        case 'project-detail':
            linkExpanderAndHover('.titled-lists ul li');
            linkExpanderAndHover('.white-cell ul li');
            linkExpanderAndHover('.grey-cell ul li');
            subNavigationHover();
            break;

        case 'template':
            linkExpanderAndHover('.titled-lists ul li');
            linkExpanderAndHover('.white-cell ul li');
            linkExpanderAndHover('.grey-cell ul li');
            subNavigationHover();
            /*initCarouselSmall('.news-feature .scrollable');*/
            /*initCarouselSmallInfo();*/
            /*initTooltip('a.pdf');*/
            break;

        case 'listing':
            toggleChildLists();
            break;
        default:
            break;
    }

    mouseOvers('.search .submit', 'url(/assets/images/go-hover.gif) no-repeat', 'url(/assets/images/go.gif) no-repeat');
    mouseOvers('.news-feature .items li', 'url(/assets/images/thumb-bg1-hover.gif) no-repeat', 'url(/assets/images/thumb-bg1.gif) no-repeat');
    mouseOvers('#search-changer .searchSubmit, .content-search-button', 'url(/assets/images/search-hover.gif) no-repeat', 'url(/assets/images/search.gif) no-repeat');
    mouseOvers('.news-feature p.top', 'url(/assets/images/carousel-up-blue.gif) no-repeat', 'url(/assets/images/carousel-up.gif) no-repeat');
    mouseOvers('.news-feature p.bottom', 'url(/assets/images/carousel-down-blue.gif) no-repeat', 'url(/assets/images/carousel-down.gif) no-repeat');

});


