$(function(){
	
	$('#galleryIndex .carCont').each(function(i){
		$(this).find('> ul li > a').each(function(i2){
			$(this).attr('href','#gal_tab_'+ i + '_' + i2);
		})
		$(this).find('> div').each(function(i2){
			$(this).attr('id','gal_tab_'+ i + '_' + i2);
		})
	}).tabs({
		collapsible: true,
		selected: -1,
		select: function(event, ui) {selectTab()},
		show: function(event, ui) { showTab(event, ui)}
	});
	
	$('.carousel ul').each(
		function(){
			var ulCar = $(this);
			ulCar.jcarousel({
		        scroll : 1,
		        wrap: 'circular',
		        auto: 2,
		        initCallback: function(c){
		        	c.stopAuto();
		        	c.allowStart = true;
		        	if (c.size() <=4){
		        		c.scroll = function(){return false;};
		        		ulCar.closest('.carousel').addClass('noCarArrows')
		        		c.allowStart = false;
		        	}
		        	carList[carList.length] = c; 
		        }
		    });
    })
    
    function selectTab(e,ui){
    	for (var i = 0;i<carList.length;i++) {
    		carList[i].stopAuto();
    	}
    }
    
    function showTab(e,ui){
    	if(carList[ui.index].allowStart){
			carList[ui.index].startAuto();
		}
    }
    
    var tile = new tileImages();
    var space = new spaceGallery();
    
	$('#galleryIndex').tabs({
		selected: 1,
		show: function(event, ui) {
			var id = $(ui.panel).attr('id');
			if (id == 'v_tab_0_1'){
				space.stop();
				tile.start();
			} else {
				tile.stop();
				space.start();
			}
		}
	});
})

var carList = [];

function tileImages(){
	var self = this;
	var tileInterval;
	this.start = function(){
		clearInterval(tileInterval)
		tileInterval = setInterval(function(){
			tileIntervalListener(list);
		},5000);
	}
	this.stop = function(){
		clearInterval(tileInterval);
	}
		
	var list = $('.imageGalleryTile li').click(function(e){
		if(e.target.tagName.toLowerCase() != 'li'){
			e.stopPropagation()
			return;
		}
		var li = $(this);
		var newImg = $('<img src="'+imageArray.shift()+'" style="display: none;" />');
		imageArray.push(li.find('img').attr('src'))
		li.append(newImg);
		li.find('img:eq(0)').remove();
		newImg.fadeIn(1500,'linear').css('z-index', 1);
	});
	function tileIntervalListener(){
		var iList =  list.eq(Math.floor(Math.random()*list.length))
		for (i=0;i<=Math.floor(Math.random()*3)+1;i++){
			iList = iList.add(list.eq(Math.floor(Math.random()*list.length)))
		}
		iList.click();
	}
	
	return this;
}

function spaceGallery() {
	var self = this;
	var inter;
	var iG = $('.imageGalleryOne');
    
     
	
    iG.find('a:last-child').css('opacity','1');
	this.start = function(){
        if ($('.imageGalleryOne img').length < 2) {
            return false; 
        }
		clearInterval(inter)
		inter = setInterval(function(){
		  var link = iG.find('a:last-child');
		  link.prev().css('opacity','1');
			link.find('img').animate({
				top: '-486px',
				left: '-905px',
				height: '1500px',
				width: '2795px',
				opacity: 0
			},1000,'linear',function(){
				$(this).closest('div').prepend($(this).parent()).end().css({
				top: '0px',
				left: '0px',
				height: '528px',
				width: '984px',
				opacity: 1
				}).parent().css('opacity','0');
			})
		},5000)
	}
	this.stop = function(){
		clearInterval(inter);
	}
	return this;
}

