// JavaScript Document

$(document).ready( function(){
														
	//showcase pagination
	var baseUrl = 'http://www.sembox.it/';
	$(function(){
	
		totalDisp = $('.showcase');
		totalDispLength = totalDisp.length;
		
		if(totalDispLength > 1){
		
			function appendPagination(totalDisp){
				totalDisp.each(function(i,e){
					if($(e).hasClass('actual')){
						actualIndexDisp = i;
					}
				});
				
				//reset pagination
				$('a#showcase-right').empty();
				$('a#showcase-left').empty();
				
				//pagination append 
				if(actualIndexDisp === 0)
					$('a#showcase-right').append('<img src="/img/frecciabianca.png" border=0 />');
				else if(actualIndexDisp+1 === totalDispLength)
					$('a#showcase-left').append('<img src="/img/frecciabiancas.png" border=0 />');
				else{
					$('a#showcase-right').append('<img src="/img/frecciabianca.png" border=0 />');
					$('a#showcase-left').append('<img src="/img/frecciabiancas.png" border=0 />');		
				}
			};
			//page loading
			appendPagination(totalDisp);	
		
			// click handler
			$('#showcaseclienti a').click(function(){
				
				//which arrow clicked
				arrowclicked = $(this).attr('id');  //showcase-left or showcase-right
				switch(arrowclicked){
						case 'showcase-left': behaviour = 'previous';break;
						case 'showcase-right': behaviour = 'next';break;
				}
				
				$(totalDisp[actualIndexDisp]).removeClass('actual').css('display', 'none');
				switch(behaviour){
					case 'next': $(totalDisp[actualIndexDisp+1]).addClass('actual').css('display', 'block');break;
					case 'previous': $(totalDisp[actualIndexDisp-1]).addClass('actual').css('display', 'block');break;
				}
				
				newTotalDisp = $('.showcase');
				newTotalDisp.click(function(){
					var url = baseUrl+"referenze-sem.asp";
					window.location = url;
				});
				appendPagination(newTotalDisp);		
						
				return false;
					
			});
		
		}
			
		//Homepage
		$('.actual').css('display','block').click(function(){
			var url = baseUrl+"referenze-sem.asp";
			window.location = url;
		});
			
		// Showcase 
		enterFunction = function(){
			$(this).addClass('active');
			$(this).removeClass ('inactive');
			$(this).find('p:first').show();
		}
		leaveFunction = function(){
			$(this).addClass('inactive');
			$(this).removeClass ('active');
			$(this).find('p:first').hide();
		}
				
		$('.showcase').hSlides({
			totalWidth: 550, 
			totalHeight: 160, 
			minPanelWidth: 125, 
			maxPanelWidth: 275,
			midPanelWidth: 183,
			activeClass: 'active',
			onEnter: enterFunction,
			onLeave: leaveFunction
		});
			
		$('.showcase li:last-child').addClass('last');
		
	});
	
	$.fn.sameBoxHeight = function(){
			

			
			heightBox = $(this);
			heightArray = new Array();
			
			heightBox.each(function(){
				heightArray.push($(this).innerHeight());
			});
			
			function max( array ){
				return Math.max.apply( Math, array );
			}; 
			
			maxHeight = max(heightArray);
			heightBox.height(maxHeight);

			
	};

	$(function() {
		//caching
		//next and prev buttons
		var $cn_next	= $('#cn_next');
		var $cn_prev	= $('#cn_prev');
		//wrapper of the left items
		var $cn_list 	= $('#cn_list');
		var $pages		= $cn_list.find('.cn_page');
		//how many pages
		var cnt_pages	= $pages.length;
		//the default page is the first one
		var page		= 1;
		//list of news (left items)
		var $items 		= $cn_list.find('.cn_item');
		//the current item being viewed (right side)
		var $cn_preview = $('#cn_preview');
		//index of the item being viewed. 
		//the default is the first one
		var current		= 1;
				
		/*
		for each item we store its index relative to all the document.
		we bind a click event that slides up or down the current item
		and slides up or down the clicked one. 
		Moving up or down will depend if the clicked item is after or
		before the current one
		*/
		$items.each(function(i){
			var $item = $(this);
			$item.data('idx',i+1);
			
			$item.bind('click',function(){
				var $this 		= $(this);
				$cn_list.find('.selected').removeClass('selected');
				$this.addClass('selected');
				var idx			= $(this).data('idx');
				var $current 	= $cn_preview.find('.cn_content:nth-child('+current+')');
				var $next		= $cn_preview.find('.cn_content:nth-child('+idx+')');
					
				$current.hide();
				$cn_preview.find('.cn_content').hide();
				$next.css({'top':'1px'}).show();
						
				/*if(idx > current){
					$current.stop().animate({'top':'-300px'},600,'easeOutBack',function(){
						$(this).css({'top':'310px'});
					});
					$next.css({'top':'310px'}).stop().animate({'top':'5px'},600,'easeOutBack');
				}
				else if(idx < current){
					$current.stop().animate({'top':'310px'},600,'easeOutBack',function(){
						$(this).css({'top':'310px'});
					});
					$next.css({'top':'-300px'}).stop().animate({'top':'5px'},600,'easeOutBack');
				}*/
				current = idx;
			});
		});
	});


	var theImages = new Array()

	//Random-loading images
	theImages[0] = '/img/visual-skater.jpg' // replace with names of images
	theImages[1] = '/img/visual-surfer.jpg' // replace with names of images
	theImages[2] = '/img/visual-scooter.jpg' // replace with names of images

	var j = 0
	var p = theImages.length;
				
	var preBuffer = new Array()

	for (i = 0; i < p; i++){
		preBuffer[i] = new Image()
		preBuffer[i].src = theImages[i]
	}
	var whichImage = Math.round(Math.random()*(p-1));
				
	$('#visual-big').empty().append('<img src="'+theImages[whichImage]+'" border=0 width="960" height="320" />');

});





