/* 
	Author: Axis41
*/

(function($) {

    // Tabs
    /* 
    * Add a class of 'tab-selected' to the tab you want to show first
    */
    var fadeSpeed = 'fast',
		isIE8 = $('html').hasClass('ie8');

    $('.tab-set a')
		.each(function() { // Activate the tab content that corresponds to the currently active tab

		    var target = $($(this).attr('href'));

		    target.addClass('tab-container');
		    if ($(this).parent().hasClass('tab-selected')) {
		        if (isIE8) {
		            $(target).show(0);
		        }
		        else {
		            $(target).fadeIn(fadeSpeed);
		        }
		    }
		})
		.click(function() {

		    var tab = $(this).closest('li'),
				target = $($(this).attr('href'));

		    if (!tab.hasClass('tab-selected')) {

		        tab.addClass('tab-selected').siblings().removeClass('tab-selected');

		        if ($(target).siblings(':visible').length > 0) {
		            if (isIE8) {
		                $(target).siblings(':visible').hide(0, function() {
		                    target.show(0);
		                });
		            }
		            else {
		                $(target).siblings(':visible').fadeOut(fadeSpeed, function() {
		                    target.fadeIn(fadeSpeed);
		                });
		            }
		        } else {
		            if (isIE8) {
		                target.show(0);
		            }
		            else {
		                target.fadeIn(fadeSpeed);
		            }
		        }

		    }

		    return false;
		});

    // Expanding/collapsing boxes
    /*
    * Just add a class of "closed" to the <section> you want collapsed.
    */
    $('.collapsible a.control')
		.each(function() {
		    if ($(this).closest('section').hasClass('closed')) $(this).addClass('closed').text('expand');
		})
		.click(function() {
		    $(this).toggleClass('closed').text(($(this).hasClass('closed') ? 'expand' : 'collapse')).closest('section').find('.collapsible-content').toggleClass('closed').slideToggle('fast');
		    return false;
		});

    // Storage finder default value replacement
    var theForm = $('#storage-finder'),
		theInputs = $('.user-location'),
		theSubmitButton = $('.storage-finder-submit');

    theInputs
		.each(function() {
		    $(this)
				.data('defaultValue', $.trim($(this).val()))
				.val($(this).data('defaultValue'))
				.bind('focusin focusout', function() {
				    if ($.trim($(this).val()) == '') {
				        $(this).val($(this).data('defaultValue'));
				    } else if ($.trim($(this).val()) == $(this).data('defaultValue')) {
				        $(this).val('');
				    }
				});
		});

    theSubmitButton
		.click(function() {

		    defaultValueSubmitted = false;

		    theInputs
				.each(function() {
				    if ($.trim($(this).val()) == $(this).data('defaultValue')) {
				        defaultValueSubmitted = true;
				    }
				});

		    if (!defaultValueSubmitted) {
		        theForm.submit();
		    } else {
		        return false;
		    }

		});

    // Equalize columns
    var tallestColumnHeight = 0;
    $('#locations-filter div > ul')
			.children()
			.each(function() {
			    var thisColumnHeight = $(this).height();
			    tallestColumnHeight = (thisColumnHeight > tallestColumnHeight) ? thisColumnHeight : tallestColumnHeight;
			})
			.css({
			    height: tallestColumnHeight
			});

    // Zebra striping
    $('.striped tbody tr:odd').addClass('alt');

    /* Re-sort results
    $('.sort-results select')
		.change(function() {
		    alert('Sort!');
		});*/

    // Gallery and scrolling menu

    var scrollerContainer = $('#gallery-scroller');

    if (scrollerContainer.length > 0) {

        var nextButton = scrollerContainer.find('.next'),
			prevButton = scrollerContainer.find('.prev'),
			scrollerHeight = scrollerContainer.find('div').innerHeight(),
			scrollable = scrollerContainer.find('div > ul'),
			scrollableHeight = scrollable.innerHeight(),
			scrollablePosition = scrollable.position(),
			overlap = scrollerHeight - scrollableHeight,
			increment = 4,
			incrementPx = increment * scrollable.children(':first').outerHeight(true),
			scrollSpeed = 400;

        // The gallery menu and large image swap
        scrollerContainer.find('li')
			.filter(':first')
				.addClass('selected')
			.end()
			.click(function() {

			    if ($(this).hasClass('selected')) return false;

			    var path = $('img', this).attr('src'),
					thisIndex = $(this).index(),
					pathArray = path.split('.jpg'),
					file = pathArray[0],
					fileSrc = file + '.jpg&width=316&height=238';

			    scrollerContainer.find('li.selected').removeClass('selected');
			    $(this).addClass('selected');

			    $('#location-image img').fadeOut('fast', function() {
			        $(this)
						.attr({
						    src: fileSrc,
						    alt: thisIndex
						})
						.fadeIn('slow');
			    });
			});

        // The gallery menu scrolling
        if (scrollable.children().length > 4) {

            if (scrollableHeight > scrollerHeight) nextButton.addClass('active');

            scrollerContainer.find('span')
				.click(function() {

				    if (scrollable.is(':animated')) return false; // Do nothing if we're already scrolling

				    var scrollDirection = $(this).hasClass('next') ? 'up' : 'down',
						currentPosition = scrollable.position(),
						oldX = currentPosition.top,
						newX = scrollDirection == 'up' ? oldX - incrementPx : oldX + incrementPx;

				    // Show/hide prev button
				    if (newX < 0) {
				        prevButton.addClass('active');
				    } else {
				        prevButton.removeClass('active');
				    }

				    // Show/hide next button
				    if (newX <= overlap) {
				        nextButton.removeClass('active');
				    } else {
				        nextButton.addClass('active');
				    }

				    // Perform the scrolling animation
				    scrollable.animate({
				        top: newX
				    }, scrollSpeed);

				});

        } else {
            scrollerContainer.addClass('no-scroll');
        }
    }

})(this.jQuery);
