$(document).ready(function(){							
	function usercode()
	{
		var tmp = unescape(window.location.href).split("?");

		if (tmp.length>1)
		{
			args = tmp[1].split("&");
			for (var i=0; i < args.length; i++) 
			{
  				var arg = args[i].split('=');
  				if (arg[0] == 'u')
				{
					return arg[1];
				}
			}
		} 				
		
		return null;
	}
	
	function modify_url(obj, attr, ucode)
	{
		var link = obj.attr(attr).toString();		

		if (link.length > 0)
		{
			var click = obj.attr('onclick');
			
			if (click != null)
			{
				click = click.toString();
				if (click.match('window.open') && link.match('http://') == null)
				{								
					link = base + link;
				}	
			}			
			 			
			if (link.match('\\?') != null)
			{				
				obj.attr(attr, link + '&' + 'u=' + ucode);
			}
			else
			{				
				obj.attr(attr, link + '?' + 'u=' + ucode);
			}														
		}		
	}
	
	var user_code = usercode();
	
	if (user_code != null)
	{		
		$("a").each(function()
		{
			var link = $(this).attr('href').toString();			
						
			if (link.length > 0 && 
				link != '#' && 
				link.match("mailto:") == null &&
				(link.match("http://") == null || link.match("langas") != null))
			{
				modify_url($(this), 'href', user_code);				
			}						
		});
		
		$("form").each(function()
		{					
			modify_url($(this), 'action', user_code);
		});
	}
	
	$('#top .c1 li a').hover(function(){
		$('span', this).show();
	}, function(){
		$('span', this).hide();
	});
	
	
	$('.hw1 .c').hide();
	$('.hw1 .c:first').show();		
	$('.hw1 h3:first').addClass('selected');
	
	$('.hw1 h3 a').mouseover(function(){
		$('.hw1 h3').removeClass('selected');		
		$('.hw1 .c').fadeOut('slow');
	});
	
	$('.hw1 .w1 h3 a').mouseover(function(){
		$('.hw1 .w1 h3').addClass('selected');		
		$('.hw1 .w1 .c').fadeIn('slow');
	});
	
	$('.hw1 .w2 h3 a').mouseover(function(){
		$('.hw1 .w2 h3').addClass('selected');		
		$('.hw1 .w2 .c').fadeIn('slow');
	});
	
	$('.hw1 .w3 h3 a').mouseover(function(){
		$('.hw1 .w3 h3').addClass('selected');		
		$('.hw1 .w3 .c').fadeIn('slow');
	});
	
	$('.hw1 .w4 h3 a').mouseover(function(){
		$('.hw1 .w4 h3').addClass('selected');		
		$('.hw1 .w4 .c').fadeIn('slow');
	});
	
	
	$('.cw6 .w tbody tr').each(function(){
		if ($(this).attr('class') != 'not-clickable-row' &&
			$(this).attr('class') != 'not-clickable-row1' && 
			$(this).attr('class') != 'not-clickable-row2') {
			$(this).click(function(){
				document.location = $('th h3 a', $(this)).attr('href');
			});
			
			$(this).hover(function(){
				$('th h3 a', this).css('text-decoration', 'underline');
			}, function(){
				$('th h3 a', this).css('text-decoration', 'none');
			});
		}
	});
	
	
	$('#top .c1 .i5 a').click(function(){
		$('body').addClass('biggertext');
		
		return false;
	});
	
	$('#top .c1 .i6 a').click(function(){
		$('body').removeClass('biggertext');
		
		return false;
	});
	
	$('#top .c1 .i3 a').jFav();
	
	
	$('.datepicker').datepick({dateFormat: 'dd/mm/yy'});
});


$(window).load(function(){	
	
/*	$('#mycarousel img').each(function(){
		var width = $(this).width();
	
		$(this).parent().parent().css('width', width);
	});
*/	
	
	function mycarousel_initCallback(carousel)
	{		
			// Disable autoscrolling if the user clicks the prev or next button.
			carousel.buttonNext.bind('click', function() {
					carousel.startAuto(0);
			});

			carousel.buttonPrev.bind('click', function() {
					carousel.startAuto(0);
			});
						
			// Pause autoscrolling if the user moves with the cursor over the clip.
			carousel.clip.hover(function() {										
					carousel.stopAuto();
			}, function() {
					carousel.startAuto();
			});
	};
	
	function mycarousel_getItemHTML(item)
	{	
    	return '<li><a target=_blank href="' + item.url + '" style="background: url(files/client/' + item.image + ') no-repeat center;"><img src="files/client/' + item.image + '" alt="" /></a></li>';
	};
	
	function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt)
	{		
    	var index = carousel.index(i, companyList.length);
    	carousel.add(i, mycarousel_getItemHTML(companyList[index - 1]));
	};

	function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt)
	{	
    	carousel.remove(i);
	};

	$('div#carousel').append('<ul id="mycarousel"></ul>');
	
	jQuery('#mycarousel').jcarousel({
			auto: 0.00000001,
			scroll: 1,
			wrap: 'circular',
			animation: 3000,
			easing: 'linear',
			initCallback: mycarousel_initCallback,
			itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
        	itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
	});
	
	
	var numTrainings = 0;
	var rollout = true; 
	
	$('#recommended-trainings').each(function()
	{		
		var middleHeight = $('#middle').height();
		var items = $('#recommended-trainings li');
		
		for (i=0; i < items.length; ++i)
		{
			if (middleHeight < $('#middle').height())
			{
				$('#recommended-trainings-show-more').show();
				break;																												
			}
			$(items[i]).show();
			numTrainings++;			
		}
		$('#recommended-trainings-show-more .rollin').hide();						
	});
	
	$('#recommended-trainings-show-more').click(function(event){
		$('#recommended-trainings').each(function(){
			
			var items = $('#recommended-trainings li');			
			for (i=numTrainings; i < items.length; i++)
			{
				if (rollout)
				{
					$(items[i]).show();
					$('#recommended-trainings-show-more .rollout').hide();
					$('#recommended-trainings-show-more .rollin').show();
				}				
				else
				{
					$(items[i]).hide();
					$('#recommended-trainings-show-more .rollout').show();
					$('#recommended-trainings-show-more .rollin').hide();
				}
			}
			
			rollout = !rollout;												
		});	
				
		event.preventDefault();
	});				
});