// Draggable and if logged in save box position to database
$( function() {
	$(".draggable").draggable({ 
		containment: '#HOME',
		handle: '.Title'	
	});
	
});

$(document).ready(function(){
	
	var Mwidth = screen.width;
	if ( Mwidth <= 1024 ) {
		// Background
		$('#BackGround').npFullBgImg("imgdir/background_s.jpg", {fadeInSpeed: 1000, center: true});
	} else {
		// Background
		$('#BackGround').npFullBgImg("imgdir/background.jpg", {fadeInSpeed: 1000, center: true});
	}
	
	/* SwapDepth */
	$(".Title").mousedown(function(){
		var tag = $(this).parent().attr("id");
		$("#" + tag ).css("zIndex", zIndex());
	});
	
	$("#Gnavi li").click(function(){
		var n = $(this).attr("class");
		if ( n == "open" ) {
			$(this).attr("class","close");
			$(this).css("backgroundPosition","left top");
		} else {
			$(this).attr("class","open");
		}
		return false;
	});
	
	$("#Gnavi li").hover(
		function(){
			$(this).css("backgroundPosition","left bottom");
		},
		function(){
			var n = $(this).attr("class");
			if ( n == "open" ) {
				$(this).css("backgroundPosition","left bottom");
			} else {
				$(this).css("backgroundPosition","left top");
		}
	});
	
	$(".draggable .Button a").click(function(){
		var n = $(this).parents(".Title").parent().attr("id").substring(1);
		$("#F"+n).hide();
		$("#G"+n).css("backgroundPosition","left top").attr("class","close");
		return false;
	});

	$("#Gnavi li a").click(function(){
		var n = $(this).parents("li").attr("class");
		var m = $(this).parents("li").attr("id").substring(1);
		if ( n == "open" ) {
			$("#F"+m).hide();
			$("#G"+m).css("backgroundPosition","left top").attr("class","close");
		} else {
			var f = $("iframe", "#F"+m).attr("src");
			if ( !f ) {
				switch( m ) {
					case "News": $("iframe", "#F"+m).attr("src","http://www.sonymusic.co.jp/Music/Arch/KS/PuffyAmiYumi/m_info.html"); $("#F"+m).css("top",yPosi(wNo)).css("marginLeft",xPosi(wNo)); break;
					case "Biography": $("iframe", "#F"+m).attr("src","http://www.puffyamiyumi.com/bio/content.html"); $("#F"+m).css("top",yPosi(wNo)).css("marginLeft",xPosi(wNo)); break;
					case "Tour": $("iframe", "#F"+m).attr("src","http://www.puffyamiyumi.com/tour/index.html"); $("#F"+m).css("top",yPosi(wNo)).css("marginLeft",xPosi(wNo)); break;
					case "Amigator": $("iframe", "#F"+m).attr("src","http://6109.jp/puffyamiyumi/?cate=1360"); $("#F"+m).css("top",yPosi(wNo)).css("marginLeft",xPosi(wNo)); break;
					case "Bbs": $("iframe", "#F"+m).attr("src","http://www.puffyamiyumi.com/bbs/index.html"); $("#F"+m).css("top",yPosi(wNo)).css("marginLeft",xPosi(wNo)); break;
					case "Staff": $("iframe", "#F"+m).attr("src","http://6109.jp/puffyamiyumi/?cate=1361"); $("#F"+m).css("top",yPosi(wNo)).css("marginLeft",xPosi(wNo)); break;
					case "Music": $("iframe", "#F"+m).attr("src","http://www.puffyamiyumi.com/music/index.html"); $("#F"+m).css("top",yPosi(wNo)).css("marginLeft",xPosi(wNo)); break;
					case "Link": $("iframe", "#F"+m).attr("src","http://www.puffyamiyumi.com/links/index.html"); $("#F"+m).css("top",yPosi(wNo)).css("marginLeft",xPosi(wNo)); break;
					case "Mail": $("iframe", "#F"+m).attr("src","http://www.puffyamiyumi.com/mail/index.html"); $("#F"+m).css("top",yPosi(wNo)).css("marginLeft",xPosi(wNo)); break;
				}
			}
			
			if ( !(m == "Fanclub") ) {
				$("#F"+m).css("zIndex", zIndex());
				$("#F"+m).show();
				$("#G"+m).css("backgroundPosition","left bottom").attr("class","open");
			}
		}
		return false;
	});
	
	
});

function infoClick() {
	var n = $("#GNews").attr("class");
	if ( n == "open" ) {
		$("#FNews").css("zIndex", zIndex());
	} else {
		var f = $("iframe", "#FNews").attr("src");
		if ( !f ) {
			$("#FNews").css("zIndex", zIndex()).css("top",yPosi(wNo)).css("marginLeft",xPosi(wNo));
		} else {
			$("#FNews").css("zIndex", zIndex());
		}
		$("#FNews").show();
		$("#GNews").css("backgroundPosition","left bottom").attr("class","open");
	}
}
	
var wNo = 1;

function xPosi(n){
//	var xPosi = -369 + ( n * 15 ) + "px";
	var xPosi = -500 + ( n * 15 ) + "px";
	wNo ++;
	return xPosi;
}

function yPosi(n){
	if ( n < 5 ) {
		var yPosi = 167 + ( n * 25 ) + "px";
	} else {
		var yPosi = 177 + ( (n-5) * 25 ) + "px";
	}
	return yPosi
}

function zIndex(){
	var z = 0;
	$(".draggable:visible").each(function(){
		var i = Number($(this).css("zIndex"));
		if ( z < i ) { z = i }
	});
	z = Number(z) + 1;
	return z;
}

