// JavaScript Document
$(document).ready(function() {
	$('div[id="lightbox"]').css('position', 'fixed');
	$('div[id="lbcontent"]').css('float', 'none');
	$('.getcalendar').removeClass('hide');
	$('.getcalendar').addClass('inline');
	// display image lightbox
  	$("a[id^='img-']").bind('click', function(){
		var linkid =$(this).attr('id');
		var phppath = "/system/snipplets/lbimage.php";
		var imgpath = $('#' + linkid +" img").attr('src');
		var imgalt = $('#' + linkid + " img").attr('alt');
		animateLB();
		$('#lightbox-image').css('display', 'inline-block');
		$('#lbcontent-image').css('display', 'inline-block');
		$('.closeit a').attr('href', "#" + linkid);
		$.post(phppath, {'imgurl' : imgpath, 'imgalt' : imgalt, 'linkid' : linkid}, function(data) {
			$('#lbcontent-image').html(data);
		});
	});
	// post box
  	$("#add-post").bind('click', function(){
		animateLB();
		$('#lightbox-post').css('position', 'fixed');
		$('#lightbox-post').css('display', 'block');
		$('#lbcontent-post').css({'display' : 'inline-block', 'float' : 'none'});
		$('.closeit a').attr('href', '#add-post');
	});
	
	// organizational calendar
	$('.calendar a').bind('click', function(){
		$(this).attr('href', '#lightbox-calendar');
		window.location = '#lightbox-calendar';
		var linkid = $(this).attr('id');
		var date = linkid.replace(/^d/, '');
		var phppath = '/system/snipplets/getevent.php';
		animateLB();
		$('#lightbox-calendar').css('position', 'fixed');
		$('#lightbox-calendar').css('display', 'block');
		$('#lbcontent-calendar').css('display', 'inline-block');
		$('.closeit a').attr('href', '#' + linkid);
		$.post(phppath, {'date' : date}, function(data) {
			$('#lbcontent-calendar').html(data);
		});
	});
	// ปฏิทินสำหรับเลือกวันที่
	$('.getcalendar').bind('click', function(){
		var dateinput = $('.receivedate').val();
		var dateid = $('.receivedate').attr('id');
		animateLB();
		$('#lightbox').css('display', 'inline-block');
		$('#lbcontent').css('display', 'inline-block');
		$('.closeit a').attr('href', '#' + dateid);
		$.post("/system/snipplets/calendar.php", {'date' : dateinput}, function(data){
			$('#lbcontent').html(data);
		});
		return true;
	});
	$('#feat_pic').bind('change', function(){
		var img = $(this).val();
		$('#sampleicon').remove();
		var tag = $('<img />').attr('src', '/system/image.php?f=' + img+'&w=50&h=50').attr('alt', 'ตัวอย่างไอค่อน').attr('id', 'sampleicon');
		tag.css('margin', '0 0 0 5px');
		tag.insertAfter($(this));
	});
	// subscribe e-mailing list
	$('div[id^="asidemenu"] a').bind('click', function(){
		if($(this).text() == 'รับข่าวสารจากเรา'){
			$(this).attr("href", "#lbcontent");
			window.location = '#lbcontent';
			//var nextid = $(this).parent('li').parent('ul').parent('div').next('div').attr('id');
			var thisid = $(this).attr('id', 'goback');
			$('.closeit a').attr('href', '#' + thisid.attr('id'));
			$('#lightbox').show();
			$('#lbcontent').slideDown('slow');
			$('#lbcontent').css('display', 'inline-block');
			$('#lbcontent').load('/system/subscribe.php');
		}
	});
	// close button
	$('*[class*="closeit"] a, #cancelform').bind('click', function(){
		$('#preview').fadeTo('slow', 0);
		$('#media').fadeTo('slow', 0);
		$('div[id^="lightbox"]').hide('slow');
		$('div[id^="lbcontent"]').hide('slow');
		$('#preview').hide();		
		$('#media').hide();
		window.location = $(this).attr('href');
		return false;
	});
	// format text area :::::::::::::::::::::::::::::::::::::::::::::::::
	// open the media box
	$('#getmedia').bind('click', function(){
		$('#media').slideDown('slow');
		$('#media').css('display', 'inline-block');
		$('div[id^="lbcontent"]').css('float', 'left');
		$('#preview').hide();
	});
	// add more text box
	$('a[id^="add"]').bind('click', function(){
		var itemnum = parseFloat($(this).attr('id').replace(/^add/, ''));
		$('#item'+(itemnum+1)).show();
		$('#add'+itemnum).hide();
		$('#remove'+itemnum).hide();
	});
	// remove text box
	$('a[id^="remove"]').bind('click', function(){
		var itemnum = parseFloat($(this).attr('id').replace(/^remove/, ''));
		$('#item'+itemnum).hide();
		$('#add'+(itemnum-1)).show();
		$('#remove'+(itemnum-1)).show();
		$('#m'+itemnum).attr('value', '');
		$('#d'+itemnum).attr('value', '');
	});
	// cancel the media box
	$('#cancelmedia').bind('click', function(){
		$('#media').slideUp('slow');
		$('#media').hide();
		$('div[id^="lbcontent"]').css('float', 'none');
	});
	// accept media 
	$('#acceptmedia').bind('click', function(){
		var allitem = $('li[id^="item"]').length;
		var checked = $('input[name="mtype"]:checked').val();
		var format = "";
		// 1=url, 2=img:l, 3=img:r, 4=thumbnail, 5=video, 6=soundcloud
		for(i=1; i <= allitem; i++){
			var url = $('#m'+i).val();
			var desc = $('#d'+i).val();
			if(url != '' && desc != ''){
				if(checked==1){
						format += ' [url=' + url + ']' + desc + '[/url] ';
				}else if(checked==2){
					format += ' [img:l=' + url + ']' + desc + '[/img] ';	
				}else if(checked==3){
					format += ' [img:r=' + url + ']' + desc + '[/img] ';	
				}else if(checked==4){
					format += '\r\n[p class((aligncenter))][img=' + url + ']' + desc + '[/img][/p]\r\n';	
				}else if(checked==5){
					format += '([img=' + url + ']' + desc + '[/img])\r\n';	
				}else if(checked==6){
					format += '\r\n[video=' + url + ']' + desc + '[/video]\r\n';	
				}else if(checked==7){
					format += '\r\n[soundcloud=' + url + ']' + desc + '[/soundcloud]\r\n';	
				}
			}
		}
		if(checked==5  && format != ''){
			format = '\r\n[thumb]\r\n' + format + '[/thumb]\r\n';	
		}
		$('.receiveformat').val($('.receiveformat').val()+format); 
		$('#media').slideUp('slow');
		$('#media').hide();
		$('div[id^="lbcontent"]').css('float', 'none');
	});
	// preview
	$('.receiveformat').bind('keyup change', function(){
		var backid = $(this).attr('id');
		$('#preview').one().fadeIn('slow').slideDown('slow');
		$('#preview').show();
		$('#media').hide('slow');
		$('div[id^="lbcontent"]').css('float', 'left');
		$.post('/system/snipplets/preview.php', {'preview' : $(this).val(), 'backid' : backid}, function(data){
			$('#preview').html(data);
		});
	});
	var delay = (function(){
	  var timer = 0;
	  return function(callback, ms){
		clearTimeout (timer);
		timer = setTimeout(callback, ms);
	  };
  })();
  // lightbox animation show()
  var animateLB = function(){
		$('div[id^="lbcontent"]').fadeTo(800, 1.0);  
  }
});
