var pounds = 1,
	footingPrice = 1;

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }

    return vars;
}

jQuery(document).ready(function($){
	
	
	$('.fancybox').fancybox({
		titlePosition:'over',
		overlayOpacity:.6,
		overlayColor:'#000'
	});
	
	$('img.swap').each(function(){
		$img = $(this);
		$img.data('src', {
			original: $img.attr('src'),
			replacement: $img.attr('rel')
		});
	});
	
	
	//dropdown
	dropdown = function(obj, ddSelector){
		
		var timer,
			max = 400,
			menu = this,
			dropdowns = obj.find(ddSelector);
		
		this.clearTimer = function(current){
			window.clearTimeout(timer);
			dropdowns.not($(current)).find('ul').slideUp(200);
		}
		this.startTimer = function(){
			window.clearTimeout(timer);
			timer = window.setTimeout(menu.clearTimer, max);
		}
		
		dropdowns.each(function(){ 
			$(this).bind('mouseenter', function(){
				menu.clearTimer(this);
				$(this).find('ul').slideDown(200);
			}).bind('mouseleave', function(){
				menu.startTimer();
			});
		});
	}
	
	var topNav = $('#navPrimary'),
		topDropdown = new dropdown(topNav, 'li:has(ul)');
	topNav.data('dropdown', topDropdown);
	
	$('img.swap').hover(function(){
			$img = $(this);
			$img.attr('src', $img.data('src').replacement);
		}, function(){
			$img = $(this);
			$img.attr('src', $img.data('src').original);
		});
	
	$('input:submit').hover(function(){
		$(this).css('background', '#CC0000');
	},function(){
		$(this).css('background', '#333');
	});
	
	if($('.productListing').length){
		origHeight = $('.productListing').height();
		
		$('.productListing li:first-child').addClass('active'); // show the first slide
		
		$('.navTitle a').each(function(){
			$(this).parents('li').find('.productBody').css('minHeight', origHeight-50); // 50 for padding
			if( $(this).height() > 35){
				$(this).addClass('long');
			}
			
			// since the body is absolutely positioned, we set the parent's height to fix layout
			var s = $(this).parents('li');
			height = s.find('.productBody').height(); 
			
			if( height > origHeight ){
				origHeight = height;
				s.parents('.productListing').css('height', origHeight);
			}
			
			// bind the tabs
			$(this).bind('click', function(){
				var par = $(this).parents('li');
				if(par.hasClass('active'))
					return false; // no need to do anything
				
				par.siblings().removeClass('active');
				par.addClass('active');
				
				
				
				return false;
			});
		});
	
	
	}
	
	if( $('.requestQuote').length ){
	
		requestType = getUrlVars();
		
		$('fieldset, input, textarea').attr('style', '');
		
		$('#arena-width, #arena-length').bind('keyup change', function(){
			
			calculateAll();
			
		});
		
		$('input:radio[name=si_contact_ex_field3]').change(function(){
			setPrice();
		});
		
		// hack, never use si contact form... hack, jquery has no nth-of-type
		$('fieldset:first').after('<div class="clear">&nbsp;</div>');
		
		
		// set value of radio from url
		if( requestType ){
			switch (requestType['type']){
				case 'dressage':
				$('#inquiry-type').val(['GGT Dressage']);
				break;
				case 'geo':
				$('#inquiry-type').val(['Geo-Textile']);
				break;
				case 'fiber':
				$('#inquiry-type').val(['Fiber']);
				break;
				case 'elasticFiber':
				$('#inquiry-type').val(['Elastic Fiber']);
				break;
				case 'dustAbsorber':
				$('#inquiry-type').val(['Dust Absorber']);
				break;
				default:
				$('#inquiry-type').val(['Geo-Textile']);
			}
		}
		
		$('#si_contact_ex_field2_10, #si_contact_ex_field2_11, #sq-footage' ).attr('readonly', 'readonly');
		setPrice();
		
	}
	
	function setPrice(){
		// removed function
		return;
		
		var footingType = $('input:radio[name=si_contact_ex_field3]:checked').val();
		
		if( footingType == 'GGT Jumping'){
			footingPrice = 1;
		}else{
			footingPrice = .8;
		}
		
		price = (footingPrice * parseFloat(pounds));
		
		
		$('#si_contact_ex_field2_11').val('$' + price.toFixed(2) );
	}
	
	
	function calculateAll( ){
	
		w = parseFloat( $('#arena-width').val() );
		l = parseFloat( $('#arena-length').val() );
		
		if( !w || !l )
			return;
		
		squareFootage = w * l;
		
		pounds = squareFootage / 3;
		
		setPrice();
		
		$('#sq-footage').val( squareFootage );//.parents('form').find('#si_contact_ex_field2_10').val( pounds );
	}
	
	$('.reqQuote').submit(function(){
		calculateAll();
	});

});

