window.onload = function () {

    // hide "prev" button
    $('.gallery_image_center #prev').hide();

	var galleryListWidth = $('.gallery_list').innerWidth();
	var galleryListInnerWidth = $('.gallery_list_inner').innerWidth();
	var galleryListHalfWidth = galleryListWidth / 2;

	$('.gallery_list_inner a.item').each(function(){

		$(this).click(function(){

            var next = $(this).parent().next().children('a');
            if(next.length > 0 ){
              // next
              $('.gallery_image_center #next').show();
            } else {
              $('.gallery_image_center #next').hide();
            }

            var prev = $(this).parent().prev().children('a');
            if(prev.length > 0 ){
              // previous
              $('.gallery_image_center #prev').show();
            } else {
              $('.gallery_image_center #prev').hide();
            }

            // title and/or description
            if ($(this).attr('title') != '' || $(this).attr('description') != '') {
              var text = '';
              if ($(this).attr('title') != '') {
                text = $(this).attr('title');
              }
              if ($(this).attr('description') != '') {
                text += ' ' + $(this).attr('description');
              }
              $('#gallery_description').text(text).show();
            } else {
              $('#gallery_description').hide();
            }
            
            var YTflag = this.href.indexOf('#youtube');
            if (YTflag !== -1) {
            	$('.gallery_image').load($(this).attr('url'));
            } else {
				$('.gallery_image')
					.html(
						'<img src="' + $(this).attr('href') + '" ' +
						'width="' + $(this).attr('largewidth') + '" ' +
						'height="' + $(this).attr('largeheight') + '" />'
						)
					.show();
            }
            
			var imgHeight = parseInt($(this).attr('largeheight'));
			if (imgHeight > 0) {
				var padTop = ($('.gallery_image_center').height() - imgHeight) / 2;
				$('.gallery_image img').css('margin-top', padTop + 'px');
			}

			$('.gallery_list a.item').removeClass('active');
			$(this).addClass('active');

			var thisPosition = $(this).position().left;
			var newPosition = thisPosition - galleryListHalfWidth + 50;
			galleryScroll(newPosition);
			return false;

		});
	});

    // move to previous image
    $('.gallery_image_center #prev').click(function(){
      var curImg = $('.gallery_list a.item.active');
      $(curImg).parent().prev().children('a').click();
      return false;
    });

    // move to next image
    $('.gallery_image_center #next').click(function(){
      var curImg = $('.gallery_list a.item.active');
      $(curImg).parent().next().children('a').click();
      return false;
    });


	if (galleryListInnerWidth > galleryListWidth) {

		$('.gallery_prev')
			.show()
			.addClass('disabled')
			.click(function(){
				var listPosition = parseInt($('.gallery_list_inner').position().left) * -1;
				var newPosition = listPosition - galleryListWidth;
				if (newPosition < 0){
					newPosition = 0;
				}
				galleryScroll(newPosition);
				return false;
			});

		$('.gallery_next')
			.show()
			.click(function(){
				var listPosition = parseInt($('.gallery_list_inner').position().left) * -1;
				var newPosition = listPosition + galleryListWidth;
				if (newPosition > galleryListInnerWidth - galleryListWidth) {
					newPosition = galleryListInnerWidth - galleryListWidth;
				}
				galleryScroll(newPosition);
				return false;
			});

	}

}

function galleryScroll (newPosition)
{

	var galleryListWidth = $('.gallery_list').innerWidth();
	var galleryListInnerWidth = $('.gallery_list_inner').innerWidth();
	var galleryListHalfWidth = galleryListWidth / 2;

	var listPosition = parseInt($('.gallery_list_inner').position().left);

	if (newPosition < 0) {
		newPosition = 0;
	}

	if (newPosition < 5) {
		$('.gallery_prev').addClass('disabled');
	} else {
		$('.gallery_prev').removeClass('disabled');
	}

	if (newPosition > galleryListInnerWidth - galleryListWidth - 5) {
		$('.gallery_next').addClass('disabled');
	} else {
		$('.gallery_next').removeClass('disabled');
	}

	$('.gallery_list').scrollTo(newPosition, 0, {duration: 700});

}
