// JavaScript Document by Sora_Amaori

	$(document).ready(function(){
		//Menu Button
		//MouseOver
		$("#BTN_TOP").mouseover(function(){ $(this).css({background:"url(img/btn_top_on.gif)"}); });
		$("#BTN_CONTACT").mouseover(function(){ $(this).css({background:"url(img/btn_profile_on.gif)"}); });
		$("#BTN_GA").mouseover(function(){ $(this).css({background:"url(img/btn_ga_on.gif)"}); });
		$("#BTN_GALLERY").mouseover(function(){ $(this).css({background:"url(img/btn_gallery_on.gif)"}); });
		$("#BTN_DOUJIN").mouseover(function(){ $(this).css({background:"url(img/btn_doujin_on.gif)"}); });
		$("#BTN_MS").mouseover(function(){ $(this).css({background:"url(img/btn_magicstore_on.gif)"}); });
		//MouseOut
		$("#BTN_TOP").mouseout(function(){ $(this).css({background:"url(img/btn_top.gif)"}); });
		$("#BTN_CONTACT").mouseout(function(){ $(this).css({background:"url(img/btn_profile.gif)"}); });
		$("#BTN_GA").mouseout(function(){ $(this).css({background:"url(img/btn_ga.gif)"}); });
		$("#BTN_GALLERY").mouseout(function(){ $(this).css({background:"url(img/btn_gallery.gif)"}); });
		$("#BTN_DOUJIN").mouseout(function(){ $(this).css({background:"url(img/btn_doujin.gif)"}); });
		$("#BTN_MS").mouseout(function(){ $(this).css({background:"url(img/btn_magicstore.gif)"}); });
		//MouseClick
		$("#BTN_TOP").mousedown(function(){
			LOADURL("top.html","TOP");
		});
		$("#BTN_CONTACT").mousedown(function(){
			LOADURL("profile.html","PROFILE");
		});
		$("#BTN_GA").mousedown(function(){
			LOADURL("apps.html","APPS");
		});
		$("#BTN_GALLERY").mousedown(function(){
			LOADURL("gallery.html","GALLERY");
		});
		$("#BTN_DOUJIN").mousedown(function(){
			LOADURL("doujin.html","DOUJIN");
		});
		$("#BTN_MS").mousedown(function(){
			LOADURL("magicstore.html","MAGICSTORE");										
		});
		
		//Start Page
		LOADURL("top.html","TOP");
		
	});
	
	//===================LOADING FUNCTION===========================
		//====Load MAIN====
		function LOADURL(URL, TAR){
			switch (TAR){
			case "TOP":
				$("#MAIN").load(URL, TOPFUN);
				break;
			case "PROFILE":
				$("#MAIN").load(URL);
				break;
			case "APPS":
				$("#MAIN").load(URL,APPSFUN);
				break;		
			case "GALLERY":
				$("#MAIN").load(URL,GALLERYFUN);
				break;		
			case "DOUJIN":
				$("#MAIN").load(URL,DOUJINFUN);
				break;		
			case "MAGICSTORE":
				$("#MAIN").load(URL);
				break;	
			default:
				$("#MAIN").load(URL);
			}
		}
		//====Load Topblog====
		function LOADTOPBLOG(PAGE){
			$("#TOP_PHP").load("topblog/top.php?page="+PAGE, TOPBLOGFUN);
		}
	
	//===================SUMMIT FORM FUNCTION=======================
	function ACTIVEREPLY(O,I){
		var FORM = $(".article:eq("+O+")").find(".tform");
		
		//=======set up the form=======
		var EXAMPLE_NAME = "Your Name.";
		var EXAMPLE_REPLY = "Your Message.";
		
		FORM.find(".tform_input").css('color','#999');
		
		FORM.find(".tform_input").each(function(i){
			switch(i){
				case 0: //name input
					$(this).val(EXAMPLE_NAME);	
					$(this).focusin(function(){
						if($(this).val() == EXAMPLE_NAME){
							$(this).css('color','#000');
							$(this).val("");
						}
					});
					$(this).focusout(function(){
						if($(this).val() == ""){
							$(this).val(EXAMPLE_NAME);
							$(this).css('color','#999');
						}
					});
					break;
				case 1: // reply input
					$(this).val(EXAMPLE_REPLY);
					$(this).focusin(function(){
						if($(this).val() == EXAMPLE_REPLY){
							$(this).css('color','#000');
							$(this).val("");
						}
					});					
					$(this).focusout(function(){
						if($(this).val() == ""){
							$(this).val(EXAMPLE_REPLY);
							$(this).css('color','#999');
						}
					});					
					break;
			}
		});
		//====validation and sending====
		FORM.find(".tform_send").mousedown(function() {
				
			var hasError = new Boolean(false);
			
		//get data
			var send_reply = FORM.find(".tform_input:eq(1)").val();
			if(send_reply == EXAMPLE_REPLY | send_reply == ""){
				FORM.find(".tform_input:eq(1)").css('color','#FF0000');
				hasError = true;
			}
	     	var send_name = FORM.find(".tform_input:eq(0)").val();
			if(send_name == EXAMPLE_NAME | send_name == ""){
				FORM.find(".tform_input:eq(0)").css('color','#FF0000');
				hasError = true;
			}
			
			if(hasError == true){return false;}
			
		//send data
			send_name = send_name.replace(/&/gi,'@#$');
			send_reply = send_reply.replace(/&/gi,'@#$');
			var dataString = 'id='+I+'&name='+ send_name + '&reply=' + send_reply;
		  	
			//alert (dataString);return false;  //Just for Testing
			
			$.ajax({
    			type: "POST",
			    url: "topblog/reply_process.php",
			    data: dataString,
			    success: function() {
							var tt = parseInt($(".article:eq("+O+")").find(".replyreturntotal").attr('href'));
							$(".article:eq("+O+")").find(".treply").attr('text','Comment ('+(tt+1)+')');
							$(".article:eq("+O+")").find(".tcomment").load("topblog/reply.php?id="+I,function(){
								ACTIVEREPLY(O,I);
							});
						 },
				error: function(){
					   	FORM.empty();
						FORM.html("Server Error.");
					   }
			});
	    });
	}
	
	//===================FUNCTION===========================
	//Top
	function TOPFUN(){
		//about content
		$("#TOP_ABOUT_BOX").hide();
		$("#TOP_T_R").html("Click and Readme");
		$("#TOP_ABOUT").mousedown(function(){
			$("#TOP_ABOUT_BOX").slideToggle(300);
			if ($("#TOP_T_R").html() == "Click and Readme"){
				$("#TOP_T_R").html("Close");
			} else {
				$("#TOP_T_R").html("Click and Readme");
			}
		});
		//link
		var CurrentLink = -1;
		$(".link_box").each(function(i){
			$(this).find(".link_content").hide();
			$(this).find(".link_menu").attr("linkid",i);
		});
		$(".link_menu").mousedown(function(){
			if($(this).attr("linkid") == CurrentLink){
				$(".link_content:eq("+CurrentLink+")").slideUp(200);
				CurrentLink = -1;
			} else {
				CurrentLink = $(this).attr("linkid");					   
				$(".link_content:eq("+CurrentLink+")").slideDown(200);
				$(".link_content").not(":eq("+CurrentLink+")").slideUp(200);
			}
		});		
		//topblog
		LOADTOPBLOG(1);		
	}
	
	
	//Topblog
	function TOPBLOGFUN(){
		//scorll the page to top when topblog is loaded
		$('html, body').animate({scrollTop:0}, 300);
		
		//reply button and display replay message
		$(".treply").each(function(i){
			$(this).attr('isOpen','CLOSE');
			$(this).attr('text',$(this).html());				
			$(".article:eq("+i+")").find(".tcomment").hide();
			$(this).mousedown(function(){ // clink on button
				if($(this).attr('isOpen') == "CLOSE"){
					var a = $(".article:eq("+i+")").attr('href');
					$(".article:eq("+i+")").find(".tcomment").load("topblog/reply.php?id="+a, function(){ //Open
																									   
						var tt = parseInt($(".article:eq("+i+")").find(".replyreturntotal").attr('href'));
						$(".article:eq("+i+")").find(".treply").attr('text','Comment ('+tt+')');
						
						$(".treply:eq("+i+")").attr('isOpen','OPEN');
						$(".treply:eq("+i+")").html('Close');
						$(".article:eq("+i+")").find(".tcomment").slideDown(300, function(){//animation
							ACTIVEREPLY(i,a); //activite the ajax form
						}); 
						
					});
				} else {
					$(".article:eq("+i+")").find(".tcomment").slideUp(300,function(){ //Close
						$(this).empty();
					});
					$(this).attr('isOpen','CLOSE');
					$(this).html($(this).attr('text'));						
				}
			});
		});
		
		//page button
		$(".php_page").mousedown(function(){
			var t = $(this).html();
			LOADTOPBLOG(t);
		});
	}
	
	
	//Apps
	function APPSFUN(){
		$(".apps_sc").each(function(i){
			$(this).find("a").lightBox();
		});
	}
	
	
	//Gallery
	function GALLERYFUN(){
		//activate lightbox
		$(".gallery").each(function(i){
			$(this).find("a").lightBox();
		});
		//gallery_preview
		$(".gallery_preview").each(function(i){
			//set img width
			var divwidth = new Number();
			divwidth = $(this).width();
			$(this).find("img").attr("width",divwidth);
			//set img src
			var tempsrc = new Number();
			tempsrc = $(".gallery:eq("+i+")").find("a:eq(0)").attr("href");
			$(this).find("img").attr("src",tempsrc);
			//set div height
			var divheight = new Number();
			divheight = $(".common_content:eq("+i+")").height();
			$(this).height(divheight+10);
			
		});
	}
	
	
	//DOUJIN
	function DOUJINFUN(){
		//slide
		$(".doujin_content").hide();
		var doujinselect = -1;
		$(".doujin_point").each(function(i){
			$(this).mousedown(function(){
				if(doujinselect==i){
					$(".doujin_content").slideUp(300);
					doujinselect = -1;
				} else {
					doujinselect = i;
					$(".doujin_content").not("eq("+i+")").slideUp(300);
					$(".doujin_content:eq("+i+")").slideDown(300);
				}
			});
		});
		//lightbox
		$(".doujin_gallery").each(function(i){
			$(this).find("a").lightBox();
		});
	}
