jQuery().ready(function() {
    //  Adding this will actually fix the jQuery scripts on the startpage.
})

jQuery().ready(function() 
{
    if (jQuery.browser.mozilla) jQuery('body').addClass('mozilla');
    if (jQuery.browser.opera) jQuery('body').addClass('opera');
    jQuery.browser.chrome = false;
    if (jQuery.browser.safari && navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
        jQuery.browser.chrome = true;
        jQuery.browser.safari = false;
    }
    if (jQuery.browser.chrome) jQuery('body').addClass('chrome');
    if (jQuery.browser.safari) jQuery('body').addClass('safari');

    jQuery('.searchlogin .search .submit').click(function() {
        jQuery(this).parents('form').submit();
    });
    jQuery('body .page .page-header #country-selector').prev('a').addClass('country-selector').parent().css('position', 'relative');
    // VIDEO
    if (jQuery('body .video-player').length == 0) {
        var backdrop = jQuery('<div class="video-backdrop"></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="top"><div></div></div><div class="player"><div></div></div><div class="bottom"><div></div></div></div>');
        jQuery('body').append(backdrop, player, outline);
    }

    jQuery('body .page a.video').click(function() {
        showVideo(this);
        return false;
    });

    jQuery('.content .tabbed-table table tbody td h4').click(function() {
        if (jQuery(this).hasClass('expanded')) {
            jQuery(jQuery(this).parent().find('div')[0]).hide();
            jQuery(this).removeClass('expanded');
        } else {
            jQuery(jQuery(this).parent().find('div')[0]).show();
            jQuery(this).addClass('expanded');
        }
    });
    jQuery('.content .tabbed-table table tbody td h4').each(function() {
        if (jQuery(this).find('img').length > 0) jQuery(this).mouseover(function() {
            infoBubble.show(jQuery(this));
        }).mouseout(function() {
            infoBubble.hide(jQuery(this));
        });
    });


    jQuery('.row span').each(function() {
        jQuery(this).width(162 - jQuery(this).parent().find('label').width());
    });
    jQuery('.slides-content .slide').hide();
    jQuery('.slides-content .slide:first').show();
    var pageFooterBoxesHeight = 0;
    jQuery('.page-footer .boxes .box').each(function() {
        if (jQuery(this).height() > pageFooterBoxesHeight) pageFooterBoxesHeight = jQuery(this).height();
    });


    // CRM Online Landing Page boxes.
    var pageFooterBoxesHeight2 = 0;
    jQuery('.page-footer .boxes.crmonline-processed .c3 .box').each(function() {
        if (jQuery(this).height() > pageFooterBoxesHeight2) pageFooterBoxesHeight2 = jQuery(this).height();
    });

    jQuery('.page-footer .boxes.crmonline-processed .c3 .box').css('height', pageFooterBoxesHeight2 + (jQuery('.page-footer .boxes.crmonline-processed .c3 .box a.redbutton').length > 0 ? 0 : 0));

    
    jQuery('.tab-menu li a').each(function(i) {
        var sI = i;
        if (!jQuery('body').hasClass('full')) jQuery(this).click(function() {
            clearTimeout(animateTimer);
            selectedIndex = sI;
            selectSlide(i);
            return false;
        });
    });
    jQuery('.slide .left a').add('.slide .right a').not('a.video').each(function(i) {
        jQuery(this).click(function() {
            showExtras(jQuery('#' + jQuery(this).attr('rel')).height(), jQuery('#' + jQuery(this).attr('rel')).html());
            return false;
        });
    });
    jQuery('.slide .right').each(function() {
        if (jQuery(this).find('img').length == 0) jQuery(this).addClass('spaced');
    });
    jQuery('.content-container .content .top a').click(function() {
        jQuery(this).blur();
        if (!jQuery(this).parent().hasClass('show')) return false;
        hideExtras();
        animateTimer = setTimeout(function() { animateSlides(); }, 2000);
        return false;
    });
    jQuery('.tabs div a').click(function() {
        jQuery('.tabs div').removeClass('selected');
        jQuery(this).parent().addClass('selected');
        var selectedIndex = 0;
        jQuery('.tabs div').each(function(i) {
            if (jQuery(this).hasClass('selected')) selectedIndex = i - 1;
        });
        jQuery('.contents > div').removeClass('selected');
        jQuery(jQuery('.contents > div')[selectedIndex + 1]).addClass('selected');
        return false;
    });
    if (!jQuery('body').hasClass('full')) animateTimer = setTimeout(function() { animateSlides(); }, animateTimeout);
    else jQuery('.tab-menu div.selected').remove();
    jQuery('.extra-sizer').after(infoBubble.container);
    if (jQuery('body .eventbubble').length == 0) jQuery('.extra-sizer').after('<div class="eventbubble" style="display: none;"></div>');




    // xForm
    if (jQuery('body .page .content div.xForm').length > 0) {
        jQuery('body .page .content div.xForm tr').addClass('top');
        jQuery('body .page .content div.xForm input[type=checkbox]').addClass('checkbox');
        jQuery('body .page .content div.xForm tr td input.text').add('body .page .content div.xForm tr td textarea').add('body .page .content div.xForm tr td select').each(function() {
            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 .content div.xForm 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 .content div.xForm tr td input[type="submit"], body .page .content div.xForm tr td input[type="button"]').each(function() {
            jQuery(this).parent().html('<div class="button">' + jQuery(this).parent().html() + '</div>').attr('colspan', 2);
        });
        jQuery('body .page .content div.xForm tr td input.required, body .page .content div.xForm tr td textarea.required, body .page .content div.xForm tr td select.required').each(function() {
            jQuery(this).removeClass('required');
            jQuery(this).parent().html('<div class="form-required">' + jQuery(this).parent().html() + '</div>');
        });
    }


    jQuery('.searchlogin label').hide();




    // Country Selector
    jQuery('body .page .page-header #country-selector').hide();
    jQuery('body .page .page-header a.country-selector').click(function() {
        jQuery('body .eventbubble').show();
        var index = 0;
        var selectedIndex = 0;
        var thisName = jQuery(this).html();
        jQuery('body .page .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 .page-header #country-selector').css('left', '-32px');
        if (selectedIndex != 0) {
            jQuery('body .page .page-header #country-selector').css('top', '-' + ((selectedIndex * 34) - 1) + 'px');
            jQuery('body .page .page-header #country-selector').fadeIn('fast', function() {
                jQuery('body .page .page-header #country-selector').animate({ top: '-12px' }, 500);
            });
        } else {
            jQuery('body .page .page-header #country-selector').css('top', '-12px');
            jQuery('body .page .page-header #country-selector').fadeIn('fast');
        }
    });
    jQuery('body .page .page-header #country-selector ul li a').click(function() {
        if (jQuery(this).parent().hasClass('selected')) {
            jQuery('body .page .page-header #country-selector').animate({ 'top': '-' + (jQuery('body .page .page-header #country-selector').height() + 12) + 'px' }, 500, '', function() {
                jQuery('body .page .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').add('body .searchlogin').mouseover(function() {
        if (jQuery('body .eventbubble').css('display') != 'block') return true;
        jQuery('body .eventbubble').hide();
        jQuery('body .page .page-header #country-selector').animate({ 'top': '-' + (jQuery('body .page .page-header #country-selector').height() + 12) + 'px' }, 500, '', function() {
            jQuery('body .page .page-header #country-selector').hide();
        });
        return false;
    });

    /*@cc_on
    @if (@_win32)
    try {
        document.execCommand("BackgroundImageCache", false, true);
    } catch (err) { };
    @end@*/

});



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');
	clearTimeout(animateTimer);
	var offset = jQuery(e).find('img').offset();

/*	var video  = jQuery('<object height="'+ height +'" width="'+ width +'" name="player" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="player">\
		<param value="player.swf" name="movie"/>\
		<param value="true" name="allowfullscreen"/>\
		<param value="always" name="allowscriptaccess"/>\
		<param value="file='+url+'&amp;image=preview.jpg" name="flashvars"/>\
		<embed height="'+ height +'" width="'+ width +'" flashvars="autostart=true;file='+url+'&amp;imagepreview.jpg" allowfullscreen="true" allowscriptaccess="always" src="player.swf" name="player2" id="player2" type="application/x-shockwave-flash"/>\
	</object>');*/

	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');


	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(function() { jQuery('#playerobject')[0].addModelListener('STATE', 'videoStateMonitor'); }, 1000);
		}
	});
}
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');
}







var animateTimer = 0;
var animateTimeout = 120000;
var extrasVisible = false;
var selectedIndex = 0;

var infoBubble = {
	timer: 0,
	container: jQuery('<div class="infobubble"></div>').hide(),
	show: function(e) {
		var offset = jQuery(e).offset();
		if(jQuery(e).find('img').attr('alt')=='') jQuery(this.container).html( jQuery(e).html().replace(/ \<.+\>/,'') );
		else jQuery(this.container).html( jQuery(e).find('img').attr('alt') );
		jQuery(this.container).css({visibility: 'hidden', display: 'block'}).css({ top: offset.top - (jQuery(this.container).height()/2), left: offset.left + e.width() + 20 }).hide().css('visibility', 'visible').fadeIn('fast');
	},
	hide: function() {
		jQuery(this.container).fadeOut('fast');
	}
}

function showExtras(height, html) {
	clearTimeout(animateTimer);
	if(extrasVisible) {
		switchExtras(height, html);
		return false;
	}
	jQuery('.content-container .content .inner').html(html);
	jQuery('.content-container .content .top').addClass('show');
	jQuery('.content-container .content .top a span').fadeIn('fast', function() {
		if(jQuery.browser.msie) jQuery(this).css({ visibility: 'visible', display: 'block' });
	});
	jQuery('.page-footer').animate({top:(height+20)+'px'}, 500, 'swing');
	jQuery('.content-container .content .inner').animate({height:(height+100)+'px'}, 500, 'swing');
	jQuery('.content-container .content').animate({marginTop:'-10px'}, 500, 'swing', function() {
		extrasVisible = true;
	});
}
function hideExtras() {
	jQuery('.content-container .content .top').removeClass('show');
	jQuery('.content-container .content .top a span').fadeOut('fast', function() {
		if(jQuery.browser.msie) jQuery(this).css({ visibility: 'hidden', display: 'block' });
	});
	jQuery('.page-footer').animate({top:'0px'}, 500, 'swing');
	jQuery('.content-container .content .inner').animate({height:100+'px'}, 500, 'swing');
	jQuery('.content-container .content').animate({marginTop:'7px'}, 500, 'swing', function() {
		extrasVisible = false;
	});
}
function switchExtras(height, html) {
	var time = 250;
	clearTimeout(animateTimer);
	jQuery('.page-footer').animate({top:'0px'}, time, 'swing');
	jQuery('.content-container .content .inner').animate({height:100+'px'}, time, 'swing');
	jQuery('.content-container .content').animate({marginTop:'7px'}, time, 'swing', function() {
		jQuery('.content-container .content .inner').html(html);
		jQuery('.page-footer').animate({top:(height+20)+'px'}, time, 'swing');
		jQuery('.content-container .content .inner').animate({height:(height+100)+'px'}, time, 'swing');
		jQuery('.content-container .content').animate({marginTop:'-10px'}, time, 'swing');
	});
}
function animateSlides() {
	if(selectedIndex==jQuery('.tab-menu ul li').length-1) selectedIndex = 0;
	else selectedIndex++;
	selectSlide(selectedIndex);
	animateTimer = setTimeout(function() { animateSlides(); }, animateTimeout);
}
function selectSlide(selectedIndex) {
	hideExtras();
	jQuery('.slides-container').animate({top:'254px'}, 500, 'swing', function() {
		jQuery('.slides-container').animate({top:'0px'}, 500, 'swing');
	});
	var ie6 = jQuery.browser.msie && jQuery('.ie6 .tab-menu div.selected').length>0;
	jQuery('.tab-menu div.selected').animate( (ie6? {top:'-30px'}:{marginTop:'-30px'}), 500, 'swing', function() {
		jQuery('.slides-content .slide').hide();
		jQuery(jQuery('.slides-content .slide')[selectedIndex]).show();
		jQuery('.tab-menu div.selected').css('left', selectedIndex*190+( ie6?10:5) ).html( jQuery(jQuery('.tab-menu ul li')[selectedIndex]).html() ).animate( (ie6? {top:'-2px'}:{marginTop:'0px'}), 500, 'swing');
	});
}
function animateExtras() {
	jQuery('.page-footer').animate({top:'50px'}, 500, 'swing', function() {
		jQuery('.page-footer').animate({top:'0px'}, 500, 'swing');
	});
	jQuery('.content-container').animate({top:'-20px'}, 500, 'swing', function(){
		jQuery('.content-container').animate({top:'0px'}, 500, 'swing', function(){
			animateExtras();
		});
	});
}
