var imageCount = 0;
var zIndexBase = 100;
var currentIndex = 1;
var galleryElementWidth = 0;
var waitTime = 3000;

$(document).ready(function() {
	
	ieVersion = getInternetExplorerVersion();
	
	imageCount = $('#frontPageImage img').length; 
    $('#frontPageImage img').each(function(i) {
        if(i == 0) {
            $(this).css({'z-index' : i+2});
        } else {
            $(this).css({'z-index' : i+2, 'opacity' : 0});
        }
        if(i == imageCount-1) {
        	firstSrc = $('#frontPageImage img:eq(0)').attr('src');
        	lastSrc = $('#frontPageImage img:eq('+(imageCount-1)+')').attr('src');
        }
    });
    $('#frontPageImage').prepend("<img alt='' src='"+lastSrc+"'/>");
    $('#frontPageImage').append("<img alt='' src='"+firstSrc+"'/>");
    $('#frontPageImage img:eq('+(imageCount+1)+')').css({'z-index' : (imageCount + 2), opacity: 0});
    $('#frontPageImage img:eq(0)').css({'z-index' : 1, opacity: 0});

    $('.bg-swicher .prev').click(function(e) {
        e.preventDefault();
        selectPrevImage(currentIndex);
    });
    
    $('.bg-swicher .next').click(function(e) {
        e.preventDefault();
        selectNextImage(currentIndex);
    });

    $(window).resize(frontpage_image_resize);
    displayFrontPageImage();
    /*displayReadyTimeout = setTimeout(function() {
        displayFrontPageImage();
    }, 1000);*/
    
});

function displayFrontPageImage() {
    if($('#frontPageImage img:eq(0)').height() < 100) {
        displayReadyTimeout = setTimeout(function() {
            displayFrontPageImage();
        }, 500);
    } else {
        frontpage_image_resize();
        if(ieVersion == 6 || ieVersion == 7) { $('body.home, html').css({'overflow': 'hidden'}); } else {
            $('#frontPageImage').css({opacity : 1});
        } 
    }
}

function frontpage_image_load() {
    $('#frontPageImage').animate({'opacity' : 1}, {
        duration:1000, 
        queue:false, 
        easing:"swing"
    });
}

function frontpage_image_resize() {
	var frontpage_image = $("#frontPageImage img");
	var w = frontpage_image.attr("width");
	var h = frontpage_image.attr("height");
	var r = w/h;
	var window_w = $(window).width();
	var window_h = $(window).height();
	var window_r = window_w/window_h;
	var new_w; var new_h;
	if (window_r >= r) {
		new_w = window_w;
		new_h = window_w/r;
	} else {
		new_w = window_h*r;
		new_h = window_h;
	}
	frontpage_image.attr("width", new_w);
	frontpage_image.attr("height", new_h);
	if(window_w < new_w) {
        offset = (new_w - window_w) / 2;
        $("#frontPageImage img").css({'margin-left': '-'+offset+'px'});
    } else {
        $("#frontPageImage img").css({'margin-left': '0px'});
    }
}

function selectPrevImage(index) {
    index = index-1;
    currentIndex = index; 
    $('#frontPageImage img').stop(true, true);
    $('#frontPageImage img:eq('+index+')').css({'opacity' : 1});
    $('#frontPageImage img:eq('+(index+1)+')').css({'opacity' : 0});
    if(index == 0) {
        $('#frontPageImage img:eq('+(imageCount+1)+')').css({opacity : 0});
        $('#frontPageImage img:eq('+(imageCount)+')').css({opacity : 1});
        $('#frontPageImage img:eq(0)').css({opacity : 0});
        currentIndex = imageCount;
    } else {
        $('#frontPageImage img:eq('+(index-1)+')').css({opacity : 0});
        $('#frontPageImage img:eq('+index+')').css({opacity : 1});
    }
}

function selectNextImage(index) {
    index++;
    currentIndex = index;
    $('#frontPageImage img').stop(true, true);
    $('#frontPageImage img:eq('+index+')').css({'opacity' : 1});
    if(index-1 == imageCount) {
        $('#frontPageImage img:eq('+(imageCount)+')').css({opacity : 0});
        $('#frontPageImage img:eq('+(imageCount+1)+')').css({opacity : 0});
        $('#frontPageImage img:eq(1)').css({opacity : 1});
        currentIndex = 1;
    } else {
        $('#frontPageImage img:eq('+(index-1)+')').css({opacity : 0});
        $('#frontPageImage img:eq('+(index)+')').css({opacity : 1});
    }
}

function animatePrevImage(index) {
    index = index-1;
    currentIndex = index; 
    $('#frontPageImage img').stop(true, true);
    $('#frontPageImage img:eq('+index+')').css({'opacity' : 1});
    $('#frontPageImage img:eq('+(index+1)+')').animate({'opacity' : 0}, {
        duration:1000, 
        queue:false, 
        easing:"swing",
        complete: function() {
            if(index == 0) {
                $('#frontPageImage img:eq('+(imageCount+1)+')').css({opacity : 0});
                $('#frontPageImage img:eq('+(imageCount)+')').css({opacity : 1});
                $('#frontPageImage img:eq(0)').css({opacity : 0});
                currentIndex = imageCount;
            } else {
                $('#frontPageImage img:eq('+(index-1)+')').css({opacity : 0});
                $('#frontPageImage img:eq('+index+')').css({opacity : 1});
            }
        }
    });
}

function animateNextImage(index) {
    index++;
    currentIndex = index;
    $('#frontPageImage img').stop(true, true);
    $('#frontPageImage img:eq('+index+')').animate({'opacity' : 1}, {
        duration:1000, 
        queue:false, 
        easing:"swing",
        complete: function() {
            if(index-1 == imageCount) {
                $('#frontPageImage img:eq('+(imageCount)+')').css({opacity : 0});
                $('#frontPageImage img:eq('+(imageCount+1)+')').css({opacity : 0});
                $('#frontPageImage img:eq(1)').css({opacity : 1});
                currentIndex = 1;
            } else {
                $('#frontPageImage img:eq('+(index-1)+')').css({opacity : 0});
                $('#frontPageImage img:eq('+(index)+')').css({opacity : 1});
            }
        }
    });
}

$(window).bind("load", function() {
	$("#pageLoader").hide();
});


