/* configurations */
var CONFIG = {
    'resizeDivide' : 2,
    'minHeight' : 240, 
    'sideMargin' : 100,
    'spinRate': 300
}

var currentSlideIndex = 0;

/* handy little function that lets me get variables from the url */
function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}

// custom easing called "custom"
$.easing.custom = function (x, t, b, c, d, s) {
    if (s == undefined) s = 0.10158;
    return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
}

$.easing.elasout = function(x, t, b, c, d) {
	var s=1.70158;var p=0;var a=c;
	if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
	if (a < Math.abs(c)) { a=c; var s=p/4; }
	else var s = p/(2*Math.PI) * Math.asin (c/a);
	return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
};


function isSlides() {
    if ($('#launcher #slides').length > 0) {
        return true;
    }
    else {
        return false;
    }
}

function isPage() {
    if ($('#launcher #page').length > 0) {
        return true;
    }
    else {
        return false;
    }
}

// this setups the launcher window
// this should be run after a new ajax is performed on the launcher
function setupLauncher() {

    // these mostly apply to just slides
    if (isSlides()) {
       
       $('#launcher #slides .pane').serialScroll({
    		items:'li',
    		cycle: false,
    		speed: 0,
    		easing:'easeOutQuart', //use this easing equation for a funny effect
    	});

    	$('#left').click(function () {
    	   $('#launcher #slides .pane').trigger( 'prev' ); 
    	   $(this).disableSelection();
	    });
 
     	$('#right').click(function () {
    	   $('#launcher #slides .pane').trigger( 'next' ); 
    	   $(this).disableSelection();
	    });
        
    }
    else if (isPage()) {
        
    }

}

// this handles all the actual interface resizing
// including any video tags, launcher container divs, adjustment of arrows etc
function resizeLauncher() {

    // get the latest size of the viewport of the browser
    // the viewport is the bit inside the browser chrome, the actual page size
    viewportWidth = $(window).width();
    viewportHeight = $(window).height();
    
    // the height of the .head, .body and .foot of the launcher
    launcherHeight = $('#launcher').outerHeight(true);
    launcherHeadHeight = $('#launcher .head').outerHeight(true);
    launcherFootHeight = $('#launcher .foot').outerHeight(true);
    
    launcherBodyHeight = viewportHeight - launcherHeadHeight - launcherFootHeight - 20;
    
    if (launcherBodyHeight < CONFIG['minHeight']) { launcherBodyHeight = CONFIG['minHeight']; }
    
    // adjust the necessary css dynamically to succesfully resize all the interface bits
    $('#launcher, #launcher .container').css({
        'width': launcherBodyHeight+(launcherBodyHeight/CONFIG['resizeDivide'])+(CONFIG['sideMargin']*2)+'px',
        'height': launcherBodyHeight+'px'
    });
    
    if (isSlides()) {
        $('#launcher #slides, #launcher #slides .pane, #launcher #slides .pane ul.scrollable li.slide').css({
            'width': (launcherBodyHeight+(launcherBodyHeight/CONFIG['resizeDivide']))+'px',
            'height': launcherBodyHeight+'px'
        }); 

        // is there text to go with it?
        launcherMediaHeight = launcherBodyHeight - $('#launcher #slides .text').height();

        $('#launcher #slides .media, #launcher #slides .media img, #launcher #slides .media     , #launcher #slides .media .video-js-box').css({
            'width': (launcherMediaHeight+(launcherMediaHeight/CONFIG['resizeDivide']))+'px',
            'height': launcherMediaHeight+'px'
        });
        
    }
    else if (isPage()) {
        $('#launcher #page').css({
            'width': launcherBodyHeight+(launcherBodyHeight/CONFIG['resizeDivide'])+'px',
            'height': launcherBodyHeight+'px'
        });         
    }


    if (isSlides()) {
        // get height of container ajax panel
        var slidesHeight = $('#launcher .body .ajax').height();
    
        // adjust arrow heights
        $('#launcher #slides .arrow').css('top', (slidesHeight/2)+10+'px'); 

    }
}

// for visual effect, we will now spin the launcher
// this effect is taken off the old website flash effect
function spinLauncher() {
    $('#launcher #slides .pane').scrollTo('li:eq('+($('#launcher #slides .slide').length-1)+')',0);
    $('#launcher #slides .pane').scrollTo('li:eq(0)', $('#launcher #slides .pane .slide').length*CONFIG['spinRate'] , { easing: 'custom' });
}

// this will preload the content in the launcher
// then it will run a callback function passed into
function preloadContent(callback) {
    
    // get images from page
    var imageList = [];
    $('#launcher img').each(function () {
        imageList.push($(this).attr('src'));
    });

    // only actually preload if there are any images
    // once complete, it will run the callback.
    if (imageList.length > 0) {
        // now preload the images
        $.loadImages(imageList, function () {
            
            // load of images complete, so now run the callback function passed in
            callback();

        });
    }
    else {
        
        // no images to load, so now run the callback function passed in
        callback();            
    }    
}


// this setups the size stuff
// should be run once, when the page first loads
function initResize() {

    var viewportWidth = $(window).width();
    var viewportHeight = $(window).height();
    
    $(window).resize(function () {
        
        // stops scrollbar appearing when resizing the browser
        $('body').css('overflow', 'hidden');

    });
    
    $(window).resizeComplete(function() {

    
        var ajax = $('#launcher .ajax');
        ajax.css('visibility', 'hidden');

        // shows scrollbar again
        $('body').css('overflow', 'auto');
        


        resizeLauncher();
        
        $('#launcher #slides .pane').trigger( 'goto', 0);
        
        ajax.css('visibility', 'visible');

    });    
}

// run this stuff only when the DOM has finished loading
$(document).ready(function () {
    
    // initialises the resize, to establish size to show in present viewport size
    initResize();
   
    // indicate that the loader is loading
    $('#launcher').addClass('loader');
    
    // hide launcher container
    $('#launcher .body').css({'opacity': 0});  
    $('body').css('overflow', 'hidden');  // stop scrollbars appearing       

    
    preloadContent(function () {
        
        // if preload is complete...
        resizeLauncher();

        // because new DOM is in place, setup the launcher again
        // setup the videos again and run the resize to make sure...
        setupLauncher();
        
        spinLauncher();
        
        // hide loader
        $('#launcher').removeClass('loader');

        // show launcher container
        $('#launcher .body').css({'opacity': 1});   
        $('body').css('overflow', 'hidden');  // show scrollbars as normal   
        
    });   
 
});
