﻿// jQuery for setting equal heights to maintain consistent look across DIVs
$.fn.equalHeights = function() {
    var tallest = 0;

    $(this).each(function() {
        if ($(this).height() > tallest) { tallest = $(this).height(); }
    });

    $(this).each(function() {
        $(this).css({ 'height': tallest });
    });

    return this;
};

// Load those images with 80% of their original size!
$(window).load(function () {
    $('img.tourmap').each(function () {
        $(this).width($(this).width() * 0.80);
        $(this).fadeIn('slow');
    });
});

$(document).ready(function() {
    // These correctly size divs in order for them to keep looks consistent across the compared tours
    $('.countries-size').equalHeights();
    $('.cities-size').equalHeights();
    $('.dates-thisyear-size').equalHeights();
    $('.dates-nextyear-size').equalHeights();
    $('.title-size').equalHeights();
    $('.rating-size').equalHeights();

    // FancyZoom ftw
    $('.disclaimer-link').fancyZoom({ content: 'disclaimer' });

    $('.ViewMapBtn').unbind('click').bind('click', function() {
        $('#TourMap').attr('src', $(this).attr('mapurl'));
        return false;
    }).fancyZoom();

    // Grab url params
    var tour1 = queryHash("c1");
    var tour2 = queryHash("c2");
    var tour3 = queryHash("c3");

    // Now it's time to remove the unused DIVs (in case they're only comparing 2 tours)
    if (tour3 == null) { $('#tour_three').css({ 'display': 'none' }); }

    // Just use the querystring/hash as the return link to keep all filters
    if (tour3 == null && tour2 == null && tour1 == null) {
        $('#returnLink').removeAttr('href').attr('href', '/search/');
    }
    else {
        $('#returnLink').removeAttr('href').attr('href', '/search/#' + window.location.search.substring(1));
    }

    if (tour3 == null) {
        $('.RemoveOne').unbind('click').bind('click', function() {
            alert('You must keep at least 2 tours to compare.');
            return false;
        });
        $('.RemoveTwo').unbind('click').bind('click', function() {
            alert('You must keep at least 2 tours to compare.');
            return false;
        });
    }
});

// Rather than the usual querystring with param=value&param2=value2, we're using a hash, so this just
// splits it up with '.' separating elements instead of the usual '&', and '-' instead of '='
function queryHash(what) {
    string = window.location.search.substring(1);
    array = string.split(".");
    for (i = 0; i < array.length; i++) {
        temp = array[i].split("-");
        if (temp[0] == what) {
            return temp[1];
        }
    }
    return null;
}
