﻿$(function () {
	//Get our elements for faster access and set overlay width
	var div = $('div.sc_menu'),
                 ul = $('div.sc_menu ul'),
	// unordered list's left margin
                 ulPadding = 15;

	//Get menu width
	var divWidth = div.width();

	//Remove scrollbars
	div.css({ overflow: 'hidden' });

	//Find last image container
	var lastLi = ul.find('li:last-child');

	//When user move mouse over menu
	div.mousemove(function (e) {

		//As images are loaded ul width increases,
		//so we recalculate it each time
		var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

		var left = (e.pageX - div.offset().left) * (ulWidth - divWidth) / divWidth;
		div.scrollLeft(left);
	});
});

/*
* Finds all elements that have a class of dynamic-height or dynamic-width and sets their
* heights and widths appropriately offset by the heights or widths of all elements that
* have a class of height-offset or width-offset.
*/
function setDynamicDimensions() {
	var heightOffset = 0;
	var widthOffset = 0;

	// Calculate the height offset
	$(".height-offset").each(function () {
		heightOffset += $(this).outerHeight();
	});

	// Calculate the width offset
	$(".width-offset").each(function () {
		widthOffset += $(this).outerWidth();
	});

	$(".dynamic-height").each(function () {
		// Take into account this objects margin, border and padding
		var myOffset = 0;
		myOffset += parseInt($(this).css('padding-top'));
		myOffset += parseInt($(this).css('padding-bottom'));
		myOffset += parseInt($(this).css('margin-top'));
		myOffset += parseInt($(this).css('margin-bottom'));
		myOffset += parseInt($(this).css('border-top-width'));
		myOffset += parseInt($(this).css('border-bottom-width'));

		$(this).css("height", $(window).height() - heightOffset - myOffset + "px");
	});

	$(".dynamic-width").each(function () {
		// Take into account this objects margin, border and padding
		var myOffset = 0;
		myOffset += parseInt($(this).css('padding-left'));
		myOffset += parseInt($(this).css('padding-right'));
		myOffset += parseInt($(this).css('margin-left'));
		myOffset += parseInt($(this).css('margin-right'));
		myOffset += parseInt($(this).css('border-left-width'));
		myOffset += parseInt($(this).css('border-right-width'));

		$(this).css("width", $(window).width() - widthOffset - myOffset + "px");
	});
}

$(function () {
	setDynamicDimensions();
});

window.onresize = function () {
	setDynamicDimensions();
}
