/*
 * BROWSER DETECTION
 * 
 * Style selector .opera .elements {}
 */
var browserType = null;
$(document).ready(function() {
	jQuery.each(jQuery.browser, function(i, val) {
		if (i == 'version' && browserType != null) {
			$('html').addClass(browserType + '-' + val.replace(".", "-"));
		} else {
			browserType = i;
			$('html').addClass(i);
		}
	});
});

/*
 * END
 */

jQuery(window).load(function() {

	// find the elements to be eased and hook the hover event
	$('div.slideMenu ul li a').hover(function() {

		// if the element is currently being animated (to a easeOut)...
		if ($(this).is(':animated')) {
			$(this).stop().animate({
				width : "305px"
			}, {
				duration : 450,
				easing : "easeOutQuad"
			});
		} else {
			// ease in quickly
			$(this).stop().animate({
				width : "305px"
			}, {
				duration : 400,
				easing : "easeOutQuad"
			});
		}
		// show label
		$(this).find('span').show();
	}, function() {
		// on hovering out, ease the element out
		if ($(this).is(':animated')) {
			$(this).stop().animate({
				width : "90px"
			}, {
				duration : 400,
				easing : "easeInOutQuad"
			})
		} else {
			// ease out slowly
			$(this).stop('animated:').animate({
				width : "90px"
			}, {
				duration : 450,
				easing : "easeInOutQuad"
			});
		}
		// hide label
		$(this).find('span').hide();
	});

	/*
	 * albums
	 */
	$('#albumsItemsList img.hide').hide();
	var currentAlbumIndex = 1;
	var albumsListSelector = $('#albumsItemsList');

	function showAlbumElement(index) {
		var href = $('#albumsItemsList a:eq(' + (index - 1) + ')').attr('href');

		$('#largeAlbumsImage img.current').hide();// ('fast', function() {
		$('#largeAlbumsImage img.current').attr('src', href).bind('onreadystatechange load', function() {
			if (this.complete) {
				var el = $(this);
				$('#largeAlbumsImage img.current').fadeIn('slow', function() {
					$('#largeAlbumsImage img.back').attr('src', el.attr('src'));
					// alert('xxx');
				});
			}
		});
		/*
		 * $(this).attr('src', href); var el = $(this); setTimeout(function() {
		 * el.fadeIn('slow'); }, 300);
		 */
		// });
		$('#albumsItemsList a').find('img').removeClass('active');
		$('#albumsItemsList a:eq(' + (index - 1) + ')').find('img').addClass('active');

		// prev
		if (index <= 1) {
			$('#largeAlbumsImage .previous').fadeOut();
		} else {
			$('#largeAlbumsImage .previous').show();
		}

		// next
		if (index > $('#albumsItemsList a.img').length - 1) {
			$('#largeAlbumsImage .next').fadeOut();
		} else {
			$('#largeAlbumsImage .next').show();
		}

		currentAlbumIndex = index;

		return false;
	}

	if (albumsListSelector.length > 0) {

		/*
		 * slider
		 */
		$('#slider').jCarouselLite({
			btnNext : ".nextS",
			btnPrev : ".prevS",
			vertical : true,
			visible : 5,
			scroll : 5,
			start : 0,
			circular : false
		});

		// showAlbumElement(1);

		$(".thumbImg").css("opacity", "0.6");
		$(".thumbImg").hover(function() {
			$(this).stop().animate({
				opacity : 1.0
			}, "slow");
		}, function() {
			$(this).stop().animate({
				opacity : 0.6
			}, "slow");
		});

		albumsListSelector.find('a.img').each(function(index, value) {
			$(this).click(function() {
				currentAlbumIndex = index + 1;
				showAlbumElement(currentAlbumIndex);
				return false;
			});
		});

		// next
		$('#largeAlbumsImage .next').click(function() {
			currentAlbumIndex += 1;
			showAlbumElement(currentAlbumIndex);
			return false;
		});
		// previous
		$('#largeAlbumsImage .previous').click(function() {
			currentAlbumIndex -= 1;
			showAlbumElement(currentAlbumIndex);
			return false;
		});

		$('#largeAlbumsImage').hover(function() {
			$(this).find('.controls').fadeIn();
		}, function() {
			$(this).find('.controls').fadeOut();
		});

	}
	/*
	 * index
	 */
	var currentIndexElement = 0;
	var changeTime = 3500;
	function changeIndexImage() {
		if (currentIndexElement > $('#bigImageIndexList li').length - 1) {
			currentIndexElement = 0;
		}

		$('#bigImageIndex').fadeOut('fast', function() {
			/*
			 * $('#bigImageIndex').attr('src', $('#bigImageIndexList li:eq(' +
			 * (currentIndexElement - 1) + ')').html()); setTimeout(function() {
			 * $('#bigImageIndex').fadeIn('slow', function() {
			 * setTimeout(function() { changeIndexImage(); }, changeTime); }); },
			 * 300);
			 */
			$(this).attr('src', $('#bigImageIndexList li:eq(' + (currentIndexElement - 1) + ')').html()).bind('onreadystatechange load', function() {
				if (this.complete) {
					$(this).fadeIn(300);
					setTimeout(function() {
						changeIndexImage();
					}, changeTime);
				}
			});
		});

		currentIndexElement += 1;
	}

	/*
	 * setTimeout(function() { changeIndexImage(); }, changeTime);
	 */

	$('#indexSlideShow').cycle({
		fx : 'fade'
	});

});

