$().ready(function(){
	if ($('#main_block').length) {
		main_HP_block_function();
	}
});


function main_HP_block_function()
{
	/**
	 * Check for "debug" in the URI.
	 * If exists, cancel the cycle.
	 */
	if (location.search.indexOf('debug') != -1) return;
	
	var main_block = $('#main_block');
	var main_content = main_block.find('.main_content'), main_btns = main_block.find('#main_buttons').find('a');
//	var map_areas = main_block.find('map').find('area');
	
	var cycle_opt = {
		timeout: 4000,
		pager: '#main_block_pager',
		pause: true, 
    pauseOnPagerHover: true,
    before: function(currSlideElement, nextSlideElement, options, forwardFlag){
			$(currSlideElement).removeAttr('active');
			$(nextSlideElement).attr('active','active');
			var thisnum = parseInt(currSlideElement.className.substring(currSlideElement.className.length-1));
			var nextnum = parseInt(nextSlideElement.className.substring(nextSlideElement.className.length-1));
			if (thisnum > nextnum)
			{
				// Reached end of slideshow, switch to next one
				index = main_content.index($(currSlideElement).parent());
				var nextblock = index == main_content.length-1 ? 0 : index+1;
				location.href = main_btns.get(nextblock).href;
				main_btns.eq(nextblock).click();
			}
		},
		after:	function(currSlideElement, nextSlideElement, options, forwardFlag){
			main_block.find('#main_block_pager a.activeSlide').blur();
		}
	};
	
	// attach the cycle() to the first button
	main_block.find('#content_donate').cycle($.extend(cycle_opt, {startingSlide:0}));
	
	main_btns.each(function(){
		$(this).bind('click', function(){
			var content_id = this.id.substring('button_'.length), chosen_content, active_slide;
			main_content.each(function(){
				if (this.id.substring('content_'.length) == content_id && this.style.display == 'none')
				{
					chosen_content = this;
					$(chosen_content).children().each(function(){
						if ($(this).attr('active') == 'active')
							active_slide = parseInt(this.className.substring(this.className.length-1));
					});
				}
			});
			if (chosen_content)
			{
				main_block.find('#main_block_pager').empty();
				
				main_block.find('.main_content:visible').cycle('stop').fadeOut('slow', function(){
					$(chosen_content).fadeIn('slow', function(){
						$(chosen_content).cycle($.extend(cycle_opt, {startingSlide:active_slide}));
					});
				});
			}
		});
	});
	
	// find hash from URL
	if (window.location.hash != '') {
		main_btns.filter('[href="'+window.location.hash+'"]').click();
	}
}
