
window.addEvent('domready', function() {
	
	helpSetup();
	
	/* the filter tabs */
	
	var ta = $('tabAll');
	var tr = $('tabRecipes');
	var tn = $('tabNutrition');
	var tp = $('tabPeople');
	var tt = $('tabProducts');
	
	
	if( ta != null ) { 
		ta.addEvent('click', function() {
			$$('.article').setStyle('display', 'block');
			ta.addClass('active');
			if( tr != null ) { tr.removeClass('active'); }
			if( tn != null ) { tn.removeClass('active'); }
			if( tp != null ) { tp.removeClass('active'); }
			if( tt != null ) { tt.removeClass('active'); }
		});
	}

	if( tr != null ) {
		tr.addEvent('click', function() {
			$$('.article').setStyle('display', 'none');
			$$('.article').filterByClass('recipes').setStyle('display', 'block');
			tr.addClass('active');
			if( ta != null ) { ta.removeClass('active'); }
			if( tn != null ) { tn.removeClass('active'); }
			if( tp != null ) { tp.removeClass('active'); }
			if( tt != null ) { tt.removeClass('active'); }
		});
	}
	
	if( tn != null ) {
		tn.addEvent('click', function() {
			$$('.article').setStyle('display', 'none');
			$$('.article').filterByClass('nutrition').setStyle('display', 'block');
			tn.addClass('active');
			if( ta != null ) { ta.removeClass('active'); }
			if( tr != null ) { tr.removeClass('active'); }
			if( tp != null ) { tp.removeClass('active'); }
			if( tt != null ) { tt.removeClass('active'); }
		});
	}
	
	if( tp != null ) {
		tp.addEvent('click', function() {
			$$('.article').setStyle('display', 'none');
			$$('.article').filterByClass('people').setStyle('display', 'block');
			tp.addClass('active');
			if( ta != null ) { ta.removeClass('active'); }
			if( tn != null ) { tn.removeClass('active'); }
			if( tr != null ) { tr.removeClass('active'); }
			if( tt != null ) { tt.removeClass('active'); }
		});
	}

	if( tt != null ) {		
		tt.addEvent('click', function() {
			$$('.article').setStyle('display', 'none');
			$$('.article').filterByClass('products').setStyle('display', 'block');
			tt.addClass('active');
			if( ta != null ) { ta.removeClass('active'); }
			if( tn != null ) { tn.removeClass('active'); }
			if( tp != null ) { tp.removeClass('active'); }
			if( tr != null ) { tr.removeClass('active'); }
		});
	}
	
	/* the Accordion */
	
	var tgl = $$('h2.accToggle');
	var sld = $$('ul.accSlide');
	
	var accordion = new Accordion(tgl, sld, {
			opacity: false,
			alwaysHide: true,
			duration: 200
	});
	
	/* switches for the tag displays */

	var relSwitch = $('relatedSwitch');
	
	if( relSwitch != null ) {
		relSwitch.addEvent('click', function() {
			if( $$('.minortag')[0].getStyle('display') == 'none' ) {
				$$('.minortag').setStyle('display', 'inline');
				relSwitch.setText('[less...]');
			}
			else {
				$$('.minortag').setStyle('display', 'none');
				relSwitch.setText('[more...]');
			}
			
			accordion.display(0).chain(function() {
				accordion.display(0);
			});
		});
	}
	
	var clSwitch = $('cloudSwitch');
	
	if( clSwitch != null ) {
		clSwitch.addEvent('click', function() {
			if( $('shortcloud').getStyle('display') == 'none' ) {
				$('shortcloud').setStyle('display', 'block');
				$('longcloud').setStyle('display', 'none');
				clSwitch.setText('[more...]');
			}
			else {
				$('shortcloud').setStyle('display', 'none');
				$('longcloud').setStyle('display', 'block');
				clSwitch.setText('[less...]');
			}
			
			var nCloud = tgl.length - 2;
		
			accordion.display(nCloud).chain(function() {
				accordion.display(nCloud);
			});
		});
	}
	
	
	var specArea = $('spec-area');
	
	if( specArea != null ) {
		var contentSize = $('content').getSize();
		var theSpace = window.getHeight() - contentSize.size.y - 70 - 30;
		var theHeight = 210;
		specArea.setStyle('height', theHeight);
		$('promotion').setStyle('height', theHeight);
		var thePadding = ( theSpace > theHeight ) ? ( (theSpace - theHeight)* 0.61803 + 10 ) : ( 10 );
		$('content').setStyle('margin-bottom', thePadding);
	}
	
	/*
	var specMore = $('morespec');
	
	if( specMore != null ) {
		$('morebutton').addEvent('click', function() {
			specMore.setStyle('display', 'block');
			$('morebutton').setStyle('display', 'none');
			window.fireEvent('resize', null, 100);
		});
	}
	
	*/
	
	var commToggle = $('comments-toggle');
	
	if( commToggle != null ) {
		commToggle.addEvent('click', function() {
			$('discuss-area').setStyle('display', 'inline');
			commToggle.setStyle('display', 'none');
		});
	}
	
	var bgimage = $('bgimage');
	
	if( bgimage != null ) { /* bgimage backgrounds must be 358px tall and around 400px wide. */
		var offset = window.getHeight() - 458;
		var bgposStr = '100% ' + offset + 'px';
		bgimage.setStyle('background-position', bgposStr);
    var bgimagepath = 'url("/wp-content/themes/hsgtheme/images/bg/bg' 
                + $random(1,9) + '.jpg")';
    // var bgimagepath = 'url("http://hillstreetgrocer.com/wp-content/themes/hsgtheme/images/bg/bg' 
    //            + $random(1,9) + '.jpg")';
		bgimage.setStyle('background-image', bgimagepath);
	}
	
	var strips = $('strips');
	
	if( strips != null ) { /* set minimum height equal to screen size */
		strips.setStyle('height', 'auto');
		var stripsSize = strips.getSize();
		if( stripsSize.size.y < window.getHeight() ) {
			strips.setStyle('height', window.getHeight());
		}
	} 
	
	var delitoggle = $('deli-image');
	var canapetoggle = $('canape-image');
	if( delitoggle != null ) {
		delitoggle.addEvent('click', function() {
			if( $('deli-details').getStyle('display') == 'none' ) {
				$('deli-details').setStyle('display', 'block');
			}
			else {
				$('deli-details').setStyle('display', 'none');
			}

		var strips = $('strips');
		
		if( strips != null ) { /* set minimum height equal to screen size */
			strips.setStyle('height', 'auto');
			var stripsSize = strips.getSize();
			if( stripsSize.size.y < window.getHeight() ) {
				strips.setStyle('height', window.getHeight());
			}
		} 
	
		});
	}
	if( canapetoggle != null ) {
		canapetoggle.addEvent('click', function() {
			if( $('canape-details').getStyle('display') == 'none' ) {
				$('canape-details').setStyle('display', 'block');
			}
			else {
				$('canape-details').setStyle('display', 'none');
			}
		});
	}
	
	if( window.getWidth() < 980 ) {
		$('footer').setStyle('width', window.getWidth());
	}
	
	/* for order send page */
	var conditionstoggle = $('termstoggle');
	if( conditionstoggle != null ) {
		conditionstoggle.addEvent('click', function() {
			$('orderconditions').setStyle('display', 'block');
		});
		$('sendbtn').addEvent('click', function(evt) {
			var termsok = $('termsok');
			if( !termsok.getProperty('checked') ) {
				alert('Please agree to the terms and conditions or else cancel.');
				evt = new Event(evt).stop();
			}
		});
	}
	
	var ordertotal = $('total');
	if( ordertotal != null ) {
		var t = 0;
		$$('input.ord-row-tprice').each(function(elt) {
			t += elt.getProperty('value').toFloat();
		});
		t = Math.round(t * 100) / 100;
		ordertotal.setText(t);
	}	
	
});

window.addEvent('resize', function() {

	var specArea = $('spec-area');
	
	if( specArea != null ) {
		var contentSize = $('content').getSize();
		var theSpace = window.getHeight() - contentSize.size.y - 70 - 30;
		var theHeight = 210;
		specArea.setStyle('height', theHeight);
		$('promotion').setStyle('height', theHeight);
		var thePadding = ( theSpace > theHeight ) ? ( (theSpace - theHeight)* 0.61803 + 10 ) : ( 10 );
		$('content').setStyle('margin-bottom', thePadding);
	}
	

});
