var tat_savebox = {
	URLEncode: function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
  while(x<c.length){var m=r.exec(c.substr(x));
    if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
    }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
    o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
  while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
  t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;},
  
	init: function() {
		jQuery('body').append('<div id="savebox" style="position:absolute; top:0px; left:0px; font-family:Georgia,\'Times New Roman\',Times,serif; font-size:15px; font-weight:normal; align:left"></div>');
		this.reset();
	},
	reset: function() {
		jQuery('#savebox').html('<div class="background" style="position:absolute; top:0px; left:0px; width:'+jQuery(document).width()+'px; height:'+jQuery(document).height()+'px; z-index:15; background-color:black"></div>');
		jQuery('#savebox .background').fadeTo('fast', 0.5);
		jQuery('#savebox').hide();
	},
	bookmark: function(title) {
		this.box('<b>' + title + jQuery.translate('</b> has been saved to your Bookmarks.<br /><br />Find all your saved pages in your My Account &gt; My Preferences.'), false);
	},
	search: function(title, description, keyword) {
		if(keyword == "") 
			keywordMes = '';
		else
			keywordMes = ', '+jQuery.translate('Keyword')+' &quot;' + keyword + '&quot;';
		this.box(jQuery.translate('Your Search  below  has been saved to your Saved Searches.')+'<br /><b>' + title + ': </b>' + description + keywordMes + '<br /><br />'+jQuery.translate('Find all your saved searches in your My Account &gt; My Preferences.'), false);
	},
	box: function(title, isFailed) {
		this.customBox(title, (isFailed == false ? jQuery.translate('Success') : jQuery.translate('We\'re Sorry!')));
	},
	customBox: function(title, header) {
		var position = 'fixed';
		if (jQuery.browser.msie && jQuery.browser.version.substr(0,1) < 7) {
			position = 'absolute';
		}
		jQuery('#savebox').append('<div style="position:'+position+'; top:150px; left:'+(jQuery(document).width()-468)/2+'px; z-index:16"><table width="468" border="0" cellspacing="0" cellpadding="0"><tr><td style="background-color:#005dad; padding:10px"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr style="height:24px"><td align="left" valign="top" class="subjectin-white">' + header + '!</td><td valign="top" align="right"><img src="fileadmin/templates/images/close1.gif" style="cursor: pointer" width="13" height="13" onclick="tat_savebox.reset()" alt="X" /></td></tr></table><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td style="background-color:#FFFFFF; padding:12px" align="left">' + title + '<br /><br /></td></tr><tr><td style="background-color:#FFFFFF; padding-bottom:12px" align="center"><img src="fileadmin/templates/images/done.gif" style="cursor: pointer" width="56" height="27" alt="Done" onclick="tat_savebox.reset()" /></td></tr></table></td></tr></table></div>');
		jQuery('#savebox').show();
	},
	fail: function() {
		this.box(jQuery.translate('You must be logged-in to use this feature!'), true);
	},
	saveBookmark: function() {
		var sid = '';
		var text = document.cookie;
		if (text.length > 0) {
			var start = text.indexOf('fe_typo_user=');
			var end = text.length;
			
			if (start != -1) {
				start = start + ('fe_typo_user=').length;
				end = text.indexOf(';', start);
				if (end == -1) {
					end = text.indexOf('&', start);
					if (end == -1) {
						end = text.length;
					}
			    }
			    sid = jQuery.trim(unescape(text.substring(start, end)));
			}
		}
		
		if (sid != '') {
			var title = document.title;
			var saveData = {
				t: 'bookmark',
				title: title,
				url: this.URLEncode(document.location),
				sid: sid
			};
			jQuery.get('fileadmin/scripts/common.php', saveData, function(data) {
				if (data == '1') {
					tat_savebox.bookmark(title);
				} else {
					tat_savebox.box(jQuery.translate('Failed to bookmark this page. Please contacts administrator.'), true);
				}
			});
		} else {
			this.box(jQuery.translate('Please enables cookie before use this feature.'), true);
		}
	}
};

jQuery(document).ready(function() {
	tat_savebox.init();
});

