function SmoothScroll() {
	$('.scroll').click(function() {
	  if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
	  && location.hostname == this.hostname) {
		var $target = $(this.hash);
		$target = $target.length && $target
		|| $('[name=' + this.hash.slice(1) +']');
		if ($target.length) {
		  var targetOffset = $target.offset().top;
		  $('html,body')
		  .animate({scrollTop: targetOffset}, 1000);
		 return false;
		}
	  }
	});
}

function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

// Accordion
function Accordion() {

   	var togglers = $('.accordion .toggler');
   	var elements = ".accordion .element";

   var elementsWithErrors = $("ul.accordion li div.element:has(ul li div.error)");

   if (elementsWithErrors.size() > 0) {
   		$(elements).hide();   
   		elementsWithErrors.show();
   	}
   	else {
   		$(elements).hide();
   		$(elements + ':first').show()
   		$('.accordion .toggler a span').addClass('active');
   	}


   	togglers.click(
	function() {
		var checkElement = $(this).next();
		if ((checkElement.is(elements)) && (checkElement.is(':visible'))) {
			$(elements + ':visible').slideUp(300);
			$(this).children().removeClass('active');
			return false;
		}
		if ((checkElement.is(elements)) && (!checkElement.is(':visible'))) {
			$(elements + ':visible').slideUp(300);
			checkElement.slideDown(300);
			$(this).children().addClass('active');
			return false;
		}
	});

}


$(document).ready(function() {

    $.addthis('Barracuda');
    $(document).pngFix();
    SmoothScroll();
    Accordion();

    $("a[rel*='external']").attr({ target: "_blank" });

    var volatile = $(".field input")
    volatile.focus(function(event) {
        if ($(this).val() == $(this).attr("title")) {
            $(this).val("");
        } 
    }, volatile.blur(function(event) {
        if ($(this).val() == "") {
            $(this).val($(this).attr("title"));
        }
    }
    ));

    $(".barToggler").click(function(e) {
        e.preventDefault();
        $("#barAccordian").slideToggle("fast");
    });

    Cufon.replace('h1, #nav li, #socialBar form label, #socialBar a, .menuList span, .voucherDownload span, #content .participantList h2, #content .contact h2, .alertMail h2, .alertFacebook h2, .bookingContainer h2, #content #searchBars h2', { fontFamily: 'avant-garde', hover: true });
    Cufon.replace('.module .hd h1, .module .hd h2', { fontFamily: 'avant-garde', hover: true/*, textShadow: '#575757 1px 1px'*/ });

    jQuery.fn.fadeToggle = function(speed, easing, callback) {
        return this.animate({ opacity: 'toggle' }, speed, easing, callback);
    };

    var fadeToggler = $(".loginToggler")
    var fadeElement = $(".loginOverlay")

    $(fadeToggler).click(function(event) {
        event.preventDefault();
        fadeElement.fadeToggle("normal");
    });

    $('#overlayMap').empty();
    $('#overlayMap').flash({
        src: '/_images/flash/test.swf',
        wmode: 'transparent',
        width: 440,
        height: 280
    });

    //Custom selectboxes

    var selectBoxWidth;

    if ($('.galleryFilter .select select')) {
        selectBoxWidth = '250px';
    }
    else {
        selectBoxWidth = '160px';
    }

    $('#content .bookingForm .select select').selectmenu({ style: 'dropdown', width: '303px' });
    $('#content .bookingForm .dateSelect select').selectmenu({ style: 'dropdown', width: '95px' });
    $('#content .checkoutForm .dateSelect select').selectmenu({ style: 'dropdown', width: '95px' });
    $('#content .contact .select select').selectmenu({ style: 'dropdown', width: '299px' });
    $('#content .checkoutForm .select select').selectmenu({ style: 'dropdown', width: '299px' });
    $('#content .contact .dateSelect select').selectmenu({ style: 'dropdown', width: '93px' });
    $('#content #searchBars .select select').selectmenu({ style: 'dropdown', width: '167px' });
    $('#barAccordian fieldset .field.select select').selectmenu({ style: 'dropdown', width: '204px' });


    $('input').customInput();

    $('.checkboxField .selectPackage input').each(function(index) {
        $(this).attr('checked', false);
        $(this).next().removeClass('checked')
    });

    $('.checkboxField .selectPackage input').click(function() {
        $(this).parent().next().children('dd').toggleClass('selected');
    });

    var slideCounter = $('#carousel ul li').size();
    $("#carousel ul:not(:first) a").attr("tabIndex", "-1");
    $("#carousel ul").carouFredSel({
        items: 1,
        scroll: {
            pauseOnHover: true,
            easing: "easeOutQuint",
            duration: 1000,
            onBefore: function(oldItems, newItems) {
                oldItems.children('a').attr("tabIndex", "-1");
                newItems.children('a').removeAttr("tabIndex");
            }
        },
        auto: 6000,
        prev: {
            button: ".prev",
            key: "left"
        },
        next: {
            button: ".next",
            key: "right"
        }
    });


    $('#sideCol .flashContainer').empty();
    $('#sideCol .flashContainer').flash({
        src: '/_images/flash/Call-to-Action-pod.swf',
        width: 252,
        height: 202,
        WMODE: 'transparent',
        flashvars: { xmlPath: "/_images/flash/xml.xml", configPath: "/_images/flash/config.xml" }

    });

    jQuery('#vcardCarousel').jcarousel({
        auto: 3,
        scroll: 1,
        wrap: 'last',
        initCallback: mycarousel_initCallback
    });

    // Tool Tip

    $('ul.venueFacilities li').each(function() {
        $(this).qtip({
            show: { delay: 0 },
            content: $(this).children('span').html(),
            style: {
                tip: 'topMiddle',
                name: 'dark'
            },
            position: {
                corner: {
                    target: 'bottomMiddle',
                    tooltip: 'topMiddle'
                }
            }   
        });
    });

    function getContent(el) {
        var text = el.html();
        return text
    }

    //Lightbox

    var lightbox = $(".barGallery").ariaLightbox({
        imageArray: "a.lightbox",
        altText: function() {
            // $(this) is the triggered element (in this case the link element)
            return $(this).find("img").attr("alt");
        },
        descText: function() {
            return $(this).find("img").attr("title");
        },
        useDimmer: true,
        pos: "auto",
        titleText: "Gallery",
        em: false,
        disableWidth: 1,
        disableHeight: 1
    });

    //Iframe
    $('.iframe').click(function(e) {
        e.preventDefault();
        var $this = $(this);

        $('<div><iframe class="externalSite" width="100%" height="100%" frameborder="0" src="' + this.href + '" /></div>').dialog({
            title: ($this.attr('title')) ? $this.attr('title') : 'External Site',
            width: 700,
            height: ($this.attr('rel')) ? $this.attr('rel') : 500,
            close: function(event, ui) {
                $(".ui-widget-overlay").remove();
                $(this).dialog("destroy");
                $(".externalSite").remove();
            }
        });

        $(".ui-dialog").has('iframe').before("<div class='ui-widget-overlay'></div>");
        $(".ui-widget-overlay").css("height", $(document).height() + "px").css("width", $(document).width() + "px").css("z-index", "1001");

        $(window).resize(function() {
            $(".ui-widget-overlay").css("height", $(document).height() + "px").css("width", $(document).width() + "px");
        });

    });

    //Our Brands
    $(".ourBrands .bd ul li a").hover(function() {
        $(this).children('span').stop().animate({ top: 0 });
    }, function() {
        $(this).children('span').stop().animate({ top: 118 });
    });

});
