$(document).ready(function(){
	var options = {
		handleLgImages: 'drag',
		displayNav: true,
		handleUnsupported: 'remove',
		autoplayMovies: false,
		keysClose: [27],
		listenOverlay: false,
		loadingImage: projectUrl + projectUri + 'img/loading.gif'
    };
	Shadowbox.init(options);

	$.ajaxSetup({
		cache: true,
		error: function() {
			alert('Your browser does not support modern JavaScript. It is recommended that you upgrade to the latest version or you may experience errors while browsing.');
			Shadowbox.close();
		},
		timeout: 25000
	});

	// add overlay DIV for left menu toggle arrows (which are in background)
	$('#menu li').each(function() {
		$(this).prepend('<div class="toggle_overlay">&nbsp;</div>');
	});

	// gets filled with the URL of item that's being toggled
	var current_item_url = '';
	// if true, no more items gets toggled, as all sub-items
	// of the given item are now handled
	var stop_toggle = true;

	$(".toggle_overlay").click(function() {
		// check each li padding against the clicked one
		// and make them all visible/hidden, then change
		// the current li class
		stop_toggle = true;
		var pad = $(this).parent().css('padding-left').slice(0,-2);
		current_item_url = $(this).parent().find('a').attr('href');
		var className = $(this).parent().attr('class');
		
		//remove levelx from class string
		var levelPos = className.indexOf('level');
		var levelLength = className.substr(levelPos, 10).indexOf(' ');
		var tmp = className;
		//trim starting levelx string
		if (levelLength != -1) {
			className = trim(className.slice(0, levelPos) +
						className.slice(levelPos+levelLength+1, 9999));
		}
		//trim trailing levelx string
		if (levelPos >= (className.length-7)) {
			className = className.slice(0, -className.length+levelPos-1);
		}

		$(this).parent().parent().find('li').each(function() {
			if ($(this).find('a').attr('href') == current_item_url) {
				if (stop_toggle) {
					// allow toggle of "sub-items"
					stop_toggle = false;
				}

				if ($(this).css('padding-left').slice(0,-2) > pad) {
					if ((className == 'selected_item') || (className == 'sub_open')) {
						$(this).hide('fast');
					} else {
						$(this).show('fast');
					}
				}
			} else {
				if (!stop_toggle) {
					if ($(this).css('padding-left').slice(0,-2) > pad) {
						if ((className == 'selected_item') || (className == 'sub_open')) {
							$(this).hide('fast');
							$(this).removeClass('sub_open');
						} else {
							if ($(this).css('padding-left').slice(0, -2) == parseInt(pad) + menuPad) {
								$(this).show('fast');
							}
						}
					} else {
						// disable toggle of any more items
						// all "sub-items" were toggled and
						// we have just found another item
						// on same level as our item
						stop_toggle = true;
					}
				}
			}
		});

		// at last, change the class name for our current li
		// to reflect toggle arrow change
		if ((className == 'selected_item') || (className == 'sub_open')) {
			if (className == 'selected_item') {
				$(this).parent().removeClass('selected_item');
				$(this).parent().addClass('unselected_item');
			} else {
				$(this).parent().removeClass('sub_open');
			}
		} else {
			if (className == 'unselected_item') {
				$(this).parent().removeClass('unselected_item');
				$(this).parent().addClass('selected_item');
			} else {
				$(this).parent().addClass('sub_open');
			}
		}
	});

	// add onclick event for each top filtering option
	$('#product_catalog_manufacturers ul li input').each(function() {
		$(this).bind('click', function() {
			document.getElementById('manufacturer_form').submit();
		});
	});

	// sorting selection box
	$('#sort').bind('change', function() {
		document.getElementById('sort_form').submit();
	});

	// JS fix for MSIE and its rows bg color displaying problem
	$('.comparison_highlight > td').each(function() {
		$(this).css('background-color', '#B77322');
	});

	// assign an onclick event for each link in comparison popup,
	// so we display product info in main window instead of the iframe
	// where we are now
	$('.comparison_head a').each(function() {
		if (($(this).attr('href') == '#') && ($(this).attr('rel') != '')) {
			$(this).bind('click', function() {
				window.top.document.location.href = $(this).attr('rel');
			});
		} 
	});

	$('.comparison_body a').each(function() {
		if (($(this).attr('href') == '#') && ($(this).attr('rel') != '')) {
			$(this).bind('click', function() {
				window.top.document.location.href = $(this).attr('rel');
			});
		} 
	});
});
