$(document).ready(function(){

	function setElementHeight() {
	
		var windowHeight = $(window).height();
		var footerHeight = $('.footer').height();
		var contentY	 = $('.content_container').offset().top;
	
		$('.content_container').css('min-height', windowHeight-footerHeight-contentY);
		
		var mainPaddingTop 		= parseInt($('.main_content').css('padding-top').replace('px', ''));
		var mainPaddingBottom 	= parseInt($('.main_content').css('padding-bottom').replace('px', ''));
		var mainMinHeight		= windowHeight-footerHeight-contentY-(mainPaddingTop+mainPaddingBottom);
		
		$('.main_content').css('min-height', mainMinHeight);
		
		var headerImageHeight		= $('.header_image').height();
		var CategoryMenu			= $('.category_menu').outerHeight();
		var TextBorderTop			= parseInt($('.text_content').css('border-top-width').replace('px', ''));
		var leftColHeight			= $('.left_col').height();
		var rightColHeight			= $('.right_col').height();
		var textContentMinHeight	= mainMinHeight-headerImageHeight-CategoryMenu-TextBorderTop;
		
		if(leftColHeight > rightColHeight && leftColHeight > textContentMinHeight)
		{
			textContentMinHeight = leftColHeight;
		}
		
		if(rightColHeight > leftColHeight && rightColHeight > textContentMinHeight)
		{
			textContentMinHeight = rightColHeight;
		}
		
		$('.text_content').css('min-height', textContentMinHeight);
		
		/*
		 * Set Category Menu width
		 */
		var catWidth 	= $('.category_menu').width()-2;
		
		var catItemWidth = Math.round(catWidth/$('.category_menu li').length);
		var lostWidth = (catItemWidth*$('.category_menu li').length);
		
		$('.category_menu li').width(catItemWidth-1);
		$('.category_menu li').css('margin', '0 1px 0 0');
		$('.category_menu li.lastChild').css('margin', '0');
		$('.category_menu li.lastChild').width(catItemWidth+1);
		//$('.category_menu li.lastChild').width(catItemWidth+lostWidth);
	}
	
	$(window).resize(setElementHeight);
	
	setElementHeight();
	
	/*
	 * page tabs
	 */
	$('.page_tabs li').click(function(e){
	
		e.preventDefault();
		
		var tabId = $(this).find('a').attr('href');
		
		$('.tab_content').children('div').css('display', 'none');
		
		$('.tab_content '+tabId).css('display', 'block');
		
		$('.page_tabs li').removeClass('selected');
		
		$(this).addClass('selected');
	});
	
	/*
	 * Brochure send by mail check
	 */
	$('#sendmail_check').click(function(e){
		
		if($('#sendmail_check:checked').length > 0)
		{
			$('#sendmail').slideDown();
		}
		else
		{
			$('#sendmail').slideUp();
		}
	});
	
	if($('#sendmail_check:checked').length > 0)
	{
		$('#sendmail').css('display', 'block');
	}
	
	/*
	 * Subscribe location
	 */
	$('.start_date').change(function(){
		
		var id = $(this).val();
		
		var rel = $(this).attr('rel');
		
		var content 	= $.ajax({
							 url: ROOT_PATH+'ajax/locations.php?id='+id,
							 type: "GET",
							 async: false,
							 dataType: 'html'
						  }).responseText;
		
		$('#'+rel).html(content);
	});
	
	$("a.lightbox").each(function()
	{
		var dWidth 		= parseInt($(this).attr('href').match(/width=[0-9]+/i)[0].replace('width=',''));
		var dHeight 	=  parseInt($(this).attr('href').match(/height=[0-9]+/i)[0].replace('height=',''));
	
		$(this).fancybox({ 
			'width'			:	dWidth,  
			'height'		:	dHeight,
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'overlayShow'	:	true,
			'type'			:	'iframe',
			'scrolling'		:	'no'
		});
	});
	
	$('.showinfo').click(function(e){
		
		e.preventDefault();
		
		if($(this).hasClass('slide'))
		{
			var href = $(this).attr('href');
			
			if($(this).hasClass('show'))
			{
				$(href).slideUp();
				$(this).removeClass('show');
			}
			else
			{
				$(this).addClass('show');
				$(href).slideDown();
			}
		}
	});
});
