﻿/// <reference path="../includes/js/jquery-1.3.2.min.js" />
/// <reference path="../includes/js/jcarousellite_1.0.1.min.js" />
/// <reference path="../includes/js/jquery.history.js" />


var loadingMessage = null;
var ctrl = null;
var isNewState = false;

function pageLoad() {
    // Attaching Client-Side Navigation
    Sys.Application.add_navigate(onNavigate);

    // Making sure the "content" is at least as tall as the sidebar
    // this is used to make sure the "loading" message has a place
    // big enought to "look" right.
    $('#content_shell').css('min-height',$('#sidebar').height());

    // Initial Setup
    buildCarousels();
    attachHandlers();

    // Global Variables
    loadingMessage = $('#loading');
    ctrl = $('#content');

    // Parsing parameters
    if (!parseParameters())
        loadMain(false);
}

function showLoadingMessage() {
    var shell = $("#content_shell");
    shell.mask("Loading...");
}


function loadData(control, id, addHistory) {
    // Making sure we have values
    addHistory = (typeof (addHistory) != 'undefined') ? addHistory : true;
    id = (typeof (id) != 'undefined') ? id : null;

    // Add this new "content" to the history by default
    if (addHistory) {
        isNewState = true;
        Sys.Application.addHistoryPoint({ control: control, id: id });
    }

    // Making sure the service proxy has been loaded
    if ((typeof (training) != 'undefined') && (typeof (training.GetControl) != 'undefined')) {
        // Showing loading message
        //loadingMessage.show();
        showLoadingMessage();

        // Calling service
        training.GetControl(control + '.ascx', (id) ? [id] : [], onComplete, onError);
    }
    else
        alert('Remote Service could not be loaded. Please reload this page or contact customer support.');
}

function parseParameters() {
    var type = getUrlParameter('control');
    var result = false;

    if (type != "") {
        var id = getUrlParameter('id');

        if (id != "") {
            switch (type) {
                case "video":
                    loadVideo(parseInt(id), false);
                    result = true;
                    break;
                case "audio":
                    loadAudio(parseInt(id), false);
                    result = true;
                    break;
                case "document":
                    loadDocument(parseInt(id), false);
                    result = true;
                    break;
            }
        }
    }

    return result;

}

function buildCarousels() {
    $("#videos").jCarouselLite(
                { speed: 1000,
                    circular: true,
                    visible: 2,
                    btnNext: "#videos_right",
                    btnPrev: "#videos_left"
                });

    $("#audio").jCarouselLite(
                { speed: 1000,
                    circular: true,
                    visible: 2,
                    btnNext: "#audio_right",
                    btnPrev: "#audio_left"
                });

    $("#documents").jCarouselLite(
                { speed: 1000,
                    circular: true,
                    visible: 1,
                    btnNext: "#documents_right",
                    btnPrev: "#documents_left"
                });
}


function attachHandlers() {
    $('#videos_all').click(function() {
        loadVideoList();
    });

    $('#audio_all').click(function() {
        loadAudioList();
    });

    $('#documents_all').click(function() {
        loadDocumentList();
    });
}

/* Ajax Callbacks */
function onComplete(result) {
    hideLoadingMessage();
    ctrl.html(result);
}

function onError(result) {
    hideLoadingMessage();
    alert("An error has occurred while processing your request.\n\nServer Error: " + result.get_message());
}

function onNavigate(sender, e) {
    var state = e.get_state();

    if (isNewState)
        isNewState = false;
    else {
        if (typeof (state.control) != 'undefined')
            loadData(state.control, parseInt(state.id), false);
        else
            loadMain(false);
    }

}


/* Helpers */
function loadVideo(id, addToHistory) {
    loadData('video', id, addToHistory);
}

function loadVideoList(addToHistory) {
    loadData('videolist', null, addToHistory);
}

function loadAudio(id, addToHistory) {
    loadData('audio', id, addToHistory);
}

function loadAudioList(addToHistory) {
    loadData('audiolist', null, addToHistory);
}

function loadDocument(id, addToHistory) {
    loadData('document', id, addToHistory);
}

function loadDocumentList(addToHistory) {
    loadData('documentlist', null, addToHistory);
}

function loadMain(addToHistory) {
    loadData('main', null, addToHistory);
}

function hideLoadingMessage() {
//    if (loadingMessage != null)
    //        loadingMessage.hide();
    var shell = $("#content_shell");
    shell.unmask();
}