var last_ajax_url = '';
var last_active_filter_tab = 0;
var transition_in_effect = false;

$(document).ready(function(){
	$.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.');
		},
		timeout: 25000
	});

	// make external pages load in new window
	$('a[rel=external_link]').bind('click', function() {
		window.open($(this).attr('href'));
		return false;
	});
	
	// add overlay DIV for left menu toggle arrows (which are in background)
	$('#left-bar ul#left-menu li > ul > li').each(function() {
		$(this).prepend('<div class="toggle_overlay">&nbsp;</div>');
	});

	$(".toggle_overlay").click(function() {
		var e = $(this);
		var e_li = e.parent('li');
		var e_ul_parent = e.parent('li').parent('ul').parent('li').parent('ul');
		if ((e_ul_parent.attr('id') == 'left-menu') && (!/sub_open/.test(e_li.attr('class')))) {
			$('ul#left-menu > li > ul > li > ul:visible').toggle();
			$('ul#left-menu li.sub_open').toggleClass('sub_open');
		}
		e_li.toggleClass('sub_open');
		e_li.find('ul:first').toggle();
	});

	// 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', '#e9e9e9');
	});

	// 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');
			});
		} 
	});

	$('a.zoom_image, a.zoom_image_gallery').lightBox(
		{
			imageLoading: '/design/lightbox/lightbox-ico-loading-'+window.f_lang.lang_id+'.gif',
			imageBtnClose: '/design/lightbox/lightbox-btn-close-'+window.f_lang.lang_id+'.gif',
			imageBtnPrev: '/design/lightbox/lightbox-btn-prev-'+window.f_lang.lang_id+'.gif',
			imageBtnNext: '/design/lightbox/lightbox-btn-next-'+window.f_lang.lang_id+'.gif',
			imageBlank: '/design/lightbox/lightbox-blank.gif',
			txtImage: window.f_lang.image,
			txtOf: window.f_lang.out_of
		}
	);

	$('#top-menu li').hover(function() {
		$(this).children('ul:first').show();
	}, function() {
		$(this).children('ul:first').hide();
	});

	// assemble filter tabs, so they change checkboxes when clicked upon and position tabs next to each other
	var first_tab_done = false;
	var last_positioned_tab_element = false;
	var last_tab_index = 0;
	var main_filter_content_div = false;
	$('#main-content > form > fieldset > div.product_detail_tab').each(function() {
		var e = $(this);
		// this is the first tab, we don't need to reposition it
		if (!first_tab_done) {
			last_positioned_tab_element = e;
			first_tab_done = true;
			e.attr('id', 'fltr'+last_tab_index);
			e.addClass('products_filter_tabs_active');
			// move this tab's content inside another div within our current content div
			var c_div = e.next('div').next('div');
			main_filter_content_div = c_div;
			c_div.html('<div id="fltr'+last_tab_index+'_content">'+c_div.html()+'</div>');
		} else{
			// move this tab's content into main filters DIV and hide it
			e.next('div.cleaner').remove();
			var c_div = e.next('div');
			main_filter_content_div.append('<div id="fltr'+last_tab_index+'_content" style="display: none">'+c_div.html()+'</div>');
			c_div.remove();
			// reposition this tab the end
			e.attr('id', 'fltr'+last_tab_index).insertAfter(last_positioned_tab_element);
			last_positioned_tab_element = e;
		}
		last_tab_index++;

		// bind onClick to each tab, so we display the correct content
		e.click(function() {
			if (window.transition_in_effect) {
				return false;
			} else {
				window.transition_in_effect = true;
			}

			// remove active class from previously active tab
			$('#fltr'+window.last_active_filter_tab).removeClass('products_filter_tabs_active');

			var e = $(this);
			var eid = e.attr('id');
			var ec = $('#fltr'+window.last_active_filter_tab+'_content');
			e.addClass('products_filter_tabs_active');
			ec.animate({
				opacity: 0
			}, 500, function() {
				// set height attribute of the main filter contents DIV to our new content's height
				var new_ec = $('#'+eid+'_content');
				var h = new_ec.height();
				$('#catf-box').animate({
					height: h+'px'
				}, 400, function() {
					window.last_active_filter_tab = parseInt(eid.replace('fltr', ''));

					// hide previous content completely
					ec.css({
						'display'	: 'none'
					});
	
					// show new content
					new_ec.css({
						'opacity'	: 0,
						'display'	: 'block'
					}).animate({
						opacity: 1
					}, 500, function() {
						window.transition_in_effect = false;
					});
				});
			});
		});
	});

	// on product page, store data of each tab into a jQuery data stream and remove all content DIVs except for the first one
	$('#product_detail > div.product_detail_tab').each(function() {
		var e = $(this);
		var cntnt = $('#'+e.attr('id').replace('_tab', ''));
		e.data('content', cntnt.html());
		// store content height as well for later animations
		var h = cntnt.height();
		h = (h ? h+'px' : '20px');
		e.data('h', h);

		// remove the actual content DIV if not the first one
		if (!first_tab_done) {
			first_tab_done = true;
			main_filter_content_div = cntnt;
			e.addClass('products_filter_tabs_active');
			// add content into a sub-div
			main_filter_content_div.html('<div>'+main_filter_content_div.html()+'</div>');
			// add fixed height, as this is required for first animation to display correctly
			main_filter_content_div.css('height', h);
		} else {
			cntnt.remove();
		}

		// bind onClick to each tab here
		e.click(function() {
			if (window.transition_in_effect) {
				return false;
			} else {
				window.transition_in_effect = true;
			}
	
			// remove active class from previously active tab
			$('#product_detail > div.product_detail_tab').removeClass('products_filter_tabs_active');
	
			var e = $(this);
			e.addClass('products_filter_tabs_active');
			// hide previous content completely
			main_filter_content_div.children('div:first').animate({'opacity' : 0}, 500, function() {
				var subdiv = main_filter_content_div.children('div:first');
				subdiv.html(e.data('content'));
				// adjust content section height
				main_filter_content_div.animate({height: e.data('h')}, 400, function() {
					// show the new content
					subdiv.animate({'opacity': 1}, 500, function() {
						window.transition_in_effect = false
					});
				});
			});
		});
	});
	
	// homepage carousel
	$('.carousel .hidden').show();
	$('.carousel').infiniteCarousel({
		displayTime: 8000,
		textholderHeight: .25,
		displayProgressBar: 0,
		imagePath: '/img/',
		displayThumbnailBackground: false,
		autoHideControls: true
	});
});
