var imageMargin = 0;
var slide = 1;
var bodyWidth;
var bodyHeight;
var imageWidth;

$(document).ready(function() {

	// INITIATE VARIABLES AND RUN INITIAL SCRIPTS
	imageWidth = $('#image-marquee-inner img').width();	
	startSlideshow();
	changeBackgroundImageSize(true);
	
	//fade out marquee buttons
	for(i=2;i<=3;i++) {
		$('#app'+i).fadeTo(1,0.5);
	}
	
	// RESIZE BACKGROUND IMAGE ON WINDOW RESIZE
	$(window).resize(function() {
		changeBackgroundImageSize(false);
	});
	
	// BACKGROUND SLIDESHOW
	$('.marquee-button').mouseenter(function(){
		$(this).fadeTo(1,1);
	}).mouseleave(function(){
		if ($(this).attr('id').charAt(3) != slide)
		$(this).fadeTo(1,0.5);
	});
	
	$('.marquee-button').click(function(){
		$(document).stopTime('slideshow');
		slide = $(this).attr('id').charAt(3);
		goHere();
		$(document).stopTime('restart');
		$(document).oneTime(20000, 'restart', function() {startSlideshow();});
	});
	
    $(window).bind('blur', function(){
        $(document).stopTime('slideshow');
		$(document).stopTime('restart');
    });
    $(window).bind('focus', function(){
		startSlideshow();
    });
    // IE EVENTS
    $(document).bind('focusout', function(){
        $(document).stopTime('slideshow');
		$(document).stopTime('restart');
    });
    $(document).bind('focusin', function(){
        startSlideshow();
    });
	
	// CONTACT MENU
	$('.contactform').click(slideMenu);
	$('#temp').click(hideMenu);
	

	if (getUrlVars()["req"] == 'contact') {
		slideMenu();
	}
});

function startSlideshow() {
	$(document).everyTime(7000, 'slideshow', function() {
		if (slide<3) {
			slide++;
		} else {
			slide=1;
		}
		goHere();
	}, 0);
}

function goHere(skipAnimate) {
	imageMargin = (slide-1)*imageWidth;
	
	//fade out marquee buttons
	for(i=1;i<=5;i++) {$('#app'+i).fadeTo(1,0.5);}
	$('#app'+slide).fadeTo(1,1.0);
	
	//skip animation if the window is just being resized
	if(skipAnimate) {
		$('#image-marquee-inner, #type-marquee-inner').css('margin-left', Number(-imageMargin));
	} else {
		$('#image-marquee-inner, #type-marquee-inner').animate({marginLeft: Number(-imageMargin)}, 500, 'swing');
	}
}

function changeBackgroundImageSize(init) {
	bodyWidth = $('body').width();
	bodyHeight = $('body').height();

	if($('body').height()/$('body').width() >= 768/1024) {
		$('.marquee-image').css('height',bodyHeight);
		$('.marquee-image').css('width','auto');
	} else {
		$('.marquee-image').css('width',bodyWidth);
		$('.marquee-image').css('height','auto');
	}

	imageWidth = $('#image-marquee-inner img').width();
	$('.marquee-copy-wrapper').css('width',imageWidth);

	if (!init) {
		goHere(true);
	}
}

function slideMenu() {
	$(document).stopTime('contact-cover');
	$('#left-pane').css('z-index','500');
	$('#left-pane').animate({marginLeft: '0'}, 500, 'swing');
	$('#temp').css('display','block');
}

function hideMenu() {
	$(document).oneTime(500, 'contact-cover', function() {
		$('#left-pane').css('z-index','30');	
	});
	$('#left-pane').animate({marginLeft: '-460px'}, 500, 'swing');
	$('#temp').css('display','none');
}

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}

