$(function() {
	
	// submenu
	$('#menu li').hover(function() {
		$(this).children('ul').stop(true, true).slideDown(700, 'easeOutElastic');
	}, function() {
		$(this).children('ul').stop(true, true).slideUp(600, 'easeOutBounce');
	});
	
	// slider
	$('#slider').bxSlider({
		mode: 'fade',
		controls: 'false',
		pager: false,
		auto: true,
		pause: 3000,
		onBeforeSlide: function(currentSlideNumber) {
			if( currentSlideNumber == 5 ) {
				$('#slider_top_img').stop().fadeTo(150, 0);
			}
			else if( currentSlideNumber == 0 ) {
				$('#slider_top_img').stop().fadeTo(150, 1);
			}
		}
	});
	
	$('.fancybox').fancybox();
	$('a.youtube').click(function() {
		$.fancybox({
				'padding'			: 0,
				'autoScale'			: false,
				'transitionI	n'	: 'none',
				'transitionOut'	: 'none',
				'title'				: this.title,
				'width'				: 680,
				'height'				: 495,
				'href'				: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'				: 'swf',
				'swf'					: {
					'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});
		return false;
	});
	
	$('.fotogaleria .fancybox img').each(function() {
		var margin_top = ($(this).height() < $(this).parent('a').height()) ? ($(this).parent('a').height()-$(this).height())/2 : 0;
		console.log(margin_top)
		$(this).css({
			'margin-top': margin_top,
		});
	});
	
	$('a.youtube img, .fotogaleria .fancybox').hover(function() {
		$(this).stop().fadeTo(250, 0.75);
	}, function() {
		$(this).stop().fadeTo(250, 1);
	});
	
	// time remaining
	$('#remaining_days').text( days_between(new Date(2011,05,10), new Date()) );
	
	// sidebar align
	if( $('.left_content').outerHeight(true) > $('#sidebar_conttent').outerHeight(true) && $('#sidebar_conttent .middle').height() > $('#sidebar_conttent .middle').css('min-height') ) {
		if( !($.browser.webkit) || $('.subpage .left_content').height() != undefined ) {
			$('#sidebar_conttent .middle').height( $('.left_content').height() - 150 );
		}
		else {
			$('#sidebar_conttent .middle').height( $('.left_content').height() );
		}
	}
	else if( $('.subpage .left_content').outerHeight(true) < $('.subpage #sidebar_conttent').outerHeight(true) ) {
		$('.subpage .left_content .large_box .middle').height( $('.subpage #sidebar_conttent .middle').height() - 50 );
	}
	
	// footer align
	if( $(window).height() > $('#wrapper').height() + $('#footer_container').height() ) {
		$('#wrapper').height( $(window).height() - $('#footer_container').height() );
	}
	$(window).resize(function() {
		if( $(window).height() > $('#wrapper').height() + $('#footer_container').height() ) {
			$('#wrapper').height( $(window).height() - $('#footer_container').height() );
		}
	});
	
	// middle footer height fix
	var maxHeight = 0;
	$('#footer_middle .box').each(function() {
		if( $(this).height() > maxHeight ) {
			maxHeight = $(this).height();
		}
	});
	$('#footer_middle .box').each(function() {
		$(this).height( maxHeight );
	});
	
});



function days_between(date1, date2) {

	// The number of milliseconds in one day
	var ONE_DAY = 1000 * 60 * 60 * 24

	// Convert both dates to milliseconds
	var date1_ms = date1.getTime()
	var date2_ms = date2.getTime()

	// Calculate the difference in milliseconds
	var difference_ms = Math.abs(date1_ms - date2_ms)
	
	// Convert back to days and return
	return Math.round(difference_ms/ONE_DAY)

}

