
jQuery(document).ready(function() {

    // PROMOTER HEIGHT FIX
    if (jQuery('body #page #body div.promoter').length > 0) {

        jQuery('body #page #body div.promoter').css('height', ((jQuery('body #page #body div.promoter div.text').height() > jQuery('body #page #body div.promoter div.background').height()) ? jQuery('body #page #body div.promoter div.text').height() : jQuery('body #page #body div.promoter div.background').height()));
    }

    jQuery('#flashDiv object').ready(function() {
        jQuery('#flashDiv').css({
            height:   '340px',
            background: 'transparent url(' + jQuery('#flashFallBack img').attr('src') + ') -2px 0 no-repeat'
        });
    });


    // VIDEO
    if (jQuery('body .video-player').length == 0) {
        var backdrop = jQuery('<div class="video-backdrop" style="height: ' + jQuery('#aspnetForm').height() + 'px;"></div>').click(function() { hideVideo() }).hide();
        var outline = jQuery('<div class="video-outline"></div>').hide();
        var player = jQuery('<div class="video-player" style="display: none;"><div class="close"><img src="/crmonline/styles/images/close.png" /></div><div class="top"><div></div></div><div class="player"><div></div></div><div class="bottom"><div></div></div></div>');
        player.find('.close').click(function() { hideVideo() });
        jQuery('body').append(backdrop, player, outline);
    }

    jQuery('body a.video').click(function() {
        showVideo(this);
        return false;
    });


    jQuery('#menu-global label').hide();
    jQuery('body #eventbubble').hide();
    jQuery('#menu-main li.selected').className

    var bottomNavMax = 0;
    jQuery('body #page #bottom-nav div').each(function() {
        if (bottomNavMax < jQuery(this).height()) bottomNavMax = jQuery(this).height();
    }); jQuery('body #page #bottom-nav div').css('height', bottomNavMax);

    var bottomBoxMax = 100;
    jQuery('body #page #bottom-box-container div .content').each(function() {
        if (bottomBoxMax < jQuery(this).height()) bottomBoxMax = jQuery(this).height();
    });
    jQuery('body #page #bottom-box-container div .content').css('height', bottomBoxMax + (jQuery('body #page #bottom-box-container div .content a.more').length > 0 ? 20 : 0));
    jQuery('body #page #bottom-box-container').css('height', '100%');

    // FLOATERS SEPARATED BY VERTICAL GRADIENT
    if (jQuery('body #page #body div.separated-floaters div').length > 0) {
        var floaterBoxMax = 0;
        jQuery('body #page #body div.separated-floaters div').each(function() {
            if (floaterBoxMax < jQuery(this).height()) floaterBoxMax = jQuery(this).height();
        });
        if (jQuery('body #page #body div.separated-floaters div').parents().filter('div.customer-cases').length > 0) floaterBoxMax = floaterBoxMax + 25;
        jQuery('body #page #body div.separated-floaters div').css('height', floaterBoxMax);
    }

    // GRADIENT FLOATERS
    if (jQuery('body #page #body div.gradient-floaters div').length > 0) {
        jQuery('body #page #body div.gradient-floaters div div h2').each(function() {
            jQuery(this).click(function() {
                window.location.href = jQuery(this).find('a').attr('href');
            });
        });
    }

    // FRONTPAGE MENU EVENT HANDLER
    if (jQuery('body #page #body div.frontpage div.menu ul').length > 0) {
        jQuery('body #page #body div.frontpage div.menu ul li').mouseover(function() {
            jQuery(this).addClass('hover');
        });
        jQuery('body #page #body div.frontpage div.menu ul li').mouseout(function() {
            jQuery(this).removeClass('hover');
        });
    }

    // XFORM
    if (jQuery('body #page #body div.xForm').length > 0) {
        jQuery('body #page #body div.xForm table tr').addClass('top');
        jQuery('body #page #body div.xForm table tr td input[type="text"], body #page #body div.xForm table tr td textarea, body #page #body div.xForm table tr td select').each(function() {
            jQuery(this).addClass('text');
            jQuery(jQuery(this).parent().parent().children()[0]).addClass('label').html('<label for="' + this.id + '">' + jQuery(jQuery(this).parent().parent().children()[0]).html() + '</label>');
        });
        jQuery('body #page #body div.xForm table tr td input[type="submit"], body #page #body div.xForm table tr td input[type="button"]').not('#campaign input[type="button"]').each(function() {
            jQuery(this).parent().html('<div class="action">' + jQuery(this).parent().html() + '</div>');
        });
        jQuery('body #page #body div.xForm table tr td fieldset').each(function() {
            if (jQuery(jQuery(this).parent().parent().children()[0]).html() != '') jQuery(jQuery(this).parent().parent().children()[0]).addClass('label').html('<label for="">' + jQuery(jQuery(this).parent().parent().children()[0]).html() + '</label>');
        });
        jQuery('body #page #body div.xForm table tr td input.required, body #page #body div.xForm table tr td textarea.required, body #page #body div.xForm table tr td select.required').each(function() {
            jQuery(this).removeClass('required');
            jQuery(this).parent().html('<div class="form-required">' + jQuery(this).parent().html() + '</div>');
        });
        jQuery('body #page #body #campaign div.xForm table tr td input[type="submit"], body #page #body #campaign div.xForm table tr td input[type="button"]').each(function() {
            jQuery(this).parent().html('<div class="button">' + jQuery(this).parent().html() + '</div>');
        });
        jQuery('body #page #body #campaign div.xForm table tr td div.button, body #page #body #campaign div.xForm table tr td p').each(function() {
            jQuery(this).parents('tr').addClass('noborder');
        });
    }




    // PROCESS FLOATERS
    if (jQuery('body #page #body div.process-large').length > 0) {
        var floaterBoxMax = 0;
        var floaterBoxMin = 179;
        jQuery('body #page #body div.process-large div div').each(function() {
            if (floaterBoxMax < jQuery(this).height()) floaterBoxMax = jQuery(this).height();
        });
        if (floaterBoxMax + 80 < floaterBoxMin) jQuery('body #page #body div.process-large').css('height', floaterBoxMin);
    }




    // TAB HANDLERS
    if (jQuery('body #page #body div.tab-container').length > 0) {
        var thisScroll = 0;
        if (window.location.hash != '#tab-none' && window.location.hash != '') {
            jQuery('body #page #body div.tab-container div.tab-content div.tab').hide();
            jQuery('body #page #body div.tab-container div.tab-content div' + window.location.hash + '.tab').show();

            jQuery('body #page #body div.tab-container table.tabs').addClass('active');
            jQuery('body #page #body div.tab-container table.tabs td.tab').removeClass('selected');
            jQuery('body #page #body div.tab-container table.tabs td.tab a[href=' + window.location.hash + ']').parent().addClass('selected');
        } else {
            jQuery('body #page #body div.tab-container table.tabs').removeClass('active');
            jQuery('body #page #body div.tab-container table.tabs td.tab').removeClass('selected');
            jQuery('body #page #body div.tab-container div.tab-content div.tab').hide();
        }
        jQuery('body #page #body div.tab-container table.tabs tr').mouseover(function() {
            jQuery(this).addClass('hover');
        });
        jQuery('body #page #body div.tab-container table.tabs tr').mouseout(function() {
            jQuery(this).removeClass('hover');
        });
        jQuery('body #page #body div.tab-container table.tabs td.tab a').each(function() {
            jQuery(this).parent().parent().children().filter('td').click(function() {
                thisScroll = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
                var thisHash = jQuery(this).parent().children().filter('td.tab').children().filter('a').attr('hash');
                if (thisHash != window.location.hash) {
                    window.location.hash = thisHash;
                } else {
                    window.location.hash = '#tab-none';
                }
                window.scrollTo(0, thisScroll);
                if (window.location.hash != '#tab-none' && window.location.hash != '') {
                    jQuery('body #page #body div.tab-container div.tab-content div.tab').hide();
                    jQuery('body #page #body div.tab-container div.tab-content div' + window.location.hash + '.tab').show();

                    jQuery('body #page #body div.tab-container table.tabs').addClass('active');
                    jQuery('body #page #body div.tab-container table.tabs td.tab').removeClass('selected');
                    jQuery('body #page #body div.tab-container table.tabs td.tab a[href=' + window.location.hash + ']').parent().addClass('selected');
                } else {
                    jQuery('body #page #body div.tab-container table.tabs').removeClass('active');
                    jQuery('body #page #body div.tab-container table.tabs td.tab').removeClass('selected');
                    jQuery('body #page #body div.tab-container div.tab-content div.tab').hide();
                }
                return false;
            });
        });
        jQuery('body #page #body div.tab-container div.tab a.close-tab').click(function() {
            jQuery('body #page #body div.tab-container table.tabs').removeClass('active');
            jQuery('body #page #body div.tab-container table.tabs td.tab').removeClass('selected');
            jQuery('body #page #body div.tab-container div.tab-content div.tab').hide();
            window.location.hash = '#tab-none';
            window.scrollTo(0, 0);
            return false;
        });
        setTimeout("window.scrollTo(0,0);", 100);
    }

    // Country Selector
    jQuery('body #page #header #country-selector').hide();
    jQuery('body #page #header #menu-global li.country > a').click(function() {
        if (jQuery('body #eventbubble').length == 0) jQuery('body').append('<div id="eventbubble"></div>');
        jQuery('body #eventbubble').show();
        jQuery('#flashDiv object').hide();
        var index = 0;
        var selectedIndex = 0;
        var thisName = jQuery(this).html();
        jQuery('body #page #header #country-selector li a span').each(function() {
            if (jQuery(this).html() == thisName) {
                jQuery(this).parent().parent().addClass('selected');
                selectedIndex = index;
            }
            index++;
        });
        jQuery('body #page #header #country-selector').css('left', '-32px');
        if (selectedIndex != 0) {
            jQuery('body #page #header #country-selector').css('top', '-' + ((selectedIndex * 34) - 1) + 'px');
            jQuery('body #page #header #country-selector').fadeIn('fast', function() {
                jQuery('body #page #header #country-selector').animate({ top: '-12px' }, 500);
            });
        } else {
            jQuery('body #page #header #country-selector').css('top', '-12px');
            jQuery('body #page #header #country-selector').fadeIn('fast');
        }
    });
    jQuery('body #page #header #menu-global li.country ul li a').click(function() {
        if (jQuery(this).parent().hasClass('selected')) {
            jQuery('body #page #header #country-selector').animate({ 'top': '-' + (jQuery('body #page #header #country-selector').height() + 12) + 'px' }, 500, '', function() {
                jQuery('body #page #header #country-selector').hide();
            });
            return false;
        } else {
            jQuery(this).css('background-color', '#2b85bf');
            jQuery(this).css('color', '#fff');
        }
    });
    jQuery('body #eventbubble').add('body #logo').mouseover(function() {
        jQuery('body #eventbubble').hide();
        jQuery('body #page #header #country-selector').animate({ 'top': '-' + (jQuery('body #page #header #country-selector').height() + 12) + 'px' }, 500, '', function() {
            jQuery('body #page #header #country-selector').hide();
            jQuery('#flashDiv object').show();
        });
        return false;
    });
});


var videoWidth, videoHeight, afterURL;

function showVideo(e) {
    jQuery('.video-backdrop').css('opacity', 0).show().fadeTo('fast', 0.65);
    var width = new Number(jQuery(jQuery(e).find('span')[0]).html());
    var height = new Number(jQuery(jQuery(e).find('span')[1]).html());
    afterURL = jQuery(jQuery(e).find('span')[2]).html();
    var url = jQuery(e).attr('href');
    var offset = jQuery(e).offset();

    var player = '/CrmOnline/Styles/mediaplayer/player.swf';

    if (url.match(/youtube.com/)) {
        player = url.split('v=')[1].replace('&', '?');
        player = 'http://youtube.com/v/' + player + '&autoplay=1';
    }

    var so = new SWFObject(player, 'playerobject', width, height, '9', '#');

    so.addParam('allowfullscreen', 'true');
    so.addParam('allowscriptaccess', 'always');
    so.addParam('wmode', 'opaque');
    so.addVariable('image', '/CrmOnline/Styles/images/body-background.png');
    so.addVariable('file', url);
    so.addVariable('skin', '/CrmOnline/Styles/mediaplayer/modieus.zip');
    so.addVariable('frontcolor', 'ffffff');
    so.addVariable('lightcolor', 'cc9900');
    so.addVariable('screencolor', 'ffffff');
    so.addVariable('stretching', 'fill');
    so.addVariable('autostart', 'true');

    jQuery('.video-outline').show().css({
        opacity: 0.75,
        width: jQuery(e).find('img').width() - 2,
        height: jQuery(e).find('img').height() - 2,
        left: offset.left,
        top: offset.top
    }).animate({
        width: width,
        height: height,
        left: (jQuery(window).width() / 2) - (width / 2),
        top: (jQuery(window).height() / 2) - (height / 2)
    }, 'normal', 'swing', function() {
        jQuery('.video-outline').hide();
        jQuery('.video-player').hide().css({
            width: Number(width) + 10 + 'px',
            height: Number(height) + 'px',
            left: (jQuery(window).width() / 2) - (width / 2) - 5,
            top: (jQuery(window).height() / 2) - (height / 2) - 5
        }).fadeIn('fast');
        so.write(jQuery('.video-player').find('.player div')[0]);
        if (afterURL != '') {
            setTimeout(attachVideoListener, 1000);
        }
    });
}

function attachVideoListener() {
    if (jQuery('#playerobject').length == 0) {
        setTimeout(attachVideoListener, 1000);
    } else {
        jQuery('#playerobject').get(0).addModelListener('STATE', 'videoStateMonitor');
    }
    return false;
}

function videoStateMonitor(player) {
    if (player.newstate == 'COMPLETED') {
        this.window.focus();
        window.location.href = afterURL;
    }
}


function hideVideo() {
    jQuery('.video-player').find('.player div').empty();
    jQuery('.video-player').animate({
        width: '20px',
        height: '10px',
        left: (jQuery(window).width() / 2) - 10,
        top: (jQuery(window).height() / 2) - 5
    }, 'fast');
    jQuery('.video-backdrop').add('.video-outline').add('.video-player').fadeOut('fast');
}
