$(function () {

	$('.artist').each(function () {
		var thiz = this,
			title = $(thiz).find('.pic img').attr('data-name'),
			titlec = $('<div class="title"></div>');

		if ( ! title ) { return true; }

		$.each(title.split(''), function (i, v) {
			titlec.append('<span class="alpha_' + ( v.toLowerCase() == ' ' ? 'space' : v.toLowerCase() ) + '"></span>');
		});

		titlec.appendTo( thiz );
		
		titlec.data('WIDTH', 0);
		titlec.find('span').each(function () {
			var w = $(this).width();
			w = titlec.data('WIDTH') + w + 1;
			titlec.data('WIDTH', w);
		});
		
		var $title = $(thiz).find('.title');
		$title.data('timeout', 0);
		
		$(thiz).find('.pic')
			.bind('mouseenter', function () {
				$(this).stop().animate({opacity: 0});
				$title.trigger('resetscroll');
				$title.scrollTo('0', {
					duration: 0,
					axis: 'x',
					easing: 'linear'
				});
				$title.css('opacity', 1);
				$title.trigger('scrollatonce');
			})
			.bind('mouseleave', function () {
				$(this).stop().animate({opacity: 1});
				$title.trigger('resetscroll');
				$title.css('opacity', 0);
			});
			
		$title.bind('resetscroll', function () {
			clearTimeout($title.data('timeout'));
			$title.stop();
			/*
			$title.scrollTo('0', {
				duration: 0,
				axis: 'x',
				easing: 'linear'
			});
			*/
		});
		
		$title.bind('scrollatonce', function () {
			// console.log('scrollatonce');
			$title.data('WIDTH', 0);
			$title.find('span').each(function () {
				var _w = $(this).width();
				_w = $title.data('WIDTH') + _w + 1;
				$title.data('WIDTH', _w);
			});
			
			var W = $title.data('WIDTH');
			var w = $title.width();
			var l = $title.scrollLeft();
			
			var dw = W - w - l;
			
			// console.log(dw);
			
			// console.log(dw);
			
			if (dw <= 0) {
				return false;
			}
			
			$title.trigger('resetscroll');
			
			var timeout = setTimeout(function () {
				// console.log('timeout');
				$title.scrollTo('+=' + dw + 'px', {
					duration: dw * 25,
					axis: 'x',
					easing: 'linear',
					onAfter: function () {
						var h = $title.html();
						$title.html(
							h +
							'<span class="alpha_separator"></span>' +
							h
						);
						
						setTimeout(function () {
							$title.trigger('scrollatonce');
						}, 0);
					}
				});
			}, (l == 0) ? 1100 : 0);
			
			$title.data('timeout', timeout);
		});
	});
	
	$('#slideshowc')
		.data('timeout', 0)
		.bind('next', function (e, d) {
			var THIS = this;
			
			clearTimeout($(THIS).data('timeout'));
			
			function __f() {
				var z = $(THIS).find('div:visible').next();
				if ( ! z.find('img').size() ) {
					z = $(THIS).find('div:first');
				}

				var i = $(THIS).find('div').index( z ) + 1;

				$(THIS).find('div').css('display', 'none').css('opacity', 0);

				z.css('display', 'block').stop().animate({opacity: 1}, 300);

				$(THIS).trigger('next');
			}
			
			var q = setTimeout(function () {
				__f();
			}, 10000);
			
			$(THIS).data('timeout', q);
			
			if (d) {
				clearTimeout($(THIS).data('timeout'));
				__f();
			}
		})
		.bind('prev', function (e, d) {
			var THIS = this;
			
			clearTimeout($(THIS).data('timeout'));
			
			function __f() {
				var z = $(THIS).find('div:visible').prev();
				if ( ! z.find('img').size() ) {
					z = $(THIS).find('div:last');
				}

				var i = $(THIS).find('div').index( z ) + 1;

				$(THIS).find('div').css('display', 'none').css('opacity', 0);

				z.css('display', 'block').stop().animate({opacity: 1}, 300);

				$(THIS).trigger('next');
			}
			
			var q = setTimeout(function () {
				__f();
			}, 1500);
			
			$(THIS).data('timeout', q);
			
			if (d) {
				clearTimeout($(THIS).data('timeout'));
				__f();
			}
		})
		.bind('slideshow', function () {
			var THIS = this;

			$(THIS).find('div:eq(0)').css('display', 'block').css('opacity', 1);

			$(THIS).trigger('next');
		});

	$('#slideshowc')
		.trigger('slideshow');
		
	$('#slideshow-next')
		.bind('click', function () {
			$('#slideshowc').trigger('next', true);
		});
		
	$('#slideshow-prev')
		.bind('click', function () {
			$('#slideshowc').trigger('prev', true);
		});
		
	$('#more')
		.bind('click', function () {
			$('.artist[data-permanent="1"]').addClass('dnone');
			$('.artist[data-permanent="0"]').removeClass('dnone');
			$(this).addClass('dnone');
			$('#back').removeClass('dnone');
			$('#more-tooltip').stop().animate({opacity: 0});
		})
		.bind('mouseenter', function () {
			$('#more-tooltip').stop().animate({opacity: 1});
		})
		.bind('mouseleave', function () {
			$('#more-tooltip').stop().animate({opacity: 0});
		});
		
	$('#back')
		.bind('click', function () {
			$('.artist[data-permanent="0"]').addClass('dnone');
			$('.artist[data-permanent="1"]').removeClass('dnone');
			$(this).addClass('dnone');
			$('#more').removeClass('dnone');
		});
		
	$('#artist-date')
		.bind('makeitfull', function () {
			var $artists0 = $('#artist-date .artist[data-permanent="0"]');
			var must0 = $artists0.size() % 4;
			if (must0 != 0) {
				must0 = 4 - must0;
			}
			
			var $artists1 = $('#artist-date .artist[data-permanent="1"]');
			var must1 = $artists1.size() % 4;
			if (must1 != 0) {
				must1 = 4 - must1;
			}
			
			if (must0 != 0) {
				for (i = 0; i < must0; i++) {
					$('<div></div>')
						.addClass('artist')
						.addClass('dnone')
						.attr('data-permanent', '0')
						.appendTo($('#artist-date'));
				}
			}
			
			if (must1 != 0) {
				for (i = 0; i < must1; i++) {
					$('<div></div>')
						.addClass('artist')
						.attr('data-permanent', '1')
						.appendTo($('#artist-date'));
				}
			}
		});
	$('#artist-date').trigger('makeitfull');
	
	$('#artist-alpha')
		.bind('makeitfull', function () {
			var $artists0 = $('#artist-alpha .artist[data-permanent="0"]');
			var must0 = $artists0.size() % 4;
			if (must0 != 0) {
				must0 = 4 - must0;
			}
			
			var $artists1 = $('#artist-alpha .artist[data-permanent="1"]');
			var must1 = $artists1.size() % 4;
			if (must1 != 0) {
				must1 = 4 - must1;
			}
			
			if (must0 != 0) {
				for (i = 0; i < must0; i++) {
					$('<div></div>')
						.addClass('artist')
						.addClass('dnone')
						.attr('data-permanent', '0')
						.appendTo($('#artist-alpha'));
				}
			}
			
			if (must1 != 0) {
				for (i = 0; i < must1; i++) {
					$('<div></div>')
						.addClass('artist')
						.attr('data-permanent', '1')
						.appendTo($('#artist-alpha'));
				}
			}
		});
	$('#artist-alpha').trigger('makeitfull');
	
	$('#sort-alpha')
		.bind('mouseenter', function () {
			if ($('#sort-alpha').hasClass('active')) {
				return false;
			}
			
			$('#sort-alpha-tooltip').stop().animate({opacity: 1});
		})
		.bind('mouseleave', function () {
			$('#sort-alpha-tooltip').stop().animate({opacity: 0});
		})
		.bind('click', function () {
			$('#sort-date').removeClass('active');
			$('#sort-alpha').addClass('active');
			$('#artist-date').addClass('dnone');
			$('#artist-alpha').removeClass('dnone');
			$('#sort-alpha-tooltip').stop().animate({opacity: 0});
		});
		
	$('#sort-date')
		.bind('mouseenter', function () {
			if ($('#sort-date').hasClass('active')) {
				return false;
			}
			
			$('#sort-date-tooltip').stop().animate({opacity: 1});
		})
		.bind('mouseleave', function () {
			$('#sort-date-tooltip').stop().animate({opacity: 0});
		})
		.bind('click', function () {
			$('#sort-alpha').removeClass('active');
			$('#sort-date').addClass('active');
			$('#artist-alpha').addClass('dnone');
			$('#artist-date').removeClass('dnone');
			$('#sort-date-tooltip').stop().animate({opacity: 0});
		});
		
	$('#about')
		.bind('click', function (e) {
			e.preventDefault();
			$('body').css('overflow', 'hidden');
			$('#about-page').removeClass('dnone');
		});
		
	$('#about-page')
		.bind('click', function () {
			$('body').css('overflow', 'auto');
			$(this).addClass('dnone');
		});
		
	$('#footer .arash')
		.bind('mouseenter', function () {
			$('#footer .arash-tooltip')
				.css('opacity', '0')
				.css('display', 'block')
				.stop().animate({opacity: 1});
		})
		.bind('mouseleave', function () {
			$('#footer .arash-tooltip')
				.css('display', 'none');
		});
		
	$('#footer .top a')
		.bind('click', function (e) {
			e.preventDefault();
			$(document).scrollTo(0, {duration: 150});
		});
		
	$('#newsletter input[type="text"]')
		.bind('focus', function () {
			$('#email-error').css('display', 'none');
			$('#newsletter').addClass('input');
		})
		.bind('blur', function () {
			if ($(this).val() == '') {
				$('#newsletter').removeClass('input');
			}
		});
		
	$('#newsletter input[type="submit"]')
		.bind('mouseenter', function () {
			$('#newsletter .submit')
				.css('opacity', '0')
				.css('display', 'block')
				.stop().animate({opacity: 1});
		})
		.bind('mouseleave', function () {
			$('#newsletter .submit')
				.css('display', 'none');
		});
		
	$('form')
		.bind('submit', function ( e ) {
			e.preventDefault();

			function emailCheck(emailStr) {
				patt = /^[_a-zA-Z0-9-](\.{0,1}[_a-zA-Z0-9-])*@([a-zA-Z0-9-]{2,}\.){0,}[a-zA-Z0-9-]{1,}(\.[a-zA-Z]{2,4}){1,2}$/;
				if ( emailStr.match(patt) ) { return true; }
				else { return false; }
			}

			if ( ! emailCheck( $('#email-input').val() ) ) {
				$('#email-input')
					.trigger('blur');

				$('#email-error')
					.css('opacity', 0)
					.css('display', 'block')
					.stop()
					.animate(
						{opacity: 1},
						{duration: 200}
					);
			} else {
				$.ajax({
					url: 'index.php',
					type: 'post',
					data: { email: $('#email-input').val() },
					error: function () {
						alert('Ooops! Please try again.');
					},
					success: function ( d ) {
						if ( d == 'true' ) {
							$('#email-success')
								.css('display', 'block')
								.stop()
								.animate(
									{opacity: 1},
									{duration: 200}
								);
						} else {
							alert('Ooops! Please try again.');
						}
					}
				});
			}
		});
		
	$('#footerbarc')
		.bind('mouseenter', function () {
			$('#footerbar').stop().animate({opacity: 1});
		})
		.bind('mouseleave', function () {
			$('#footerbar').stop().animate({opacity: 0.95});
		});
		
	$(window)
		.bind('scroll', $.debounce(10, function (e) {
			var t = $(window).scrollTop();
			if (t) {
				$('#footer .top').removeClass('deactive');
			}
			else {
				$('#footer .top').addClass('deactive');
			}
		}));
	
});
