﻿// updated by Abdul 12 Aug 2009
(function(jq) {
    jq(document).ready(function() {
        // disable right click;
        document.onmousedown = function noshow(e){
            try { 
                if ( event.button == 2 || event.button == 3 ) return false; 
            } catch (e) { 
                if (e.which == 3) return false; 
            } 
        };
        document.oncontextmenu = function() { return false; }
        document.ondragstart   = function() { return false; }
        // 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.items = jq('#mainPromo .promo').get();
            this.curr = 0;
        };
        
        PromoScroll.prototype.bind = function(){
            var self = this;
            jq('#mainPromo .nav a').bind('click', function(){
                self.trigger.apply(self, arguments);
                return false;
            });    
        };
        PromoScroll.prototype.trigger = function(e){
            if(e.target.alt.toLowerCase() === 'next'){
                this.curr = ( this.curr == this.items.length-1 ) ? 0 : this.curr+1;
            }else{
                this.curr = ( this.curr == 0 ) ? this.items.length-1 : this.curr-1;
            };
            return this.render();
        };
        
        PromoScroll.prototype.render = function(){
            jq('#mainPromo .promo').addClass("hide");
            jq('body').css({background: 'url('+promoImages[this.curr]+') top center no-repeat'});
            jq(this.items[this.curr]).removeClass("hide");    
            return true;        
        };
        
        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(){
                        if(j === promoImages.length){
                            this.allLoaded = true;
                        }
                    };
                    image.src = promoImages[j];
                })();
            }
        };
        PromoScroll.prototype.isLoaded = function(){
            //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);