function show_sub_menu(id) {
	if(!$(id).is(":visible")) {
		$('#sub ul:visible').not('.right').stop(true, true).fadeOut(100);
		$(id).stop(true, true).delay(200).fadeIn(100);
		
		return false;
	}
}


function carousel_prev() {
	if (parseInt($('ul#carousel_container').css('left')) < -973) {
		$('ul#carousel_container').animate({"left": "+=974"}, 500);
	} else {
		$('ul#carousel_container').animate({"left": ($('ul#carousel_container').children().length - 1) * -974}, 500);
	}
}

function carousel_next() {
	var right_border = ($('ul#carousel_container').children().length - 2) * -974;
	if(parseInt($('ul#carousel_container').css('left')) < right_border) {
		$('ul#carousel_container').animate({"left": "0"}, 500);
	} else {
		$('ul#carousel_container').animate({"left": "-=974"}, 500);
	}
}

function show_box(id) {
	$('#box_background').bind('click', function() {
		hide_box(id);
	});
	
	$('#box_background').fadeIn(300);
	$('#' + id).fadeIn(300);
	return false;
}

function hide_box(id) {
	$('#box_background').fadeOut(300);
	$('#' + id).fadeOut(300);
	return false;
}

function get_tweets() {
    $.getJSON(
        'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=mistergreenev&count=10&include_rts=true&callback=?',
        {},
        show_tweets,
        'jsonp'
    );
}

function show_tweets(tweets) {
    var str = $('#tweets ul').html();
    $.each(tweets, function(index,value)
    {
        var dt = new Date(value.created_at);
        str+= '<li><p>';
        str+= value.text;
        str+= '</p>';
        str+= '</li>';
    });
    $('#tweets ul').html(str);
	$('#tweets ul').fadeIn(1000);
}

$.fn.equalHeights = function(px) {
	$(this).each(function(){
		var currentTallest = 0;
		$(this).children().each(function(i){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
		if (!px && !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm();
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
		$(this).children().css({'min-height': currentTallest}); 
	});
	return this;
};

$(document).ready(function() {
	//Fade in social media bar.
	$('#social').delay(500).animate({top: '0px'}, 300);
	
	//Show first submenu.
	show_sub_menu('#wat_doen_we');
	
	$('#wat_doen_we_link').mouseover(function() {
		show_sub_menu('#wat_doen_we');
		return false;
	});

	//Bind function to move left.
	$('div#carousel_left a').bind('click', function() {
	  carousel_prev();
	  return false;
	});
	
	//Bind function to move right.
	$('div#carousel_right a').bind('click', function() {
	  carousel_next();
	  return false;
	});
	
	$('#items').equalHeights(true);
	
	if($("div#tweets")) {
		get_tweets();
	}  

    $("#brands").change(function() {
        $('#types').load('http://mistergreen.nl/offers/types/' + $("#brands").val());
    });

});

Cufon.replace('#top a');
Cufon.replace('#sub a');
Cufon.replace('h1');
Cufon.replace('h2');
Cufon.replace('h3');;
Cufon.replace('.button');
Cufon.replace('#tweets h1');
