Cufon.replace('a.navlink', {hover: true});
Cufon.replace('.cufon_text');
Cufon.replace('.cufon_text_hover',{hover: true});

//Cufon.replace('p');
Cufon.replace('a.more_link');

var frontend = {
	opacity_show: {opacity: 1},
	opacity_hide: {opacity: 0},
	opacity_time: 500,
	search_timeout: null,
    init: function()
    {
		if($.browser.msie)
    	{

    		frontend.opacity_show = {};
    		frontend.opacity_hide = {};
    		frontend.opacity_time = 0;
    	}
        frontend.search();
        frontend.subNavMargin();
        frontend.newsletter();
        if($('.generic_form').length > 0)
        {
        	form.init();
        }
    },
    subNavMargin: function()
    {
        $('ul.subnav').css('marginTop',($('div#page_header').outerHeight()+10)+'px');
    },
    threeColumnHeights: function(selector)
    {
    	var selector = selector;
    	$(selector).each(function(index){
            if(index % 3 == 0)
            {
                var height = $(this).height();
                if($(this).next(selector).height() > height)
                {
                    height = $(this).next(selector).height();
                }
                if($(this).next(selector).next(selector).height() > height)
                {
                    height = $(this).next(selector).next(selector).height();
                }
                height += 20;
                $(this).height(height);
                $(this).next(selector).height(height);                
                $(this).next(selector).next(selector).height(height); 
            }
        });

    },
    fourColumnHeights: function(selector)
    {
    	var selector = selector;
    	$(selector).each(function(index){
            if(index % 4 == 0)
            {
                var height = $(this).height();
                if($(this).next(selector).height() > height)
                {
                    height = $(this).next(selector).height();
                }
                if($(this).next(selector).next(selector).height() > height)
                {
                    height = $(this).next(selector).next(selector).height();
                }
                if($(this).next(selector).next(selector).next(selector).height() > height)
                {
                    height = $(this).next(selector).next(selector).next(selector).height();
                }
                height += 20;
                $(this).height(height);
                $(this).next(selector).height(height);                
                $(this).next(selector).next(selector).height(height); 
                $(this).next(selector).next(selector).next(selector).height(height); 
            }
        });

    },
    newsletter: function(){
        $('.newsletter').click(function(e){
            e.preventDefault();      
            if($(this).hasClass('frontpage_newsletter'))
            {
                $('#newsletter_container').css('left',($(this).offset().left-88)+'px').css('top',($(this).offset().top-378)+'px');
            }
            else
            {
                $('#newsletter_container').css('left',($(this).offset().left-300)+'px').css('top',($(this).offset().top-388)+'px');
            }
            $('#newsletter_container').show().animate(frontend.opacity_show,frontend.opacity_time);
        });
        
        $('#close_newsletter').click(function(e){
        	e.preventDefault();
        	$('#newsletter_container').animate(frontend.opacity_hide,frontend.opacity_time, function(){$(this).hide()});
        });

    },
    search: function()
    {
    
        $('.search').click(function(e){
            e.preventDefault();
            $('h1').hide();
            $('#search_input input').val('');
            $('#search_box').show();
            $('#search_input input').focus();
        });
        
        $('#search_input a').click(function(e){
            e.preventDefault();
            $('#search_input input').val('');
            $('#search_box').hide();
            $('#search_results').html('').hide();
            $('h1').show();
        });
    
        $('#search_input input').keyup(function(e){
            e.preventDefault();
            if($('#search_results:visible').length == 0)
            {
                $('#search_results').html('<li class="loading">Loading</li>').show();
            }
            clearTimeout(frontend.search_timeout);
            if($(this).val() != '')
            {
                frontend.search_timeout = setTimeout("$.ajax({url: '/search?query="+$(this).val()+"',dataType: 'json',async: 'false',cache: 'false',success: function(response){$('#search_results').html(response.html);}});",500);
            }
            else
            {
                $('#search_results').html('').hide();
            }
        });
    }
    
}

$(function(){
    frontend.init();
});

