﻿var thisPage = 'home'
var ticks = new Array();
var specificTimer = null;
var divs = new Array();
var slideActive = false;
var movieActive = false;
var fading = false;
var legalLoaded = false;

function GetURLParam(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}
function LClick(id) {
    var url = 'handleClick.ashx?e=';
    if (thisPage === 'news') { url = '../handleClick.ashx?e=' };
    $.ajax({
        type: 'post',
        async: true,
        url: url + id,
        data: '{}',
        contentType: 'text/plain; charset=utf-8',
        dataType: 'text',
        error: function (e) {
        },
        success: function (data) {
        }
    });
}

function FireUpEvents(page) {
    thisPage = page;
    LClick('page:' + thisPage);
    GetKey();
    $('.menu-link').bind({
        mouseenter: function (e) {
            $(this).addClass('menu-hover');
        },
        mouseleave: function (e) {
            $(this).removeClass('menu-hover');
        }
    });
    $('#back-panel, #dialog-close').click(function () {
        LClick('backpanel:close');
        if (!fading) {
            if (slideActive == true) {
                $('#dialog-content').cycle('stop');
                slideActive = false;
            }
            if (movieActive == true) {
                if (typeof swfobject != 'undefined') {
                    swfobject.removeSWF();
                }
            }
            DisplayForm(null, 'hide', 0, 0, null);
        }
    });
    $('.icon-panel-large').click(function () {
        LClick('icon:' + $(this).attr('id'));
        var cId = $(this).attr('id');
        var sId = cId + '-content';
        $('.icon-panel-large').children('.icon-selected-large').each(function () {
            if ($(this).hasClass('selected')) {
                $(this).removeClass('selected');
                $(this).hide();
            }
        });
        $(this).children('.icon-selected-large').show();
        $(this).children('.icon-selected-large').addClass('selected');
        $('#top-panel-right .c').children('.content').each(function(){
            $(this).addClass('hidden');
            $(this).removeClass('selected');
            if ($(this).attr('id') == sId){
                $(this).addClass('selected content');
                $(this).removeClass('hidden');
            };
        });
    });

    $('.icon-large').bind({
        mouseenter: function (e) {
            $(this).prev('div').show();
        },
        mouseleave: function (e) {
            if (!$(this).prev('div').hasClass('selected')) {
                $(this).prev('div').hide();
            }
        }
    });
    $('.slide-activate').click(function () {
        LClick('slide:' + $(this).attr('id'));
        var myContent = $(this).attr('id');
        DisplayForm(null, 'show', 790, 570, myContent, true);
    });

    $('#pause').click(function () {$('#dialog-content').cycle('pause');LClick('slidePlayerPause');});
    $('#play').click(function () {LClick('slidePlayerPlay');$('#dialog-content').cycle('resume');});
    $('#rewind').click(function () {LClick('slidePlayerRewind');$('#dialog-content').cycle('prev');});
    $('#forward').click(function () {LClick('slidePlayerForward');$('#dialog-content').cycle('next');});

    $('#sign-up-link').click(function () {
        $('#sign-up-form').slideToggle(150);
    });
    $('#sign-up-email').bind({
        focus: function (e) {
            if ($(this).val() == 'your email address') {
                $(this).val('');
            }
        }
    });
    $('#legal-info, #legal-info2').click(function () {
        LClick(page + ':legal');
        var myContent = '#legal-info-more';
        if (!legalLoaded) {
            $('#legal-info-more').load('../legal.htm #legal-notice', function () {
                legalLoaded = true;
                DisplayForm(null, 'show', 790, 450, myContent, null);
            });
        }
        else {
            DisplayForm(null, 'show', 790, 450, myContent, null);
        }
    });
    switch (page) {
        case 'home':
            {
                $('#seeHow').click(function () {
                    var myContent = '#seeHow-more';
                    DisplayForm(null, 'show', 790, 450, myContent, null);
                });
                $('#bottom').cycle({
                    fx: 'fade',
                    timeout: 3000,
                    speed: 3000
                });
                $('.icon-panel-flat').click(function () {
                    LClick('homeIcon:' + $(this).children('img:first').attr('id'));
                    $('.icon-panel-flat').children('.icon-selected-flat').each(function () {
                        $(this).hide();
                        $(this).removeClass('selected');
                    });
                    $(this).children('.icon-selected-flat').show();
                    $(this).children('.icon-selected-flat').addClass('selected');
                });
                $('.icon-flat').bind({
                    mouseenter: function (e) {
                        $(this).prev('div').show();
                    },
                    mouseleave: function (e) {
                        if (!$(this).prev('div').hasClass('selected')) {
                            $(this).prev('div').hide();
                        }
                    }
                });
                break;
            }
        case 'courses':
            {
                $('#courses-content-explaination').click();
                $('.icon-panel-standard').click(function () {
                    LClick('coursesIcon:' + $(this).attr('id'));
                    $('.icon-panel-standard').children('.icon-selected-standard').each(function () {
                        $(this).hide();
                        $(this).removeClass('selected');
                    });
                    $(this).children('.icon-selected-standard').show();
                    $(this).children('.icon-selected-standard').addClass('selected');
                });
                $('.icon-standard').bind({
                    mouseenter: function (e) {
                        $(this).prev('div').show();
                    },
                    mouseleave: function (e) {
                        if (!$(this).prev('div').hasClass('selected')) {
                            $(this).prev('div').hide();
                        }
                    }
                });
                break;
            }

        case 'about':
            {
                $('.profile-picture').bind({
                    mouseenter: function (e) {
                        var myContent = '#' + $(this).attr('id') + '_content';
                        $(myContent).show();
                        LClick('aboutUs:view-profile: ' + myContent);
                    },
                    mouseleave: function (e) {
                        var myContent = '#' + $(this).attr('id') + '_content';
                        setTimeout(function () {                            
                            $(myContent).hide();
                        }, 2000);
                        LClick('aboutUs:close-profile: ' + myContent);
                    }
                });
                $('.profile-content').each(function () {
                    var myOwner = '#' + $(this).attr('id').split('_')[0];
                    var myOwnersPosition = $(myOwner).offset();
                    $(this).css({ top: myOwnersPosition.top + 130, left: myOwnersPosition.left - 500 });
                });
                $('#sign-up-form').css({ 'z-index': GetZ() });
                break;
            }
        case 'contact':
            {
                $('.map-link').bind({
                    mouseenter: function (e) {
                        $('.map-content').hide();
                        var myContent = '#' + $(this).attr('id') + '_content';
                        $(myContent).show();
                        LClick('contact:view-map: ' + myContent);
                    }
                });

                break;
            }
        case 'unsubscribe':
            {
                $('#my-email-address').focus(function () {
                    if ($(this).val() == 'your email address') {
                        $(this).val('');
                    }
                });
                break;
            }
    }
    FireUpChat();
}

function ShowPanel(obj) {
    var myContent = '#' + $(obj).attr('id') + '-more';
    switch (thisPage) {
        case 'home':
            {
                LClick('homePanel:' + myContent);
                DisplayForm(obj, 'show', 790, 450, myContent, null);
                break;
            }
        case 'services':
            {
                LClick('servicesPanel:' + myContent);
                DisplayForm(obj, 'show', 790, 450, myContent, null);
                break;
            }
        case 'courses':
            {
                LClick('coursesPanel:' + myContent);
                DisplayForm(obj, 'show', 790, 450, myContent, null);
                break;
            }
    }
}

function GetZ() {var h = 0;$('div').each(function () {var z = parseInt($(this).css('z-index'));h = z > h ? z : h;});return h;}

function DisplayForm(obj, func, w, h, source, isSlideShow) {
    var Z = GetZ();var mTop = Math.round(h / 2);var mLeft = Math.round(w / 2);
    switch (func) {
        case 'show':
            {
                $('#back-panel').css({ 'height': '100%', 'width': '100%', 'z-index': Z + 10 });
                $('#dialog').css({ 'height': h + 'px', 'width': w + 'px', 'margin-top': -mTop, 'margin-left': -mLeft, 'z-index': Z + 11 });
                $('#dialog > .ol, .or').css({ 'height': h + 'px' });
                $('#dialog-content').css({ 'height': h + 'px' });
                fading = true;
                $('#back-panel').fadeTo(500, 0.95, function () {
                    fading = false;
                    if (isSlideShow) {
                        var slideShowContent = source.split('_')[1] + '.htm';
                        $('#slide-player').show();
                        $('#dialog-content').load(slideShowContent, function () {
                            $('#dialog').show(function () {
                                $('#dialog-content').cycle({
                                    fx: 'scrollLeft',
                                    speedIn: 1000,
                                    speedOut: 1000,
                                    easeIn: 'easeInSine',
                                    easeOut: 'easeOutSine',
                                    speed: 8000,
                                    delay: 1000
                                });
                                slideActive = true;
                            });
                        });
                    } else {
                        if (source.indexOf('.htm') != -1) {
                            $('#dialog-content').load(source, function () {
                                var vParams = { 'autostart': 'true' };
                                swfobject.embedSWF("videos/SMART_Demo.swf", "media", "640", "498", "9.0.28", "videos/expressInstall.swf", vParams);
                                movieActive = true;
                            });
                        }
                        else {
                            source = $(source).html();
                            $('#dialog-content').html(source);
                        }
                        $('#dialog').show(function () {
                            if (thisPage === 'courses') { $('.course-accordion').accordion(); }
                        });
                    }
                });
                break;
            }
        case 'hide':
            {
                $('#slide-player').hide();
                $('#dialog-content').html('');
                $('#dialog').hide();
                $('#back-panel').fadeOut()
                break;
            }
    }
}
