
$(document).ready(function () {

    // footer resources div
    $("#resourcesLink").toggle(
        function () {
            $("#resourcesDiv").stop().animate({ height: "270px", top: "120px" }, 500);
        },
        function () {
            $("#resourcesDiv").stop().animate({ height: "10px", top: "430px" }, 500);
        }
    );

    // set fancybox to handle A with class of popup
    $("a.popup").fancybox({
        'titleShow': true,
        'titlePosition': 'inside',
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'easingIn': 'easeOutBack',
        'easingOut': 'easeInBack'
    });

    // Book Now tab
    var bookingPanel = $('.navWrapper .bookingFrameWrapper');
    var moveableElements = $('.moveable');

    $(".navWrapper .bookNowTab").toggle(
        function () {
            if (bookingPanel.is(':not(:animated)')) {

                // if IE just open in new window
                if ($('html').hasClass('ie7')) {
                    window.open('http://www.condorbooking.co.uk/ExtResnew/startbooking.asp?LANGUAGE=E');
                    return false;
                }
                bookingPanel.slideDown(800, function () { });

                // slide down elemetns beneath booking div
                moveableElements.animate(
                    { 'margin-top': 500 },
                    1200,
                    'jswing',
                    function () { }
                );
            }
        },
        function () {
            if (bookingPanel.is(':not(:animated)')) {

                bookingPanel.slideUp(800, function () { });

                // slide up elemetns beneath booking div
                moveableElements.animate(
                    { 'margin-top': 0 },
                    1000,
                    'jswing',
                    function () { }
                );
            }
        }
    )


    /* Mouse over element, toggle another (no bubbling/queuing

    $('.dutyFreeProduct').mouseover(function () {
    $(this).children('.dutyFreeProduct h2').fadeOut(300, function () {
    });
    });

    $('.dutyFreeProduct').mouseleave(function () {
    $(this).children('.dutyFreeProduct h2').stop(true, true).fadeIn(500, function () {
    });
    });
    */

});




$(function () {

    $('div.gallery-header').gallery({
        duration: 500,
        autoRotation: 8000,
        listOfSlides: 'div > ul > li',
        effect: 'fade'
    });
    $('div.gallery').gallery({
        duration: 500,
        slideElement: 2,
        autoRotation: 12000,
        listOfSlides: 'div.gallery-holder > ul > li',
        switcher: 'ul.swicher > li'
    });
    $('div.gallery-block').each(function () {
        var _this = $(this);
        $(this).gallery({
            duration: 500,
            autoRotation: 12000,
            nextBtn: 'a.next-link',
            prevBtn: 'a.prev-link',
            direction: true,
            listOfSlides: 'div.holder > ul.info > li',
            switcher: 'ul.swicher > li'
        });

    });
    initSlide();
});

function initSlide(){
	var hold = $('div.slide-hold');
	var h = hold.height()
	var link = hold.parents('.booking.form').find('input.submit');
	var h2 = hold.parents('.booking.form').find('h2').eq(0);
	
	if(hold.hasClass('open')){
		h2.show();
	}
	else{
		hold.height(0);		
	}

	link.click(function(){
		if(hold.hasClass('open')){
			hold.removeClass('open');
			hold.animate({height:0},300);
			h2.hide();
		}
		else{
			hold.addClass('open');
			hold.animate({height:h},300);
			h2.show();
		}
		return false;
	});
}


(function($) {
	$.fn.gallery = function(options) { return new Gallery(this.get(0), options); };
	
	function Gallery(context, options) { this.init(context, options); };
	
	Gallery.prototype = {
		options:{},
		init: function (context, options){
			this.options = $.extend({
				duration: 700,
				slideElement: 1,
				autoRotation: false,
				effect: false,
				listOfSlides: 'ul > li',
				switcher: false,
				disableBtn: false,
				nextBtn: 'a.link-next, a.btn-next, a.next',
				prevBtn: 'a.link-prev, a.btn-prev, a.prev',
				circle: true,
				direction: false,
				event: 'click',
				IE: false
			}, options || {});
			var _el = $(context).find(this.options.listOfSlides);
			if (this.options.effect) this.list = _el;
			else this.list = _el.parent();
			this.switcher = $(context).find(this.options.switcher);
			this.nextBtn = $(context).find(this.options.nextBtn);
			this.prevBtn = $(context).find(this.options.prevBtn);
			this.count = _el.index(_el.filter(':last'));
			
			if (this.options.switcher) this.active = this.switcher.index(this.switcher.filter('.active:eq(0)'));
			else this.active = _el.index(_el.filter('.active:eq(0)'));
			if (this.active < 0) this.active = 0;
			this.last = this.active;
			
			this.woh = _el.outerWidth(true);
			if (!this.options.direction) this.installDirections(this.list.parent().width());
			else {
				this.woh = _el.outerHeight(true);
				this.installDirections(this.list.parent().height());
			}
			
			if (!this.options.effect) {
				this.rew = this.count - this.wrapHolderW + 1;
				if (!this.options.direction) this.anim = '{marginLeft: -(this.woh * this.active)}';
				else this.anim = '{marginTop: -(this.woh * this.active)}';
				eval('this.list.css('+this.anim+')');
			}
			else {
				this.rew = this.count;
				this.list.css({opacity: 0}).removeClass('active').eq(this.active).addClass('active').css({opacity: 1}).css('opacity', 'auto');
				this.switcher.removeClass('active').eq(this.active).addClass('active');
			}
			
			this.initEvent(this, this.nextBtn, true);
			this.initEvent(this, this.prevBtn, false);
			if (this.options.disableBtn) this.initDisableBtn();
			if (this.options.autoRotation) this.runTimer(this);
			if (this.options.switcher) this.initEventSwitcher(this, this.switcher);
		},
		initDisableBtn: function(){
			this.prevBtn.removeClass('prev-'+this.options.disableBtn);
			this.nextBtn.removeClass('next-'+this.options.disableBtn);
			if (this.active == 0 || this.count+1 == this.wrapHolderW) this.prevBtn.addClass('prev-'+this.options.disableBtn);
			if (this.active == 0 && this.count == 1 || this.count+1 == this.wrapHolderW) this.nextBtn.addClass('next-'+this.options.disableBtn);
			if (this.active == this.rew) this.nextBtn.addClass('next-'+this.options.disableBtn);
		},
		installDirections: function(temp){
			this.wrapHolderW = Math.ceil(temp / this.woh);
			if (((this.wrapHolderW - 1) * this.woh + this.woh / 2) > temp) this.wrapHolderWwrapHolderW--;
		},
		fadeElement: function(){
			if ($.browser.msie && this.options.IE){
				this.list.eq(this.last).css({opacity:0});
				this.list.removeClass('active').eq(this.active).addClass('active').css({opacity:'auto'});
			}
			else{
				this.list.eq(this.last).animate({opacity:0}, {queue:false, duration: this.options.duration});
				this.list.removeClass('active').eq(this.active).addClass('active').animate({
					opacity:1
				}, {queue:false, duration: this.options.duration, complete: function(){
					$(this).css('opacity','auto');
				}});
			}
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
			this.last = this.active;
		},
		scrollElement: function(){
			eval('this.list.animate('+this.anim+', {queue:false, duration: this.options.duration});');
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active / this.options.slideElement).addClass('active');
		},
		runTimer: function($this){
			if($this._t) clearTimeout($this._t);
			$this._t = setInterval(function(){
				$this.toPrepare($this, true);
			}, this.options.autoRotation);
		},
		initEventSwitcher: function($this, el){
			el.bind($this.options.event, function(){
				$this.active = $this.switcher.index($(this)) * $this.options.slideElement;
				if($this._t) clearTimeout($this._t);
				if ($this.options.disableBtn) $this.initDisableBtn();
				if (!$this.options.effect) $this.scrollElement();
				else $this.fadeElement();
				if ($this.options.autoRotation) $this.runTimer($this);
				return false;
			});
		},
		initEvent: function($this, addEventEl, dir){
			addEventEl.bind($this.options.event, function(){
				if($this._t) clearTimeout($this._t);
				$this.toPrepare($this, dir);
				if ($this.options.autoRotation) $this.runTimer($this);
				return false;
			});
		},
		toPrepare: function($this, side){
			if (($this.active == $this.rew) && $this.options.circle && side) $this.active = -$this.options.slideElement;
			if (($this.active == 0) && $this.options.circle && !side) $this.active = $this.rew + $this.options.slideElement;
			for (var i = 0; i < $this.options.slideElement; i++){
				if (side) { if ($this.active + 1 <= $this.rew) $this.active++; }
				else { if ($this.active - 1 >= 0) $this.active--; }
			};
			if (this.options.disableBtn) this.initDisableBtn();
			if (!$this.options.effect) $this.scrollElement();
			else $this.fadeElement();
		},
		stop: function(){
			if (this._t) clearTimeout(this._t);
		},
		play: function(){
			if (this._t) clearTimeout(this._t);
			if (this.options.autoRotation) this.runTimer(this);
		}
	}
}(jQuery));
