/**
 *	AUTHOR:
 * 	Cube Interactive 
 *  Michał Daniel, m.daniel@icube.pl
 *  www.icube.pl
 *   
 *	Based on jQuery JavaScript Library v1.3.2
 *  http://jquery.com/   
 */  


var icube_carousel = {
	elements : 6, // ilość elementów
	current : 2,
	offset : 2,
	run : null, // interval
	speed : 3500,
	item_width : 37,
	fade_speed : 800,
	direction : 1, // 1 = right, 0 = left
	
	init: function() {	
		this.elements = $('#selems ul').children().size();
		this.item_width = $('#selems li').outerWidth() + 8; 	
		this.switchElement(this.current);
	
		for (var i = 1; i <= this.elements; i++)
		{
			$('#s-element_'+i).click(function() {
				icube_carousel.goTo(parseInt(this.id.substring(this.id.indexOf('_')+1)));
				return false;
		    });
		}		
				
	 	$('#showcase-prev').click(function() {
		 	icube_carousel.resetInterval();	
		 	icube_carousel.prev();
			return false;		
		}); 
		
		$('#showcase-next').click(function() {	
			icube_carousel.resetInterval();
			icube_carousel.next();
			return false;		
		});	
		
		this.run = setInterval('icube_carousel.auto()', this.speed); 
	},
	
	resetInterval: function() {
		clearInterval(this.run);
		this.run = setInterval('icube_carousel.auto()', this.speed);
	},
	
	stop: function() {
		clearInterval(this.run);	
	},
	
	runcarousel: function() {
		this.run = setInterval('icube_carousel.auto()', this.speed);	
	},	
	
	auto: function() {
		if (this.current == this.elements) {
			this.direction = 0;
		}
		if (this.current == 1 && this.direction == 0) {
			this.direction = 1;
		}
		
		if (this.direction == 1) this.next();
		else this.prev();
	},
	
	next: function() {
		if (this.current <= (this.elements - this.offset) && this.current > this.offset) {		
			var left_indent = parseInt($('#selems ul').css('left')) - this.item_width;
			$('#selems ul').animate({'left' : left_indent}, 200, function () {
				$('#selems ul').css({'left' : left_indent});		
			});
		}
		if (this.current < this.elements) {
			this.current++;
			this.switchElement(this.current);
		}	
	},
	
	prev: function() {
		if (this.current >= (this.offset+1) &&
			this.current <= (this.elements - this.offset)) {		 		
			var left_indent = parseInt($('#selems ul').css('left')) + this.item_width;
			$('#selems ul').animate({'left' : left_indent}, 200, function () {
				$('#selems ul').css({'left' : left_indent});			
			});
		}
		
		if (this.current > 1) {
			this.current--;
			this.switchElement(this.current);
		}	
	},
	
	fadeAllOut: function(cid) {
		for (var i = 1; i <= this.elements; i++)
			if (i != cid) {
				$('#s-details_'+i).fadeOut(this.fade_speed);
				if ($.browser.msie) {
					$('#s-details_'+i+' *').fadeOut(this.fade_speed);
				}	
			}	
	},
		
	switchElement: function(element) {
		$('#selems ul li *').removeClass();
		$('#s-element_'+element).addClass('active');	
		this.fadeAllOut(element);
		$('#s-details_'+element).fadeIn(this.fade_speed);
		if ($.browser.msie) {
			$('#s-details_'+element+' *').fadeIn(this.fade_speed);
		}
	},
	
	goTo: function(element) {
		if (element > this.current) { // w prawo
			var left_indent = parseInt($('#selems ul').css('left'));
			while (element > this.current) 
			{	
				if (this.current < (this.elements - this.offset) && this.current > this.offset) {		
					left_indent -= this.item_width;
				}
				if (this.current < this.elements) {
					this.current++;
				}
			}	
		} else { // w lewo
			var left_indent = parseInt($('#selems ul').css('left'));
			while (element < this.current) 
			{	
				if (this.current > (this.offset+1) &&
					this.current <= (this.elements - this.offset)) {		 		
					left_indent += this.item_width;
				}
				
				if (this.current > 1) {
					this.current--;
				}
			}		
		}
		$('#selems ul').animate({'left' : left_indent}, 200, function () {
						$('#selems ul').css({'left' : left_indent});		
		});
		this.resetInterval();
		this.switchElement(this.current);
		return false;		
	}
}


//$ = jQuery.noConflict(); 
$(document).ready(function() {

	icube_carousel.init(); 

	// if portfolio
	var portfolio_elems = $('.j-portfolio').size();
	for (var i = 1; i <= portfolio_elems; i++)
	{
		if (i < 10) {
			photos_thumbs = $('#j-portfolio-thumbs-0'+i+' a');
		} else {
			photos_thumbs = $('#j-portfolio-thumbs-'+i+' a');
		}	
		photos_thumbs.each(function() { 
			$('#'+this.id).click(function() {
			 	var main_id = this.id.slice(-4,-2);
			 	var current_id = this.id.slice(-1);
			 	showPortfolioElement(main_id, current_id);
			 	return false;
			 });	
		});
	}	
	
});

function showPortfolioElement(main_id, current_id)
{
	$('#j-portfolio-thumbs-'+main_id+' a').removeClass('active');
	$('#j-portfolio-'+main_id+'-'+current_id).addClass('active');


	$('#j-portfolio-big-'+main_id+' a').fadeOut(500);
	$('#j-portfolio-big-'+main_id+' a').removeClass('active');
	
	$('#j-portfolio-'+main_id+'-'+current_id+'big').fadeIn(500);
	$('#j-portfolio-'+main_id+'-'+current_id+'big').addClass('active');
}

$(document).ready(function() {
	icube_logos.init();
});

var icube_logos = {

	right_elements_amount : 1, 
	right_current_element : 1,	
	right_run : null,
	right_speed_time : 4000,
	
	init: function() {	
		
		this.right_elements_amount = Math.ceil($('#right-moving-logos div').size() / 6 );
		if (this.right_elements_amount > 1) this.right_run = setInterval('icube_logos.runRightSlider()', this.right_speed_time); 
				
	}, 

	runRightSlider: function() {
		if (this.right_current_element == this.right_elements_amount) {
			this.showRight(1);
			this.right_current_element = 1; 
		} else {
			this.showRight(this.right_current_element + 1);
			this.right_current_element++; 
		}		
	},
	
	showRight: function(cid) {
		if (cid == 1) var tmp = 0;
		else var tmp = ((cid-1)*80)*-1;
		
		$("#right-moving-logos").animate({ 
		    top: tmp+"px"
		  }, 900);
	}		
}
