var Config = {
	site_url: '/index.php/',
	base_url: '/',
	product_type: '',
	product_type_id_accordian_index: 0,
	product_type_id: '',
	product_subtype_id: '',
	product_tab_current: '',
	add_to_cart_params: ['width=600', 'height=400', 'scrollbars=1', 'location=1', 'resizable=1', 'status=1', 'toolbar=1']
};

var Tools = {
	show_dimensions: function(el) {
		alert($(el).getSize().size.x + 'x' + $(el).getSize().size.y);
	},
	in_element: function(event, el) {
		var event = new Event(event);
		var el_coordinates = el.getCoordinates();
		// added a + 1 to fix issue with ie...
		if ( (event.page.x >= el_coordinates.left && event.page.x <= (el_coordinates.left + el_coordinates.width)) && (event.page.y >= el_coordinates.top && event.page.y <= (el_coordinates.top + el_coordinates.height)) ) {
			return true;
		}
		return false;
	},
	get_js_req: function(k) {
		var js_uri = location.href.split('#')[1] || '';
		js_uri = js_uri.split('/');
		for (var i = 0, count = js_uri.length; i <= count; i = i + 2 ) {
			if (js_uri[i] == k) {
				return js_uri[i + 1] ? js_uri[i + 1] : '';
			}
		}
		return '';
	}
};

var Products = {
	start:function() {
		// update filters
		var selects = ['product_type_id', 'product_type_2_id', 'product_activity_type_id'];
		selects.each(function(v) {
			if ($(v)) {
				$(v).addEvent('change', function() {
					Products.update_filter();
				});
			}

		});

		if ($('products_per_page')) {
			$('products_per_page').addEvent('change', function() {
				Products.update_per_page();
			});
		}
		/*
		Removed by request
		var accordion = new Accordion('h6.acc_toggler1', 'ul.acc_element1', {
			opacity: true,
			display: Config.product_type_id_accordian_index
		}, $('products_links'));

		// fixes accordian flicker...until I can find a better way.
		$('products_links').getElements('ul.acc_element1').each(function(v) {
			v.setStyle('display', '');
		});
		*/

		window.addEvent('load', function() {
			$$('h6.acc_toggler1').each(function(v, k) {
				if (k == Config.product_type_id_accordian_index) {
					var type_id = v.id.split('_').pop();
					if ($('nav_product_subtypes_' + type_id)) {
						var fx = new Fx.Slide($('nav_product_subtypes_' + type_id));	
						fx.hide();
						$('nav_product_subtypes_' + type_id).setStyle('display', '');
						fx.toggle();
					}
				}
			});
		});

		if ($('add_to_cart_link')) {
			$('add_to_cart_link').addEvent('click', function(event) {
				event = new Event(event);
				event.stop();
				
				if ($('product_quantity').getValue() > 0) {
					window.open(this.getProperty('href').replace('add=1', 'add=' + $('product_quantity').getValue()), 'win_gerbercart', Config.add_to_cart_params.join(','));
				}
			});
		}
		
	},

	update_filter: function() {
		if (Config.product_type_id == '') {
			window.location = Config.site_url + 'products/' + Config.product_type + '/' + Config.product_subtype_id + '/' + ($('product_type_2_id') ? $('product_type_2_id').getValue() : '0') + '/' + ($('product_activity_type_id') ? $('product_activity_type_id').getValue() : '0');
		} else {
			// we are on a custom product page
			window.location = Config.site_url + 'products/' + Config.product_type + '/' + ($('product_type_id') ? $('product_type_id').getValue() : '0') + '/' + ($('product_activity_type_id') ? $('product_activity_type_id').getValue() : '0');
		}
	},

	update_per_page: function() {
		window.location = $('per_page_base_url').getValue() + ($('products_per_page') ? $('products_per_page').getValue() : '0') + '/0';
	},

	start_product_tabs: function() {
		var tabs = ['key_features', 'tech_specs', 'reviews_and_demos'];
		Config.product_tab_current = tabs[0];
		tabs.each(function(v) {
			$(v + '_link').addEvent('click', function() {
				$(Config.product_tab_current + '_li').removeClass('current');
				$(Config.product_tab_current + '_container').setStyle('display', 'none');
				$(v + '_li').addClass('current');
				$(v + '_container').setStyle('display', '');
				Config.product_tab_current = v;
			});
		});

	},
	
	add_pager_events: function() {
		if ($('product_img_pager')) {
			$('product_img_pager').getElements('a').each(function(v, k) {
				if (v.id != 'product_img_zoom') {
					v.addEvent('click', function() {
						window.location = v.getProperty('href');
						var prouct_image_id = Tools.get_js_req('image');
						var myAjax = new Ajax (
								Config.site_url + 'ajax/get_product_img/' + prouct_image_id, {
									method : 'get',
									onComplete : function(response) {
										Products.add_pager_events();
		
									},
									evalScripts: true
								}
							).request();
					});
				}
			});
		}
	},
	
	check_js_uri_events: function() {
	
	},
	
	start_featured_product_subtypes: function(product_subtype_ids_featured, product_subtype_id_selected) {
		var product_subtype_id_default = product_subtype_id_selected;

		product_subtype_ids_featured.each(function(v) {
			$('product_subtype_button_' + v).addEvent('mouseenter', function() {
				if ($('product_subtype_description_' + product_subtype_id_selected)) {
					$('product_subtype_description_' + product_subtype_id_selected).setStyle('display', 'none');
					$('product_subtype_button_' + product_subtype_id_selected).removeClass('current');
				}
				$('product_subtype_description_' + v).setStyle('display', '');
				product_subtype_id_selected = v;
			});
		});

		$('product_subtype_buttons').addEvent('mouseleave', function() {
			$('product_subtype_description_' + product_subtype_id_selected).setStyle('display', 'none');
			$('product_subtype_description_' + product_subtype_id_default).setStyle('display', '');
			product_subtype_id_selected = product_subtype_id_default;
			$('product_subtype_button_' + product_subtype_id_selected).addClass('current');
		});

	},
	start_product_zoom: function(img_filename) {
		if ($('product_img_zoom')) {
			$('product_img_zoom').addEvent('click', function() {
				new Moognify(this,{duration: 300, source: img_filename});
			});
		}
	}
};


var Site = {
	start_faq: function() {
		$('faq_cat_id').addEvent('change', function() {
			window.location = $('service_and_support_base_url').getValue() + '/' + $('faq_cat_id').getValue();
		});
	},
	start_glossary: function(glossary_selections) {
		glossary_selections = glossary_selections.split(' ');
		Site.glossary_selection_index = 0;
		glossary_selections.each(function(v, k) {
			$('glossary_link_' + k).addEvent('click', function() {
				$('glossary_link_' + Site.glossary_selection_index).removeClass('current');
				$('glossary_' + Site.glossary_selection_index).setStyle('display', 'none');
				$('glossary_' + k).setStyle('display', '');
				$('glossary_link_' + k).addClass('current');

				Site.glossary_selection_index = k;
			});
		});
	}
};
