var run_show = true;
var images = new Array()


$(document).ready(function() {

	//alert(document.location.href);
	preload(
		"http://www.yomega.com/images/backgrounds/bg-pro.jpg",
		"http://www.yomega.com/images/backgrounds/bg-player.jpg",
		"http://www.yomega.com/images/backgrounds/bg-rookie.jpg",
		"http://www.yomega.com/images/backgrounds/bg-welcome.jpg"
	);
	
	$('.advert').delay(3000).slideToggle(250);
	
	if (document.location.href == 'http://www.yomega.com/index.html' || document.location.href == 'http://www.yomega.com/' || document.location.href == 'http://yomega.com/index.html' || document.location.href == 'http://yomega.com/'){

		get_tabs();

	}
	

});


function preload() {
	for (i = 0; i < preload.arguments.length; i++) {
		images[i] = new Image();
		images[i].src = preload.arguments[i];
	}
}

function get_tabs(){

	//$.get('http://yomegastore.com/home_tabs.php', function(data) {

  		//$('#tabs').html(data);

		start_rotation('start');

//	});

}



function rotate(tab){

	swapBackground(tab);

	swapBanner(tab);

	makeActive(tab);

	resetLink(tab);

	reset_tab_clicks();

}

function reset_tab_clicks(){

	$('.tab').each(function(){

			$(this).click(function(){

			run_show = false;

			rotate(this);

		});

	});  

}



function resetLink(tab){

	$('#rotating').click(function(){

		//alert('clicked');

		if($(tab).attr('link') != ''){

			document.location = $(tab).attr('link');

		}

	});

}



function swapBackground(tab){

	var type = $(tab).attr('type');

	$('#fade-bg').fadeTo(500,0, function() {

    	$('#fade-bg').css('background-image','url("images/backgrounds/bg-'+type+'.jpg")');

		$('#fade-bg').fadeTo(1000,1);

  	});

}



function swapBanner(tab){

	var image = $(tab).attr('imagesrc');

	$('#rotating').fadeOut('slow', function() {

		$('#rotating').css('cursor','pointer');

    	$('#rotating').css('background-image','url("http://yomegastore.com/img-u/'+image+'")');

		$('#rotating').fadeIn();

  });

}



function makeActive(tab){

	$('.tab').each(function(){

		$(this).removeClass('active')	

	});  

	$(tab).addClass('active');

}



function start_rotation(cur_tab){

	if(cur_tab == 'start' || $(cur_tab).next().attr('type') === undefined){

		cur_tab = $('.tab').first();

	}

	else{

		cur_tab = $(cur_tab).next();

	}

	

	if(run_show == true){

		rotate(cur_tab);

		setTimeout(function() {start_rotation(cur_tab);} , 8000); 

	}

}

