﻿$(function () {
	if ($(document.body).hasClass('ie6')) {
		try {
			// Fix background flickering
			document.execCommand('BackgroundImageCache', false, true);
		} catch(e) {}
	}

	var header = $('#header');
	var content = $('#content');
	var leftBlock = $('#left');
	var secNavigation = $('#sec-navigation');
	var miscNavigation = $('#misc-navigation');
	var productNavigation = $('#product-navigation');
	var shadowLeft = $('#shadow-left');
	var shadowTop = $('#shadow-top');

	var headerHeight = header.height();
	var headerWidth = header.width();
	var miscMargin = parseInt(miscNavigation.css('margin-top'));
	var miscHeight = miscNavigation.outerHeight(1) - miscMargin;

	var win = $(window);

	var div = $('<div>', {css: {position: 'fixed', top: '10px', left: 0}}).appendTo(document.body);
	var supportFixed = div[0].offsetTop === 10;
	div.remove();

	// is this layout currently using fixed positioning (for head and left)
	var usingFixed = false;

	win.resize(function() {
		var winHeight = win.height();
		var winWidth  = win.width();

		miscNavigation.css('margin-top',20);
		var miscPos = miscNavigation.offset().top;
		if (usingFixed) {
			// fix jquery bug with fixed positioned elements
			miscPos -= win.scrollTop();
		}
		// check if it is possible to position the miscNavigation at the bottom of the initial screen (the fold)
		miscNavigation.css('margin-top', Math.max(miscMargin, winHeight - miscPos - miscHeight + miscMargin));

		// check if the complete layout can be fixed positioned
		if (!supportFixed) {
			return;
		}

		var canUseFixed = true;

		if (headerWidth > winWidth) {
			canUseFixed = false;
		}

		if (leftBlock.height() + leftBlock[0].offsetTop > winHeight) {
			canUseFixed = false;
		}

		if (canUseFixed && !usingFixed) {
			// apply fixed positioning
			$(document.body).addClass('fixed');
			shadowLeft.appendTo(header);
			shadowTop.appendTo(header);
			productNavigation.appendTo(header);
			usingFixed = true;
		} else if (!canUseFixed && usingFixed) {
			// position absolute
			$(document.body).removeClass('fixed');
			shadowLeft.appendTo(content);
			shadowTop.appendTo(content);
			productNavigation.appendTo(content);
			usingFixed = false;
		}
	}).resize();

	$('label.placeholder').each(function() {
		var label = $(this);
		var input = $('#' + label.attr('for'));
		if (input.val()) {
			// currently text within the input, hide the label
			label.addClass('hidden');
		}

		input.focus(function() {
			label.addClass('partial');
		}).blur(function() {
			if (!input.val()) {
				// it's empty, show the label
				label.removeClass('partial hidden');
			}
		}).keypress(function() {
			label.addClass('hidden');
		});

		if (input.closest('form').attr('id') != 'search' && input.parent()[0] === label.parent()[0]) {
			// position the label at the same position where the text would start within the input
			var parent = input.parent();
			if (parent.css('position') == 'static') {
				parent.css('position', 'relative');
			}
			label.css('position', 'absolute');
			var offset = input.position();
			label.css({
				top:   offset.top  + parseInt(input.css('padding-top'),10)  + parseInt(input.css('border-top-width'),10),
				left:  offset.left + parseInt(input.css('padding-left'),10) + parseInt(input.css('border-left-width'),10),
				width: input.width()
			});
			input.css('display', 'block');
		}
	});

	$(document).delegate('a[href*=#]', 'click', function(event) {
		if (!$(document.body).hasClass('fixed')) {
			// only fixing scrolling, when this is the fixed layout
			return;
		}
		// check if this link points to an anchor within this document
		if ((this.pathname == location.pathname || '/' + this.pathname == location.pathname) &&
			this.search == location.search) {
			// internal link, check if link can be found
			var fragment = this.hash.substr(1);
			if (!fragment) {
				// ignore empty fragments
				return;
			}
			var element = $('#' + fragment);
			if (!element.length) {
				element = $('*[name=' + fragment + ']');
			}

			if (!element.length) {
				// didn't find the destination of the anchor
				return;
			}

			// ok, got the element. Scroll to it (minus the fixed header)
			location.hash = fragment;
			window.scrollTo(win.scrollLeft(), element.offset().top - content.position().top);

			event.preventDefault();
			event.stopPropagation();
		}
	});
});

