$(document).ready(function(){
	var h = $(document).height();
	var w = $(document).width();
	
	var sw = screen.width;
	var sh = screen.height;
	var ratio = sw/sh;

	$('#holder').css({'width' : w + 'px', 'height' : h + 'px'})
	
	$(window).resize(function() {
		var rw = $(window).width();
		var rh = $(window).height();
		
		$('#holder').css({'width' : rw + 'px', 'height' : rh + 'px'});
	});

	var folder = '';
	if(ratio < 1.6) {
		folder = '1440x880/';
	}
	else {
		folder = '1440x735/';
	}
	
	var rand = Math.floor(Math.random()*img.length);
	
	$('#wrapper').html('');
	for(var i=0; i<img.length; i++){
		var html = '';
		if(links[i]){
			html +='<li class="panel im_' + i + '"><a href="'+links[i]+'"><img src="'+siteURL+'uploads/pages/' + page_id + '/' + folder + img[i] + '" alt="" title="" /></a></li>';
		}
		else{
		html +='<li class="panel im_' + i + '"><img src="'+siteURL+'uploads/pages/' + page_id + '/' + folder + img[i] + '" alt="" title="" /></li>';
		}
		$('#wrapper').append(html);
		if(i == rand){
			showFirstImage(i);
		}
	}
	
	var all = img.length-1;
	showimages();
	if(all){
		startshow(all, rand);
	}
	
	/* for iPad */
	if (!isNaN(window.orientation)) {
		var orientation = (window.orientation == 0 || window.orientation == 180) ? 'portrait' : 'landscape';
		
		if(orientation == 'landscape') {
			$('#wrapper img').css({'width' : 'auto', 'height' : '100%'});
		}
	}
	
	window.onorientationchange = function() {
		var orientation = (window.orientation == 0 || window.orientation == 180) ? 'portrait' : 'landscape';
		
		if(orientation == 'landscape') {
			$('#wrapper img').css({'width' : 'auto', 'height' : '100%'});
		}
		else {
			$('#wrapper img').attr('style', '');
		}
	}
});

function showFirstImage(i) {
	setTimeout(function(){
		$('.im_' + i).css('visibility','visible').hide().fadeIn(2000);
	}, 2000);
}

function startshow(all, rand) {
	var d = new Date();
	
	setInterval(function(){
		var d = new Date();
		
		if($('.panel').length > 0){
			if(rand == all){
				rand = 0;
			}
			else {
				rand++;
			}
		}
		
		$('.im_' + rand).css('visibility','visible').hide().fadeIn(1000);
		
		if(rand == 0) {
			$('.im_' + (all)).fadeOut(1000);
		}
		else {
			$('.im_' + (rand-1)).fadeOut(1000);
		}
	}, 5000);
}

function showimages() {
	$('#holder').css({'visibility' : 'visible'});
	
	setTimeout(function(){
		$('body').removeClass('loader');
	}, 5000);
}

