//Avoid flash of unformated content 
//Use in combination with the no-js-blink class
$('html').addClass('js');

$(document).ready( function()
{	
	$('#kaijo-information-name').css({ paddingTop: ($('#kaijo-information-container').height()- $('#kaijo-information-name').height()) / 2 });
	//$('.image-replace').jQIR();
	GNAVIWedding.imageRollover.init();
	
	switch( $('#main-photos-container li').length ){
		case 1:
			$('#main-photos-container li').css({ paddingLeft: 210, paddingRight: 210 });
			break;
		case 2:
			$('#main-photos-container li:eq(0)').css({ paddingLeft: 106 });
			$('#main-photos-container li:eq(1)').css({ paddingLeft: 28, paddingRight: 106 });
			break;
		case 3:
			$('#main-photos-container li:eq(1)').css({ paddingLeft: 29, paddingRight: 29 });
			break;
		case 4:
			$('#main-photos-container li:eq(1)').css({ paddingLeft: 29, paddingRight: 29 });
			$('#main-photos-container li:eq(3)').css({ paddingLeft: 210, paddingRight: 210 });
			break;
		case 5:
			$('#main-photos-container li:eq(1)').css({ paddingLeft: 29, paddingRight: 29 });
			$('#main-photos-container li:eq(3)').css({ paddingLeft: 106 });
			$('#main-photos-container li:eq(4)').css({ paddingLeft: 28, paddingRight: 106 });
			break;
		case 6:
			$('#main-photos-container li:eq(1)').css({ paddingLeft: 29, paddingRight: 29 });
			$('#main-photos-container li:eq(4)').css({ paddingLeft: 29, paddingRight: 29 });
			break;
	}
	$('#kuchikomi .toggle').hide();
	$('#kuchikomi .blog-link').toggle( function(){
		$('#kuchikomi .toggle').slideDown('slow');
		$('#kuchikomi .blog-link a').text('閉じる');
		return false;
	},
	function(){
		$('#kuchikomi .toggle').slideUp('slow');
		$('#kuchikomi .blog-link a').text('八芳園を話題にしているブログをもっと表示');
		return false;
	});
	


	//support for images under the flash gallery 
	$('#main-photos-container li').each( function(){
	$(this).css({ textAlign: 'center' });
	});

	whatImage = 0;
	customHeight = 0;
	customHeight2 = 0;
	
	$('#main-photos-container li').each( function(){
		if( whatImage++ < 3 ){
			totalHeight = $(this).find('img').height() + $(this).find('.text').height();
			if( totalHeight > customHeight ){
				customHeight = totalHeight;
			}
		}
		else{
			totalHeight = $(this).find('img').height() + $(this).find('.text').height();
			if( totalHeight > customHeight2 ){
				customHeight2 = totalHeight;
			}
		}
	});

	whatImage = 0;
	extraPadding = 0;
	if( $('#main-photos-container li').length > 3 ){
		extraPadding = 20;
	}
	$('#main-photos-container li').each( function(){
		if( whatImage++ < 3 ){ 
			$(this).height( customHeight + extraPadding );
		}
		else{
			$(this).height( customHeight2 );
		}
	});



	/*Block pattern support javascript*/

	//support for block pattern 6
	$('.block-style6-container').each( function(){
		style6width = 0;
			
		$(this).find('div img').each( function(){
			if( $(this).width() > 185 ){
				$(this).width( 185 );
			}
			style6width += $(this).width();
		});
		$(this).find('ul').width( style6width );
	});
	
	//support for block pattern 5
	$('.block-style5-container').each( function(){
		$(this).find('.top').each( function(){
			style5topheight = 0;
			targetImage = $(this).find('img');
			style5topheight = targetImage.height();	
			targetImage.css({ paddingTop: 200-style5topheight });
		});
	});
	
	//support for block pattern 2
	$('.block-style2-container').each( function(){
		first_image_height = $(this).find('ul li:eq(0) img').height();
		second_image_height = $(this).find('ul li:eq(1) img').height();

		if( first_image_height > second_image_height ){
			$(this).find('ul li:eq(1) img').css({ paddingTop: first_image_height-second_image_height+'px' });
		}
		else if( first_image_height < second_image_height ){
			$(this).find('ul li:eq(0) img').css({ paddingTop: second_image_height-first_image_height+'px' });
		}
	});
	
	//Automatically centers the content for block pattern 3
	customStyle = $('.block-style3-container');
	if( customStyle.length > 0  ){
		customStyle.each( function(){
			totalSize = 0;
			$(this).find('img').each( function(){
				totalSize += $(this).attr('width');
			});
			$(this).css({
				width: totalSize,
				margin: '0px auto'
			});
		});
	}
	/*Block pattern support javascript*/
	
	$('#main-base-info-container #toggle-widget').append('<span id="show-all-list" class="open-widget xs"><a href="#">会場データをもっと表示</a></span>');
	$('#main-base-info-container .toggle').hide();
	$('#show-all-list a').toggle( function(){
		$('#main-base-info-container .toggle').slideDown('slow');
		$('#show-all-list a').text('閉じる');
		$('#show-all-list').addClass('close-widget').removeClass('open-widget');
		return false;
	},
	function(){
		$('#main-base-info-container .toggle').slideUp('slow');
		$('#show-all-list a').text('会場データをもっと表示')
		$('#show-all-list').addClass('open-widget').removeClass('close-widget');
		return false;
	});
	
	$('#kaijo-information-contact a').click( function(){
		popup = $('#main-about-telephone');
		if( popup.hasClass( 'hide-if-js' ) ){
			popup.removeClass('hide-if-js');
			popup.hide();
		}
		pos = $(this).position();
		popup.css({
			position: 'absolute',
			top: pos.top,
			left: pos.left,
			width: 200,
			backgroundColor: '#fff',
			border: '5px solid #ed4b79',
			paddingBottom: 0,
			color: '#684f34',
			cursor: 'pointer',
			level: 2
		});
		popup.find('#main-about-telephone-container').css({
			border: '0px solid'
		});
		popup.find('strong').css({
			borderBottom: '1px solid #c3bc9c',
			color: '#ed4b79',
			display: 'block',
			marginBottom: 10
		});
		if( popup.find('.close').length == 0 ){
			popup.find('p').append('<p class="close"><a href="#" onclick="return false;">閉じる</a></>');
			popup.find('.close').css({
				borderTop: '1px solid #c3bc9c',
				display: 'block',
				marginTop: 10
			});
		}
		popup.slideDown('200');
		popup.click( function(){
			$(this).slideUp('200');
		});
		return false;
	});
	$('#favorite-help').click( function(){
		popup = $('#main-about-favorite');
		if( popup.hasClass( 'hide-if-js' ) ){
			popup.removeClass('hide-if-js');
			popup.hide();
		}
		pos = $(this).position();
		popup.css({
			position: 'absolute',
			top: pos.top,
			left: pos.left-110,
			width: 200,
			backgroundColor: '#fff',
			border: '5px solid #ed4b79',
			paddingBottom: 0,
			color: '#684f34',
			cursor: 'pointer',
			level: 2
		});
		popup.find('#main-about-favorite-container').css({
			border: '0px solid'
		});
		popup.find('strong').css({
			borderBottom: '1px solid #c3bc9c',
			color: '#ed4b79',
			display: 'block',
			marginBottom: 10
		});
		if( popup.find('.close').length == 0 ){
			popup.find('p').append('<p class="close"><a href="#" onclick="return false;">閉じる</a></>');
			popup.find('.close').css({
				borderTop: '1px solid #c3bc9c',
				display: 'block',
				marginTop: 10
			});
		}
		popup.slideDown('200');
		popup.click( function(){
			$(this).slideUp('200');
		});
		return false;
	});
	$('#sendmobile').click( function(){
		window.open(this.href, '_blank','width=700,height=550');
		return false;
	});
});

GNAVIWedding = {};

GNAVIWedding.imageRollover =
{
	init: function(){
		
		this.preload();

		$(".rollover").hover(
			function () { $(this).find("img").attr( 'src', GNAVIWedding.imageRollover.newimage($(this).find("img").attr('src')) ); },
			function () { $(this).find("img").attr( 'src', GNAVIWedding.imageRollover.oldimage($(this).find("img").attr('src')) ); }
		);
	},

	preload: function(){
		$(window).bind('load', function() {
			$('.rollover').each( function( key, elm ) { $('<img>').attr( 'src', GNAVIWedding.imageRollover.newimage( $(this).find("img").attr('src') ) ); });
		});
	},

	newimage: function( src ){
		return src.substring( 0, src.search(/(\.[a-z]+)$/) ) + '_OV' + src.match(/(\.[a-z]+)$/)[0];
	},

	oldimage: function( src ){
		return src.replace(/_OV\./, '.');
	}
};