jQuery(function() {
	if($.browser.mozilla) $('body').addClass('mozilla');
	if($.browser.opera) $('body').addClass('opera');
	$.browser.chrome = false;
	if($.browser.safari && navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
		$.browser.chrome = true;
		$.browser.safari = false;
	}
	if($.browser.chrome) $('body').addClass('chrome');
	if($.browser.safari) $('body').addClass('safari');

	$('.searchlogin .search .submit').click(function(){
		$(this).parents('form').submit();
	});
	$('body .page .page-header #country-selector').prev('a').addClass('country-selector').parent().css('position','relative');
	// VIDEO
	if($('body .video-player').length==0) {
		var backdrop = $('<div class="video-backdrop"></div>').click(function(){hideVideo()}).hide();
		var outline = $('<div class="video-outline"></div>').hide();
		var player = $('<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>');
		$('body').append(backdrop, player, outline);
	}
	
	$('body .page .slide a.video').click(function(){
		showVideo(this);
		return false;
	});
	
	$('.content .tabbed-table table tbody td h4').click(function() {
		if($(this).hasClass('expanded')) {
			$($(this).parent().find('div')[0]).hide();
			$(this).removeClass('expanded');
		} else {
			$($(this).parent().find('div')[0]).show();
			$(this).addClass('expanded');
		}
	});
	$('.content .tabbed-table table tbody td h4').each(function() {
		if($(this).find('img').length>0) $(this).mouseover(function() {
			infoBubble.show($(this));
		}).mouseout(function() {
			infoBubble.hide($(this));
		});
	});
	
	
	$('.row span').each(function() {
		$(this).width(162 - $(this).parent().find('label').width());
	});
	$('.slides-content .slide').hide();
	$('.slides-content .slide:first').show();
	var pageFooterBoxesHeight = 0;
	$('.page-footer .boxes .box').each(function() {
		if($(this).height()>pageFooterBoxesHeight) pageFooterBoxesHeight=$(this).height();
	});
	$('.tab-menu li a').each(function(i){
		var sI = i;
		if(!$('body').hasClass('full')) $(this).click(function(){
			clearTimeout(animateTimer);
			selectedIndex = sI;
			selectSlide(i);
			return false;
		});
	});
	$('.slide .left a').add('.slide .right a').not('a.video').each(function(i){
		$(this).click(function(){
			showExtras($('#' + $(this).attr('rel')).height(), $('#' + $(this).attr('rel')).html());
			return false;
		});
	});
	$('.slide .right').each(function(){
		if($(this).find('img').length==0) $(this).addClass('spaced');
	});
	$('.content-container .content .top a').click(function(){
		$(this).blur();
		if(!$(this).parent().hasClass('show')) return false;
		hideExtras();
		animateTimer = setTimeout(function() { animateSlides(); }, 2000);
		return false;
	});
	$('.tabs div a').click(function() {
		$('.tabs div').removeClass('selected');
		$(this).parent().addClass('selected');
		var selectedIndex = 0;
		$('.tabs div').each(function(i){
			if($(this).hasClass('selected')) selectedIndex = i-1;
		});
		$('.contents > div').removeClass('selected');
		$($('.contents > div')[selectedIndex+1]).addClass('selected');
		return false;
	});
	if(!$('body').hasClass('full')) animateTimer = setTimeout(function() { animateSlides(); }, animateTimeout);
	else $('.tab-menu div.selected').remove();
	$('.extra-sizer').after(infoBubble.container);
	if($('body .eventbubble').length==0) $('.extra-sizer').after('<div class="eventbubble" style="display: none;"></div>');
	

	
	
	// xForm
	if($('body .page .content div.xForm').length>0) {
		$('body .page .content div.xForm tr').addClass('top');
		$('body .page .content div.xForm input[type=checkbox]').addClass('checkbox');
		$('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 () {
			$($(this).parent().parent().children()[0]).addClass('label').html( '<label for="'+ this.id +'">' + $($(this).parent().parent().children()[0]).html() + '</label>' );
		});
		$('body .page .content div.xForm tr td fieldset').each(function () {
			if($($(this).parent().parent().children()[0]).html()!='') $($(this).parent().parent().children()[0]).addClass('label').html( '<label for="">' + $($(this).parent().parent().children()[0]).html() + '</label>' );
		});
		$('body .page .content div.xForm tr td input[type="submit"], body .page .content div.xForm tr td input[type="button"]').each(function () {
			$(this).parent().html( '<div class="button">' + $(this).parent().html() + '</div>' ).attr('colspan', 2);
		});
		$('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 () {
			$(this).removeClass('required');
			$(this).parent().html( '<div class="form-required">' + $(this).parent().html() + '</div>' );
		});
	}
	
	
	$('.searchlogin label').hide();
	
	
	
	
	// Country Selector
	$('body .page .page-header #country-selector').hide();
	$('body .page .page-header a.country-selector').click(function() {
		$('body .eventbubble').show();
		var index = 0;
		var selectedIndex = 0;
		var thisName = $(this).html();
		$('body .page .page-header #country-selector li a span').each(function() {
			if($(this).html() == thisName) {
				$(this).parent().parent().addClass('selected');
				selectedIndex = index;
			}
			index++;
		});
		$('body .page .page-header #country-selector').css('left', '-32px');
		if(selectedIndex!=0) {
			$('body .page .page-header #country-selector').css('top', '-' + ((selectedIndex*34) - 1) + 'px');
			$('body .page .page-header #country-selector').fadeIn('fast', function() {
				$('body .page .page-header #country-selector').animate({top: '-12px'}, 500);
			});
		} else {
			$('body .page .page-header #country-selector').css('top', '-12px');
			$('body .page .page-header #country-selector').fadeIn('fast');
		}
	});
	$('body .page .page-header #country-selector ul li a').click(function() {
		if($(this).parent().hasClass('selected')) {
			$('body .page .page-header #country-selector').animate({'top':'-'+($('body .page .page-header #country-selector').height()+12)+'px'}, 500, '', function() {
				$('body .page .page-header #country-selector').hide();
			});
			return false;
		} else {
			$(this).css('background-color','#2b85bf');
			$(this).css('color','#fff');
		}
	});
	$('body .eventbubble').add('body .logo').add('body .searchlogin').mouseover(function() {
		if($('body .eventbubble').css('display')!='block') return true;
		$('body .eventbubble').hide();
		$('body .page .page-header #country-selector').animate({'top':'-'+($('body .page .page-header #country-selector').height()+12)+'px'}, 500, '', function() {
			$('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) {
	$('.video-backdrop').css('opacity',0).show().fadeTo('fast', 0.65);
	var width = new Number($($(e).find('span')[0]).html());
	var height = new Number($($(e).find('span')[1]).html());
	afterURL = $($(e).find('span')[2]).html();
	var url = $(e).attr('href');
	clearTimeout(animateTimer);
	var offset = $(e).find('img').offset();

/*	var video  = $('<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 so = new SWFObject('/CrmOnline/Styles/mediaplayer/player.swf', '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');


	$('.video-outline').show().css({
		opacity: 0.75,
		width: $(e).find('img').width()-2,
		height: $(e).find('img').height()-2,
		left: offset.left,
		top: offset.top
	}).animate({
		width: width,
		height: height,
		left: ($(window).width()/2)-(width/2),
		top: ($(window).height()/2)-(height/2)
	},'normal', 'swing', function() {
		$('.video-outline').hide();
		$('.video-player').hide().css({
			width: Number(width)+10 +'px',
			height: Number(height) +'px',
			left: ($(window).width()/2)-(width/2)-5,
			top: ($(window).height()/2)-(height/2)-5
		}).fadeIn('fast');
		so.write($('.video-player').find('.player div')[0]);
		if(afterURL!='') {
			setTimeout(function() { $('#playerobject')[0].addModelListener('STATE', 'videoStateMonitor'); }, 1000);
		}
	});
}
function videoStateMonitor(player) {
	if(player.newstate == 'COMPLETED') {
		this.window.focus();
		window.location.href = afterURL;
	}
}


function hideVideo() {
	$('.video-player').find('.player div').empty();
	$('.video-player').animate({
		width: '20px',
		height: '10px',
		left: ($(window).width()/2)-10,
		top: ($(window).height()/2)-5
	}, 'fast');
	$('.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: $('<div class="infobubble"></div>').hide(),
	show: function(e) {
		var offset = $(e).offset();
		if($(e).find('img').attr('alt')=='') $(this.container).html( $(e).html().replace(/ \<.+\>/,'') );
		else $(this.container).html( $(e).find('img').attr('alt') );
		$(this.container).css({visibility: 'hidden', display: 'block'}).css({ top: offset.top - ($(this.container).height()/2), left: offset.left + e.width() + 20 }).hide().css('visibility', 'visible').fadeIn('fast');
	},
	hide: function() {
		$(this.container).fadeOut('fast');
	}
}

function showExtras(height, html) {
	clearTimeout(animateTimer);
	if(extrasVisible) {
		switchExtras(height, html);
		return false;
	}
	$('.content-container .content .inner').html(html);
	$('.content-container .content .top').addClass('show');
	$('.content-container .content .top a span').fadeIn('fast', function() {
		if($.browser.msie) $(this).css({ visibility: 'visible', display: 'block' });
	});
	$('.page-footer').animate({top:(height+20)+'px'}, 500, 'swing');
	$('.content-container .content .inner').animate({height:(height+100)+'px'}, 500, 'swing');
	$('.content-container .content').animate({marginTop:'-10px'}, 500, 'swing', function() {
		extrasVisible = true;
	});
}
function hideExtras() {
	$('.content-container .content .top').removeClass('show');
	$('.content-container .content .top a span').fadeOut('fast', function() {
		if($.browser.msie) $(this).css({ visibility: 'hidden', display: 'block' });
	});
	$('.page-footer').animate({top:'0px'}, 500, 'swing');
	$('.content-container .content .inner').animate({height:100+'px'}, 500, 'swing');
	$('.content-container .content').animate({marginTop:'7px'}, 500, 'swing', function() {
		extrasVisible = false;
	});
}
function switchExtras(height, html) {
	var time = 250;
	clearTimeout(animateTimer);
	$('.page-footer').animate({top:'0px'}, time, 'swing');
	$('.content-container .content .inner').animate({height:100+'px'}, time, 'swing');
	$('.content-container .content').animate({marginTop:'7px'}, time, 'swing', function() {
		$('.content-container .content .inner').html(html);
		$('.page-footer').animate({top:(height+20)+'px'}, time, 'swing');
		$('.content-container .content .inner').animate({height:(height+100)+'px'}, time, 'swing');
		$('.content-container .content').animate({marginTop:'-10px'}, time, 'swing');
	});
}
function animateSlides() {
	if(selectedIndex==$('.tab-menu ul li').length-1) selectedIndex = 0;
	else selectedIndex++;
	selectSlide(selectedIndex);
	animateTimer = setTimeout(function() { animateSlides(); }, animateTimeout);
}
function selectSlide(selectedIndex) {
	hideExtras();
	$('.slides-container').animate({top:'254px'}, 500, 'swing', function() {
		$('.slides-container').animate({top:'0px'}, 500, 'swing');
	});
	var ie6 = $.browser.msie && $('.ie6 .tab-menu div.selected').length>0;
	$('.tab-menu div.selected').animate( (ie6? {top:'-30px'}:{marginTop:'-30px'}), 500, 'swing', function() {
		$('.slides-content .slide').hide();
		$($('.slides-content .slide')[selectedIndex]).show();
		$('.tab-menu div.selected').css('left', selectedIndex*190+( ie6?10:5) ).html( $($('.tab-menu ul li')[selectedIndex]).html() ).animate( (ie6? {top:'-2px'}:{marginTop:'0px'}), 500, 'swing');
	});
}
function animateExtras() {
	$('.page-footer').animate({top:'50px'}, 500, 'swing', function() {
		$('.page-footer').animate({top:'0px'}, 500, 'swing');
	});
	$('.content-container').animate({top:'-20px'}, 500, 'swing', function(){
		$('.content-container').animate({top:'0px'}, 500, 'swing', function(){
			animateExtras();
		});
	});
}