
(function($){

	$(document).ready(function(){
		/* Apply popup styles. */
		$('#gnavi > li > ul').each(function(i){
			var w = 0;
			/* Calc child elements width. */
			$(this)
				.show()
				.children('li')
					.each(function(){
						w += $(this).width();
					})
				.end()
			;
			$(this)
				.hide()
				.css({
					width: w +'px',
					opacity: 0.8
				})
			;

			/* Right based popup (wedding guide only). */
			if (i == 3) {
				$(this).css({
					left:'auto',
					right: 0
				});
			}
		});

		/* Define popup behavior. */
		$('#gnavi > li').hover(
			function(){
				$(this).children('ul').slideDown(128);
				var flash = document['aexternal'];
				if (flash && flash.setOver) {
					flash.setOver($('#gnavi > li').index(this) + 1);
				}
			},
			function(){
				$(this).children('ul').slideUp(128);
				var flash = document['aexternal'];
				if (flash && flash.setOver) {
					flash.setOver(0);
				}
			}
		);
		$('#gnavi > li > ul').hover(
			function(){
				var imgSrc = $(this).siblings('a').children('img.over').attr('src');
				$(this).siblings('a').children('img.over').attr('src', imgSrc.replace(/^(.+)(\.[a-z]+)$/, '$1_over$2'));
			},
			function(){
				var imgSrc = $(this).siblings('a').children('img.over').attr('src');
				$(this).siblings('a').children('img.over').attr('src', imgSrc.replace(/^(.+)_over(\.[a-z]+)$/, '$1$2'));
			}
		);

		/* Set year in copyrights. */
		$('#navi-footer .copy').html('&copy;'+ (new Date().getFullYear() || '2009') +' Chapelle des Anges.  All Rights Reserved.');

		/* Set rollover. */
		$('img.over')
			.each(function(){
				this.preloaded = new Image;
				this.preloaded.src = this.src.replace(/^(.+)(\.[a-z]+)$/, '$1_over$2');
			})
			.hover(
				function(){
					this.src = this.src.replace(/^(.+)(\.[a-z]+)$/, '$1_over$2');
				},
				function(){
					this.src = this.src.replace(/^(.+)_over(\.[a-z]+)$/, '$1$2');
				}
			)
		;

		/* Setup link click handler. */
		$('a')
			/* Popup. */
			.filter('.popup')
				.click(function(){
					win(this.href);
					return false;
				})
			.end()
			/* Google Tracker for cross domain. */
			.each(function(){
				if (this.hostname === 'form.takami-bridal.com') {
					/* Override click. */
					this.onclick = function(){
						var tracker = _gaq._getAsyncTracker();
						win(tracker._getLinkerUrl(this.href));
						return false;
					};
				}
			})
		;

	});

}(jQuery.noConflict()));



/**
 * Open popup.
 */
function win(url) {
	window.open(url, 'popup2', 'status=yes,toolbar=no,location=yes,menubar=yes,scrollbars=yes,width=630');
}



