$(document).ready(function(){
	var z = 100;
	var tabs = $(".panel_inside").length;	
	var startpanel = Math.floor(Math.random()*tabs)
	/* var startpanel = Math.floor((2 - tabs + 1) * Math.random() + tabs); */
	$('.panel_mouse').fadeTo(0, 0);
	$('#panel').children().fadeTo(0, 0);
	$('#panel_'+startpanel).fadeTo("slow", 1);
	$('#panel_'+startpanel).css('z-index', z);	
	$('#userbar').fadeTo(0, 0);

	var index = startpanel;
	var nextindex = startpanel+1;
	$('#slidenumber').text(nextindex + '/' + tabs);
	if(nextindex == tabs){
		var nextindex = 0;
	}
	var slidetype = "slide";
	var pause= 0;
	
	if(tabs != 0) {
		next();
	}
	
	function next() {
		slidetype = "slide";
		if(pause == 0){
			timenext = setTimeout(function(){
				z++;
				index = nextindex;
				$('.panel_link img').fadeTo("fast", 1);
				$('#link_'+nextindex).fadeTo("fast", 0);
				$('#panel_'+nextindex).siblings().fadeTo("slow", 0);
				$('#panel_'+nextindex).fadeTo("slow", 1);
				$('#panel_'+nextindex).css('z-index', z);
				nextindex = nextindex+1;
				$('#slidenumber').text(nextindex + '/' + tabs);
				if(nextindex >= tabs){
					nextindex = 0;
				}
				next();
			}, 12500);
		}
	}
	$('#forward').click(function() {
		z++;
		if(index != tabs-1){
			index = index+1;
			clearTimeout(timenext);
			$('.panel_link img').fadeTo("fast", 1);
			$('#link_'+index).fadeTo("fast", 0);
			$('#panel_'+index).siblings().fadeTo("slow", 0);
			$('#panel_'+index).fadeTo("slow", 1);
			$('#panel_'+index).css('z-index', z);
			nextindex = index+1;
			$('#slidenumber').text(index+1 + '/' + tabs);
			if(nextindex >= tabs){
				nextindex = 0;
			}
			next();
		}
	});	
	$('#back').click(function() {
		z++;
		if(index != 0){
			index = index-1;
			clearTimeout(timenext);
			$('.panel_link img').fadeTo("fast", 1);
			$('#link_'+index).fadeTo("fast", 0);
			$('#panel_'+index).siblings().fadeTo("slow", 0);
			$('#panel_'+index).fadeTo("slow", 1);
			$('#panel_'+index).css('z-index', z);
			nextindex = index+1;
			$('#slidenumber').text(index+1 + '/' + tabs);
			if(nextindex >= tabs){
				nextindex = 0;
			}
			next();
		}
	});
	$('.pause').click(function() {
		if(pause == 0){
			clearTimeout(timenext);
			pause = 1;
			$(".pause").toggle();
		} else {
			pause = 0;
			$(".pause").toggle();
			next();
		}
	});
	
/*---------- Hover panels ----------------------------------------------------------------------------------- */
	
	var backto;
	var laptopin;
	var mobilein;
	var tagin;
	var laptopout;
	var mobileout;
	var tagout;

	function backToPanels(){
		if(slidetype == "product"){
			z++;
			slidetype = "slide";
			startpanel = Math.floor(Math.random()*tabs)
			$('.panel_mouse').fadeTo(0, 0);
			$('#panel').children().fadeTo(0, 0);
			$('#panel_'+startpanel).fadeTo("fast", 1);
			$('#panel_'+startpanel).css('z-index', z);
			index = startpanel;
			if(tabs == startpanel){
				nextindex = 0;
			} else {
				nextindex = startpanel+1;
			}
			$('#slidenumber').text(nextindex + '/' + tabs);
			if (tabs != 0) {
				next();
			}
		}
	}
		
	$('#home_laptop').mouseover(function() {
		$('#home_laptop_img').attr('src', '/brandings/images/homepage/laptop_over.png');
		laptopin = setTimeout(function(){
			z++;
			slidetype = "product";
			clearTimeout(timenext);
			clearTimeout(laptopout);
			clearTimeout(mobileout);
			clearTimeout(tagout);
			$('#panel_laptop').siblings().fadeTo("fast", 0);
			$('#panel_laptop').fadeTo("fast", 1);
			$('#panel_laptop').css('z-index', z);
			$('#slidenumber').text('0/' + tabs);
		}, 500);
	});
	$('#home_laptop').mouseout(function() {
		$('#home_laptop_img').attr('src', '/brandings/images/homepage/laptop.png');
		clearTimeout(laptopout);
		clearTimeout(mobileout);
		clearTimeout(tagout);
		clearTimeout(laptopin);
		laptopout = setTimeout(function(){
			backToPanels();
		}, 5000);
	});
	
	$('#home_mobile').mouseover(function() {
		$('#home_mobile_img').attr('src', '/brandings/images/homepage/smartphone_over.png');
		mobilein = setTimeout(function(){
			z++;
			slidetype = "product";
			clearTimeout(timenext);
			clearTimeout(mobileout);
			clearTimeout(laptopout);
			clearTimeout(tagout);
			$('#panel_mobile').siblings().fadeTo("fast", 0);
			$('#panel_mobile').fadeTo("fast", 1);
			$('#panel_mobile').css('z-index', z);
			$('#slidenumber').text('0/' + tabs);
		}, 500);
	});
	$('#home_mobile').mouseout(function() {
		$('#home_mobile_img').attr('src', '/brandings/images/homepage/smartphone.png');
		clearTimeout(laptopout);
		clearTimeout(mobileout);
		clearTimeout(tagout);
		clearTimeout(mobilein);
		mobileout = setTimeout(function(){
			backToPanels();
		}, 5000);
	});
	
	$('#home_tablet').mouseover(function() {
		$('#home_tablet_img').attr('src', '/brandings/images/homepage/tablet_over.png');
		tagsin = setTimeout(function(){
			z++;
			slidetype = "product";
			clearTimeout(timenext);
			clearTimeout(tagout);
			clearTimeout(mobileout);
			clearTimeout(laptopout);
			$('#panel_tablet').siblings().fadeTo("fast", 0);
			$('#panel_tablet').fadeTo("fast", 1);
			$('#panel_tablet').css('z-index', z);
			$('#slidenumber').text('0/' + tabs);
		}, 500);
	});
	$('#home_tablet').mouseout(function() {
		$('#home_tablet_img').attr('src', '/brandings/images/homepage/tablet.png');
		clearTimeout(laptopout);
		clearTimeout(mobileout);
		clearTimeout(tagout);
		clearTimeout(tagsin);
		tagout = setTimeout(function(){
			backToPanels();
		}, 5000);
	});
	
});
