
/* The slide images are contained in the slides array. */
var slides = new Array('http://www.haasgroupintl.com/images/tile_1.jpg',
					   'http://www.haasgroupintl.com/images/tile_2.jpg',
					   'http://www.haasgroupintl.com/images/tile_3.jpg',
					   'http://www.haasgroupintl.com/images/tile_4.jpg',
					   'http://www.haasgroupintl.com/images/tile_5.jpg',
					   'http://www.haasgroupintl.com/images/tile_6.jpg',
					   'http://www.haasgroupintl.com/images/tile_7.jpg',
					   'http://www.haasgroupintl.com/images/tile_8.jpg');


$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */
	
	/*jQuery.fn.clearSlideInt = function() {
    	var o = $(this[0]) // It's your element
		//alert(auto);
		clearInterval(auto);
	};*/
	$('.arrow.left').click(function(){
		prev();
		
		/* Clearing the autoadvance if we click one of the arrows */
		clearInterval(auto);
	});
	
	$('.arrow.right').click(function(){
		next();
		clearInterval(auto);
	});
	
	$('.AD').click(function(){
		nextV(0);
		clearInterval(auto);
	});
	$('.AM').click(function(){
		nextV(1);
		clearInterval(auto);
	});
	$('.AT').click(function(){
		nextV(2);
		clearInterval(auto);
	});
	$('.EL').click(function(){
		nextV(3);
		clearInterval(auto);
	});
	$('.EN').click(function(){
		nextV(4);
		clearInterval(auto);
	});
	$('.FB').click(function(){
		nextV(5);
		clearInterval(auto);
	});
	$('.GO').click(function(){
		nextV(6);
		clearInterval(auto);
	});
	$('.MA').click(function(){
		nextV(7);
		clearInterval(auto);
	});

	/* Preloading all the slide images: */

	for(var i=0;i<slides.length;i++)
	{
		(new Image()).src=slides[i];
	}
	
	/* Shoing the first one on page load: */
	transition(1);
	
	
	/* Setting auto-advance every 10 seconds */
	 startSlideInt();
	
	/*var auto;
	
	auto=setInterval(function(){
		next();
	},11*1000);
	
	jQuery.fn.startSlideInt = function() {
		var auto;
    	auto=setInterval(function(){
			next();
		},11*1000);
		alert("start slides!");
	};*/
	
	
	//alert(document.getElementById("mosaic-slideshow").style.zIndex);
});
var auto;
function startSlideInt(){
	auto;
	
	auto=setInterval(function(){
		next();
	},11*1000);
}
function clearSlideInt() {
    	clearInterval(auto);
}
var current = {};
function transition(id)
{
	/* This function shows the individual slide. */
	
	if(!slides[id-1]) return false;
	
	if(current.id)
	{
		/* If the slide we want to show is currently shown: */
		if(current.id == id) return false;
		
		/* Moving the current slide layer to the top: */
		current.layer.css('z-index',1);
		//alert(document.getElementById("mosaic-slideshow").style.zIndex);
		//document.getElementById("mosaic-slideshow").style.zIndex = 100;
		/* Removing all other slide layers that are positioned below */
		$('.mosaic-slide').not(current.layer).remove();
	}
	
	/* Creating a new slide and filling it with generateGrid: */
	var newLayer = $('<div class="mosaic-slide">').html(generateGrid({rows:2,cols:4,image:slides[id-1]}));

	/* Moving it behind the current slide: */
	newLayer.css('z-index',0);

	$('#mosaic-slideshow').append(newLayer);
	
	if(current.layer)
	{
		/* Hiding each tile of the current slide, exposing the new slide: 
		alert($('.tile',current.layer).length);
		alert($('.tile',current.layer)[9]);*/
		//myarray.sort(function() {return 0.5 - Math.random()}) //Array elements now scrambled
		/*var rndm = new Array;
		for(i=0;i<$('.tile',current.layer).length;i++){
			rndm.push($('.tile',current.layer)[i]);
		}
		alert(rndm.length);
		for(i=0;i<rndm.length;i++){
			function(i){
				var tile = rndm[i];
				setTimeout(function(){
					tile.css('visibility','hidden');
				},i*200);
			};	
		}*/
		var rndm = new Array;
		rndm = $('.tile',current.layer);
		rndm.sort(function() {return 0.5 - Math.random()}) //Array elements now scrambled
		rndm.each(function(i){
			var tile = $(this);
			setTimeout(function(){
				tile.css('visibility','hidden');
			},i*200);
		})
	}
	
	/* Adding the current id and newLayer element to the current object: */
	current.id = id;
	current.layer = newLayer;
	
}

function next()
{
	if(current.id)
	{
		transition(current.id%slides.length+1);
	}
}

function prev()
{
	if(current.id)
	{
		transition((current.id+(slides.length-2))%slides.length+1);
	}
	
}

function nextV(cur)
{
	if(cur == 0)
	{
		transition(0%slides.length+1);
	}
	else if(cur == 1)
	{
		transition(1%slides.length+1);
	}
	else if(cur == 2)
	{
		transition(2%slides.length+1);
	}
	else if(cur == 3)
	{
		transition(3%slides.length+1);
	}
	else if(cur == 4)
	{
		transition(4%slides.length+1);
	}
	else if(cur == 5)
	{
		transition(5%slides.length+1);
	}
	else if(cur == 6)
	{
		transition(6%slides.length+1);
	}
	else if(cur == 7)
	{
		transition(7%slides.length+1);
	}
}

function prevV(cur)
{
	if(cur == 0)
	{
		transition((0+(slides.length-2))%slides.length+1);
	}
	else if(cur == 1)
	{
		transition((1+(slides.length-2))%slides.length+1);
	}
	else if(cur == 2)
	{
		transition((2+(slides.length-2))%slides.length+1);
	}
	else if(cur == 3)
	{
		transition((3+(slides.length-2))%slides.length+1);
	}
	else if(cur == 4)
	{
		transition((4+(slides.length-2))%slides.length+1);
	}
	else if(cur == 5)
	{
		transition((5+(slides.length-2))%slides.length+1);
	}
	else if(cur == 6)
	{
		transition((6+(slides.length-2))%slides.length+1);
	}
	else if(cur == 7)
	{
		transition((7+(slides.length-2))%slides.length+1);
	}
	
}
/* Width and height of the tiles in pixels: */
var tabwidth=176, tabheight=190;/*186*/

function generateGrid(param)
{
	/* This function generates the tile grid, with each tile containing a part of the slide image */
	
	/* Creating an empty jQuery object: */
	var elem = $([]),tmp;
	 
	 


	for(var i=0;i<param.rows;i++)
	{
		for(var j=0;j<param.cols;j++)
		{
			tmp = $('<div>', {
					"class":"tile",
					"css":{
						"background":' url('+param.image+') no-repeat '+(-j*tabwidth)+'px '+(-i*tabheight)+'px',
						"z-index": '10' //LP
					}
					
						
					
			});
			
			/* Adding the tile to the jQuery object: */
			elem = elem.add(tmp);
		}
		
		/* Adding a clearing element at the end of each line. This will clearly divide the divs into rows: */
		elem = elem.add('<div class="clear"></div>');
	}
	
	return elem;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
