jQuery(function(){
	//About / team
	jQuery('#about_left a').click(function(){
		if(jQuery(this).hasClass('dont_activate_me')) return true;
		if(jQuery(this).hasClass('active')) return false;
		var rel = jQuery(this).attr('href');
		jQuery('#about_left a').removeClass('active');
		jQuery(this).addClass('active');

		jQuery('#about_right > div').hide();
		jQuery(rel).css({opacity : 0}).show()

		jQuery(rel).animate({opacity : 1}, 300);
		var api = jQuery(rel + ' .about_content').data('jsp');
		api.reinitialise();

		return false;
	});

	//Clients list / case studies
	jQuery('#cl_col2 a').click(function(){
		if(jQuery(this).hasClass('active')) return false;
		var rel = jQuery(this).attr('href');
		jQuery('#cl_col2 a').removeClass('active');
		jQuery(this).addClass('active');

		jQuery('#cl_col3 > div').hide();
		jQuery(rel).css({opacity : 0}).show().animate({opacity : 1}, 300);
		return false;
	});

	//Portfolio
	var acc = 0;
	jQuery('#cc_in a').each(function(){
		acc += jQuery(this).width() + 61;
	});
	jQuery('#cc_in').width(acc);
	if(acc > jQuery('#categories_cont').width())
		jQuery('#cc_left, #cc_right').show();
	jQuery('#cc_left')
		.mouseout(cc_stop)
		.mouseover(function(){
			cc_dir = +1;
			cc_start();
		})
	jQuery('#cc_right')
		.mouseout(cc_stop)
		.mouseover(function(){
			cc_dir = -1;
			cc_start();
		})
/*
		jQuery('#cc_in a').click(function(){
		if(!jQuery(this).hasClass('active')){
			jQuery('#cc_in a').removeClass('active');
			jQuery(this).addClass('active');

			var href = jQuery(this).attr('href');
			jQuery('.portfolio_cat').hide();
			jQuery(href).css({opacity : 0}).show().animate({opacity : 1}, 300);
		}
		return false;
	})
*/
	cc_pos = parseInt(jQuery('#cc_in').css('left'));
	cc_pos = cc_pos ? cc_pos : 0;

	jQuery('.portfolio_tin2').each(function(){
		var acc = 0;
		jQuery('a', this).each(function(){
			acc += jQuery(this).width() + 15;
		});
		acc += 10;
		jQuery(this).width(acc);
		if(acc > jQuery(this).parent().width())
			jQuery('.portfolio_tleft, .portfolio_tright', jQuery(this).parent().parent()).show();

		jQuery('.portfolio_tleft', jQuery(this).parent().parent())
			.mouseout(pf_stop)
			.mouseover(function(){
				pf_dir = +1;
				pf_el = jQuery('.portfolio_tin2', jQuery(this).parent());
				pf_start();
			})
		jQuery('.portfolio_tright', jQuery(this).parent().parent())
			.mouseout(pf_stop)
			.mouseover(function(){
				pf_dir = -1;
				pf_el = jQuery('.portfolio_tin2', jQuery(this).parent());
				pf_start();
			})
		jQuery('a', this).click(function(){
			if(!jQuery(this).hasClass('active')){
				jQuery('a', jQuery(this).parent()).removeClass('active');
				jQuery(this).addClass('active');
				
				var href = jQuery(this).attr('href');
				jQuery('.portfolio_prod', jQuery(this).parent().parent().parent().parent()).hide();
				jQuery(href).css({opacity : 0}).show().animate({opacity : 1}, 300);
				
				var clientid=jQuery(this).attr('clientid');
				var catid=jQuery(this).attr('catid');
				jQuery.ajax({
					type: "POST",
					url: "dynamic.php",
					data: {clientid: clientid, catid:catid},
					success: function(html)
					{
						if (jQuery("#cat"+catid+"_prod"+clientid+" > .portfolio_slides img").length) 
						{
							//alert('yes');
						}
						else
						{
							//alert('no');
							jQuery( "#cat"+catid+"_prod"+clientid+" > .portfolio_slides" ).empty();
							jQuery( "#cat"+catid+"_prod"+clientid+" > .portfolio_slides" ).append(html);
							
							jQuery( "#cat"+catid+"_prod"+clientid+" > .portfolio_slides" ).nivoSlider({
								effect : 'fade',
								animSpeed:1000
							});
							jQuery( "#cat"+catid+"_prod"+clientid+" > .portfolio_text" ).jScrollPane();
						}
					}
				});
				
			}
			return false;
		})
	})
})

var cc_dir = 0, cc_int = null, cc_pos = 0, cc_diff = 0;
function cc_stop(){
	if(cc_int)
		clearInterval(cc_int);
}
function cc_start(){
	cc_int = setInterval(cc_process, 10);
	cc_diff = jQuery('#cc_in').width() - jQuery('#categories_cont').width() - 1;
}
function cc_process(){
	cc_pos += cc_dir * 2;
	cc_pos = Math.min(0, Math.max(-cc_diff, cc_pos))
	jQuery('#cc_in').css({left : cc_pos + 'px'});
}

var pf_dir = 0, pf_int = null, pf_pos = 0, pf_diff = 0, pf_el = null;
function pf_stop(){
	if(pf_int)
		clearInterval(pf_int);
}
function pf_start(){
	pf_int = setInterval(pf_process, 10);
	pf_diff = jQuery(pf_el).width() - jQuery(pf_el).parent().width() - 15;
}
function pf_process(){
	pf_pos += pf_dir * 2;
	pf_pos = Math.min(0, Math.max(-pf_diff, pf_pos))
	jQuery(pf_el).css({left : pf_pos + 'px'});
}
