var imgWidth = 1024;
var imgHeight = 577;
var displayWidth;
var displayHeight;
var wTheHeight;
var hTheWidth;
//----------------------
var imgs_array = []; //stores all src of images for preloading
var img_count = 0; //no. of images
var imgs_loaded = false; //whether all images loaded
//----------------------
var slides_int; //interval that controls the slideshow
var slide_dur = 6000; //duration of each slide
var slide_active = -1; //current active slide
//==================================================================================================================
$(document).ready(function(){
	$('.homePreloader').show();
	pg_resize();
	$(window).resize(function(){
		 pg_resize();
	});
	//----------------------
	slideshow();
});
//==================================================================================================================
//when browser resizes
function pg_resize(){
	displayWidth = $("#holder").width();
	wTheHeight = imgHeight / imgWidth * displayWidth;
	//----------------------
	displayHeight = $("#holder").height();
	hTheWidth = imgWidth / imgHeight * displayHeight;
	//----------------------
	if(Number(wTheHeight)>=Number(displayHeight)){//if height by width bigger/equal to browser height
		$("#home img").css({"width":Number(displayWidth), "height":Number(wTheHeight)});
	}else{//if height by width less than browser height
		$("#home img").css({"width":Number(hTheWidth), "height":Number(displayHeight)});
	}
	//----------------------
	$('.homePreloader').css({top:($('#holder').height()-110)*.5, left:($('#holder').width()-80)*.5});
}
//==================================================================================================================
function slideshow(){
	//----------------------
	$.each($('#home img'), function(){
		$(this).attr('class', 'slide'+img_count+' '+$(this).attr('class'));
		$(this).hide();
		imgs_array.push($(this).attr('src'));
		img_count++;
	});
	//----------------------
	$.preload(imgs_array, {
		onFinish :function(){
			if(img_count>1){
				slides_int = setInterval(slideshow_cycle, slide_dur);
				imgs_loaded = true;
			}
			$('.homePreloader').fadeOut(1000, function(){$(this).html('')});
			slideshow_cycle();
		}
	});
}

//==================================================================================================================
function slideshow_cycle(){
	//----------------------
	slide_activeTag = $('#home img.slide'+slide_active);
	slide_activeTag.css('z-index', 0);
	slide_activeTag.delay(1200).fadeOut(1, function(){$(this).css('z-index', -1).hide()})
	//----------------------
	slide_active++;
	if(slide_active>=img_count)slide_active=0;
	slide_activeTag = $('#home img.slide'+slide_active);
	slide_activeTag.css({'z-index': 1});
	slide_activeTag.fadeIn(1200, 'easeOutExpo');
}
//==================================================================================================================
