var IPOXProducts, ipoxproducts;

IPOXProducts = function ($) {
	var that = this;
	
	this.current_slug = "_init_";
	this.rb_content = {};
	this.prod_menu_offtop = 0;
	
	this.init = function () {
		var loc = document.location.toString(), routes = ROUTES, lang = LANG;
		if (loc.indexOf('/' + routes.products[lang] + '/') >=0 && $('.ipox-products-list-group').length > 0) {
			
			this.build_products_nav();
			this.build_products_tooltips();
			this.build_products_reqbox_buttons();
			
			this.build_product_anchors();
			
			this.rb_init();
		}
	};
	
	this.build_products_nav = function () {
		setInterval(function () {that.check_loc();}, 100);
		
		$('.ipox-products-list-group-master').each(function () {
			var group = this;
			$(this).find('.ipox-products-list-group-head').first().click(function (event) {
				event.preventDefault();
				event.stopImmediatePropagation();
				that.show($(group).attr('slug'));
			});
		});
		$('.ipox-products-list-group').each(function () {
			var group = this;
			if (!$(this).hasClass('group-child')) {
				$(this).find('.ipox-products-list-group-head').first().click(function (event) {
					event.preventDefault();
					event.stopImmediatePropagation();
					that.show($(group).attr('parent-slug') + '/' + $(group).attr('slug') + '/');
				});
			}
		});
		
		/* scroll function */
		this.prod_menu_offtop = $('.ipox-products-sidebar-left').offset().top;
		if($.browser.msie && $.browser.version < 8) {
			$('.ipox-products-sidebar-left').css('left', '0px');
		}
		$(window).bind('scroll', function () {
			var so = $(window).scrollTop();
			if (so > that.prod_menu_offtop) {
				$('.ipox-products-sidebar-left').addClass('fixed');
				if($.browser.msie && $.browser.version < 8) {
					$('.ipox-products-sidebar-left').css('left', $('#main').offset().left - 10 + 'px');
				}
			} else {
				$('.ipox-products-sidebar-left').removeClass('fixed');
				if($.browser.msie && $.browser.version < 8) {
					$('.ipox-products-sidebar-left').css('left', '0px');
				}
			}
		});
	};
	this.build_products_tooltips = function () {
		$('.ipox-product-list-entry-application-tooltip-trigger').hover(
			function () {
				var tt = $('#' + $(this).attr('tooltip'));
				try { $(document.body).append(tt); }
				catch (e) {}
				tt.show();
				tt.css({
					left: $(this).offset().left - 112 + 'px',
					top: $(this).offset().top + 25 + 'px'
				});
			},
			function () {
				$('.ipox-product-list-entry-application-tooltip').each(function () { $(this).hide(); });
			}
		);
	};
	this.build_products_reqbox_buttons = function () {
		// reqbox-addto
		$('.reqbox-addto').each(function () {
			var data = {prod_id: 0, prod_title: ''};
			try {
				data.prod_id = parseInt($(this).attr('prod-id'), 10);
				data.prod_title = $(this).attr('prod-title');
				if (data.prod_id > 0) {
					$(this).click(function () {
						that.rb_add(data);
					});
				}
			} catch (e) {}
		});
	};
	this.build_product_anchors = function () {
		$('a.product-anchor').each(function () {
			$(this).attr('id', 'nav-' + $(this).attr('pos-id').replace(/\//g, '_'));
		});
	};
	
	/* navigation / accordeon */
	this.pre_check_loc = function () {
		var loc = document.location.toString(), routes = ROUTES, lang = LANG;
		if (loc.indexOf('/' + routes.products[lang] + '/') >=0 && loc.indexOf('#') < 0) {
			if (loc.indexOf('category/') >= 0) {
				loc = loc.replace('category/', '');
			}
			try {
				document.location = loc.replace('/' + routes.products[lang] + '/', '/' + routes.products[lang] + '/#');
			} catch (e) {}
		}
	};
	this.check_loc = function () {
		var loc;
		loc = document.location.toString().split('#');
		loc = loc.length > 1 ? loc[1] : "";
		if (loc !== this.current_slug) {
			if (loc.length === 0) {
				this.show_all();
			} else {
				this.show(loc);
			}
		}
	};
	this.set_cur_loc = function () {
		var loc = document.location.toString().split('#')[0];
		document.location = loc + '#' + this.current_slug;
	};
	this.hide_all = function () {
		$('.ipox-products-list-group').each(function () {
			$(this).removeClass('open');
		});
		$('.ipox-products-list-group-master').each(function () {
			$(this).removeClass('open');
		});
		$('.product-navigation-accordion .cat-item').each(function () {
			$(this).removeClass('active');
		});
	};
	this.show = function (slug) {
		var master = "", group = "";
		
		slug = slug.split('/');
		
		if (slug[slug.length - 1].length === 0) slug.pop();
		if (slug.length < 2 || $('.ipox-products-list-group.' + slug[1]).length === 0) { 
			this.show_default(slug[0]);
		} else {
			this.hide_all();
			$('.ipox-products-list-group-master.' + slug[0]).first().addClass('open');
			$('.ipox-products-list-group.' + slug[1]).first().addClass('open');
			$('.product-navigation-accordion .cat-item-' + $('.ipox-products-list-group-master.' + slug[0]).first().attr('catid')).addClass('active');
			$('.product-navigation-accordion .cat-item-' + $('.ipox-products-list-group.' + slug[1]).first().attr('catid')).addClass('active');
			this.current_slug = slug.join('/') + '/';
			$('.ipox-products-submenu li a').each(function () {
				var href = "";
				try {
					href = $(this).attr('href');
				} catch (e) {}
				$(this).removeClass('active');
				href = href.split('#');
				if (href.length > 1 && href[1] === that.current_slug) {
					$(this).addClass('active');
				}
			});
			this.set_cur_loc();
			this.auto_nav_to();
		}
	};
	this.show_default = function () {
		var master = arguments && arguments[0] ? arguments[0] : "_false_", 
			slug = '';
		if ($('.ipox-products-list-group-master.' + master).length > 0) {
			master = $('.ipox-products-list-group-master.' + master).first();
		} else {
			master = $('.ipox-products-list-group-master').first();
		}
		try {
			slug += master.attr('slug') + '/' + master.find('.ipox-products-list-group').first().attr('slug') + '/';
			this.show(slug);
		} catch (e) {}
	};
	this.show_all = function () {
		$('.ipox-products-list-group-master').each(function () {
			$(this).addClass('open');
		});
		$('.product-navigation-accordion > li.cat-item').each(function () {
			$(this).addClass('active');
		});
		this.current_slug = "";
		this.set_cur_loc();
	};
	this.auto_nav_to = function () {
		var d, t;
		d = (window.contentWindow || window).document || window.ownerDocument || window;
		t = $.browser.safari || d.compatMode == 'BackCompat' ? d.body : d.documentElement;
		try { $(t).animate({scrollTop: $('a#nav-' + this.current_slug.replace(/\//g, '_')).offset().top}, 0); } catch (e) { }
	};
	
	/* request box */
	this.rb_init = function () {
		var i, ipp;
		if (userstorage.rb) {
			for (i in userstorage.rb) {
				this.rb_add({
					prod_id: i,
					prod_title: userstorage.rb[i]
				}, false);
			}
		}
		this.rb_check();
		$('#ipox-products-request-data a.download').click(function () {
			ipp = window.open($('#ipox-products-request-data a.download').attr('req-target'), 'ipox_chemicals_request', 'width=640,height=640,resizable=no,toolbars=false,dependent=yes,location=no,status=no,menubar=no');
		});
	};
	this.rb_add = function (data) {
		var h_el, h_el_dl, update = arguments.length > 1 && arguments[1] === false ? false : true;
		if (!this.rb_content.hasOwnProperty(data.prod_id)) {
			h_el = document.createElement('li');
			$('#ipox-products-request-data .download-list').append($(h_el));
			$(h_el).html(data.prod_title);
			$(h_el).attr('id', 'ipox-rb-p-' + data.prod_id);
			h_el_dl = document.createElement('a');
			$(h_el).append($(h_el_dl));
			$(h_el_dl).addClass('remove-this');
			$(h_el_dl).click(function () {
				that.rb_rem(data.prod_id);
			});
			this.rb_content[data.prod_id] = {
				'title': data.prod_title,
				'el': h_el
			};
			if (update) this.rb_check();
		}
	};
	this.rb_rem = function (id) {
		try {
			$(this.rb_content[id].el).remove();
			delete(this.rb_content[id]);
			this.rb_check();
		} catch (e) {}
	};
	this.rb_check = function () {
		var i, content_storage = {};
		if (!obj_empty(this.rb_content)) {
			$('#ipox-products-request-data').addClass('full');
		} else {
			$('#ipox-products-request-data').removeClass('full');
		}
		for (i in this.rb_content) {
			if (this.rb_content.hasOwnProperty(i)) {
				content_storage[i] = this.rb_content[i].title;
			}
		}
		ipoxstorage.store('rb', content_storage);
		ipoxstorage.get_data();
	};
	
	/* init */
	this.pre_check_loc();
	$(document).ready(function () {
		that.init();
	});
};
ipoxproducts = new IPOXProducts(jQuery);

