$(document).ready(function(){$.inbev.load()});

$.inbev = {
    load: function() {
        this.add_events();
    },
    add_events: function() {
        //this.set_background_height();
        //$(window).resize(function() {$.inbev.set_background_height()});
        this.beautify();
        this.rotate_header();
        this.faq();
		this.tradition();
		this.pressdistributor();
		this.germanymap();
    },
    beautify: function() {
        // last class in teaser lists
        $('#RightPart .teaser-gray ul li:last').addClass('last');
		$('#fullPage #homepageHeader div.header').css('display','none');

        // remove empty links from teasers
        $('#RightPart .teaser-gray a.more-link').each(function() {
            $(this).parent().css('padding-bottom', 10);
            if ($(this).html() == '') $(this).remove();
        });
		
		// remove empty links from teasers (ContentPart)
		$('.readMore').each( function(i) {
			var src = $(this).find('a').attr('href');
			if(src == '') $(this).hide();
		});
		
		$('#ContentPart .csc-textpic-intext-left div img').each(function(i) {
			$(this).css('margin-top', '4px');
		});
    },
	
    set_background_height: function() {
        //$('#backgroundWrap').height($(document).height());
    },
    rotate_header: function() {
        // default header
        if ($('#fullPage #content .contentHeader > div').length > 1)
        {
            var rand = this.rand(1, $('#fullPage #content .contentHeader > div').length);
            $('#fullPage #content .contentHeader > div').hide();
            $('#fullPage #content .contentHeader > div:nth-child(' + rand + ')').show();
        }

        // header homepage
        if ($('#homepageHeader > div').length > 1)
        {
            var rand = this.rand(1, $('#homepageHeader > div').length);
            $('#homepageHeader > div').hide();
            $('#homepageHeader > div:nth-child(' + rand + ')').show();
			$('#homepageHeader div.header').show();
        }
    },
    rand: function rand(min,max) {
        if( min > max ) return -1;
        if( min == max ) return min;

        return (min + parseInt(Math.random() * ( max-min+1 )));
    },
    faq: function() {
        $('#ContentPart .faq-container div').children('p, ul, table, img').hide();
		var i = 0;
        $('#ContentPart .faq-container div').each(function() {
            if($(this).hasClass('csc-default')){
				$(this).attr('id', 'faq-' + i);
				$(this).find('div').attr('id', 'faq-' + i);
				i++;
			}
			
            if ($(this).find('h2 span').length == 0) $(this).find('h2').append('<span></span>');
        });
        $('#ContentPart .faq-container div h2').click(function() {
			var headline = $(this);
			if(!$(this).hasClass('active')){ 
				
				var id = headline.parent().parent().attr('id');
				$('#ContentPart .faq-container div[id!="' + id + '"] h2').removeClass('active');
				$('#ContentPart .faq-container div[id!="' + id + '"]').find('p, ul, table, img').slideUp('fast', function() {
				   headline.addClass('active');
				   
				   headline.parent().parent().find('p, ul, table, img').slideDown();
				   return false;
				});
			} else {
				headline.parent().parent().find('p, ul, table, img').stop().slideUp();
				headline.removeClass('active');
			}
        });
        //$('#ContentPart .faq-container div:first h2').click();
    },
	
	tradition: function() {
		/* some css fixes */
		$('.tradition-items div:last-child .tradition-item').css('background', 'none');
		$('.tradition-item .date .orange').parent()
		.parent()
		.find('.desc')
		.css('padding-top', '7px');
	},
	
	pressdistributor: function() {
		if($('#press-distributor input').hasClass('error')){
			$('#press-distributor p.error').show();
		}
		else
		{
			$('#press-distributor p.error').hide();
		}
	},
	
	germanymap: function() {
		$('.map .dot').hover( function() {
			$('.site-desc').hide();
			$('.brand_img').hide();
			$('.site-detail').hide();
			var name = $(this).attr('rel');
			$('.site-desc[rel="'+name+'"]').stop().fadeIn();
			$('.site-desc[rel="'+name+'"]').next().stop().fadeIn();
			$('.site-detail[rel="'+name+'"]').stop().show();
		}, function () {
		
		});
	}
};

function rand(min,max) {
	if( min > max ) return -1;
	if( min == max ) return min;

    return (min + parseInt(Math.random() * ( max-min+1 )));
}

function show_brand_desc(id) 
{

	$('.brand_desc').hide();
	$('.brand_desc[rel="'+id+'"]').show();
}


