var selslide = 0;
var prevslide = 0;
var animate = true;
var margin_top = 73;
var ajaxroot = '/';
var ajaxtimeout = 2000;

var tooltipcfg = {
	effect : 'slide',
	position : 'top center',
	opacity : 0.9,
	tipClass : 'tooltip ui-corner-all'
};

$(document).ready(function () {
	$.trackPage('UA-21994308-2');

	$('#slide-' + 3).click();
	
	$('#admin-tabs').tabs({
		fx: {
			opacity: 'toggle',
			duration: 'fast'
		},
		selected: 0
	});
	
	$('#admin-pages ul').sortable({
		stop: function (event, ui) {
			sortList($(this), 'pages');
		}
	});
	
	$('#admin-projects ul').sortable({
		stop: function (event, ui) {
			sortProjects($(this));
		}
	});
	
	$('#admin-members ul').sortable({
		stop: function (event, ui) {
			sortList($(this), 'members');
		}
	});
	
	$('#admin-partners ul').sortable({
		stop: function (event, ui) {
			sortList($(this), 'partners');
		}
	});
	
	$('#quote-content #content').NobleCount('#remaining1', {
		max_chars: 220,
		block_negative: true
	});
	$('#quote-content #author').NobleCount('#remaining2', {
		max_chars: 20,
		block_negative: true
	});
});

function setCookie (key, value) {
	$.ajax({
		type : 'POST',
		url : ajaxroot + 'ajax/setcookie',
		data: 'key=' + key + '&value=' + value,
		dataType : 'json',
		async: false,
		cache : 'false',
		timeout : ajaxtimeout,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			ajaxError(textStatus);
		},
		success : function(json) {
		}
	});
}

function getCookie (key, defaultValue) {
	$.ajax({
		type : 'POST',
		url : ajaxroot + 'ajax/getcookie',
		data: 'key=' + key + '&default=' + defaultValue,
		dataType : 'json',
		async: false,
		cache : 'false',
		timeout : ajaxtimeout,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			ajaxError(textStatus);
		},
		success : function(json) {
			return json.value;
		}
	});
}

function setLanguage (lang) {
	setCookie('language', lang);
	window.location.reload();
}

function sortList (el, func) {
	var arr = el.sortable('toArray');
	var data = 'status=' + el.attr('id');
	for (i = 0; i < arr.length; i++) {
		var j = i + 1;
		data += '&' + arr[i] + '=' + j;
	}
	$.ajax({
		type : 'POST',
		url : ajaxroot + 'ajax/sort' + func,
		data: data,
		dataType : 'text',
		async: false,
		cache : 'false',
		timeout : ajaxtimeout,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			ajaxError(textStatus);
		},
		success : function(text) {
		}
	});
}

function getSlideCoords (id) {
	var slideMargin = 238;
	
	if (id == 1) {
		var left = 3 * slideMargin + 3;
		var top = 0 * slideMargin + margin_top + 3;
	} else if (id == 2) {
		var left = 3 * slideMargin + 3;
		var top = 1 * slideMargin + margin_top + 3;
	} else if (id == 3 || id == 4 ||  id == 5 || id == 6) {
		var left = (id - 3) * slideMargin + 3;
		var top = 2 * slideMargin + margin_top + 3;
	} else if (id == 7 || id == 8 ||  id == 9 || id == 10) {
		var left = (id - 7) * slideMargin + 3;
		var top = 3 * slideMargin + margin_top + 3;
	}
	
	var result = new Array();
	result['left'] = left;
	result['top'] = top;
	
	return result;
}

function displayPreviousSlide (id, coords, page_id) {
	$('#slide-frame-' + id)
		.css({'z-index': 9000});
	$('#main-viewport-content')
		.fadeOut(500, function () {
			$('#slide-content-' + id)
				.animate({'width': 228, 'height': 228}, 1000);
			$('#slide-frame-active-' + id)
				.animate({'width': 0, 'height': 0}, 500, function () {
					$(this).css({'display': 'none'});
				});
			$('#slide-frame-' + id)
				.animate({'left': 239, 'top': margin_top + 121, 'width': 230, 'height': 230}, 1000)
				.animate({'left': coords['left'], 'top': coords['top']}, 500, function () {
					$(this).css({'cursor': 'pointer'});
				});
	});
}

function displayCurrentSlide (id, coords, page_id) {
	$.getJSON(ajaxroot + 'ajax/getquote', function(data) {
		$('#slide-frame-active-' + id + ' .slide-mask #content').html(data.content);
		$('#slide-frame-active-' + id + ' .slide-mask #author').html(data.author);
	});
	$('#slide-frame-active-' + id)
		.css({'display': 'block'})
		.animate({'width': 230, 'height': 230}, 500);
	$('#slide-frame-' + id)
		.css({'left': coords['left'], 'top': coords['top'], 'z-index': 9002, 'cursor': 'default'})
		.animate({'left': 239, 'top': margin_top + 121, 'width': 230, 'height': 230}, 500, function () {
			$('#slide-content-' + id)
				.animate({'width': 704, 'height': 466}, 500);
		})
		.animate({'left': 3, 'top': margin_top + 3, 'width': 706, 'height': 468}, 1000, function () {
			$.ajax({
				type : 'POST',
				url : ajaxroot + 'ajax/getpage',
				data: 'page_id=' + page_id,
				dataType : 'text',
				cache : 'false',
				timeout : ajaxtimeout,
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					ajaxError(textStatus);
				},
				success : function(text) {
					$('#main-viewport-content')
						.html(text)
						.fadeIn(500, function () {
							prevslide = id;
							animate = true;
						});
				}
			});
		});
}

function animateSlide (id, page_id) {
	if (animate) {
		if (selslide != id) {
			animate = false;
			selslide = id;
			
			if (prevslide != 0) {
				displayPreviousSlide(prevslide, getSlideCoords(prevslide));
			}
			
			$('html, body').animate({scrollTop: 0}, 500);
			
			displayCurrentSlide(id, getSlideCoords(id), page_id);
		}
	}
}

function showSecondContent (left, top, content) {
	$('#mask')
		.click(function () {
			hideSecondContent();
		})
		.css({'width': $('.container').width(),'height': $(document).height()})
		.fadeTo("fast", 0.8)
		.fadeIn(500);
	$('#second-content p').html(content);
	var bg = $('#second-content p #bg').val();
	$('#second-content')
		.css({'background-image': 'url("' + ajaxroot + 'media/images/projects/backgrounds/' + bg + '")', 'left': left, 'top': top})
		.fadeIn(1000);
}

function showProjectDetails (id) {
	$.ajax({
		type : 'POST',
		url : ajaxroot + 'ajax/getprojectdetails',
		data: 'id=' + id,
		dataType : 'text',
		cache : 'false',
		timeout : ajaxtimeout,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			ajaxError(textStatus);
		},
		success : function(text) {
			showSecondContent(5, margin_top + 4, text);
		}
	});
}

function showFooterContent (content) {
	$.ajax({
		type : 'POST',
		url : ajaxroot + 'ajax/getfootercontent',
		data: 'content=' + content,
		dataType : 'text',
		cache : 'false',
		timeout : ajaxtimeout,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			ajaxError(textStatus);
		},
		success : function(text) {
			$('html, body').animate({scrollTop: 0}, 500);
			showSecondContent(5, margin_top + 4, text);
		}
	});
}

function hideSecondContent () {
	$('#mask').fadeOut(1000);
	$('#second-content').fadeOut(500);
}

function popupDeletePage (id) {
	$('#page-delete').dialog({
		modal: true,
		title: 'Sterge Pagina',
		width: 'auto',
		height: 'auto',
		resizable: false,
		buttons: {
			Da: function () {
				deletePage(id);
				$(this).dialog('close');
			},
			Nu: function () {
				$(this).dialog('close');
			}
		},
		zIndex: 9999
	});
	$('#page-delete').dialog('open');
}

function deletePage (id) {
	$.ajax({
		type : 'POST',
		url : ajaxroot + 'ajax/deletepage',
		data: 'id=' + id,
		dataType : 'text',
		cache : 'false',
		timeout : ajaxtimeout,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			ajaxError(textStatus);
		},
		success : function(text) {
			$('#admin-pages ul li#' + id).remove();
		}
	});
}

function popupAddQuote () {
	$('#quote-content #content').val('');
	$('#quote-content #author').val('');
	$('#quote-content').dialog({
		modal: true,
		title: 'Adauga Citat',
		width: 'auto',
		height: 'auto',
		resizable: false,
		buttons: {
			Salveaza: function () {
				addQuote($('#quote-content #content').val(), $('#quote-content #author').val(), $('#quote-content #locale').val());
				$(this).dialog('close');
			},
			Renunta: function () {
				$(this).dialog('close');
			}
		},
		zIndex: 9999
	});
	$('#quote-content').dialog('open');
}

function addQuote (content, author, locale) {
	$.ajax({
		type : 'POST',
		url : ajaxroot + 'ajax/addquote',
		data: 'content=' + content + '&author=' + author + '&locale=' + locale,
		dataType : 'json',
		cache : 'false',
		timeout : ajaxtimeout,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			ajaxError(textStatus);
		},
		success : function(json) {
			$.get(ajaxroot + 'ajax/quotesection/' + json.id, function (data) {
				$('#admin-quotes ul').append(data);
			});
		}
	});
}

function popupEditQuote (id) {
	$('#quote-content #content').val($('#admin-quotes ul li#' + id + ' p').html());
	$('#quote-content #author').val($('#admin-quotes ul li#' + id + ' span').html());
	$('#quote-content #locale').val($('#locale-' + id).val());
	$('#quote-content').dialog({
		modal: true,
		title: 'Modifica Citat',
		width: 'auto',
		height: 'auto',
		resizable: false,
		buttons: {
			Salveaza: function () {
				editQuote(id, $('#quote-content #content').val(), $('#quote-content #author').val(), $('#quote-content #locale').val());
				$(this).dialog('close');
			},
			Renunta: function () {
				$(this).dialog('close');
			}
		},
		zIndex: 9999
	});
	$('#quote-content').dialog('open');
}

function editQuote (id, content, author, locale) {
	$.ajax({
		type : 'POST',
		url : ajaxroot + 'ajax/editquote',
		data: 'id=' + id + '&content=' + content + '&author=' + author + '&locale=' + locale,
		dataType : 'json',
		cache : 'false',
		timeout : ajaxtimeout,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			ajaxError(textStatus);
		},
		success : function(json) {
			$('#admin-quotes ul li#' + id + ' p').html(json.content);
			$('#admin-quotes ul li#' + id + ' span').html(json.author);
		}
	});
}

function popupDeleteQuote (id) {
	$('#quote-delete').dialog({
		modal: true,
		title: 'Sterge Citat',
		width: 'auto',
		height: 'auto',
		resizable: false,
		buttons: {
			Da: function () {
				deleteQuote(id);
				$(this).dialog('close');
			},
			Nu: function () {
				$(this).dialog('close');
			}
		},
		zIndex: 9999
	});
	$('#quote-delete').dialog('open');
}

function deleteQuote (id) {
	$.ajax({
		type : 'POST',
		url : ajaxroot + 'ajax/deletequote',
		data: 'id=' + id,
		dataType : 'json',
		cache : 'false',
		timeout : ajaxtimeout,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			ajaxError(textStatus);
		},
		success : function(json) {
			$('#admin-quotes ul li#' + id).remove();
		}
	});
}

function popupDeleteProject (id) {
	$('#project-delete').dialog({
		modal: true,
		title: 'Sterge Proiect',
		width: 'auto',
		height: 'auto',
		resizable: false,
		buttons: {
			Da: function () {
				deleteProject(id);
				$(this).dialog('close');
			},
			Nu: function () {
				$(this).dialog('close');
			}
		},
		zIndex: 9999
	});
	$('#project-delete').dialog('open');
}

function deleteProject (id) {
	$.ajax({
		type : 'POST',
		url : ajaxroot + 'ajax/deleteproject',
		data: 'id=' + id,
		dataType : 'text',
		cache : 'false',
		timeout : ajaxtimeout,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			ajaxError(textStatus);
		},
		success : function(text) {
			$('#admin-projects ul li#' + id).remove();
		}
	});
}

function popupDeleteMember (id) {
	$('#member-delete').dialog({
		modal: true,
		title: 'Sterge Membru',
		width: 'auto',
		height: 'auto',
		resizable: false,
		buttons: {
			Da: function () {
				deleteMember(id);
				$(this).dialog('close');
			},
			Nu: function () {
				$(this).dialog('close');
			}
		},
		zIndex: 9999
	});
	$('#member-delete').dialog('open');
}

function deleteMember (id) {
	$.ajax({
		type : 'POST',
		url : ajaxroot + 'ajax/deletemember',
		data: 'id=' + id,
		dataType : 'text',
		cache : 'false',
		timeout : ajaxtimeout,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			ajaxError(textStatus);
		},
		success : function(text) {
			$('#admin-members ul li#' + id).remove();
		}
	});
}

function popupDeletePartner (id) {
	$('#partner-delete').dialog({
		modal: true,
		title: 'Sterge Partener',
		width: 'auto',
		height: 'auto',
		resizable: false,
		buttons: {
			Da: function () {
				deletePartner(id);
				$(this).dialog('close');
			},
			Nu: function () {
				$(this).dialog('close');
			}
		},
		zIndex: 9999
	});
	$('#partner-delete').dialog('open');
}

function deletePartner (id) {
	$.ajax({
		type : 'POST',
		url : ajaxroot + 'ajax/deletepartner',
		data: 'id=' + id,
		dataType : 'text',
		cache : 'false',
		timeout : ajaxtimeout,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			ajaxError(textStatus);
		},
		success : function(text) {
			$('#admin-partners ul li#' + id).remove();
		}
	});
}
