$(document).ready(function(){
	$('#js-go_id').click(
		function()
		{
			var db_host	=	$('#js-db-host-id').val();		
			var db_port	=	$('#js-db-port-id').val();		
			var db_user	=	$('#js-db-user-id').val();		
			var db_pass	=	$('#js-db-pass-id').val();		
			$.ajax({
					url			:	"index.php?setup=get_database&db_host="+encodeURIComponent(db_host)+"&db_port="+encodeURIComponent(db_port)+"&db_user="+encodeURIComponent(db_user)+"&db_pass="+encodeURIComponent(db_pass),
					type			:	'GET',
					dataType		:	'json',
					success		:	function(json){
						var database_data	=		json;
						if(database_data=='no connect'){
							alert(_INSTALL_LANG['database_cant_be_connected']);
							return;
						}else{
							$('#js-db-list-id')[0].options.length=0;	
							$("#js-db-list-id")[0].options.add(new Option(_INSTALL_LANG['database_in_all'].replace('$1',database_data.length),''));
							$("#js-db-list-id")[0].onchange	=	function(){
								$('#js-db-name-id').val(this.value);
							}
							for(i in database_data){
								var option_obj			=		new Option(database_data[i],database_data[i]);
								$("#js-db-list-id")[0].options.add(option_obj);
							}	
						}	
					}
				}
			);
		}	
	);


	$('#js-install-at-once').click(
		function(){
			if(jQuery.trim($("#js-db-host-id").val())==''){
				alert(_INSTALL_LANG['host_of_database_cant_be_empty']);
				$("#js-db-host-id")[0].focus();
				return;	
			}
			if(jQuery.trim($("#js-db-user-id").val())==''){
				alert(_INSTALL_LANG['username_of_database_cant_be_empty']);
				$("#js-db-user-id")[0].focus();
				return;	
			}	
			if(jQuery.trim($("#js-db-name-id").val())==''){
				alert(_INSTALL_LANG['database_cant_be_empty']);
				$("#js-db-name-id")[0].focus();
				return;	
			}	
			if(jQuery.trim($("#js-admin-name-id").val())==''){
				alert(_INSTALL_LANG['admin_name_cant_be_empty']);
				$("#js-admin-name-id")[0].focus();
				return;	
			}	
			if(jQuery.trim($("#js-admin-password-id").val())==''){
				alert(_INSTALL_LANG['admin_password_cant_be_empty']);
				$("#js-admin-password-id")[0].focus();
				return;	
			}	
			if(jQuery.trim($("#js-admin-password-id").val())!=jQuery.trim($("#js-admin-password2-id").val())){
				alert(_INSTALL_LANG['please_confirm_your_password']);
				$("#js-admin-password-id")[0].focus();
				return;	
			}
			if(jQuery.trim($("#js-admin-email-id").val())==''){
				alert(_INSTALL_LANG['admin_mailbox_cant_be_mepty']);
				$("#js-admin-email-id")[0].focus();
				return;	
			}	


			install_box_str	=	'<div style="border: 1px solid rgb(0, 0, 0); display: block; text-align: left; position: absolute; top: 154px; left: 432px; width: 300px; z-index: 5;" id="js-monitor">';
				install_box_str	+=	'<h3 id="js-monitor-title" style="cursor: move;">'+_INSTALL_LANG['install_program_monitor']+'</h3>';
				install_box_str	+= '<div style="background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; padding-bottom: 20px;">'; 
					install_box_str	+= '<img src="images/loading.gif" id="js-monitor-loading"/><br/>';
					install_box_str	+= '<strong style="color: blue; width: 65%; float: left; margin-left: 3px;" id="js-monitor-wait-please"> '+_INSTALL_LANG['install_is_processing']+'............</strong>';
					install_box_str	+=	'<span style="color: gray; cursor: pointer; float: right; margin-right: 3px;" id="js-monitor-view-detail">'+_INSTALL_LANG['hide_details']+'</span>';
				install_box_str	+=	'</div>';   
				install_box_str	+= '<div id="message_box" style="background-color:white;border-top:1px solid black;width:300px;height:235px;line-height:20px; overflow-y:auto; "></div>';
			install_box_str	+='<img style="display:none;position: absolute; top:7px; right: 10px; cursor: pointer;" src="./images/close.gif" id="js-monitor-close"/>';
			install_box_str	+='</div>';
			$(install_box_str).appendTo("body");
    		var left	=	(document.documentElement.clientWidth-$('#js-monitor').width())/2+"px";
			var top	=	(document.documentElement.clientHeight-$('#js-monitor').height()) / 2 + document.documentElement.scrollTop + "px";	
			$('#js-monitor').css({"left":left,"top":top});

			var d = new Draggable();
		   d.bindDragNode("js-monitor", "js-monitor-title");
			
			$("#js-monitor-close").click(
					function(){
						$("#js-monitor").remove();
						$("#js-install-at-once")[0].disabled	=	false;
					}
				);
			$("#js-monitor-view-detail").click(
					function(){
						if($("#message_box").css("display")!='none'){
							$(this).html(_INSTALL_LANG['display_details']);
							$("#message_box").hide();
						}else{
							$(this).html(_INSTALL_LANG['hide_details']);
							$("#message_box").show();
						}
					}
				);
				this.disabled	=	true;
				setTimeout(function(){
					install_db();
					if(go_on_install){
						install_test_data();
					}
					if(go_on_install){
						install_admin();
					}
					if(go_on_install){
						install_site_tpl();
					}	
					if(go_on_install){
						install_site_type();
					}	
					if(go_on_install){
						pad_complete	=	install_pad_data_input();
					}
					if(go_on_install){
							install_ok();
					}
				},1);
		}	
	);

function install_ok()
{
	var s	=	setInterval(function(){  
		if(!pad_complete){
			try{
				$("#js-monitor-wait-please").html(_INSTALL_LANG['successful_install']);
				$("#js-monitor-loading")[0].src	=	'images/loading2.gif';
				$("#js-monitor-close").css({display	:	'block'});
				window.location.href	=	'index.php?setup=install_ok';
			}catch(e){
			}	
			clearInterval(s);
		}
	},1000);
}

function install_pad_data_input()
{
	tmp_links_arr		=	$("#pad_links").val().split("\n");		
	var c		=	0;
	var st	=	1;
	var s		=	tmp_links_arr.length;
	if(s>100){
		st	= 	parseInt(parseInt(s/10)*st);
	}
	for(i in tmp_links_arr){
		var pad_link	=	$.trim(tmp_links_arr[i]);				
		if(!pad_link){
			continue;
		}	
		c++;
		var fun_send_ajax_pad_link	=	send_ajax_pad_link(pad_link);
		setTimeout(fun_send_ajax_pad_link,st);
	}
	$(":file").each(function(i){
		if(this.name.match("pad_file_name")){
			var pad_file_obj	=	$(this);				
			if(!pad_file_obj.val()){
				return true;
			}
			pad_file_obj.attr("id","pad_file_"+i); 	
			c++;
			pad_file_id	=	"pad_file_"+i;	
			var fun_send_ajax_pad_file		=	send_ajax_pad_file(pad_file_id)
			setTimeout(fun_send_ajax_pad_file,1);
		}
	});
	return c;
}

function send_ajax_pad_link(pad_link)
{
	return (function(){
	 $.ajax({
				url			:	'index.php?setup=system_configure_save',	
				type			:	'POST',
				data			:	'type=install_pad_data_input&pad_link='+pad_link,
				dataType		:	'json',
				success		:	function(json){
					setup_arr	=	json;
					setup_display(setup_arr);
					pad_complete--;
				},
				error			:	function(){
					pad_complete--;
				}
			});	
	});
	
}

function send_ajax_pad_file(pad_file_id)
{
	return (function(){
					$.ajaxFileUpload({
					url				:	'index.php?setup=system_configure_save&type=install_pad_data_input',
					secureuri		:	false,
					fileElementId	:	pad_file_id,
					dataType			:  'json',
					success			: function(data, status){
						setup_display(data);
						pad_complete--;
					},
					error: function (data, status, e){
						pad_complete--;
					}
				});		
			});
}



function install_site_tpl()
{
	var url_par	=	'';
	$(":radio").each(function(i){
		if(this.name.match('site_tpl')){
			if(this.checked){
				url_par+='&site_tpl='+encodeURIComponent(this.value);
				return false;
			}			
		}
	});
	if(!url_par){
		return true;
	}	
	$.ajax({
		url			:	'index.php?setup=system_configure_save',	
		type			:	'POST',
		data			:	'type=install_site_tpl'+url_par,
		dataType		:	'json',
		async 		:	false,
		success		:	function(json){
			setup_arr	=	json;
			setup_display(setup_arr);
		}	
	});	

}


function install_site_type()
{
	var url_par	=	'';
	$(":radio").each(function(i){
		if(this.name.match('site_type')){
			if(this.checked){
				url_par+='&site_type='+encodeURIComponent(this.value);
				return false;
			}			
		}
	});
	if(!url_par){
		return true;
	}	
	$.ajax({
		url			:	'index.php?setup=system_configure_save',	
		type			:	'POST',
		data			:	'type=install_site_type'+url_par,
		dataType		:	'json',
		async 		:	false,
		success		:	function(json){
			setup_arr	=	json;
			setup_display(setup_arr);
		}	
	});
}

function install_admin()
{
		var admin_name		=	$("#js-admin-name-id").val();
		var admin_pass		=	$("#js-admin-password-id").val();
		var admin_email	=	$("#js-admin-email-id").val();
		
	$.ajax({
		url			:	'index.php?setup=system_configure_save',	
		type			:	'POST',
		data			:	'type=install_admin&admin_name='+encodeURIComponent(admin_name)+"&admin_pass="+encodeURIComponent(admin_pass)+'&admin_email='+encodeURIComponent(admin_email),
		dataType		:	'json',
		async 		:	false,
		success		:	function(json){
			setup_arr	=	json;
			setup_display(setup_arr);
		}	
	});


}


function install_db()
{
	var db_host	=	$('#js-db-host-id').val();		
	var db_port	=	$('#js-db-port-id').val();		
	var db_user	=	$('#js-db-user-id').val();		
	var db_pass	=	$('#js-db-pass-id').val();		
	var db_name	=	$('#js-db-name-id').val();
	var db_pre	=	$('#js-db-prefix-id').val();

	$.ajax({
		url			:	'index.php?setup=system_configure_save',	
		type			:	'POST',
		data			:	'type=install_db&db_user='+encodeURIComponent(db_user)+"&db_host="+encodeURIComponent(db_host)+'&db_pass='+encodeURIComponent(db_pass)+'&db_port='+encodeURIComponent(db_port)+"&db_name="+encodeURIComponent(db_name)+"&db_pre="+encodeURIComponent(db_pre),
		dataType		:	'json',
		async 		:	false,
		success		:	function(json){
			setup_arr	=	json;
			setup_display(setup_arr);
		}	
	});		
}

var go_on_install	=	true;
function setup_display(setup_arr)
{
	for(i in setup_arr){
			if(setup_arr[i]['err']){
					var display_err	=	setup_arr[i]['err'];
					$("<p style='margin-left:20px;'><b>"+setup_arr[i]['bewrite']+"</b>...............................<b style='color:red'>"+_INSTALL_LANG['error']+"</b></p>").appendTo("#message_box");
					break;
			}else{
					$("<p style='margin-left:20px;'><b>"+setup_arr[i]['bewrite']+"</b>...............................<b style='color:blue'>"+_INSTALL_LANG['success']+"</b></p>").appendTo("#message_box");
			}
	}
	
	if(typeof display_err!="undefined"){
		$("<p style='margin-left:20px;'><b style='color:red'>"+display_err+"</b></p>").appendTo("#message_box");
		if(i!='install_pad_data_input'){
			$("#js-monitor-close").css({display	:	'block'});
			go_on_install	=	false;
			$("#js-monitor-wait-please").html(_INSTALL_LANG['install_quit']);
			$("#js-monitor-loading")[0].src	=	'images/loading2.gif';
		}
	}else{
		go_on_install	=	true;
	}
	$("#message_box")[0].scrollTop = $("#message_box")[0].scrollHeight;
}
/*
function fun_setup_display(message,error){
	    return (function(){
	    	if(error){
					$("<p style='margin-left:20px;'>"+message+"...............................<b style='color:red'>Error</b></p>").appendTo("#message_box");
			}else{
					$("<p style='margin-left:20px;'>"+message+"...............................<b style='color:blue'>Succeed</b></p>").appendTo("#message_box");
			}
			$("#message_box")[0].scrollTop = $("#message_box")[0].scrollHeight;
		});
}
*/

function install_test_data()
{
	var url_par	=	'';
	$(":radio").each(function(i){
		if(this.name.match('test_data')){
			if(this.checked){
				url_par+='&test_data='+encodeURIComponent(this.value);
				return false;
			}			
		}
	});
	if(!url_par){
		return true;
	}

	$.ajax({
		url			:	'index.php?setup=system_configure_save',	
		type			:	'POST',
		data			:	'type=install_test_data'+url_par,
		dataType		:	'json',
		async 		:	false,
		success		:	function(json){
			setup_arr	=	json;
			setup_display(setup_arr);
		}	
	});
}




});

function add_pad_file()
{
	var tr_html	=	'<tr><td><a href="javascript:void(0);" onclick="del_pad_file(this)"><b>[-]</b>&nbsp;</a> <input type="file" name="pad_file_name" size=40> </td></tr>';
	$(tr_html).insertAfter("#pad_file_tr");
}
function del_pad_file(obj)
{
	$(obj).parent().parent().remove();
}
