fft = {
	user_id:null,
	
	imageTarget :null,
	
	imageWidth:480,
	imageHeight:321,
	
	minWidth:50,
	minHeight:50,
	maxWidth:250,
	maxHeight:250,
	
	serTitel:null,
	serDate:null,
	ser:null,
	
	sizerID:'#resizeMe',
	
	targetID:null,

	start:function (targetID) {
		if ($('#'+targetID).length > 0) {
		
			fft.stop();
			$(document.body).append('<div id="fft-saver"><h1>Person auswählen</h1><form id="fft-saver-taged-user"><div id="fft-saver-pchoser"><center><img src="facebook_connect/images/loader.gif" /></center></div></form><span style="color:#666">Kommentar hinzufügen:</span><textarea id="fft-textarea" style="height:60px; width:215px;" onfocus="this.value="""></textarea><button class="fft-button" onclick="fft.save()">Speichern </button></div>');
			$(document.body).append('<div id="resizeMe"><div id="resizeSE"></div><div id="resizeE"></div><div id="resizeNE"></div><div id="resizeN"></div><div id="resizeNW"></div><div id="resizeW"></div><div id="resizeSW"></div><div id="resizeS"></div></div>');
			
			$('#fft-saver-pchoser').load('facebook_connect/load_frinds.php');
			
			var imageTop = $('#'+targetID).offset().top;
			var imageLeft = $('#'+targetID).offset().left;
			
			$('#fft-saver').css({'top':imageTop - 5,'left':imageLeft + fft.imageWidth + 10});
			
			fft.targetID = targetID;
			
			$(fft.sizerID).Resizable(
				{
					minWidth: fft.minWidth,
					minHeight: fft.minHeight,
					maxWidth: fft.maxWidth,
					maxHeight: fft.maxHeight,
					minTop: imageTop,
					minLeft: imageLeft,
					maxRight: imageLeft+fft.imageWidth,
					maxBottom: imageTop+fft.imageHeight,
					dragHandle: true,
					handlers: {
						se: '#resizeSE',
						e: '#resizeE',
						ne: '#resizeNE',
						n: '#resizeN',
						nw: '#resizeNW',
						w: '#resizeW',
						sw: '#resizeSW',
						s: '#resizeS'
					}
				}
			).css ({'left':imageLeft,'top':imageTop});
		}else{
			alert("The target = '"+targetID+"' dossen't exist! Please check targetID!");	
		}
	},
	
	save:function () {
		
		var imageTop  = $('#'+fft.targetID).offset().top;
		var imageLeft = $('#'+fft.targetID).offset().left;

		var sizerTop  = $(fft.sizerID).offset().top;
		var sizerLeft = $(fft.sizerID).offset().left;

		var relativTop  = sizerTop - imageTop;
		var relativLeft = sizerLeft - imageLeft;
		var sizerWidth  = $(fft.sizerID).css('width');
		var sizerHeight = $(fft.sizerID).css('height');
		
		var imageRec = "facebook_image_drwa.php?fotopath="+fft.imageTarget+"&left="+relativLeft+"&top="+relativTop+"&width="+sizerWidth+"&height="+sizerHeight;
		var comment = document.getElementById('fft-textarea').value;
		
		var tagedUser = ($('#fft-saver-taged-user').serialize()).toString();
			tagedUser = tagedUser.split('fft-tag-user=')[1];
			
		fft.stop();
		fft_box.showbox();
		fft_box.loadbox('Wo möchtest du deinen Eintrag veröffentlichen? <ul><li rel="pinwand"><h1>Pinwand</h1><p>Das Foto mit Kommentar auf deiner Pinwand bei Facebook und auf der Seite Veröffentlichen.</p></li><li rel="album"><h1>Fotoalbum</h1><p>Das Foto mit Kommentar in einer deiner Fotalbem auf Facebook und auf der Seite Veröffentlichen.</p></li></ul><div style="clear:both"></div>');		
		$('#fft-status-box-area>ul>li').hover(function(){
				$(this).addClass('fft-hover');
			},function(){
				$(this).removeClass('fft-hover');
				}).click(function () {
					var rel = $(this).attr('rel');
					
					if (rel == "pinwand"){
						fft_box.clearbutton();
						fft.savetofeed(relativLeft,relativTop,sizerWidth,sizerHeight,comment,imageRec,tagedUser);
					}
					
					if (rel == "album") {
						fft_box.clearbutton();
						fft.savetoalbum(relativLeft,relativTop,sizerWidth,sizerHeight,comment,fft.imageTarget,tagedUser);
					}
					
					});
				
				
		fft_box.centerbox();
		$(fft_box.createButton('CLOSE')).click(function(){fft_box.hidebox();});

	},
	
	
	savetoalbum:function (left,top,width,height,comment,image,tagedUser) {
		fft_box.clearbox();
		fft_box.showloaderbar();
		
		$.get("facebook_connect/get_albums.php",{'titel':fft.serTitel+" "+fft.serDate},function (data) {
			fft_box.loadbox(data);
			fft_box.hideloaderbar();
			
			$(fft_box.createButton('OK')).click (function () {
				fft_box.clearbutton();
				
				var postfild = $('#fft-umage-uploadform').serialize();
					postfild += '&titel='+fft.serTitel;
					postfild +=	'&comment='+comment;
					postfild +=	'&date='+fft.serDate;
					postfild += '&image='+image;
					postfild += '&ser='+fft.ser;
					postfild +=	'&top='+top;
					postfild += '&left='+left;
					postfild += '&width='+width;
					postfild += '&height='+height;
					postfild += '&pic='+fft.imageTarget;
					postfild += '&tagedUser='+tagedUser;
					
					
					
					fft_box.loadbox('Eintrag wird veröffentlicht...');
					fft_box.showloaderbar();
					fft_box.centerbox();
					fft_box.loadprewiev("<img src='http://"+window.location.hostname+"/"+image+"' width='150' style='float:right;' hspace='5'/>"+comment+"<div style='clear:both'></div>");
					fft_box.centerbox();
					fft.stop();
					
					
				
					$.post("facebook_connect/post_entry.php",postfild,function (code) {
						if (code.indexOf('001') != -1 || code.indexOf('003') != -1) {
							fft_box.loadbox('<strong style="color:#F00">Ein interner Fehler ist aufgetreten, bitte versuchen Sie es später noch einmal.</strong>');
							fft_box.hideloaderbar();
							$(fft_box.createButton('OK')).click(function() {
								fft.stop();
								fft_box.hidebox();
								});
						}
						
						if (code.indexOf('002') != -1) {
							fft_box.loadbox('<strong style="color:#0C3">Der Eintrag wurde auf Facebook veröffentlicht!</strong>');
							fft_box.hideloaderbar();
							$(fft_box.createButton('OK')).click(function() {
								fft.stop();
								fft_box.hidebox();
								$.fancybox.reload();
								});
						}
						
					});
				
				});
			
			});
	},
	
	
	savetofeed:function (left,top,width,height,comment,image,tagedUser) {
		fft_box.loadbox('Eintrag wird veröffentlicht...');
		fft_box.showloaderbar();
		fft_box.centerbox();
		fft_box.loadprewiev("<img src='http://"+window.location.hostname+"/frontend/facebook_connect/"+image+"' width='150' style='float:right;' hspace='5'/>"+comment+"<div style='clear:both'></div>");
		fft_box.centerbox();
		fft.stop();
		
		$.post("facebook_connect/post_entry.php",{'titel':fft.serTitel,
													'comment':comment,
													'date':fft.serDate,
													'image':image,
													'ser':fft.ser,
													'top':top,
													'left':left,
													'width':width,
													'height':height,
													'pic':fft.imageTarget,
													'tagedUser':tagedUser
													},function (code) {									
			if (code.indexOf('001') != -1 || code.indexOf('003') != -1) {
				fft_box.loadbox('<strong style="color:#F00">Ein interner Fehler ist aufgetreten, bitte versuchen Sie es später noch einmal.</strong>');
				fft_box.hideloaderbar();
				$(fft_box.createButton('OK')).click(function() {
					fft.stop();
					fft_box.hidebox();
					});
			}
			
			if (code.indexOf('002') != -1) {
				fft_box.loadbox('<strong style="color:#0C3">Der Eintrag wurde auf Facebook veröffentlicht!</strong>');
				fft_box.hideloaderbar();
				$(fft_box.createButton('OK')).click(function() {
					fft.stop();
					fft_box.hidebox();
					$.fancybox.reload();
					});
			}
			
		});
	},
	
	delete_entry:function (ID,elem) {
		$(elem).html('<img src="facebook_connect/images/loader-smal.gif" border="0"/>');
		$.get('facebook_connect/imagetag_delete.php',{ID:ID},function (data) {
			if (data.indexOf('001') != -1) {
				$.fancybox.reload();
			}
			});
	},
	
	
	removeFeedback:function () {
		$('#fft-feedback-container').remove();
	},
	
	stop:function () {
		$(fft.sizerID).remove();
		$('#fft-saver').remove();
	},
		
	initStatusbar:function () {
		$('#facebook-status-container').css({'display':'block'});
		var height = $('#facebook-status-inner').innerHeight()+3;
		$('#facebook-status-container').css({'top':-height});	
		$('#facebook-status-container').hover (
			function () {
				$(this).stop().animate({'top':0},300);
				},
			function () {
				var height = $('#facebook-status-inner').innerHeight()+3;
				$(this).stop().animate({'top':-height},300);
				}
		);
	}
}



fft_box = {
	
	showbox:function () {
		if ($('#fft-statsu-box-container').length > 0) fft_bod.hidebox();
		if (fft.targetID) {
			$('#'+fft.targetID).append ('<div id="fft-statsu-box-container"><div id="fft-status-box-inner-padding"><div id="fft-status-box-inner-content"> <h1> Far-Out > Facebook </h1><div id="fft-status-box-area"></div><div id="fft-status-box-loader-container"></div><div id="fft-status-box-preview"></div><div id="fft-status-box-bottom"></div></div></div></div>');
			$('#fft-statsu-box-container').stop().fadeIn('fast');
		}
	},
	
	hidebox:function () {
		$('#fft-statsu-box-container').stop().fadeOut('fast',function () {
			$('#fft-statsu-box-container').remove();
			});
		
	},
	
	centerbox:function () {
		var t = $('#fft-statsu-box-container');
		var width = $(t).innerWidth();
		var height = $(t).innerHeight();
		$(t).css({'left':'50%','top':'50%','marginLeft':-width/2,'marginTop':-height/2});
	},
	
	
	loadbox:function (htmlData) {
		$('#fft-status-box-area').html(htmlData);
	},
	
	clearbox:function () {
		$('#fft-status-box-area').html(' ');
		$('#fft-status-box-preview').html(' ');
	},
	
	loadprewiev:function (htmldata) {
		$('#fft-status-box-preview').html(htmldata);
	},
	
	
	createButton:function (label) {
		var string = '<button class="fft-button" id="fft-button-'+label+'">'+label+'</button>';
		$('#fft-status-box-bottom').append(string);
		return $('#fft-button-'+label);
	},
	
	
	showloaderbar:function () {
		$('#fft-status-box-loader-container').html("<center><img src='facebook_connect/images/loader.gif'/></center>");
	},
	
	
	hideloaderbar:function () {
		$('#fft-status-box-loader-container').html('<span></span>');
	},
	
	clearbutton:function () {
		$('.fft-button').each(function () {
			$(this).remove();
			});
	}
}
$(document).ready(
			function() {
			fft.initStatusbar();
        });
	
