﻿(function(jq) {
    jq(document).ready(function() {
        // redirect market selector forms;
        jq.each(['#exploreWorld', '.countrySwitcher'], function(i, v){
            jq(v+' input[type="image"]')    
                .bind('click', function() {
                    window.location = jq(this).siblings().filter("select").attr("value");
                    return false;
                });
        })
        
        //promo panel toggle;
        function PromoScroll(){
            this.curr = 0;
            this.items = jq('#mainPromo .promo');
        };
        
        PromoScroll.prototype.bind = function(){
            var self = this;
            jq('#mainPromo .nav a').bind('click', function(e){
                self.trigger.call(self, e);
                return false;
            });    
        };
        PromoScroll.prototype.trigger = function(e){
            var func = this[e.target.alt.toLowerCase()];
            var num = func.call(this);
            jq('#mainPromo .promo').addClass("hide");
            this.setBackground();
            return jq(this.items[num]).removeClass("hide");
        };
        
        PromoScroll.prototype.next = function(){
            return this.curr = ( this.curr == this.items.length-1 )? 0 : this.curr+1;
        };
        
        PromoScroll.prototype.back = function(){
            return this.curr = ( this.curr == 0 )? this.items.length-1 : this.curr-1;    
        };
        
        PromoScroll.prototype.load = function(){
            var self = this;
            if (!(typeof promoImages === undefined )) return false;
            for( var i = 0; i < promoImages.length; i++ ){
                (function(){
                    var j = i;
                    var image = new Image();
                    image.onload = function() {
                        self.isLoaded.call(self, image.src, [image.width, image.height], j);
                    };
                    image.src = promoImages[i];
                })();
            }
        };
        
        PromoScroll.prototype.setBackground = function(){
            return jq('body').css({background: 'url('+promoImages[this.curr]+') top center no-repeat'});
        };
        
        PromoScroll.prototype.isLoaded = function(i, a, n){
            //console.log(i+", "+a[0]+", "+a[1]+", "+n);
        };
    
        var promo = new PromoScroll();
        promo.bind();
        promo.load();
        // qbtb panel functionality;
        var firstPass = true;
        init();
        
        jq('.qbtb .options a').bind('click', function(){
            hideAll();
            showPanel(this);
            return false;
        });
        
        function init(){
            hideAll();
            jq('.qbtb div.flights').show();
        };
        
        function showPanel(panel){
            var el;
            jq('.qbtb div.'+panel.className).show();
            if( ! isFirstPass() ){
                el = jq(panel).parent().get(0);
                jq(el).addClass('active');
                toggleNav(el);
            }
        };
        
        function hideAll(){
            jq('.qbtb .panel').hide();
            if( ! isFirstPass() ){
                jq('.options li').filter('.active')
                    .removeClass('active');
            }
        };
        
        function toggleNav(el){
            var parent = jq(el).parent().get(0);
            jq('.qbtb .nav').append(parent);
        }
        
        function isFirstPass(){
            if( firstPass ){
                firstPass = false;
                return true;
            }else{
                return firstPass;
            }
        };
    });
})(jQuery);