﻿var interval;
var slider;
var sectionWidth;
var totalWidth;

var slideInterval = 120000;

$(function() {

    $('#BookNow').attr('disabled', 'disabled');

    $('#images').wrapInner('<div id="slidecontainer"><div id="slider" /></div>');
    slider = $('#slider');



    var imgs = slider.find('img');
    var img = $(imgs[0]);

    var r = (imgs.length % 4);
    var dummy = new Array();

    while ((r % 4) != 0) {

        dummy.push('<div class="img-dummy">&nbsp;</div>');
        r++;

    }

    slider.append(dummy.join(''));

    var width = (img.width() + parseInt(img.css('marginRight'), 10));
    totalWidth = width * (imgs.length + dummy.length);
    sectionWidth = width * 4;

    // console.log(width);
    slider.width(totalWidth);

    if (imgs.length > 4) {
        $('#slidecontainer').before('<img id="larrow" src="/content/img/site/l-arrow.png" width="23" height="23" /><img id="rarrow" src="/content/img/site/r-arrow.png" width="23" height="23" />');
        $('#rarrow').bind('click', slideLeft);
        $('#larrow').bind('click', slideRight);
        $('#larrow, #rarrow').bind('mouseover', function() {
            clearInterval(interval);
        }).bind('mouseout', function() {
            interval = setInterval(slideLeft, slideInterval);
        });

            interval = setInterval(slideLeft, slideInterval);

        slider.find('a').bind('mouseover', function() {
            clearInterval(interval);
        }).bind('mouseout', function() {
            interval = setInterval(slideLeft, slideInterval);
        }).bind('click', function() {
            var href = $(this).attr('href');
            if (href.match(/jpg$/gi))
                $('#lge').html('<img src="' + href + '" width="729" />');
            else
                $('#lge').load('/embedfeed/729/400/' + href.substring(1) + '/', function() { });

            return false;
        });

    }
    else {

        slider.find('a').bind('click', function() {
            var href = $(this).attr('href');
            if (href.match(/jpg$/gi))
                $('#lge').html('<img src="' + href + '" width="729" />');
            else
                $('#lge').load('/embedfeed/729/400/' + href.substring(1) + '/', function() { });

            return false;
        });

    }




    $('#ctab2').hide();
    $('#ctabs a[href=#ctab1]').addClass('current');
    $('#ctabs a').click(function() {
        var t = $(this);

        $('#ctabs a').removeClass();
        $('.ctab').hide();

        t.addClass('current');
        $(t.attr('href')).show();
        t.blur();
        return false;
    });

});

function slideLeft() {

    if ((totalWidth + parseInt(slider.css('left'), 10)) > sectionWidth)
        slider.animate({ left: '-=' + sectionWidth }, 500, function() { });
    else
        slider.animate({ left: '0' }, 500, function() { });

}

function slideRight() {

    if (parseInt(slider.css('left'), 10) < 0)
        slider.animate({ left: '+=' + sectionWidth }, 500, function() { });
    else
        slider.animate({ left: (totalWidth - sectionWidth) * -1 }, 500, function() { });

}
