var quickContactAutoShow = true;
var quickContactAutoHide = null;
var quickContactFormSent = false;

var expressInstallPath = "/static/js/lib/swfobject/expressInstall.swf";
var defaultFlashParams = {
	wmode: 'transparent',
	menu: false
};

$(document).ready(function() {
	
	// Transparent PNGs
	jQuery.ifixpng('/static/images/global/pixel.gif');
	$('.t-png').ifixpng();
	
	var menuRegExp = /\/(on|off|over)\/([a-z0-9\-]+\.png)$/ig;
	
	// Menu Overlays
	$('#menu > ul').children(':not(.active)').hover(
		function() {
			var menuImg = $(this).children('a').children('img');
			menuImg.attr('src', menuImg.attr('src').replace(menuRegExp, "/over/$2"));
		},
		function() {
			var menuImg = $(this).children('a').children('img');
			menuImg.attr('src', menuImg.attr('src').replace(menuRegExp, "/off/$2"));
		}
	);
	
	$('#menu-services > ul').children(':not(.active)').hover(
		function() {
			var menuImg = $(this).children('a').children('img');
			menuImg.attr('src', menuImg.attr('src').replace(menuRegExp, "/over/$2"));
		},
		function() {
			var menuImg = $(this).children('a').children('img');
			menuImg.attr('src', menuImg.attr('src').replace(menuRegExp, "/off/$2"));
		}
	);
	
	// Services Menu Dropdown
	$('#menu-services').hover(
		function() {
			$('#menu-services > ul').show();
		},
		function() {
			$('#menu-services > ul').hide();
		}
	);
	
	// Quick Contact Form
	$(window).bind('scroll resize load', positionQuickContact);
	$('#quick-contact-toggle').click(function() {
		disableQuickContactAutoHide();
		toggleQuickContact();
	})
	
	if (!(($.cookies.get('qcAutoLoaded') == 1) || !$.cookies.test())) {
		$.cookies.set('qcAutoLoaded', 1);
		window.setTimeout(function() {
			toggleQuickContact();
			quickContactAutoHide = window.setTimeout(toggleQuickContact, 8000);
		}, 1000);
	}
	
	$('#quick-contact-form input, #quick-contact-form textarea').focus(function() {
		disableQuickContactAutoHide();
	});
	
	if (!($.browser.msie && (parseInt($.browser.version) < 7))) {
		$('#quick-contact-form').validate({
			rules: {
				name: 'required',
				email: {
					required: true,
					email: true
				}
			},
			messages: {
				name: 'Please enter your name',
				email: {
					required: 'Please enter your e-mail',
					email: 'Please enter a valid e-mail'
				}
			},
			errorPlacement: function(error, element) {
				error.appendTo( element.parent('dd').prev('dt') );
			},
			submitHandler: function(form) {
				$('#qc-submit').val('Sending...').attr('disabled', 'disabled');
				var formData = {
					name: $('#qc-name').val(),
					email: $('#qc-email').val(),
					phone: $('#qc-phone').val(),
					details: $('#qc-details').val(),
					ref: window.location.href
				};
				$.post('/contact-us/process-form/', formData, function(data) {
					if (data.success) {
						quickContactFormSent = true;
						toggleQuickContact();
					} else {
						$('#qc-submit').val('Send').removeAttr('disabled');
						alert('There was an error in processing your form.\n\nPlease try again or contact us via e-mail or phone.');
					}
				}, 'json');
			}
		});
	}
	
	// Image pop-ups
	$('a.gallery').fancybox({
		'hideOnContentClick': true,
		'itemLoadCallback': getGalleryMulti,
		'frameWidth': 900,
		'frameHeight': 535
	});
	
});

function getGalleryMulti(el, opts) {
	var galleryCt = $(el).metadata().images;
	if (galleryCt == undefined) {
		galleryCt = 1;
	}
	for (var i=1; i<=galleryCt; i++) {
		opts.itemArray.push({
			url: $(el).attr('href').replace(/([0-9]+)\.jpg$/ig, i + ".jpg")
		});
	}
}

function disableQuickContactAutoHide() {
	if (quickContactAutoHide) {
		window.clearTimeout(quickContactAutoHide);
	}
}

function positionQuickContact() {
	var vpBot = $(window).height() + $(window).scrollTop();
	var fPos = $('#footer').offset({ margin: false });
	if (vpBot > fPos.top) {
		$('#quick-contact').addClass('docked');
	} else {
		$('#quick-contact').removeClass('docked');
	}
}
	
var qcVisible = 0;
var qcDuration = 250;
var qcVisibleHeight = '175px';
var qcHiddenHeight = '40px';

function showQuickContact() {
	if (!qcVisible) {
		toggleQuickContact();
	}
}

function toggleQuickContact() {
	var qc = $('#quick-contact');
	var targetImg = $('#quick-contact-toggle img');
	if (qcVisible) {
		qc.animate({height: qcHiddenHeight}, qcDuration, function() {
			targetImg.fadeOut('def', function() {
				if (!quickContactFormSent) {
					$(this).attr('src', '/static/images/global/quick-contact/header-show.png').fadeIn('def');
				} else {
					$(this).attr('src', '/static/images/global/quick-contact/header-sent.png').fadeIn('def');
					$('#quick-contact-toggle').css('cursor', 'default').unbind('click', toggleQuickContact);
				}
			});
			qcVisible = 0;
		})
	} else {
		qc.animate({height: qcVisibleHeight}, qcDuration, function() {
			targetImg.fadeOut('def', function() {
				$(this).attr('src', '/static/images/global/quick-contact/header-hide.png').fadeIn('def');
			});
			qcVisible = 1;
		})
	}
	$('#quick-contact').css('height', '52px');
}
