/* 
// #########################
// # >_JR9th
// # >_jQuery.aRose (Function)
// #########################
*/


$(function() {
	
	// Set WaterMark
	$("#SMSG").Watermark("Share Something...");		
	$("#SIMG").Watermark("Example : http://www.osk101.com/img/logo.jpg");		
	$("#SVDO").Watermark("Example : Dtuur1q-GhU");		


	// MSG Focus
	$("#SMSG").focus(function() {
		$(this).height(50);
		$(this).elastic();
		$(this).trigger('update');
		$("#ShareBox").show();
			
	return false;
	});


	// Example
	$(document).ready(function() {
		
		// Ex IMG
		$("#SIMG").keyup(function() {
		
			var IMG=$(this).val();
				
				if(IMG.length > 0) {
					$("#ExIMG").show();
					$("#ExIMG").html("<img src='"+IMG+"' width='120' border='0' />");
				} else {
					$("#ExIMG").hide();
				}
		return false;
		});		
		
		// Ex VDO
		$("#SVDO").keyup(function() {

			var VDO=$(this).val();
				
				if(VDO.length > 0) {
					$("#ExVDO").show();
					$("#ExVDO").html("<img src='http://img.youtube.com/vi/"+VDO+"/2.jpg' width='120' border='0' />");
				} else {
					$("#ExVDO").hide();
				}
		return false;
		});
				
	});


	// Type IMG Click
	$("#TypeIMG").click(function() {
		$("#BoxIMG").toggle();
		$("#SVDO").val('');
		$("#SVDO").blur();
		$("#ExVDO").hide();
		$("#BoxVDO").hide();
	return false;
	});

	// Type VDO Click
	$("#TypeVDO").click(function() {
		$("#BoxVDO").toggle();
		$("#SIMG").val('');
		$("#SIMG").blur();
		$("#ExIMG").hide();
		$("#BoxIMG").hide();
	return false;
	});


	// Info IMG
	$("#FAQIMG").click(function() {
		$("#InfoIMGBox").toggle();
	return false;
	});
	
	// Info VDO
	$("#FAQVDO").click(function() {
		$("#InfoVDOBox").toggle();
	return false;
	});


	// Share Add.
	$("#SEND").click(function() {

		var element = $(this);   
		var SMSG = $("#SMSG").val();
		var SIMG = $("#SIMG").val();
		var SVDO = $("#SVDO").val();
		var SBY = $("#SBY").val();	
				
    	var dataString = 'SMSG='+ SMSG +'&SIMG=' + SIMG + '&SVDO=' + SVDO +'&SBY=' + SBY;
			
		if(SMSG=='Share Something...' || SMSG=='') {
			alert(">_ไม่พบข้อมูล ที่ต้องการส่ง !");
		} else {
			
			$("#SLoad").show();
			$("#SLoad").fadeIn(400).html('<img src="img/loading/loading.gif">');

			$.ajax({
				type: "POST",
  				url: "source/Add.Share.php",
   				data: dataString,
  				cache: false,
  				success: function(html){
 
  					$("#ShareUpdate").prepend(html);
  					$("#ShareUpdate").slideDown("slow");
   					$("#SMSG").val('');
					$("#SMSG").blur();
   					$("#SIMG").val('');
					$("#SIMG").blur();
   					$("#SVDO").val('');
					$("#SVDO").blur();
					$("#ExIMG").hide();
					$("#ExVDO").hide();
					$("#BoxIMG").hide();
					$("#BoxVDO").hide();
					$("#SLoad").hide();
	
  				}
 			});
		}
	return false;
	});


	// Show Box (IMG)
	$(".ShowIMG").live("click", function() {
		
		var ID = $(this).attr("id");
		
		var max_size = 400;
		
		$("#PIMG"+ID).each(function(i) {
		
			if ($(this).height() > $(this).width()) {
    			var h = max_size;
    			var w = Math.ceil($(this).width() / $(this).height() * max_size);
  			} else {
    			var w = max_size;
    			var h = Math.ceil($(this).height() / $(this).width() * max_size);
  			}
  				$(this).css({ height: h, width: w });
			});

		$("#IMGShowBox"+ID).toggle();
		$("#ShareIMGMSG"+ID).toggle();
	
	return false;
	});	
	
	// Show Box (YouTube)
	$(".ShowVDO").live("click", function() {
		
		var ID = $(this).attr("id");
	
		$("#VDOShowBox"+ID).show();
		$("#ShareVDOMSG"+ID).hide();
	
	return false;
	});	


	// Join
	$(".JoinShowAdd").live("click", function() {
		
		var element = $(this);
		var ID = element.attr("id");
		
		$("#Join"+ID).toggle();
		$("#JMSG"+ID).Watermark("Say Something...");	
		
		$("#JMSG"+ID).focus(function() {
			$("#Jimg"+ID).show();
			$("#Jimg5"+ID).show();
			$("#Jsend"+ID).show();
			$("#JMSG"+ID).height(30);
			$("#JMSG"+ID).elastic();
			$("#JMSG"+ID).trigger('update');
		});
	return false;
	});	
		
	// Join Add.
	$(".JoinAdd").live("click", function() {
		
	var element = $(this);
	var ID = element.attr("id");

	var JMSG= $("#JMSG"+ID).val();
	var JBY= $("#JBY").val();
	
	var dataString = 'JMSG='+ JMSG + '&SID='+ ID +'&JBY='+ JBY;
						
		if(JMSG=='Say Something...' || JMSG=='') {
			alert(">_ไม่พบข้อมูล ที่ต้องการส่ง !");
		} else {

		$("#JLoad"+ID).show();
		$("#JLoad"+ID).html('<img src="img/loading/loading.gif" />');

			$.ajax({
				type: "POST",
				url: "source/Add.Join.php",
				data: dataString,
				cache: false,
				success: function(html){
					$("#JoinUpdate"+ID).append(html);
					$("#JMSG"+ID).val('');
					$("#JMSG"+ID).blur();
					$("#JLoad"+ID).hide();
 				}
 			});
		}
	return false;
	});	


	// View All Comment
	$(".ViewAll").live("click", function()  {

	var element = $(this);
	var ID = element.attr("id");
	
		$.ajax({
			type: "POST",
			url: "source/Show.Join.php",
			data: "SID="+ ID, 
			cache: false,
			success: function(html){
			
				$("#View"+ID).prepend(html);
				$("#ViewAll"+ID).remove();
				$("#ViewJoinShow"+ID).remove();
				
			}
		});

	return false;
	});

	// Show More Post.
	$('.more').live("click",function() {
		var ID = $(this).attr("id");
		if(ID) {
			$("#more"+ID).html('<img src="img/loading/loading.gif" />');
			$.ajax({
				type: "POST",
				url: "source/Show.More.php",
				data: "LastMSG="+ ID, 
				cache: false,
				success: function(html) {
					
					$("#ShowMore").append(html);
					$("#more"+ID).remove();
				
				},
			});
		} else {
			$(".ShowMore_Area").html('The End Of Data');
		}
	return false;
	});
	
});
