var tabAnimation = false;
var currprodid = 1;

function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}


function startanim() {
	var prodad = document.getElementById('prodad');
	if(prodad) {
	opacity("prodad", 100, 0 , 400);
	var prodad1 = document.getElementById('prodad'+currprodid);
	prodad.innerHTML = prodad1.innerHTML;
	if(currprodid == 3) {
		currprodid = 0;
	}
	currprodid++;
	opacity("prodad", 0, 100 , 400);
	setTimeout("startanim()", 5000);
	}
}

function showchat() {
	var chatwidget = document.getElementById('chatwidget').style;
	var chatText = document.getElementById('chatText');
	if(chatwidget.display == "") {
		chatwidget.display = "none";
		chatText.innerHTML = "Live Chat";
	}
	else {
		chatwidget.display = "";
		chatText.innerHTML = "<span style='color:#f4861e'>Close Live Chat</span>";
	}
}

function fixHeaders(){
	jQuery('.inner-content > h1').each(
		function(){
			if(! jQuery(this).text().length)
				return;
			$(this).hide();
			var bgcolor = '';
			font = 'myriadpro';
			size = '18';
			color = '007ECB';
			bgcolor = 'FFFFFF';
			width = jQuery(this).width() + 20;

			var src = '/imagetext.php?title='+encodeURIComponent(jQuery(this).text())+'&font='+font+'&size='+size+'&width='+(jQuery(this).width() + 30)+'&height='+jQuery(this).height()+'&color='+color+'&bgcolor='+bgcolor+'&top=top&foreg=EAF7FF';
			img = '<img src="'+src+'" title="'+jQuery(this).text()+'" alt="'+jQuery(this).text()+'" width="'+(jQuery(this).width() + 30)+'px" height="'+jQuery(this).height()+'px" border="0" />';
			jQuery(this).html( img ).css({paddingBottom: 8 });
			$(this).show();
		}
	);

	jQuery('.thematic-block-left h2, .thematic-block h2').each(
		function(){
			if(! jQuery(this).text().length)
				return;
			$(this).hide();
			var bgcolor = '';
			font = 'myriadpro';
			size = '15';
			color = '006EB1';
			bgcolor = 'FFFFFF';
			width = jQuery(this).width() + 20;

			var src = '/imagetext.php?title='+encodeURIComponent(jQuery(this).text())+'&font='+font+'&size='+size+'&width='+width+'&height='+jQuery(this).height()+'&color='+color+'&bgcolor='+bgcolor+'&top=top&foreg=EAF7FF';
			img = '<img src="'+src+'" title="'+jQuery(this).text()+'" alt="'+jQuery(this).text()+'" width="'+width+'px" height="'+jQuery(this).height()+'px" border="0" />';
			jQuery(this).html( img ).css({paddingBottom: 0 });
			$(this).show();
		}
	);

	jQuery('.submenu h2').each(
		function(){
			if(! jQuery(this).text().length)
				return;
			$(this).hide();
			var bgcolor = '';
			font = 'myriadpro';
			size = '15';
			color = '006EB1';
			bgcolor = 'FFFFFF';
			width = jQuery(this).width() + 20;

			var src = '/imagetext.php?title='+encodeURIComponent(jQuery(this).text())+'&font='+font+'&size='+size+'&width='+width+'&height='+jQuery(this).height()+'&color='+color+'&bgcolor='+bgcolor+'&top=top&foreg=EAF7FF';
			img = '<img src="'+src+'" title="'+jQuery(this).text()+'" alt="'+jQuery(this).text()+'" width="'+width+'px" height="'+jQuery(this).height()+'px" border="0" />';
			jQuery(this).css({paddingBottom: 0 }).html('');
			$(img).insertBefore(this);
			$(this).show();
		}
	);

	jQuery('div.portfolio-project-list h2, div.portfolio-info-list h2').each(
		function(){
			if(! jQuery(this).text().length)
				return;
			$(this).hide();
			var bgcolor = '';
			font = 'myriadpro';
			size = '15';
			color = '007ECB';
			bgcolor = 'FFFFFF';
			width = jQuery(this).width() + 20;

			var src = '/imagetext.php?title='+encodeURIComponent(jQuery(this).text())+'&font='+font+'&size='+size+'&width='+width+'&height='+jQuery(this).height()+'&color='+color+'&bgcolor='+bgcolor+'&top=top&foreg=EAF7FF';
			img = '<img src="'+src+'" title="'+jQuery(this).text()+'" alt="'+jQuery(this).text()+'" width="'+width+'px" height="'+jQuery(this).height()+'px" border="0" />';
			jQuery(this).html( img ).css({paddingBottom: 0 });
			$(this).show();
		}
	);
}

function initTabs() {
	$('#tabs .navi a').each(function(ind){

		$(this).click(function(){
			if($(this).parent('li').is('.here') || tabAnimation)
				return false;

			tabAnimation = true;

			//activate tab
			$('#tabs .navi li').removeClass('here');
			$(this).parent('li').addClass('here');

			//hide container
			$('#tabs .tab-content:visible').fadeOut(function(){
				//show container
				$('#tabs .tab-content:eq('+ind+')').fadeIn(function(){ tabAnimation = false; });
			});

			return false;
		});
	});
}

function createSelect( id_prefix ){
	var hideQJTimeout = false;

	function hideQuickJump(){
		resetQuickJump();
		hideQJTimeout = setTimeout(function(){ if( ! $('#'+id_prefix+'Options li.select-option-hovered').length ){ $('#'+id_prefix+'Options').fadeOut(); } }, 500);
	}
	function resetQuickJump(){
		clearTimeout( hideQJTimeout );
	}

	$('#'+id_prefix+'Area').click(function(){
		if( $('#'+id_prefix+'Options').is(':hidden') ){
			resetQuickJump();
			if( $('#'+id_prefix+'Options').is('.select-options-invisible') )
				$('#'+id_prefix+'Options').removeClass('select-options-invisible').addClass('select-options-visible').appendTo('body');

			$('#'+id_prefix+'Options').css({ position: 'absolute', top: $(this).offset().top + $(this).height() - 1, left: $(this).offset().left + ($.browser.mozilla ? 1 : 0), width: $(this).width() - 2 }).show();

			var offset = $('#'+id_prefix+'Options').offset().top + $('#'+id_prefix+'Options').height();
			offset -= $(window).height();

			var ScrollTop = document.body.scrollTop;
			if (ScrollTop == 0)	{
			    if (window.pageYOffset)
			        ScrollTop = window.pageYOffset;
			    else
			        ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
			}

			offset -= ScrollTop;

			if( offset > 0 ){

				if( $(this).offset().top - $('#'+id_prefix+'Options').height() > ScrollTop ){
					$('#'+id_prefix+'Options').css({ position: 'absolute', 'top': $(this).offset().top - $('#'+id_prefix+'Options').height() - 2, left: $(this).offset().left + ($.browser.mozilla ? 1 : 0), width: $(this).width() - 2 });
				}else{
					$.scrollTo('+=' + (offset + 10) + 'px', { duration:500, easing:'swing' });
				}
			}

		}else{
			$('#'+id_prefix+'Options').hide();
		}
	}).mouseover( resetQuickJump ).mouseout( hideQuickJump )
	.children('#'+id_prefix+'Options').hover( resetQuickJump, hideQuickJump )
	.children('li').hover(function(){
		resetQuickJump();
		$(this).addClass('select-option-hovered');
	},function(){
		$(this).removeClass('select-option-hovered');
	}).click(function(){
		$('#'+id_prefix+'Text').text( $(this).text() );
		document.location = $(this).attr('title');
	});
}



jQuery(document).ready(function() {

	$('div.menu > ul').menu({
		container: '#all',
		showAnimation: 100,
		hideAnimation: 100,
		horizontal: ['.inner-menu3'],
		vertical: ['.inner-menu']
	});

	initTabs();
	fixHeaders();

	createSelect('quickJumpSelect');
	startanim();

});

