function sendMailTo(name, subDomain, topLevelDomain) {
    window.location = 'mailto:' + name + '@' + subDomain + '.' + topLevelDomain;
}
function initMap() {
    var map = new GMap2(document.getElementById("map"));	
    map.setCenter(new GLatLng(34.101766,-118.340693), 15);
    map.setUIToDefault();	
    var point = new GLatLng(34.101466,-118.340699);
    map.addOverlay(new GMarker(point));
}
function ShowProject(projectURL) {
    var location = window.location.toString();
    var projectFragmentIndex = location.indexOf('#project:');
    if (projectURL == null & projectFragmentIndex > -1)
        projectURL = decodeURI(location.substring(projectFragmentIndex + 9));

    if (projectURL != null) {
        var colorBoxWidth = '614px';
        var colorBoxHeight = '640px';
        
        $.fn.colorbox({
            scrolling: false,
            height: colorBoxHeight, 
            width: colorBoxWidth,
            iframe: true,
            href: projectURL + "?lightbox",
            open: true,
            title: false
        });
    }
}

function slideshow () {
    //recursive call
	$(window).load(function () {
	    var targ = '#ticker .active';
	    fadeOut(targ);
	});
	
	//each function
	function fadeOut (targ) {
	    $(targ).fadeOut(900, function () {
	        var x = $(this).next('li');
	        if(!x.length == 0) 
			    targ = x;
			else 
			    targ = '#ticker .active';
			
			fadeIn (targ);
		});
	}
	
	function fadeIn (targ) {
	    $(targ).animate({"opacity": "show"}, 300, function () {
		    //create delay before pic change in ms eg 3000 = 3 secs
		    $(targ).animate({opacity: 1.0}, 3500);
		    //fade out image and replace
		    fadeOut(targ);
	    });
	}
}

var updateProjectUrl = false;

$(document).ready(function() {
    /* font replacement */
    Cufon.replace('h1, .h1, h2, .h2, .primary .current, .pressDate, .secondary .navHeading, .subNav .current', { fontFamily: 'Klavika' });
    Cufon.replace('.primary a:not(.current), .subNav a:not(.current), .bigButton, .projects a', { fontFamily: 'Klavika', hover: true });
    Cufon.replace('.secondary .current, .vcard', { fontFamily: 'Century Gothic' });
    Cufon.replace('.secondary a:not(.current)', { fontFamily: 'Century Gothic', hover: true });
    
    if ($.browser.msie && $.browser.version < '8')
    {
        // IE 6/7 hack; homePromo is covering topnav
        $('.homePromo').css({ 'margin-top' : '0' });
    }
    // set the high of the iframe in the lightbox
    
    if ($('.lightbox').length === 1)
    {
        var theFrame = $('#cboxIframe', parent.document.body);
        if (theFrame.length === 1)
        {
            var frameH, bodyH, theDiv, divH;
            frameH = theFrame.height();
            bodyH = $(document.body).height();
            if (frameH < bodyH)
            {
                theDiv = $('.liquid');
                divH = theDiv.height() - (bodyH - frameH);
                theDiv.height(divH + 'px').css({ 'overflow-y' : 'scroll' });
            }
            else if (bodyH < frameH)
            {
                theDiv = $('.projectContent');
                divH = theDiv.height() + (frameH - bodyH);
                theDiv.height(divH + 'px'); 
            }
        }
    }
    $('#cboxClose').hover(
        function () {
            $('#cboxTopCenter, #cboxLoadedContent').addClass('closeHover');
            $('#cboxIframe').contents().find('.lightbox').addClass('closeHover');
        },
        function () {
            $('#cboxTopCenter, #cboxLoadedContent').removeClass('closeHover');
            $('#cboxIframe').contents().find('.lightbox').removeClass('closeHover');
        }
    );

    $('.homeServices li a').hover(
        function () {
            $(this).closest('li').addClass('hover');
        }, 
        function () {
            $(this).closest('li').removeClass('hover');
        }
    ).find('.imgWrap').prepend('<div class="cover"></div>');
    
    $('a.external').click(function() {
        window.open(this.href);
        return false;
    });

    $('#playVideo').click(function() {
        $.fn.colorbox({
            //scrolling: false,
            //height: '600px', 
            width: '614px',
            iframe: false,
            inline: true,
            href: '#agencyVideoContent .lightbox',
            open: true,
            title: false
        });
    });
    if ($('#ticker').length !== 0) {
        slideshow ();
    };
    if ($('#map').length === 1)
    {
        // if the google map is there, then add an window unload event and load map
        $(window).unload(function () {
            GUnload();
        });
        initMap();
    }

    var projectLinks = $("a[href][rel='project']").get();
    for (var i = 0; i < projectLinks.length; i++) {
        projectLinks[i].href = "#project:" + encodeURI(projectLinks[i].href);
        $(projectLinks[i]).click(function() {
            ShowProject(decodeURI(this.href.substring(this.href.indexOf('#project:') + 9)));
            if (!updateProjectUrl)
                return false;
        });
    }
    
    ShowProject();
});