$(function(){
    
    $(window).resize(windowResize);
    $(window).load(windowResize);
    
    
    
	$('.top_menu a').live('click', function(){
        $('.top_menu .mn > .active').removeClass('active');
        if($('.top_menu .level2:visible').length){
            $('.top_menu .level2:visible').fadeOut(200);
        }
        $(this).addClass('active');
		if($(this).next().filter('div').length){
			$(this).next().fadeToggle(200);
			return false;
		}
	});
    
    $('.top_menu > a').mousedown(function(e){
        if ( (e.button == 4) || (e.which == 2 )){
            $('.top_menu .mn > .active').removeClass('active');
            if($('.top_menu .level2:visible').length){
                $('.top_menu .level2:visible').fadeOut(200);
            }
            $(this).addClass('active');
            if($(this).next().filter('div').length){
                $(this).next().fadeToggle(200);
                return false;
            }
            return false;
         }
         
	});
    
    $('#wrapper').live('click', function(){
        $('.top_menu .mn > .active').removeClass('active');
        if($('.top_menu .level2:visible').length){
            $('.top_menu .level2:visible').fadeOut(200);
        }
    });
	if($('#img').length){
        $('.image').each(function(){
            hiddenImg = new Image();
            hiddenImg.src = $(this).css('background-image').replace('url("', '').replace('")', '');
        });
        if($('.image').length > 1){
            setInterval(function(){
                if($('#img .images > div:visible').next().filter('div').length){
                    $('#img .images > div:visible').fadeOut(300, function(){
                        $(this).next().fadeIn(300);
                    });
                }else{
                    $('#img .images > div:visible').fadeOut(300, function(){
                        $('#img .images > div:first-child').fadeIn(300);
                    });
                }
            }, 5000);
        }
	}
	if($("a[rel='fancybox']").length){
        $("a[rel='fancybox']").fancybox({
            'titleShow'  : false,
            'transitionIn' : 'elastic',
            'transitionOut' : 'elastic'
        });
    }
    
    if($('.benefits .items').length){
        $('.benefits .items').jcarousel({
            scroll: 1,
            visible: 4,
            auto: 3,
            wrap: 'circular'
        });
    }
    
    if($('.service_text').length){
        var anc = window.location.hash.replace("#","");
        if(anc >= 1 && anc <= $('.service_text').length){
            $('.content_text table tr:nth-child('+(parseInt(anc) + (parseInt(anc)-1))+')').find('.post').click();
        }
    }
    
});

function windowResize(){
    $('.mn').css('margin-left', (1 - (1366 - $('#site').width()) / 406) * 30 + 25 + 'px');
    var max_height = 0;
    $('.benefits li').css('height', 'auto').each(function(){
        if(max_height < $(this).height()) max_height = $(this).height();
    });
    $('.benefits li').css('height', max_height + 10 + 'px');
}
