// Gets url-metadata via ajax-request and returns response to form

function getUrlMetaData(_url)
{
	var returnValue = null;

	$.ajax({
		type: "POST",
		url: pathPrefix + "/Ajax/ptdGetUrlData" + firstSeperator + "url=" + _url + sid2,
		async: false,
		success: function(responseText) {
			return responseText.split("###");
		}
	});

	return returnValue;
}

// Handles action requests from the acp part of ptdListLinksPage

function ptdAction(_action) {

	var isChecked = false;
	
	var checkBoxes = getById('ptdForm').elements["checkBox[]"];

	if(checkBoxes) {

		var count = checkBoxes.length;

		if(!count)
			if(checkBoxes.checked == true)
				isChecked = true;
			else
				isChecked = false;
	
		else
			for(var i = 0; i < count; i++)
				if(checkBoxes[i].checked == true)
					isChecked = true;

	}
	else
		isChecked = false;	

	if(isChecked == true) {
		
		var form = getById('ptdForm');
		form.elements["action"].value = _action;
		// Get action texts from form and submit action to class cls
		if (_action == "deleteLinkStats") {
			confirmAcpPopup(form, lang.get("ptd.t.confirmDeleteLinksStats"));
		}
		else if (_action == "delete") {
			confirmAcpPopup(form, lang.get("ptd.t.confirmDeleteLinks"));
		}
		else if (_action == "unlock") {
			confirmAcpPopup(form, lang.get("ptd.t.confirmUnlockLinks"));
		}
		else if (_action == "lock") {
			confirmAcpPopup(form, lang.get("ptd.t.confirmLockLinks"));
		}
		else if (_action == "changeCategories") {
			form.action = pathPrefix + "/" + lngAdministration + "/ptdLinkCategoryChangeForm/" + sid1;
			form.submit();
		}
		else {
			form.submit();
		}
	}
}

function confirmAcpPopup(form, content) {
	popup.show({
		content: content,
		title: lang.get("global.w.confirm"),
		buttons : [
			{
				label : lang.get("global.w.yes"),
				close : true,
				javascript : function() {
					form.submit();
				}
			},
			{
				label : lang.get("global.w.no"),
				close : true
			}
		]
	});
}


// Handles popup of big thumbnails

function showPopup(_id, _sender, _path) {
	
	if(!getById("largeThumb_" + _id)) {
		
		var image = document.createElement("img");
		image.src = _path;
		image.id = "largeThumb_" + _id;
		image.style.display = "none";
		image.style.padding = "5px";
		image.style.backgroundColor = "white";
		image.style.border = "1px solid #CCCCCC";
		image.style.maxWidth = "320px";
		
		document.body.appendChild(image);
				
		image.style.position = "absolute";
		image.style.left = posX(_sender) + 150 + "px";
		image.style.top = posY(_sender) - 6 + "px";
		
		image.style.display = "";
		
	} else {
		getById("largeThumb_" + _id).style.display = "";
	}

}

function hidePopup(_id) {
	getById("largeThumb_" + _id).style.display = "none";
}

function deleteLinkStats(url) 
{
	showConfirmPopup(url, lang.variables['ptd.t.confirmDeleteLinkStats'])
}

function reportLink(url) 
{
	showConfirmPopup(url, lang.variables['ptd.t.confirmReportLink'])
}

function showConfirmPopup(url, content)
{
	popup.show( {
		content : 	content,
		title : 	lang.variables['global.w.confirm'],
		buttons : [ {
			label : lang.variables['global.w.yes'],
			close : true,
			javascript : function() {
				window.location.replace(url);
		}
		}, {
			label : lang.variables['global.w.no'],
			close : true
		} ]
	});
}
