﻿$(document).ready(BindGroupEdit);
function BindGroupEdit() {
	$('a[rel=groupEdit]').click(EditOnClick);
	
	$('a[rel=groupDele]').click(DeleOnClick);
	
	$('#groupAdd').click(function(){
		$('#action').val('Add');
		$('#albumname').val('');
		$('#albumid').val('');
		$('#albumdesc').val('');
		$('#locked').removeAttr('checked');
		$('#unlock').attr('checked', 'checked');
		$('#gpMP').show();
		$('#albumname').focus();
		$('#albumname').select();
		return false;
	});
}

function EditOnClick() {
	$('#action').val('Edit');
	
	$.ajax( {
		dataType: "json",
		data: {
			Action: 'Get',
			Type: 'GroupInfo',
			GroupID: $(this).attr('groupid'),
			GroupName: ''
		},
		success: function(info) {
			OverLayHide();
			$('#albumname').val(info.GroupName);
			$('#albumid').val(info.GroupID);
			$('#albumdesc').val(info.GroupDesc);
			if(info.IsLocked == 1) {
				$('#locked').attr('checked', 'checked');
				$('#unlock').removeAttr('checked');
			} else {
				$('#locked').removeAttr('checked');
				$('#unlock').attr('checked', 'checked');
			}
			$('#gpMP').show();
			$('#albumname').focus();
			$('#albumname').select();
		},
		error: function(response) {
			var loadlay = $('#nowIsLoading');
			loadlay.text("Load on an Error");
			loadlay.html(response.responseText);
			OverLayHide();
		},
		beforeSend: beginLoadAjax
	} );
	
	return false;
}

function DeleOnClick() {
	var sendDate = {Action: 'Del', Type: 'GroupInfo', GroupID: $(this).attr('groupid')};
	
	if(!confirm('Are you sure you want to delete this album?'))
		return false;
		
	$.ajax( {
		dataType: "json",
		data: sendDate,
		success: function(info) {
			if(info.Status == "OK") {
				$('#gpMP').hide();
				GetPageHtmlData(1);
			}
		},
		error: function(response) {
			var loadlay = $('#nowIsLoading');
			loadlay.text("Load on an Error");
			loadlay.html(response.responseText);
			OverLayHide();
		},
		beforeSend: beginLoadAjax
	} );
	
	return false;
}

function btnOKOnClick() {
	if($('#action').val() == 'Add')
		AddGroupInfo();
	else if($('#action').val() == 'Edit')
		SetGroupInfo();
}

function AddGroupInfo() {
	var sendDate = {Action: 'Add', Type: 'GroupInfo', GroupName: '', GroupID: '', GroupDesc: '', IsLocked: 1};
	sendDate.GroupName = $.trim($('#albumname').val());
	sendDate.GroupID = $('#albumid').val();
	sendDate.GroupDesc = $('#albumdesc').val();
	if($('#locked').attr('checked') == undefined) {
		sendDate.IsLocked = 0;
	}
	
	if(sendDate.GroupName == "") {
		$('#albumname').focus();
		$('#albumname').select();
		return false;
	}
		
	$.ajax( {
		dataType: "json",
		data: sendDate,
		success: function(info) {
			$('#gpMP').hide();
			if(info.Status == "OK") {
				window.location = 'http://zone.aimoo.com/photo/'+$('#albumowner').val()+'/'+info.GroupName+'-'+info.GroupID;
			} else if (info.Status == "ERROR") {
				alert(info.Msg);
			}
		},
		error: function(response) {
			var loadlay = $('#nowIsLoading');
			loadlay.text("Load on an Error");
			loadlay.html(response.responseText);
			OverLayHide();
		},
		beforeSend: beginLoadAjax
	} );
	
	return false;
}

function SetGroupInfo() {
	var sendDate = {Action: 'Set', Type: 'GroupInfo', GroupName: '', GroupID: '', GroupDesc: '', IsLocked: 1};
	sendDate.GroupName = $.trim($('#albumname').val());
	sendDate.GroupID = $('#albumid').val();
	sendDate.GroupDesc = $('#albumdesc').val();
	if($('#locked').attr('checked') == undefined) {
		sendDate.IsLocked = 0;
	}
	
	if(sendDate.GroupName == "") {
		$('#albumname').focus();
		$('#albumname').select();
		return false;
	}
		
	$.ajax( {
		dataType: "json",
		data: sendDate,
		success: function(info) {
			if(info.Status == "OK") {
				OverLayHide();
				$('#gpMP').hide();
				var owner = $('#albumowner').val();
				$.each($('a[rel=group'+sendDate.GroupID+']'), function(i, n) {
					$(n).attr("href", "http://zone.aimoo.com/photo/"+owner+"/"+info.GroupName+"-"+info.GroupID);
					if($(n).attr('ref') == 'txt') {
						$(n).html(info.GroupName);
					}
				});
			}
		},
		error: function(response) {
			var loadlay = $('#nowIsLoading');
			loadlay.text("Load on an Error");
			loadlay.html(response.responseText);
			OverLayHide();
		},
		beforeSend: beginLoadAjax
	} );
	
	return false;
}