﻿$(function () {
	$('.menu-li-' + window.pid).addClass('active');
	$('.menu-li-' + window.pagetype).addClass('active');
	doCufon();

	initHomeBoxes();
	adjustPage();

	$('#slides').cycle({
		fx: 'fade',
		speed: 1750,
		timeout: 4000,
		randomizeEffects: false,
		easing: 'easeOutCubic',
		next: '#slide-next',
		prev: '#slide-prev',
		pager: '#slidePager',
		cleartypeNoBg: true
	});
	$('#slide-prev, #slide-next').bind('mouseenter mouseleave', function () {
		$(this).toggleClass('hover');
	});

	initPage();

	$(window).bind('resize', function () {
		adjustPage();
	});

	initGallery();

	$('#nav .menu-item-106').html('HOME');
	$('#nav .menu-item-109').html('CONTACT');

	initMenu();
	initSideBarMenu();

});

function initWebPage() {
	/* Called from init.js */
	$('input:button, input:submit').each(function () {
		$(this).button('destroy');
		$(this).addClass('input-submit');
		$(this).removeClass('border shadow');
	});

	$('input:text, select, textarea').removeClass('border shadow');
	$('input:text, select, textarea').addClass('input');
}

function adjustWindow() {
}

function doCufon() {
	//$('#nav > .active > a').css('color', '#FFFFFF');
	Cufon.replace('#nav > li > a, #button-real-estate, #button-development, .copyright, #footer-menu a, #bottom-links a', {
		hover: true
	});
	
	Cufon.replace('#inside-header h1, #inside-header h2, #inside-title div, .box h1', {
		textShadow: '#000000 1px 1px'
	});

	$('#mainmenu').css('visibility', 'visible');
	//Cufon.now();

}

function adjustPage() {
	adjustWindow();
	setSidebarHeight();
}

function initHomeBoxes() {
	$('.box').bind('mouseenter mouseleave', function () {
		$(this).toggleClass('hover');
	});

	$('.box').each(function () {
		$(this).bind('click', function () {
			location.href = $(this).attr('href');
		});
	});
}

function initPage() {
	fLabel = '';
	fPortfolio = false;

	switch (window.pagetype) {
		case 4:
			fLabel = '';
			fPortfolio = true;
			break;
		case 5:
			fLabel = 'Eastern Canada';
			fPortfolio = true;
			break;
		case 6:
			fLabel = 'Western Canada';
			fPortfolio = true;
			break;
	}

	if (fPortfolio) {
		$('#portfolio').fadeIn(200);
	}
	$('#page-label').html(fLabel);
}

function setSidebarHeight() {
	fHeight = $(window).height();
	if (window.pid != 1) {
		fHeight = fHeight - 200 - 159;

		fContentHeight = $('#main').height();
		if (fContentHeight > fHeight) {
			fHeight = fContentHeight + 5;
		}

		$('#sidebar').css({
			'min-height': fHeight
		});
	} else {
		fHeight = fHeight - 910;
		if (fHeight < 0) {
			fHeight = 0;
		}
		$('#home-spacer').css({
			'height': fHeight
		});
	}
}

function initGallery() {
/*
	$('.fancybox').fancybox({
		'transitionIn': 'elastic',
		'transitionOut': 'elastic',
		'speedIn': 600,
		'speedOut': 200,
		'overlayShow': true,
		'type': 'inline',
		'scrolling': 'no'
	});
*/
	$('.fancybox').fancybox();

	$('.page-image-thumb').bind('mouseenter', function () {
		$(this).animate({
			'opacity': 0.85
		});
	});

	$('.page-image-thumb').bind('mouseleave', function () {
		$(this).animate({
			'opacity': 1
		});
	});
}

function initMenu() {
	jQuery("#nav a").removeAttr('title');
	jQuery("#nav ul").css({ display: "none" }); // Opera Fix

	$('#nav > li').bind('mouseenter', function () {
		jQuery(this).find('ul:first').css({
			visibility: "visible",
			display: "none"
		}).slideDown(400, "easeInOutQuart");
	});

	$('#nav > li > ul').bind('mouseenter', function () {
		$(this).css({
			'visibility': 'visible'
		});
	});

	$('#nav > li > ul  > li').bind('mouseenter mouseleave', function () {
		$(this).toggleClass('hover');
	});

	$('#nav > li').bind('mouseleave', function () {
		jQuery(this).find('ul:first').css({
			visibility: "hidden"
		});
	});


}

/* Sidebar Collapsing */
function initSideBarMenu() {
	$('#sidebar-inner > ul > li > ul > li:last-child').css({
		'background-image': 'none'
	});

	$('.collapsable > li > ul').each(function () {
		fHeight = $(this).height();
		$(this).attr('ul-height', fHeight);
		$(this).css('height', 0);
	});



	$('.collapsable > li').bind('click', function () {
		fObj = $(this).find('ul:first');
		//sidebarMenuToggle(fObj);
	});

	fObj = $('.collapsable > .li-pid-' + window.pagetype + ' > ul');
	sidebarMenuToggle(fObj);
}

function sidebarMenuToggle(fObj) {
	fHeight = fObj.height();
	if (fHeight == 0) {
		fHeight = fObj.attr('ul-height');
	} else {
		fHeight = 0;
	}

	fObj.animate({
		'height': fHeight
	}, 700);

	fObj.toggleClass('open');
}

 /* Old Site Compatibility */
function openFlashPage(fPID) {
	fMsg = 'NOTE: This link must be updated in the Body Editor so that it references the proper link.\n\nDo you wish to open this page anyways?'
	if (confirm(fMsg)) {
		location.href = '/index.asp?pid=' + fPID;
	}
}
