function leaves(options) {
	this.container = null;
	this.timeout = null;
	
	this.settings = {
		data: [],
		index: 0,
		width: 250,
		timeout: 5000
	}
	
	var self = this,
		ps = jQuery.extend(this.settings, options);
	
	ps.data.pop();
	
	this.leaves = function() {
		var html = '<div class="fl_l"><div class="bg"></div><div class="text"><img src="/templates/Default/images/left.png" width="6" /></div></div>',
			page = 1;
		
		for(var i in ps.data) {
			page = parseInt(i) + 1;
			
			html += '<div class="fl_l"><div class="bg '+ ((parseInt(i) == ps.index) ? 'selected' : '') +'"></div><div class="text '+ ((parseInt(i) == ps.index) ? 'text_selected' : '') +'">'+ page +'</div></div>';
		}
		
		html += '<div class="fl_l"><div class="bg"></div><div class="text"><img src="/templates/Default/images/right.png" width="6" /></div></div>';
		html += '<div class="clearFix"></div>';
		
		return html;
	}
	
	this.refresh = function() {
		jQuery('#'+ ps.id +'_leaves').html( this.leaves() );
		this.events();
		
		jQuery('#'+ ps.id +'_img').children('a')
				.replaceWith('<a href="'+ ps.data[ ps.index ].url +'">'+ ps.data[ ps.index ].img +'</a>');
		
		jQuery('#'+ ps.id +'_caption').html( '<a href="'+ ps.data[ ps.index ].url +'">'+ ps.data[ ps.index ].caption +'</a>' );
	}
	
	this.timer = function() {
		clearTimeout(this.timeout);
		this.timeout = setTimeout(function() {
			jQuery('#'+ ps.id +'_leaves').find('div.fl_l').last().click();
		}, ps.timeout);
	}
	
	this.events = function() {
		
		jQuery('#'+ ps.id +'_leaves').children('div.fl_l').mouseover(function(event) {
				if(!jQuery(this).children('div.selected').length) {
					jQuery(this).children('div.bg').css({
						backgroundColor: '#fff',
						opacity: 0.3
					});
					
					if( !jQuery(this).first().length ) {
						jQuery(this).children('div.text').css({
							borderLeft: '1px solid #666',
							color: '#000'
						});
					}
				}
			})
			.mouseout(function(event) {
				if(!jQuery(this).children('div.selected').length) {
					jQuery(this).children('div.bg').css({
						backgroundColor: '#000',
						opacity: 0.3
					});
					
					if( !jQuery(this).first().length ) {
						jQuery(this).children('div.text').css({
							borderLeft: '1px solid #888',
							color: '#fff'
						});
					}
				}
			})
			.click(function(event) {
				var index = jQuery(this).index();
				
				if(index == 0) {
					if(ps.index == 0) ps.index = ps.data.length - 1;
					else ps.index -= 1;
					
					self.refresh();
				}
				
				if(index > 0 && index <= ps.data.length) {
					ps.index = index-1;
					
					self.refresh();
				}
				
				if(index > ps.data.length) {
					if(ps.index == (ps.data.length-1)) ps.index = 0;
					else ps.index += 1;
					
					self.refresh();
				}
				
				self.timer();
			});
	}
	
	this.render = function() {
		if( ps.data[ ps.index ] ) {
			var cache = jQuery('<div></div>')
				.appendTo(document.body)
				.css({
					position: 'absolute',
					top: 0,
					left: 0,
					visibility: 'hidden'
				});
			
			// кэширование рисунков
			for(var i in ps.data)
			{
				cache.html( cache.html() + ps.data[i].img );
				
				ps.data[i].img = ps.data[i].img.replace(/\<p\>/, '').replace(/\<\/p\>/, '');
			}
			
			this.container = jQuery('#' + ps.id);
			
			this.container.html('<div id="'+ ps.id +'_img" class="image">\
									 <a href="'+ ps.data[ ps.index ].url +'">'+ ps.data[ ps.index ].img +'</a>\
									 <div id="'+ ps.id +'_leaves" class="leaves_list">'+ this.leaves() +'</div>\
								 </div>\
								 <div id="'+ ps.id +'_caption" class="caption"><a href="'+ ps.data[ ps.index ].url +'">'+ ps.data[ ps.index ].caption +'</a></div>')
							.css({
								width: ps.width
							})
							.addClass('leaves');
		}
	}
	
	this.render();
	this.events();
	this.timer();
}


function sendFriend(url)
{
	var e = {};
	e['Отправить'] = function() {
		if( $.trim( $('input[name="from"]').val() ) == '' ) {
			$('#warn').show().html('Пожалуйста, укажите от кого придет сообщение');
			return false;
		}
		else $('#warn').hide();
		
		if( $.trim( $('input[name="to"]').val() ) == '' ) {
			$('#warn').show().html('Пожалуйста, укажите e-mail получателя');
			return false;
		}
		else $('#warn').hide();
		
		if( !(/[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+/.test( $('input[name="to"]').val() ) ) ) {
			$('#warn').show().html('Пожалуйста, укажите корректный e-mail получателя');
			return false;
		}
		else $('#warn').hide();
		
		$.get('/engine/ajax/itv.php?act=send_friend', {
				from: encodeURIComponent( $.trim( $('input[name="from"]').val() ) ),
				to: $.trim( $('input[name="to"]').val() ),
				link: $.trim( $('input[name="link"]').val() ),
				caption: encodeURIComponent( $.trim( $('textarea[name="caption"]').val() ) )
			}, function(response) {
		});
		
		$(this).dialog('close');
	}
	e['Отмена'] = function() {
		$(this).dialog('close');
	}
	
	$('#sendfriend').remove();
	
	$('<div id="sendfriend" title="Отправить другу" style="display: none;">\
		<form id="sendfriend_form">\
		<div class="dialog-block">\
			<div id="warn" class="warn" style="display: none;">\
				\
			</div>\
		</div>\
		<div class="dialog-block">\
			<div class="dialog-title">От кого:</div>\
			<div class="dialog-input">\
				<input class="ui-widget-content ui-corner-all" name="from" style="width: 260px; padding: 3px;" value="" />\
			</div>\
			<div class="clearFix"></div>\
		</div>\
		<div class="dialog-block">\
			<div class="dialog-title">E-Mail друга:</div>\
			<div class="dialog-input">\
				<input class="ui-widget-content ui-corner-all" name="to" style="width: 260px; padding: 3px;" value="" />\
			</div>\
			<div class="clearFix"></div>\
		</div>\
		<div class="dialog-block">\
			<div class="dialog-title">Ссылка:</div>\
			<div class="dialog-input">\
				<input class="ui-widget-content ui-corner-all" name="link" style="width: 260px; padding: 3px;" value="'+ url +'" readonly="true" />\
			</div>\
			<div class="clearFix"></div>\
		</div>\
		<div class="dialog-block">\
			<div class="dialog-title">Сообщение:</div>\
			<div class="dialog-input">\
				<textarea class="ui-widget-content ui-corner-all" name="caption" style="width: 260px; padding: 3px; height: 52px; overflow: hidden;"></textarea>\
			</div>\
			<div class="clearFix"></div>\
		</div>\
		</form>\
		\
		</div>').appendTo(document.body);
	
	$('#sendfriend').dialog({
		autoOpen: true,
		width: 471,
		buttons: e
	});
}
