var KBT = KBT || {
    init : function() {
        KBT.isiDevice = navigator.userAgent.match(/iphone|ipod|ipad|android/i) != null;
        
        
        $('#background-img').load(KBT.initBackground);
        // I love you IE!!
        $('#background-img')[0].src = 'images/background.jpg';
        // End of ie love
        $('header h1').click(KBT.showAbout);
        $('li.contact a').click(KBT.showContact);

        KBT.registerLinks();
    },
    
    registerLinks : function() {
        $('.linkedin a').click(function() {return KBT.track(this, 'linkedin');});
        $('.wordpress a').click(function() {return KBT.track(this, 'digitalmarketingblog');});
        $('.twitter a').click(function() {return KBT.track(this, 'twitter');});
        $('.facebook a').click(function() {return KBT.track(this, 'facebook');});
        $('.formspring a').click(function() {return KBT.track(this, 'formspring');});
    },
    
    track : function(t, tc) {
        window.open(t.href, '_blank').focus();
        $('#track')[0].src = "track/"+tc;
        return false;
    },
    
    initBackground : function() {
        if(KBT.isiDevice) {
            $('#background').css({'position':'absolute'});
        }

        imgWidth = $('#background-img').width();
        imgHeight = $('#background-img').height();
        
        if(!imgWidth || !imgHeight) {
            setTimeout(KBT.initBackground, 10);
            return;
        }
        
        KBT.backgroundRatio = imgWidth / imgHeight;
        $(window).resize(KBT.resizeBackground);
        KBT.resizeBackground();
        $('#background-img').show();
    },
    
    resizeBackground : function() {
		windowWidth = $(window).width();
		
		if(KBT.isiDevice) {
			windowHeight = Math.max($(window).height(), $('body').height());
			$('#background').height(windowHeight);
		} else {
			windowHeight = $(window).height();
		}
		
        windowRatio = windowWidth / windowHeight;

        if(windowRatio > KBT.backgroundRatio) {
            $('#background-img').width(windowWidth);
        } else {
	        $('#background-img').width(windowHeight * KBT.backgroundRatio);
		}
    },
    
    showAbout : function() {
        $('aside.contact').hide();
        $('aside.about').show();
        KBT.resizeBackground();
        return false;
    },
    
    showContact : function() {
        $('aside.about').hide();
        $('aside.contact').show();
        KBT.resizeBackground();
        return false;
    }
};

$(document).ready(function() {
    KBT.init();
});

