var flag = false;
var mflag = false;
var height,width;
$(document).ready(function(){
	init_samples();
	eventSet();
	appender();
	//setNavi("../");
});
function init_samples()
{
//	$.ajax({
//		url: 'enstructed.php',
//		type: 'POST',
//		success: function(msg)
//		{
//			$('#sampleViewer').html(msg);
//		}
//	});
	$(".panel:even").css("clear","both");
	$(".panel")
	.hover(function()
	{
		var id = $(this).attr("id");
		$(this).animate(
		{
			width:"250px",
			height:"250px"
		});
//		alert($("#"+id+" > dd > img").html())
		$("#"+id+" > dd > img").animate(
		{
			width:"200px",
			height:"200px"
		});
	},function()
	{
		var id = $(this).attr("id");
		$(this)
			.stop()
			.css({
				width:"200px",
				height:"200px"
			});
		$("#"+id+" > dd > img")
			.stop()
			.css({
				width:"150px",
				height:"150px"
			});
	})
	.click(function()
	{
		var id = $(this).attr("id");
		$("#lightBox").html($("#"+id+"lightbox").html());
		$("#lightBox p").append('<br /><a href="'+ $('#'+id+'link').val() + '">このページに移動する</a>');

		showLightBox();
	});
}
function appender()
{
	$('body')
		.append('<div id="simplealert"></div>');

	$('#simplealert')
		.append('<p id="simplealertmsg"></p>')
		.append('<p id="simplealertok"></p>');
	setSimpleAlert();
}

function eventSet() {
	$("#lightBack")
	.fadeTo(0,0)
	.click(function()
	{
		closeLightBox();
	});
	$("#call").click(function() {
		if(mflag == false && flag == false) {
			$(this).animate({"left":"0px"},function() {
				mflag = true;
			});
		} else {
			$(this).animate({"left":"300px"},function() {
				mflag = false;
			});
		}
	});
	$("#send").click(function() {
		sendMailAddr();
	});
}
function sendMailAddr() {
	if (!sendValidate() ) return;
	$.ajax(
	{
		url: 'maddsender.php',
		type: 'POST',
		data: {'mailaddress':$('#addressform').val()},
		success: function()
		{
			showSimpleAlert("送信完了！");
			$('#addressform').val('');
		},
		error: function()
		{
			showSimpleAlert("送信エラー");
		}
	});
}
function showLightBox()
{
	$('select').hide();
	height = document.documentElement.scrollHeight;
	width = document.documentElement.clientWidth;
	$("#lightBack").height(height).width(width).show().fadeTo(300,0.8,function()
	{
		if (navigator.userAgent.match(/AppleWebKit\/\d.+Safari\/\d.+/))
		{
			var scrollTop = document.body.scrollTop;
		}
		else
		{
			var scrollTop = document.documentElement.scrollTop;
		}

	//	var scrollTop = $('body:first').scrollTop();
		var top = (document.documentElement.clientHeight - $('#lightBox').height()) / 2 + scrollTop;
		var left = ($('html').width() - $('#lightBox').width()) / 2;
		$('#lightBox').css('top', top).css('left', left).show();

//		alert("bodyheight"+$('body:first').height()+"\nheight"+$('#lightBox').height()+"\n scrlTop" + document.documentElement.scrollTop + '\n clientheight' + document.documentElement.clientHeight);
	});
}

function closeLightBox()
{
	$("#lightBack")
	.fadeTo(0,0)
	.hide();
	$("#lightBox").hide();
	$('select').show();

}
function showSimpleAlert(msg)
{
	height = document.documentElement.scrollHeight;
	width = document.documentElement.clientWidth;
	$('#simplealert').height(height).width(width);
	$('#simplealertmsg').text(msg);
	$('#simplealert').fadeIn(200);
	$('#simplealertok')
		.hover(function()
		{
			$(this).css('backgroundImage','url(imgs/btnform2.png)');
		},
		function()
		{
			$(this).css('backgroundImage','url(imgs/btnform.png)');
		})
		.click(function()
		{
			$('#simplealert').fadeOut(200);
		});
}

function setSimpleAlert()
{
	$('#simplealert')
		.css('position','absolute')
		.css('top','0')
		.css('left','0')
		.css('zIndex','3')
		.css('backgroundColor','#000')
		.css('opacity','0.9')
		.css('textAlign','center')
		.hide();
	$('#simplealertmsg')
		.css('width','100%')
		.css('color','#fff')
		.css('marginTop', '25%')
	$('#simplealertok')
		.css('width','40px')
		.css('height','25px')
		.css('margin','0 auto')
		.css('backgroundImage','url(imgs/btnform.png)')
		.css('backgroundRepeat','no-repeat')
		.css('cursor','pointer')
}
function sendValidate() {
	var isFailed = true;
	if($("#addressform").val().
			search(/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/)
				== -1)	{
		$("#addressform").css("backgroundColor","#f99");
		isFailed = false;
	}
	return isFailed;
}