var side = 'right';
var defaultLeftMargin = -1800;
var panelMargin = 235;
var leftMargin;
var rightMargin;

$(document).ready(function() {
	
	resizeBackground();
	if ($("#panel-wrapper").parent().attr('id') == "inner-hover-wrapper") {
		repositionMiniSlider();
	} else {
		leftMargin = defaultLeftMargin + $(window).width() - panelMargin*2;
		rightMargin = defaultLeftMargin;
	}
	
	$(window).resize(function() {
		resizeBackground();
		if ($("#panel-wrapper").parent().attr('id') == "inner-hover-wrapper") {
			repositionMiniSlider();
		} else {
			if (side == 'left') slideToTheLeft(true);
			leftMargin = defaultLeftMargin + $(window).width() - panelMargin*2;
		}
	});
	
	$('#link-right').click(function() {
		if(side == 'right') {
			slideToTheLeft(false);
		} else {
			slideToTheRight(false);
		}
	});
	
	$('#link-left').click(function() {
		if(side == 'right') {
			slideToTheLeft(false);
		} else {
			slideToTheRight(false);
		}
	});
	
	$(window).scroll(function() {
		positionToolbar();
	});
});

function positionToolbar() {
    var windowTop = $(window).scrollTop();
    var windowHeight = $(window).height();
    var toolbarHeight = $("#inner-hover-wrapper").height();
    var top = windowHeight +  windowTop - toolbarHeight;
    var footerTop = $("#footer-wrapper").position().top;
    if ((top + toolbarHeight) > footerTop) {
        $("#inner-hover-wrapper").css('position','absolute').css('top',footerTop - toolbarHeight - 25).css('bottom','auto');
    } else {
        $("#inner-hover-wrapper").css('position','fixed').css('top','auto').css('bottom','20px');
    }
}

function resizeBackground() {
	$('#background-wrapper').css('width',$(window).width());
	$('#background-wrapper').css('height',$(window).height());
	resizeImages();
}

function repositionMiniSlider() {
	rightMargin = defaultLeftMargin + ($(window).width() - 800)/2 + 30 - 200;
	leftMargin = rightMargin + 800 - 130;
	if (side=='left') {
		$('#panel-wrapper').css('margin-left', leftMargin+'px');
	} else {
		$('#panel-wrapper').css('margin-left', rightMargin+'px');
	}
}

function slideToTheLeft(skipAnimate) {
	if (skipAnimate) {
		$('#panel-wrapper').css('margin-left', leftMargin+'px');
	} else {
		$('#panel-wrapper').animate({marginLeft: Number(leftMargin)}, 500, 'swing');
	}
	side = 'left';
}

function slideToTheRight(skipAnimate) {
	if (skipAnimate) {
		$('#panel-wrapper').css('margin-left', rightMargin+'px');
	} else {
		$('#panel-wrapper').animate({marginLeft: Number(rightMargin)}, 500, 'swing');	
	}
	side = 'right';
}

function resizeImages () {
	$('.resizeme img').each(function(){
		var parentHeight = $(this).parent().height();
		var parentWidth = $(this).parent().width();
		var parentID = $(this).parent().attr('id');
		
		//alert(parentID+': '+parentHeight+', '+parentWidth);
		var height = $(this).height();
		var width = $(this).width();		
		
		if(height/width <= parentHeight/parentWidth) {
			$(this).css('height',parentHeight);
			$(this).css('width','auto');
		} else {
			$(this).css('width',parentWidth);
			$(this).css('height','auto');
		}
	});
}
