/**
 * @author michal
 */
function Bookmark(me){

	var added = false;
	var title_status = me.title_status;
	
	var thumbnail = new Ext.Element(document.createElement('div'));
	thumbnail.update(getContent(me.url));
	thumbnail.on('click', me.onMaximize);
	thumbnail.addClass('x-thumbnail');
	thumbnail.applyStyles({
		width: '120px',
		height: '90px',
		overflow: 'hidden'
	})
	
	var short_title = new Ext.Element(document.createElement('div'));
	short_title.update('<small>' + getTitle(me.title) + '</small>');
	short_title.on('click', titleEdit);
	short_title.addClass('x-bookmark-bbar x-toolbar');
	short_title.applyStyles({
		cursor: 'pointer',
		overflow: 'hidden'
	});
	
	var full_title = new Ext.form.TextArea({
		value: me.title,
		cls: 'x-title-area',
		width: '100%',
		height: '100%',
		enableKeyEvents: true,
		hidden: (title_status == 1) ? false : true,
		key_count: 0
	});
	full_title.on('keyup', function(){
		me.title = full_title.getValue();
		full_title.key_count++;
		var count = full_title.key_count;
		setTimeout(function(){
			if (count == full_title.key_count) {
				var title = full_title.getValue();
				if (title) {
					me.onTitleChange(title);
				}
			}
		}, 3000);
	});
	
	var switch_element = new Ext.Element(document.createElement('div'));
	switch_element.addClass('x-switch');
	switch_element.on('click', function(){
		if (title_status == 1) {
			title_status = 0;
			full_title.hide();
		}
		else {
			title_status = 1;
			full_title.show();
		}
		me.onTitleStatusChange(title_status);
	});
	
	var el = new Ext.Panel({
		frameId: me.id,
		title: '<small>' + trimUrl(me.url, 10) + '</small>',
		height: 148,
		width: 132,
		style: 'margin-top: 8px; margin-left: 8px; float: left;',
		items: [full_title, thumbnail, short_title, switch_element],
		frame: true,
		draggable: true,
		tools: [{
			id: 'gear',
			handler: me.onGear
		}, {
			id: 'maximize',
			handler: me.onMaximize
		}, {
			id: 'close',
			handler: me.onClose
		}]
	});
	
	function getTitle(title){
	
		var trim_title = title.substring(18, 0);
		if (trim_title != title) {
			trim_title += '\u2026';
		}
		return trim_title;
	}
	
	function getContent(url){
		return '<a href="' + url + '" onclick="return false;"><img class="thumbnail" src="http://open.thumbshots.org/image.aspx?url=' + url + '" alt="' + msg.thumbnailAlt + '" /></a>'
	}
	
	function setMaximize(){
		maximize();
	}
	
	function titleEdit(o){
		full_title.show();
		title_status = 1;
		me.onTitleStatusChange(title_status);
	}
	
	this.show = function(){
		if (added == false) {
			me.renderTo.add(el);
			me.renderTo.doLayout();
			added = true;
			layout.desktopResize();
		}
		else {
			el.show();
		}
	}
	
	this.hide = function(){
		el.hide();
	}
	
	this.setUrl = function(url){
		el.setTitle('<small>' + trimUrl(url, 10) + '</small>');
		thumbnail.update(getContent(url));
	}
	
	this.setTitle = function(title){
		short_title.update('<small>' + getTitle(title) + '</small>');
		full_title.setValue(title);
	}
	
	this.setTitleStatus = function(value){
		if (title_status != value) {
			title_status = value;
			if (!added) {
				full_title.hidden = (title_status == 1) ? false : true;
			}
			else if (title_status == 1) {
				full_title.show();
			}
			else if (title_status == 0) {
				full_title.hide();
			}
		}
	}
	
	this.destroy = function(){
		me.renderTo.remove(el);
		me.renderTo.doLayout();
	}
	
	function trimUrl(url, len){
		// odstranit http:// a pod
		url = url.replace(/^.*\/{2}/, '');
		// odstranit www.
		url = url.replace(/^www\./, '');
		// odstranit parametry za otaznikem
		url = url.replace(/\?.*$/, '');
		// odstranit koncove lomitko
		url = url.replace(/\/*$/, '');
		
		// "w" je prilis siroke, proto je potreba zkratit retezec
		var pos = url.search(/www/);
		if (pos > -1) {
			len -= 2;
		}
		
		if (len > 0 && url.length > len) {
			// zkratit
			url = url.substring(len - 1, 0);
			// odstranit pripadnou tecku na konci
			url = url.replace(/\.$/, '');
			url += '\u2026';
		}
		
		return url;
	}
}


