$(document).ready(function() {
    var maxWidth;
    var maxHeight;
    
    var FADESPEED = 700;
    var currentIndex = 0;
    
    $("#price-list a").live('click', function() {
        currentIndex = $(this).parent().index();
        $("#price-list li a").removeClass("active");
        $(this).addClass("active");
        var eleWidth = $("#pricing-overflows > div").width();
        var goTo = eleWidth * currentIndex * -1;
        $("#pricing-overflows").animate({"left": goTo + "px"},FADESPEED);
        return false;
    });
    
    $("#galleryThumbs a").live('click', function() {
        var src = $(this).attr('href');
        $("#galleryThumbs a.activeThumb").removeClass('activeThumb').animate({opacity: 0.5}, { duration: 200, queue: false });
        $(this).addClass('activeThumb');
        loadImage(src);
        return false;
    });
    $("#thumbnails .wrapper a").live('mouseover mouseout',function (ev) {
        if (ev.type == 'mouseover') { $(this).animate({opacity: 1.0}, { duration: 200, queue: false }); }
        if (ev.type == 'mouseout') { if (!$(this).hasClass('activeThumb')) { $(this).animate({opacity: 0.5}, { duration: 200, queue: false }); } }
    });
    $("#previous,#next").live('click', function() {
        var src = $(this).attr('href');
        $("#thumbnails").load(src + " #thumbnails");
        return false;
    });
    
    function loadImage(src) {
        $(function () {
          var img = new Image();
          $(img)
            .load(function () {
              $(this).hide();
              $('#image-placeholder').html(this);
              $('#image-placeholder img').css('margin', '0 auto');
              ratio($(this));
              $(this).fadeIn();
            })
            .error(function () { alert("Something went wrong, please contact Waters and Waters."); })
            .attr('src',  src);
        });
    }
    function resizeGallery(id) {
        if (document.getElementById(id)) {
            maxWidth = $("#"+id).width();
            var r = maxWidth / 975;
            maxHeight = Math.round(650 * r);
            $("#"+id).css('width', maxWidth+'px');
            $("#"+id).css('height',  maxHeight+'px');
        }
    }
    function ratio(ele) {
        var x = $(ele).width();
        var y = $(ele).height();
        var new_size;
        if (x > maxWidth) {
            new_size = Math.round(y / x * maxWidth);
            $(ele).css('width', Math.round(maxWidth) + 'px');
            $(ele).css('height', new_size + 'px');
        }
        else {
            new_size = Math.round(x / y * maxHeight);
            $(ele).css('width', new_size + 'px');
            $(ele).css('height', Math.round(maxHeight) + 'px');
        }
    }
    resizeGallery("image-placeholder");
    
    if (document.getElementById('thumbnails')) {
        $("#thumbnails .holder").css('height', $("#main .holder").height()+'px');
        var firstImage = $("#galleryThumbs a").first();
        loadImage($(firstImage).attr('href'));
    }
    if (document.getElementById('leftBar')) {
        $("#leftBar .holder").css('height', $("#main .holder").height()+'px');
    }
    if (document.getElementById('galleryShow')) {
        resizeGallery("galleryShow");
        $('.slideshow').cycle({ 
            fx: 'custom', 
            speed: 600,
            timeout : 4000,
            pause:  1,
            cssBefore: {
                display: 'block',
                left: 0,  
                top:  0,  
                width: $("#galleryShow").width(),
                height: $("#galleryShow").height(),  
                opacity: 0, 
                zIndex: 1
            }, 
            animOut: { opacity: 0 }, 
            animIn: {
                left: 0,  
                top:  0,  
                width: $("#galleryShow").width() +1,
                height: $("#galleryShow").height() +1,  
                opacity: 1, 
                zIndex: 5  
            }, 
            cssAfter: { zIndex: 0,display: 'none', }
        });
    }
});
