$(function(){
	/* ENG */
	// FP text
	$('#center.eng #fp-text').pir({
		color : 'fff',
		font : '2',
		wrap : true
	});
	$('#center.eng #fp-new-text').pir({
		color : 'fff',
		font : 'y'
	});
	// Collection list item nr
	$('#center #collection .desc .nr .no').pir({
		color : '000',
		font : '3',
		wrap: true
	});
	$('#center #collection .desc .nr .nr').pir({
		color : '000',
		font : '4'
	});
	// Collection list item name
	$('#center.eng #collection .desc h1').pir({
		color : '000',
		font : '3',
		wrap: true
	});
	// Collection item nr and name
	$('#center #collection-item-content .nr .no, #ci-nr .no').pir({
		color : '969696',
		font : '3'
	});
	$('#center #collection-item-content .nr .nr, #ci-nr .nr').pir({
		color : '969696',
		font : '4'
	});
	$('#center.eng #collection-item-content h1, #ci-content h1, .eng #lux-right h1').pir({
		color : 'fff',
		font : '3',
		wrap: true
	});
	// Investments list item name
	$('#center.eng #investments .desc h1').pir({
		color : 'fff',
		hoverColor: '191919',
		font : '3'
	});
	
	/* RUS */
	// FP text
	$('#center.rus #fp-text').pir({
		color : 'fff',
		font : '11',
		wrap : true
	});
	
	// Collection list item name
	$('#center.rus #collection .desc h1').pir({
		color : '000',
		font : '10',
		wrap: true
	});
	// Collection item nr and name
	$('#center.rus #collection-item-content h1').pir({
		color : 'fff',
		font : '10',
		wrap: true
	});
	// Investments list item name
	$('#center.rus #investments .desc h1').pir({
		color : 'fff',
		hoverColor: '191919',
		font : '10'
	});
	
	
	
	// OLD
	$('#fp a span').hide();
	$('#fp #fp-collection, #fp #fp-investments').hover(function(){
		$('span', this).fadeIn(500);
	}, function(){
		$('span', this).fadeOut(500);
	});
	// NEW
	$('#fp-new #fp-collection, #fp-new #fp-investments').hover(function(){
		$('span', this).stop().fadeTo(500, 0);
		$('em', this).stop().fadeTo(500, 1);
	}, function(){
		$('span', this).stop().fadeTo(500, 1);
		$('em', this).stop().fadeTo(500, 0);
	}).find('em').css('display', 'block').fadeTo(0, 0);
	
	/* Collection item list */
	$('#collection .item .pic').hover(function(){
		$(this).closest('.item').addClass('hover');
	}, function(){
		$(this).closest('.item').removeClass('hover');
	});
	
	/* Collection item view */
	$.each(['details','description','email'], function(_, type) {
		$('#ci-menu a.'+type).click(function() {
			$(this).addClass('open').siblings().removeClass('open');
			$('#ci-text .card.'+type).fadeIn().siblings().fadeOut('open');
			$('#ci-text .card.'+type+' .scroller-open:not(.isdone)').css({
				height: '100%',
				position: 'relative',
				overflow: 'hidden'
			}).jScrollPane({
				scrollbarWidth: 10,
				scrollbarMargin: 10
			}).addClass('isdone');
			return false;
		});
	});
	if($('#ci-pics-inner a').length) {
		var cont = $('#ci-pics-inner'), pics = $('#ci-pics-inner a'), n = pics.length, delta = 0, target = 0, l = cont.offset().left, left = 0, tmp = [], i = 1;
		$.each(pics, function() {
			tmp.push($('<img>').attr('src', $(this).attr('href')));
			$(this).data('nr', i++);
		});
		while(n < 10) {
			i = 1;
			cont.append(pics.clone().each(function() {
				$(this).data('nr', i++);
			}));
			n += pics.length;
		}
		$('#ci-picnum').fadeTo(0, .8).find('.total').text(pics.length);
		cont.find('a').bind('click', function() {
			var that = $(this);
			$('<img>').attr('src', that.attr('href')).appendTo('#ci-pic').hide().fadeIn(500, function() {
					try {
					if(!$(this).hasClass('old')) {
						$(this).siblings('img').remove();
					}
				} catch(ex){
				}
			}).click(function() {
				that.next().click();
				target = 0;
				delta = -12;
			}).siblings('img').addClass('old');
			$('img', this).addClass('open').stop().fadeTo(300, 1);
			that.siblings().find('img.open').removeClass('open').stop().fadeTo(300, 0.4);
			$('#ci-picnum .nr').text(that.data('nr'));
			return false;
		}).mouseenter(function() {
			$(this).find('img').stop().fadeTo(300, 1);
		}).mouseleave(function() {
			$(this).find('img:not(.open)').stop().fadeTo(300, 0.4);
		}).find('img').fadeTo(0, 0.4);
		cont.find('a:first').click();
		setInterval(function()		{
			if(delta == 0 && target == 0) {
				return;
			}
			if(delta < target) {
				delta++;
			} else if(delta > target) {
				delta--;
			}
			left += delta;
			if(left < -74) {
				left += 74;
				cont.find('a:first').appendTo(cont);
			} else if(left > 0) {
				left -= 74;
				cont.find('a:last').prependTo(cont);
			}
			cont.css('left', left);
		}, 50);
		
		$('#ci-pics').mousemove(function(e) {
			var that = $(this), x = (e.pageX - l) << 0, w = that.width();
			if(x < w / 3) {
				target = (w - 3 * x) / w;
			} else if(x > w * 2 / 3) {
				target = 2 - 3 * x / w;
			}
			target = (target * 20) << 0;
			if(target > 1000 ||target < -1000) {
				target = 0;
			}
		}).mouseleave(function() {
			target = 0;
		});
	}
	/* OLD */
	$('#collection-item-pics .pic')
		.find('.big').css('display', 'block').fadeTo(0, 0).end()
		.find('.thumb .cover').css('background-color', '#000').css('display', 'block').fadeTo(0, .6);
	$('#collection-item-pics .thumb').mouseenter(function(){
		var toShow = $(this).closest('.pic').find('img.big');
		$('#collection-item-pics img.big').not(toShow).stop().fadeTo(600, 0);
		$('#collection-item-pics .pic .thumb .cover').not($('.cover', this)).stop().fadeTo(300, .6);
		toShow.stop().fadeTo(600, 1);
		$('.cover', this).stop().fadeTo(300, 0);
	});
	$('#collection-item-pics .pic img.big').css('display', 'block').fadeTo(0, 0);
	$('#collection-item-pics .pic-1 .thumb').mouseenter();
	/* /OLD */
	
	/* Investments list */
	$('#investments .item')
		.find('.cover').css('background-color', '#000').stop().fadeTo(0, .4);
	$('#investments .item').hover(function(){
		$('#investments .item').not(this).find('.cover').stop().fadeTo(300, .4);
		$(this).addClass('hover').find('.cover').stop().fadeTo(300, 0);
	}, function() {
		$(this).removeClass('hover').find('.cover').stop().fadeTo(300, .4);
	});
	$('#collection-item-pics .pic-1 .thumb').mouseover();
	
	/* Custom scrollbars */
	setTimeout(function(){
		$('.scroller, #ci-text .card.open .scroller-open').css({
			height: '100%',
			position: 'relative',
			overflow: 'hidden'
		}).jScrollPane({
			scrollbarWidth: 10,
			scrollbarMargin: 10
		});
		$('#ci-text .card.open .scroller-open').addClass('isdone');
	}, 100);
	
	
	// V-club form
	$('#v-club').submit(function() {
		var ok = true, t;
		t = $('[name=fname]', this);
		if(t.val() == '') { ok = false; t.css('border-color', '#c32'); }
		t = $('[name=lname]', this);
		if(t.val() == '') { ok = false; t.css('border-color', '#c32'); }
		t = $('[name=email]', this);
		if(t.val() == '') { ok = false; t.css('border-color', '#c32'); }
		
		return ok;
	});
	
	
	// Blog mail
	$('a.blog-mail-link').click(function() {
		$('#blog-mail input, #blog-mail textarea').val('');
		$('#blog-mail-post').val($(this).attr('rel'));
		$('#blog-mail').removeClass('message');
		$('#blog-mail form').fadeIn();
		return false;
	});
	$('#blog-mail .close a').click(function() {
		$('#blog-mail form').fadeOut();
		return false;
	});
	$('#blog-mail form').submit(function() {
		var name = $('#blog-mail-name').val(),
			email = $('#blog-mail-email').val(),
			email2 = $('#blog-mail-email2').val(),
			message = $('#blog-mail-message').val();
		
		if(name == '') {
			alert('Please insert your name');
			$('#blog-mail-name').focus();
			return false;
		}
		if(email == '') {
			alert('Please insert your E-mail address');
			$('#blog-mail-email').focus();
			return false;
		}
		if(email2 == '') {
			alert('Please insert your friend\'s E-mail address');
			$('#blog-mail-email2').focus();
			return false;
		}
		
		$('#blog-mail-msg').text('Sending message...');
		$('#blog-mail').addClass('message');
		$.post($('#blog-mail form').attr('action')+'/!sendmail.json', {
			post : $('#blog-mail-post').val(),
			name : name,
			email : email,
			email2 : email2,
			message : message
		}, function(data) {
			if(data == 'done') {
				$('#blog-mail-msg').text('Message sent!');
			} else {
				$('#blog-mail-msg').text('Incorrect e-mail address!');
			}
			setTimeout(function() {
				$('#blog-mail form').fadeOut(2000);
			}, 300);
		}, 'json');
		
		return false;
	});
	
	// Collection item mail
	$('#ci-mail').submit(function() {
		var email = $('#ci-mail-email').val();
		
		if(email == '') {
			alert('Please insert e-mail address');
			$('#ci-mail-email').focus();
			return false;
		}
		
		$('#ci-mail-msg').text('Sending message...').fadeIn();
		$.post($('#ci-mail').attr('action')+'/!sendmail.json', {
			item : $('#ci-mail-item').val(),
			email : email
		}, function(data) {
			if(data == 'done') {
				$('#ci-mail-msg').text('Message sent!');
			} else {
				$('#ci-mail-msg').text('Incorrect e-mail address!');
			}
			setTimeout(function() {
				$('#ci-mail-msg').fadeOut(2000);
			}, 300);
			$('#ci-mail-email').val('');
		}, 'json');
		
		return false;
	});
	
	// Invite a friend
	$('#invite-a-friend').click(function() {
		if($('#invite form').is(':visible')) {
			return false;
		}
		$('#invite input').val('');
		$('#invite').removeClass('message');
		$('#invite form').fadeIn();
		$('#invite-a-friend').fadeOut();
		return false;
	});
	$('#invite .close a').click(function() {
		$('#invite form').fadeOut();
		$('#invite-a-friend').fadeIn();
		return false;
	});
	$('#invite form').submit(function() {
		var name = $('#invite-name').val(),
			email = $('#invite-email').val();
		
		if(name == '') {
			alert('Please insert friend\'s name');
			$('#invite-name').focus();
			return false;
		}
		if(email == '') {
			alert('Please insert E-mail address');
			$('#invite-email').focus();
			return false;
		}
		
		$('#invite-msg').text('Sending invitation...');
		$('#invite').addClass('message');
		$.post($('#invite form').attr('action')+'/!invite.json', {
			name : name,
			email : email
		}, function(data) {
			if(data == 'done') {
				$('#invite-msg').text('Message sent!');
			} else {
				$('#invite-msg').text('Incorrect e-mail address!');
			}
			setTimeout(function() {
				$('#invite form').fadeOut(2000);
				$('#invite-a-friend').fadeIn();
			}, 300);
		}, 'json');
		
		return false;
	});
	
	// Luxury properties
	var lux_pics = [];
	$('#lux-pics a').click(function() {
		$('<img>').attr('src', $(this).attr('href')).appendTo('#lux-pic').hide().fadeIn(500, function() {
			try {
				if(!$(this).hasClass('old')) {
					$(this).siblings().remove();
				}
			} catch(ex){
			}
		}).siblings().addClass('old');
		$('img', this).addClass('open').fadeTo(300, 1);
		$(this).siblings().find('img.open').removeClass('open').fadeTo(300, 0.4);
		return false;
	}).mouseenter(function() {
		$(this).find('img').stop().fadeTo(300, 1);
	}).mouseleave(function() {
		$(this).find('img:not(.open)').stop().fadeTo(300, 0.4);
	}).each(function() {
		lux_pics.push($('<img>').attr('src', $(this).attr('href')));
	}).find('img').fadeTo(0, 0.4).eq(0).click();
});