// JavaScript Document

$(document).ready(function() {
						   
	// Limit height of the content boxes on Piano browse pages, but let them expand
	
	$('.search-result .piano_description').each(function() {
		
		
		
		if($(this).height() > 265) {
			
			//$(this).addClass('piano_description');
			
			$(this).addClass('no-overflow').prepend('<div class="expand"><a href="#" class="button-expand">Expand &darr;</a></div>').append('<a href="#" class="button-collapse">Collapse &uarr;</a>');
			
			//$('.button-collapse').hide();
			
			
			
				$(this).toggle(
				
					function() {
											  
						$(this).removeClass('no-overflow');
						
						return false;
													  
					}, 
							
				function() {
				
					$(this).addClass('no-overflow');
				

					
					return false;
												  
				}); // end toggle
			
		} // end if
	
	});
	
	
	
	// Add SuperFish drop-down menu for Pianos
	
	$('.block-menu_block ul.menu').superfish({
    
        delay: 400,
        animation: {height:'show'}
    
    }); // end 'superfish'
    
    $('#navigation .block-menu_block ul.menu').superfish({
    
        delay: 400,
        animation: {height:'show'}
    
    }); // end 'superfish'
    
    
    
	// Add Tooltip to links 
	
	$('.search-result .play_period a').tooltip({ 
    track: true, 
    delay: 0, 
    showURL: false, 
    showBody: "|", 
    fade: 250 
	});
	
	$('.search-result .play_rate a').tooltip({ 
    track: true, 
    delay: 0, 
    showURL: false, 
    showBody: "|", 
    fade: 250 
	});
	
	
	
	// preload images for rollovers
	
	if (document.images) {
	
		var img1 = new Image();
		var img2 = new Image();
		var img3 = new Image();
		var img4 = new Image();
		img1.src = "/wp/wp-content/uploads/button-bg2.png";
		img2.src = "/wp/wp-content/uploads/button-big-bg2.png";
		img3.src = "/wp/wp-content/uploads/button-big-orange2.png";
		img4.src = "/wp/wp-content/uploads/button-orange2.png";
	
	}
	
	
	
	$('#same_as_personal').toggle(
		function () {
			
			$('#billing_name').val($('#name').val());
			$('#billing_address').val($('#address').val());
			$('#billing_city').val($('#city').val());
			$('#billing_state').val($('#state').val());
			$('#billing_zip').val($('#zip').val());
			$('#billing_phone1').val($('#phone1').val());
			$('#billing_phone2').val($('#phone2').val());
			$('#billing_email').val($('#email').val());
			$('#billing_fax').val($('#fax').val());
			return true;
		},
		
		function() {
			$('#billing_name').val('');
			$('#billing_address').val('');
			$('#billing_city').val('');
			$('#billing_state').val('');
			$('#billing_zip').val('');
			$('#billing_phone1').val('');
			$('#billing_phone2').val('');
			$('#billing_email').val('');
			$('#billing_fax').val('');
			return true;
		}); // end "toggle"
	
	$('#select_piano_form').hide();
	
	/*$('#select_piano').toggle(
		function() {
			$('#select_piano_form').slideDown();
			$('#select_piano').hide();
		},
		
		function() {
			$('#select_piano_form').slideUp();
			$('#select_piano').show();
		}); // end "toggle"*/
	
	
	
	// Scroll nicely down the page when the "Add to Cart" button is clicked on a Piano profile
	
	$('#add-to-cart').click(function() {
									 
		$('#select_piano_form').slideDown();
		$('#select_piano').hide();
	
	}); // end 'click' 
	
	function filterPath(string) {
    return string
      .replace(/^\//,'')  
      .replace(/(index|default).[a-zA-Z]{3,4}$/,'')  
      .replace(/\/$/,'');
  }
  $('a[href*=#]').each(function() {
    if ( filterPath(location.pathname) == filterPath(this.pathname)
    && location.hostname == this.hostname
    && this.hash.replace(/#/,'') ) {
      var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
      var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
       if ($target) {
         var targetOffset = $target.offset().top;
         $(this).click(function() {
           $('html, body').animate({scrollTop: targetOffset}, 400);
           return false;
         });
      }
    }
  });
	


	
		
	
	
	// validate 'terms and conditions' and 'play period'
	
	$('#piano_form').submit(function() {
		
		if($('#play_period').val() == 'select') { 
		
			alert('Please select a play period to continue.');
			
			return false;
		
		}
		
		if($('#read_terms').val() == '') {
				
			alert('You must agree to the Terms and Conditions to continue.');
			
			return false;
			
		}
		
		if($('#name').val() == '' || $('#address').val() == '' || $('#city').val() == '' || $('#state').val() == '' || $('#zip').val() == '' || $('#phone1').val() == '' || $('#email').val() == '') {
				
			alert('Please fill out all of your delivery information, indicated by the red asterisk.');
			
			return false;
			
		}
		
		if($('#billing_name').val() == '' || $('#billing_address').val() == '' || $('#billing_city').val() == '' || $('#billing_state').val() == '' || $('#billing_zip').val() == '' || $('#billing_phone1').val() == '' || $('#billing_email').val() == '') {
				
			alert('Please fill out all of your billing information, indicated by the red asterisk.');
			
			return false;
			
		}
		
	}); // end 'submit'
	
	// add ShareThis text to piano profile
	
	$('h1 a#add-to-cart').before('<span class="st_sharethis" displayText="Send to Friend"></span>');
	$('#twitter').after('<span class="st_sharethis" displayText="Send to Friend"></span>');
	
	
	

}); // end "ready"
