function naviPicOn(sId, sNewUrl){
	var oImage = document.getElementById(sId);
	if (oImage && oImage.src) oImage.src = sNewUrl;
	return true;
}


function open_print(sUrl, sRequest){
	printwin = window.open(sUrl + '?browser=print&' + sRequest,'printwin','scrollbars=1, width=557, height=400, position left=50, position top=50, resizable=1');
	printwin.focus();
}


function open_mail(sUrl, sTitle, sRequest){
	var sUrlRequest = "";
	if(sRequest.length > 0){
		sUrlRequest = escape('?' + sRequest);
		}
	mailwin = window.open('sendlink.htm?browser=print&url=' + sUrl + sUrlRequest + '&title=' + sTitle,'mail','scrollbars=0, width=600, height=400, position left=50, position top=50, resizable=1');
	mailwin.focus();
}


function ChangeLocation(form){
	var gotourl = form.navurl.value;
	if(gotourl.indexOf("http://") != -1) {
		window.open('' + gotourl);
	} else {
		var location_url = "" + gotourl;
		location.href = location_url;		
	}	
}

$(document).ready(function() {
	Favorites.Init();
});

var Favorites = {

	Init: function () {
	
		//Message boxes
		$("#showfav").bind("click", function(i) {
			$("#facontainer").show();
			$("#title").focus();
			$("#faccontent").empty().html('<img src="loading.gif" alt="Wait" />');
			$.getJSON('favinterface?action=getfav', Favorites.ShowData);
			return false;
		});
		
		
		$("#addfac").bind("click", function(i) {
			if ($("#title").val().length == 0) {
				alert("Title kann nicht leer sein");
			}
			else {
				var sUrl = $("#facaddform").serialize();
				
				$.getJSON('favinterface?action=addfav&'+sUrl, function (data) {
					if (data.status=="ok") {
						$("#title").val("");
						$.getJSON('favinterface?action=getfav', Favorites.ShowData);
					}
					else {
						alert(data.error);
					}
				});
			}
			return false;
		});
		
		$("#closecontainer").bind("click", function (i) {
			$("#facontainer").hide();
			return false;
		});
		
		$(".delfav").live("click", function (i) {
			var iFavId = $(this).attr("rel");
			$.getJSON('favinterface?action=delfav&favid='+iFavId, function (data) {
				if (data.status=="ok") {
					$.getJSON('favinterface?action=getfav', Favorites.ShowData);
				}
				else {
					alert(data.error);
				}
			});
			return false;
		});
	},
	
	ShowData: function(data) {
		var items = [];
		
		if (data.results && data.results.length) {
			for (var i=0; i < data.results.length; i++) {
				var oRow = data.results[i]
				items.push('<li><a href="#" rel="' + oRow.id  + '" class="delfav"><img src="trash-icon.png" width="16" height="16" border="0"></a><a href="' + oRow.url  + '">' + oRow.title + '</a></li>');
			}
		}
		
		$("#faccontent").empty();
		$('<ul/>', {
			'class': 'favlist',
			html: items.join('')
		}).appendTo('#faccontent');
			
	}
	
}

