$(document).ready(function(){
	$('.handleInputDefaultValue').each(handleInputDefaultValue);
	$('.scrollToTop').click(scrollToTop);
	$("#sizeChartBottom").fancybox();
	initMainMenu();
	$('#basic-search-form').submit(basicSearchFormSubmit);
	$('#basic-search-keywords').keypress(alphaNumericOnly);
	$('#start-page-printing-dialog').click(function(){
		window.print();
	});
});

function handleInputDefaultValue(index, input) {
	input = $(input);
	if (!input.attr('default_value')) {
		input.attr('default_value', input.val());
	}

	if (input.val() == '') {
		input.val(input.attr('default_value'));
	}

	input.focus(function(){
		var input = $(this);
		if (input.val() == input.attr('default_value')) {
			input.val('');
		}
	});

	input.blur(function(){
		var input = $(this);
		if (input.val() == '') {
			input.val(input.attr('default_value'));
		}
	});
}

function smiDisplay(id){
	$("#smi"+id).show();
}
function smiHide(id){
	$("#smi"+id).hide();
}

function enlarge(id,caption){
	xOffset = 25;
	yOffset = 30;

	$("#"+id).hover(function(e){
		this.t = this.title;
		var c = (this.t != "") ? "" + this.t : "";
		$("body").append("<div id='screenshot'>"+caption+"<br /><img src='"+ this.rel +"' alt='url preview' /></div>");
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
    },
	function(){
		this.title = this.t;
		$("#screenshot").remove();
    });
	$("#"+id).mousemove(function(e){
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});

}


function enlarge(id,caption){
	xOffset = 25;
	yOffset = 30;

	$("#"+id).hover(function(e){
		this.t = this.title;
		var c = (this.t != "") ? "" + this.t : "";
		$("body").append("<div id='screenshot'>"+caption+"<br /><img src='"+ this.rel +"' alt='url preview' /></div>");
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
    },
	function(){
		this.title = this.t;
		$("#screenshot").remove();
    });
	$("#"+id).mousemove(function(e){
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});

}

$.fn.enlarge=function(){
	xOffset = 25;
	yOffset = 30;
	return this.each(function(){
		if (this.tagName!='A' || !this.rel) {
			return true;
		}
		var a = $(this);

		a.hover(
			function(e){
				this.t = this.title;
				var c = (this.t != '') ? '' + this.t : '';
				$('<div id="screenshot">'+c+'<br /><img src="' + this.rel + '" alt="" /></div>').appendTo(document.body);
				$("#screenshot")
					.css('top', (e.pageY - xOffset) + 'px')
					.css('left', (e.pageX + yOffset) + 'px')
					.fadeIn('fast');
		    },
			function(){
				this.title = this.t;
				$('#screenshot').remove();
		    }
	    );

		a.mousemove(function(e){
			$('#screenshot')
				.css('top',(e.pageY - xOffset) + 'px')
				.css('left',(e.pageX + yOffset) + 'px');
		});

	});
}

function scrollToTop() {
	$('html').animate({scrollTop:0}, 'slow');
}

function initMainMenu() {
	var main_links = $('#main-menu-container>ul>li>a');
	if (main_links.length == 0) {
		return;
	}
	var available_width = $('#main-menu-container .main-menu').width();
	for (var i=0; i<main_links.length; i++) {
		available_width -= $(main_links[i]).width();
	}
	var extra = Math.floor(available_width / main_links.length);
	if (extra > 0) {
		main_links.each(function(index, a){
			$(a).width($(a).width() + extra);
		});
	}

	$('#main-menu-container>ul>li').hover(
		function() {
			if ($(this).parent('.main-menu').length == 0) {
				return;
			}
			var submenu = $('ul', this);
			if (submenu.length == 0) {
				return;
			}
			if (!submenu.attr('_height')) {
				submenu.attr('_height', submenu.height() + 20);
			}
			$('#main-menu-container .shaddow')
				.stop()
				.css({height: 0, opacity: 0})
				.animate({height: submenu.attr('_height'), opacity: 0.95}, 'fast');
			submenu
				.stop()
				.css({height: 0, opacity: 0})
				.animate({height: submenu.attr('_height'), opacity: 1}, 'fast');
		},
		function() {
			if ($(this).parent('.main-menu').length == 0) {
				return;
			}
			var submenu = $('ul', this);
			if (submenu.length == 0) {
				return;
			}
			$('#main-menu-container .shaddow')
				.stop()
				.animate({height: 0, opacity: 1}, 'fast');
			submenu
				.stop()
				.animate({height: 0, opacity: 1}, 'fast');
		}
	);
}

function basicSearchFormSubmit() {
	var input = $('#basic-search-keywords');
	if (input.val() == input.attr('default_value')) {
		input.val('');
	}

	var keywords = encodeURIComponent(input.val().trim());
	window.location = site_url + 'search' + (keywords ? ('/keywords-' + keywords) : '');

	return false;
}

function alphaNumericOnly(e) {
	var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
	// allow enter/return key (only when in an input box)
	if(key == 13 && this.nodeName.toLowerCase() == "input")
	{
		return true;
	}
	else if(key == 13)
	{
		return false;
	}
	var allow = false;
	// allow Ctrl+A
	if((e.ctrlKey && key == 97 /* firefox */) || (e.ctrlKey && key == 65) /* opera */) return true;
	// allow Ctrl+X (cut)
	if((e.ctrlKey && key == 120 /* firefox */) || (e.ctrlKey && key == 88) /* opera */) return true;
	// allow Ctrl+C (copy)
	if((e.ctrlKey && key == 99 /* firefox */) || (e.ctrlKey && key == 67) /* opera */) return true;
	// allow Ctrl+Z (undo)
	if((e.ctrlKey && key == 122 /* firefox */) || (e.ctrlKey && key == 90) /* opera */) return true;
	// allow or deny Ctrl+V (paste), Shift+Ins
	if((e.ctrlKey && key == 118 /* firefox */) || (e.ctrlKey && key == 86) /* opera */
	|| (e.shiftKey && key == 45)) return true;

	if(key < 48)
	{
		/* '-' only allowed at start */
		if(key == 45 && this.value.length == 0) return true;
		/* only one decimal separator allowed */
		if(key == decimal.charCodeAt(0) && this.value.indexOf(decimal) != -1)
		{
			allow = false;
		}
		// check for other keys that have special purposes
		if(
			key != 8 /* backspace */ &&
			key != 9 /* tab */ &&
			key != 13 /* enter */ &&
			key != 35 /* end */ &&
			key != 36 /* home */ &&
			key != 37 /* left */ &&
			key != 39 /* right */ &&
			key != 46 /* del */
		)
		{
			allow = false;
		}
		else
		{
			// for detecting special keys (listed above)
			// IE does not support 'charCode' and ignores them in keypress anyway
			if(typeof e.charCode != "undefined")
			{
				// special keys have 'keyCode' and 'which' the same (e.g. backspace)
				if(e.keyCode == e.which && e.which != 0)
				{
					allow = true;
				}
				// or keyCode != 0 and 'charCode'/'which' = 0
				else if(e.keyCode != 0 && e.charCode == 0 && e.which == 0)
				{
					allow = true;
				}
			}
		}
	}

	if (/^[\w\d ]+$/.test(String.fromCharCode(key))) {
		allow = true;
	}

	return allow;
}

$(document).ready(function() {
	$('#search_media').click(function() {
		$("#search_media").val("");
	});
});
