/*
 * Copyright (c) 2009 Night Agency LLC
 *
 */

/**
 * @requires jQuery 1.3
 * @requires jQuery-ui version 1.7
 *
 */
if( typeof( PUREX ) === 'undefined') {
	PUREX = {};
}

PUREX.products = {
	setup: function() {
		$(document).ready( function() {
			var fragance_nav_links = $('#fragrance-navigator li a'),
				purex2_nav_links = $('#purex2-navigator li a'),
				/* tab nav for purex product page */
				purex_products = {
					nav_id: '#product-info-navigator',
					tabs: ['#description-wrapper','#faq-wrapper', '#reviews-wrapper', '#gallery-wrapper']
				},
				/* tab nav for borax product page */
				borax_info = {
					nav_id: '#borax-about-navigator',
					tabs: ['#product-info','#uses','#faq']
				},
				zout_products = {
					nav_id: '#zout-about-navigator',
					tabs: ['#product-info', '#how-to-use', '#fighting-stains', '#faq']
				},
				/**
				 *adds tab click events to each of the product tabs
				 */
				add_tab_clicks = function( nav_object ) {

					var links = $( nav_object.nav_id + ' li a');

					links.click( function() {

						var parent = $(this).parent('li');

						if( !parent.hasClass('active')) {
							$( nav_object.nav_id + ' li.active').removeClass('active');
							parent.addClass('active');
						}
						$( nav_object.tabs.join(',')).filter(':visible').hide();

						$( nav_object.tabs[ links.index(this)] ).show();

						return false;

					});
				},
				/* purex product according menu */
				activeIndex = $('ul#nav-products>li').index( $('#nav-products .selected').parents('li:first') ),
				accordionOptions = {
					autoHeight:false,
					collapsible:true,
					change: function(event, ui){
						// stain removers is its own page
						if(ui.newHeader.context && $(ui.newHeader.context.parentNode).is('#nav-products-stain-removers') ){
							//window.location=$(ui.newHeader).attr('href');
						}
					}
				},
				purex2Navigator = $('#purex2-navigator');

			accordionOptions.active = activeIndex === -1 ? 10 : activeIndex;

			$('#nav-products').accordion(accordionOptions);

			// makes sure we dont have 2 "selected" accordion headers
			$('#nav-products').bind('accordionchange', function(event, ui) {
				$('#nav-products>li.selected').removeClass('selected');
			});



			add_tab_clicks( purex_products );
			add_tab_clicks( borax_info );
			add_tab_clicks( zout_products );


			
			if ($('#borax-about-navigator').size() > 0 ) {
				$('.tab-section').hide();
				$('.tab-section:eq(1)').show();
			} else {
				$('.tab-section:gt(0)').hide();
			}

			fragance_nav_links.click(function() {
	
					$('li', $("#products-description-wrapper ul:not('.internal-desc-list')")).filter(':visible').hide();
	
					$('ul#products-images-list li').filter(':visible').hide();
	
					//get the index of the anchor's li'
					var index_string = fragance_nav_links.index(this) + '';
	
					//show the name and description of the product
					$( "li:eq(" + index_string + ")", $("#products-description-wrapper ul:not('.internal-desc-list')")).show();
	
					//show the product images
					$('ul#products-images-list li:eq(' + index_string + ')').show();
	
					return false;
				});
			purex2_nav_links.click(function() {
					//hide last the size/load	
					$("ul#purex2-size-load li").filter(':visible').hide();
	
					$('ul#products-images-list li').filter(':visible').hide();
	
					//get the index of the anchor's li'
					var index_string = purex2_nav_links.index(this) + '';
	
					//show the size/load
					$("ul#purex2-size-load li:eq(" + index_string + ")").show();
	
					//show the product images
					$('ul#products-images-list li:eq(' + index_string + ')').show();
	
					return false;
				});
			purex2Navigator.addClass('has-' + purex2_nav_links.length);

			$('.reviews-nav .post-button a').click(function(){
				var src = $(this).attr('href');
				$('<iframe src="' + src + '" frameborder="0" scrolling="vertical"  style="border:none;"></iframe>').dialog({
					modal:true,
					width:400,
					height:521
				});

				return false;
			});
			//$('#product-info-navigator ul li:eq(2) a').click();
			//begin zout product image switcher
			var zout_switcher = $('#zout-description-wrapper .switcher a');
			zout_switcher.click(function(){
				$('#zout-product-wrapper ul li').filter(':visible').hide();
				$('#zout-description-wrapper ul li.zout-description').filter(':visible').hide();
				var index_string = (zout_switcher.index(this)%3) + '';
				$('#zout-product-wrapper ul li:eq(' +index_string +'), #zout-description-wrapper ul li.zout-description:eq(' +index_string +')').show();
			});
			
			//end zout product image switcher
		});

	}()
};
