//BREADCRUMB HOVER
var timeout    = 500;
var closetimer = 0;
var bcmenuitem = 0;
var current_div = '';

function breadcrumb_open()
{
    breadcrumb_canceltimer();
    breadcrumb_close();
    bcmenuitem = $(this).find('div').css('visibility', 'visible');
    //$(this).find('div').fadeIn();
    //bcmenuitem = $(this).find('div');
}

function breadcrumb_close() {
    if(bcmenuitem) bcmenuitem.css('visibility', 'hidden');
    //if(bcmenuitem) bcmenuitem.hide();
}

function breadcrumb_timer() {
    closetimer = window.setTimeout(breadcrumb_close, timeout);
}

function breadcrumb_canceltimer() {
    if(closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}


$(document).ready(function () {
    //BREADCRUMB HOVER
    $('.breadcrumb > ul > li').bind('mouseover', breadcrumb_open);
    $('.breadcrumb > ul > li').bind('mouseout',  breadcrumb_timer);

    //Sliding left nav
    $(".slide .sidenav").hide();
    $(".slide li.sideheading").click(function(){
        $(this).toggleClass("active").next().slideToggle("slow");
        return false;
    });


	$("#tabby").tabs();
	$("#destinations").tabs();


// Uniform Form Styling Script
    $("select, input:checkbox, input:radio, input:file").uniform();
	//$("select, input:checkbox").uniform();


    /* HEADER IMAGES */

    //Below we have the cycler handle the overlay pointer, but if we have a single image,
    //we need to enable the pointer for that image initially
    /*
    var firstimg = jQuery('.banner ul li:first');
    var filoc = firstimg.children('a').attr('href');
    if(filoc && filoc != '#') {
        $('.banner .overlay').css('cursor', 'pointer');
    }*/

// Jquery Cycle Script
	$('#slider').cycle({
		fx:     'fade',
		speed:  1000,
		timeout: 6000,
		next:   '.next',
		prev:   '.prev',
        after:  function(currSlideElement, nextSlideElement, options) {
            var href = $(nextSlideElement).children('a').attr('href');
            var cur = (href && href != '#')?'pointer':'default';
            $('.banner .overlay').css('cursor', cur);
        }
	});

    $('.banner .overlay').click(function(e) {
        var b = jQuery('.banner ul li:visible');
        var loc = b.children('a').attr('href');
        if(loc && loc != '#') {
            window.location = loc;
        } else if($(this).parent(0).attr('href')) {
            window.location = $(this).parent(0).attr('href');
        }
        e.preventDefault();
        return false;
    });


// Toggle the Search filter
	$(".refine").click(function () {
		$(".refine-search").slideToggle();
		$(this).toggleClass("refineactive");
    });
    $(".close").click(function () {
		$(".refine-search").slideToggle();
    });

// Popup hider in destinations map displayer
/*
	$(".ui-widget-content ul li").toggle(
	  function () {
	    $(this).addClass("selected");
	    $(".ui-widget-content ul li").not(this).removeClass("selected");
	  },
	  function () {
	    $(this).removeClass("selected");
	  }
	);
	$(".messageBox a.close").click(
	  function () {
	  	$(".ui-widget-content ul li").removeClass("selected");
	  }
	);

	$(".messageBox a.readmore").click(
	  function () {
	  	window.location = this.href;
	  }
	);

	$("#m_destinationmap area").click(
		function () {
			$(".map-overlay li").not(this).removeClass("selected");
			$("#"+this.alt).addClass("selected");
			return false;
	  }
	);
*/

//jCarousel Plugin
    $('#carousel').jcarousel({
		vertical: false,
		scroll: 1,
		auto: 0,
		wrap: 'last',
        animation: 'slow',
		initCallback: mycarousel_initCallback
   	});

//Front page Carousel - Initial Setup
   	$('div#slideshow-carousel a img').css({'opacity': '0.5'});
   	$('div#slideshow-carousel a img:first').css({'opacity': '1.0'});
   	$('div#slideshow-carousel li a:first').append('<span class="arrow"></span>')


//Combine jCarousel with Image Display
    $('div#slideshow-carousel li a').hover(
       	function () {
       		if (!$(this).has('span').length) {
        		$('div#slideshow-carousel li a img').stop(true, true).css({'opacity': '0.5'});
   	    		$(this).stop(true, true).children('img').css({'opacity': '1.0'});
       		}
       	},
       	function () {
       		$('div#slideshow-carousel li a img').stop(true, true).css({'opacity': '0.5'});
       		$('div#slideshow-carousel li a').each(function () {
       			if ($(this).has('span').length) $(this).children('img').css({'opacity': '1.0'});
       		});
       	}
    ).click(function () {
	        $('span.arrow').remove();
			$(this).append('<span class="arrow"></span>');
       	    $('div#slideshow-main li').removeClass('current');
       	    $('div#slideshow-main li.' + $(this).attr('rel')).addClass('current');

            if($('.carousel-bot a#show-video').hasClass('chosen')) {
                $('.carousel-bot a#show-video').removeClass('chosen');
                $('.carousel-bot a#show-images').addClass('chosen');
            }

       	    return false;
	});

    $('.carousel-bot a#show-video').click(function () {
        $('span.arrow').remove();
        $('div#slideshow-carousel li a').each(function () {
            $(this).children('img').css({'opacity': '0.5'});
        });
        $('div#slideshow-main li').removeClass('current');
        $('div#slideshow-main li.p-vid').addClass('current');

        $('.carousel-bot a#show-images').removeClass('chosen');
        $('.carousel-bot a#show-video').addClass('chosen');

        return false;
    });
    $('.carousel-bot a#show-images').click(function () {
        $('div#slideshow-main li.p-vid').removeClass('current');
        first_img = $('#carousel').children(':first-child').children('a');
        $(first_img).click();
        /*
        $('div#slideshow-main li.' + first_img.attr('rel')).addClass('current');
        alert('3');
        $(first_img).append('<span class="arrow"></span>');
        alert($(first_img).children('img').attr('alt'));
        $(first_img).children('img').css({'opacity': '1.0'});
        alert('5');
        */
        $('.carousel-bot a#show-video').removeClass('chosen');
        $('.carousel-bot a#show-images').addClass('chosen');

        return false;
    });

//Carousel Tweaking
	function mycarousel_initCallback(carousel) {
		// Pause autoscrolling if the user moves with the cursor over the clip.
		carousel.clip.hover(function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});
	}



//Pop-Out Right Nav
    var prev_id = 0;

    //$(".trigger_1").add('.trigger_2').add('.trigger_3').click(function(){
    $(".trigger_1").add('.trigger_2').add('.trigger_3').add('.extra-trigger_1').click(function(){
        var trig_cls = $(this).attr('class');
        var str_loc = trig_cls.indexOf('trigger_');
        var curr_id = trig_cls.charAt(str_loc+8);

        if((prev_id != 0) && (prev_id != curr_id)) {
            $("#popout_"+prev_id).hide();
            $(".trigger_"+prev_id).removeClass("active");
        }

        $("#popout_"+curr_id).toggle();
        $(".trigger_"+curr_id).toggleClass("active");

        /* QUICK ENQUIRY */
        if((curr_id==3) && ($("#popout_3").is(":visible"))) {
            if($("#pop3_status").html() == 0) {
                //show loader image
                $("#popout_3").html('<div id="pop3_loader" align="center"><img src="/img/design/loading.gif" /></div>');
                //ajax load form...
                $.post( "/ajax_qe",
                        { ajax : "1" },
                        function(data) {
                            $("#pop3_loader").slideUp('slow');
                            $("#popout_3").html('<div id="pop3_form" style="display:none;">'+data+'</div>');
                            $("#pop3_form").slideDown('slow');
                            $("#pop3_status").html("1");
                        },
                        "html");
            }
        }

        prev_id = curr_id;
        return false;
    });

    $('#popouts-container').toggle($(window).width() >= 1024);
    $('#takemeto-left').toggle($(window).width() >= 1024);

});

