/** 
 * 
 * Manages the actions to make a present to an amateur from a user.
 * 
 * @category: global Javascript File (Controller)
 * 
 * @autor: Marcel Hoyer
 * @copyright: Onscreen.net
 */

var http = null;
var slideInAction = null;
var slideOutAction = null;
var fadeOutAction = null;
var fadeInAction = null;
var mainTimeout = null;
var subTimeout = null;
var buttonTimeout = null;
var messengerScrollPosition = 0;
var messengerScrollStep = 0;
var lastListId = 0;

if (window.XMLHttpRequest) {
	http = new XMLHttpRequest();
} else {
	http = new ActiveXObject("Microsoft.XMLHTTP");
}
	
function hideIcons(category) {
	var element = document.getElementById(category);
	element.style.display = 'none';
}

function hideAllIcons() {
	var allIcons = document.getElementsByTagName('ul');
	for (i = 0; i < allIcons.length; i++) {
		if (allIcons[i].className == 'iconList') {
			allIcons[i].style.display = 'none';
		}
	}
}

function getIcons(category, categoryId, amateurId, userId) {
	if (document.getElementById(category).firstChild) {
		showIcons(category);
	} else {
		loadIcons(category, categoryId, amateurId, userId);
	}
}

function showIcons(category) {
	if (lastListId == 0 || lastListId == category) {
		setTimeout("slide('fadeIn', '" + category + "',0.1)",200);
	} else {
		document.getElementById(category).style.display = 'block';
		hideIcons(lastListId);
	}
	lastListId = category;
	
	return false;
}

function adaptIconHeight() {
	var myheight = (window.innerHeight) ? window.innerHeight : document.documentElement.clientHeight;
	var buttonTop = myheight - 21;
	document.getElementById('regardcategories').style.height = "auto";
	document.getElementById('regardShop').style.height = myheight + "px";
	
	var downScroller = document.getElementsByTagName("p");
	
	for (var i = 0; i < downScroller.length; i++) {
		
		if (downScroller[i].className == "scoll down") {
			downScroller[i].style.top = buttonTop + "px";
		}
	}
} 

function loadRegards(amateurId) {
	if (!document.getElementById('regardShop')) {
		var values = "amateurId=" + amateurId;
		http.open('post', '../../request/regards/regardsCategoryRequest.php', true);
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.onreadystatechange = showNewRegards; 
		http.send(values);
	} else {
		showRegards();
	}
}

function loadIcons(category, categoryId, amateurId, userId) {
	var values = "amateurId=" + amateurId + "&userId=" + userId + "&categoryId=" + categoryId;
	http.open('post', '../../request/regards/regardsRequest.php', true);
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http.onreadystatechange = function() {
		showNewIcons(category); 
	}
	http.send(values);
}

function showNewRegards() {
	if (http.readyState == 4) {
		createRegardShop();
		var shop = document.getElementById('regards');
		shop.innerHTML = http.responseText;
		shop.style.display = "block";
		shop.style.left = "-140px";
		document.getElementById('regardShop').style.left = '-140px';
		if (document.getElementById('regardcategories') != null) adaptIconHeight();
		setTimeout("slide('slideIn', 'regardShop',5)",50);
	}
	
}

function showNewIcons(category) {
	if (http.readyState == 4) {
		var shop = document.getElementById(category);
		shop.innerHTML = http.responseText;
		showIcons(category);
	}
}

function showRegards() {	
	var shop = document.getElementById('regards');
	adaptIconHeight();
	shop.style.display = "block";
	shop.style.left = "-140px";
	setTimeout("slide('slideIn', 'regardShop',5)",50);
}

function createRegardShop() {
	if (!document.getElementById('regards')) {
		var regardShop = document.createElement('div');
		regardShop.id = 'regards';
		document.body.appendChild(regardShop);
	}
}

function hideRegardShop() {
	if(lastListId != 0) {
		subTimeout = setTimeout("slide('fadeOut', '" + lastListId + "',0.1)", 2000);
		lastListId = 0;
	}
	mainTimeout = setTimeout("slide('slideOut', 'regardShop',5)", 2000);
	buttonTimeout = setTimeout("closeButton()", 2000);
}

function hideInstantRegardShop() {
	if (lastListId != 0) {
		subTimeout = setTimeout("slide('fadeOut', '" + lastListId + "',0.1)", 0);
		lastListId = 0;
	}
	mainTimeout = setTimeout("slide('slideOut', 'regardShop',10)", 10);
	buttonTimeout = setTimeout("closeButton()", 10);
}

function closeRegardShop() {
	var shop = document.getElementById('regards');
	shop.innerHTML = '';
	shop.style.display = 'none';
	window.location.reload();
}

function closeButton() {
	document.getElementById('regardCloseButton').style.display = 'none';
}

function createRegardCard(regardId, amateurId) {
	var values = "amateurId=" + amateurId + "&regardId=" + regardId;
	openInlinePopup('/regards/regardCardRequest.html?' + values, 500, 330, '', 'Sende eine E-Card zu Deiner Aufmerksamkeit', 0);
}

function buyCoins() {
	hideRegardShop();
	window.location.replace('../clearing/index.html');
}

function buyRegard(regardId, amateurId) {
	var title = document.getElementById('regardTitle').value;
	var message = document.getElementById('regardMessage').value;
	var values = "regardId=" + regardId + "&amateurId=" + amateurId + "&title=" + title + "&message=" + message;
	http.open('post', '../../request/regards/regardSellingRequest.php', true);
	http.onreadystatechange = sellingFeedback;
	http.setRequestHeader(
		"Content-Type",
		"application/x-www-form-urlencoded");
	http.send(values);
}

function sellingFeedback() {
	if (http.readyState == 4){
		var shop = document.getElementById('regards');
		shop.innerHTML = http.responseText;
	}
}

function openRegardDetails(mylink, mytitle) {
	hideInstantRegardShop();
	openInlinePopup(mylink, '500', '330', '', mytitle, 0);
	return false;
}

/*
 *Scroll functions for the category view on the left side.
 */

function regardScroll(id, direction) {
	if (direction == 'down' && messengerScrollPosition >= 0) {
		messengerScrollStep = -3;
	} else if (direction == 'up' && messengerScrollPosition < 0) {
		messengerScrollStep = 3;
	}
	
	regards_nav_move(id);
	scrollProcess = window.setInterval("regards_nav_move('" + id + "')", 5);
	return false;
}

function stopScrolling() {
	clearInterval(scrollProcess);
	return false;
}

function regards_nav_move(id) {
	var movingElement = document.getElementById(id);
	var heightDiff = (window.innerHeight - movingElement.offsetHeight);
	if (messengerScrollStep > 0 && messengerScrollPosition + messengerScrollStep > 0) {
		clearInterval(scrollProcess);
		return false;
	} else if (messengerScrollStep < 0 && movingElement.offsetTop  < heightDiff) {
		clearInterval(scrollProcess);
		return false;
	}

	messengerScrollPosition += messengerScrollStep;
	movingElement.style.top = messengerScrollPosition + "px";
}

function regards_nav_stop() {
	clearInterval(scrollProcess);
}


/*
 * Slide functions for the regard shop.
 */

function slide(action, id, steps) {
	if (action == 'slideOut') {
		slideOutAction = setInterval('slideOut("' + id + '",' + steps + ')', 13);
	} else if (action == 'slideIn') {
		slideInAction = setInterval('slideIn("' + id + '",' + steps + ')', 13);
	} else if (action == 'fadeOut') {
		fadeOutAction = setInterval('fadeOut("' + id + '",' + steps + ')', 10);
	} else if (action == 'fadeIn') {
		fadeInAction = setInterval('fadeIn("' + id + '",' + steps + ')', 10);
	}
}

function slideIn(id, steps) {
	document.getElementById('regardShop').style.display = "block";
	var el = document.getElementById(id);
	var leftNum = parseInt(el.style.left);
	var newPos = leftNum + steps;

	el.style.left = newPos + 'px';

	if (leftNum >= 0) {
		el.style.left = 0;
		var close = document.getElementById('regardCloseButton');
		close.style.display = 'block';
		clearInterval(slideInAction);
	}
}

function slideOut(id, steps) {
	var el = document.getElementById(id);
	var leftNum = parseInt(el.style.left);
	var newPos = leftNum - steps;
	
	el.style.left = newPos + 'px';

	if (leftNum <= -el.offsetWidth) {
		el.style.left = -140 + 'px';
		clearInterval(slideOutAction);
		document.getElementById('regardShop').style.display = "none";
	}
}

function fadeOut(id, steps) {
	var el = document.getElementById(id).style;
	var topNum = parseFloat(el.opacity);
	var newPos = parseFloat(topNum.toFixed(1) - steps);
	
	el.opacity = newPos;
	
	if (el.opacity <= 0) {
		el.opacity = 0;
		el.display = "none";
		clearInterval(fadeOutAction);
	}

}

function fadeIn(id, steps) {
	var el = document.getElementById(id).style;
	if (!el.opacity || el.display == "none") {
		el.display = 'block';
		var topNum = 0;
	} else {
		var topNum = parseFloat(el.opacity);
	}
	var newPos = parseFloat(topNum + steps);
	
	el.opacity = newPos;
	
	if (topNum >= 1) {
		el.opacity = 1;
		clearInterval(fadeInAction);
	}
}

function checkToLeave(target) {
	var targetObj = (target.currentTarget) ? target.currentTarget : target.srcElement;
	var targetWidth = targetObj.scrollWidth;
	var targetHeight = targetObj.scrollHeight;
	var mousePositionX = target.clientX;
	var mousePositionY = target.clientY;
	var diffHeight = targetHeight - mousePositionY;
	var diffWidth = targetWidth - mousePositionX;
		
	if (targetObj.id == 'regardShop' && (diffHeight <= 0 || diffWidth <= 0)) {
		hideRegardShop();
	} else if (targetObj.className == 'iconList' && (diffHeight <= 0 || diffWidth <= 0)) {
		hideRegardShop();
	}
	
	return false;
}

function enterShop(target) {
	var targetObj = (target.currentTarget) ? target.currentTarget : target.srcElement;
	
	if (targetObj.id == 'regardShop' || targetObj.className == 'iconList') {
		if (targetObj.className == 'iconList') {
			lastListId = targetObj.id;
		}
		resetTimeouts();
	}
	
	return false;

}

function showLimit() {
		var max = 255;
		var messageText = document.getElementById('regardMessage').value;
		document.getElementById('charactersLeft').innerHTML = 255 - messageText.length;
}

function resetTimeouts() {
	clearTimeout(mainTimeout);
	clearTimeout(subTimeout);
	clearTimeout(buttonTimeout);
}
