$(function () { // Call functions on page load
	sfHover(); // suckerfish dropdowns	
	imgLink(); // remove image styles
	fixTangerine(); // fix issues with extended characters
	photoSlide(); // Photo Slideshow Functionality
  //formHide(); // Slide to hide/unhide functionality for forms
  searchShow(); // Hide and Display search box in header
  
});

// suckerfish slide down/up functionality
function sfHover() {
	var listElement = $('.nav-list-item.parent');
	listElChild = listElement.children('.sub-nav-list');
	listElChild.css('display','none');
	listElement.hover(
		function() {
			$(this).addClass('sfhover');
			//$(this).css('background-color','#FFE6F2');
			$(this).children('.sub-nav-list').stop(true,true).slideDown('fast');
		},
		function() {
			$(this).children('.sub-nav-list').stop(true,true).slideUp('fast',function() {
				$(this).parent('.nav-list-item').removeClass('sfhover');
				//$(this).parent('.nav-list-item').css('background-color','transparent');
			});
		}
	);
	
	$('.sub-nav-list-item').has('.sub-nav-2').hover(
		function() {
			$(this).addClass('sfhover2');
			$(this).children('.sub-nav-2').stop(true,true).slideDown('slow');
		},function() {
			$(this).children('.sub-nav-2').stop(true,true).slideUp('fast',function() {
				$(this).parent('.sub-nav-list-item').removeClass('sfhover2');
			});
		}
	);
}


function postContinueInit() {
	var hash = window.location.hash;
	
	var nav = createNav('.continue .post-end-title','post-continue-nav-item');
	$('.continue').prepend(nav);
	hideTitles('.continue .post-end-title');
	
	if(hash == "" || hash == "#anchor0") {
		$('.continue .continue-block.index-0').removeClass('index-0').addClass('current');
		$('.continue .continue-block.index-1').removeClass('index-1').addClass('alternate');
		$('.continue-block.alternate').css('display','none');
		$('.post-continue-nav-item a.index-0').removeClass('index-0').addClass('current');
		$('.post-continue-nav-item a.index-1').removeClass('index-1').addClass('alternate');
		$('.post-continue-nav').append('<li><a name="comments"></a></li>');
	}
	else {
		$('.continue .continue-block.index-0').removeClass('index-0').addClass('alternate');
		$('.continue .continue-block.index-1').removeClass('index-1').addClass('current');
		$('.continue-block.alternate').css('display','none');
		$('.post-continue-nav-item a.index-0').removeClass('index-0').addClass('alternate');
		$('.post-continue-nav-item a.index-1').removeClass('index-1').addClass('current');
		$('.post-continue-nav').append('<li><a name="comments"></a></li>');
	}
	
	postContinueAction('post-continue-nav-item');
	
	postCommentLinks('.go-to-comments','comments');
	postCommentLinks('.go-to-leave-comment','leave-comment');
}

function createNav(selector,className) {
	var nav = '<ul class="post-continue-nav">';
	$(selector).each(function(index) {
		$(this).parent('section').addClass('index-' + index);
		nav += "<li class='" + className + "'><a class='index-" + index + "' href='#anchor" + index + "'>" + $(this).text() + "</a></li>";
	});
	nav += "</ul>";
	return nav;
}

function hideTitles(selector) {
	$(selector).css('display','none');
}

function postContinueAction(className) {
	$('.' + className + ' a').live('click',function(event) {
		if($(this).hasClass('alternate')) {
			$('.'+ className + ' .current').removeClass('current').addClass('alternate');
			$(this).removeClass('alternate').addClass('current');
			$('.continue-block.current').fadeOut('fast',function() {
				$('.continue-block.alternate').fadeIn('fast',function() {
					$(this).removeClass('alternate').addClass('current');
				});
				$(this).removeClass('current').addClass('alternate');
			});
		}
	});
}

function postCommentLinks(linkClass,anchor) {
	$(linkClass).click(function(event) {
		if ($('.post-continue-nav-item:nth-child(2) a').hasClass('alternate')) {
			event.preventDefault();
			$('.post-continue-nav-item:nth-child(2) a').click();
			window.setTimeout(function() {
				location.href = "#" + anchor;
			}, 500);
		}
	});
}




function imgLink() {
	$('a img').parent('a').css({
		'border-bottom': 'none',
		'background-color': 'transparent'
	});
}


function fixTangerine() {
	var tangerines = $('.page-title,.home-header,.hire-title a,.leave-comment-title a,.post-title a,.more,.next-page-title a');
		
	tangerines.each(function() {
		var $this = $(this);
		var text = $this.text();
		var textChar = text.charAt(0);
		
		switch (textChar.toLowerCase()) {  
		   case "f":  
		      $this.text("\u00a0\u00a0" + text);
		      break;
		   case "g":  
		      $this.text("\u00a0" + text);    
		      break;
		   case "j":  
		      $this.text("\u00a0\u00a0" + text);    
		      break;
		   case "p":  
		      $this.text("\u00a0" + text);  
		      break;
		   case "s":  
		      $this.text("\u00a0" + text);    
		      break;
		   case "x":  
		      $this.text("\u00a0" + text);    
		      break;
		   case "y":  
		      $this.text("\u00a0\u00a0" + text);    
		      break;   
		   default:  
		      break;
		} 
	});
}




// Photo Slideshow
function photoSlide() {
	var photoIndex = getHash();
	var liNum = $('.photoslide li').length;

	photoSlideInit(liNum);
	photoSlideFirst(photoIndex,liNum);
	photoSlideAction(liNum);
}


function photoSlideInit(liNum) {
	$('.photoslide li').addClass("photoSlideInit");
	$('.photoslide').addClass('photoSlideWrapper').prepend('<div class="photoSlideLeft"><a href="">Previous</a></div><div class="photoSlideRight"><a href="">Next</a></div>').append('<div class="photoSlideLeft"><a href="">Previous</a></div><div class="photoSlideRight"><a href="">Next</a></div><div class="clear"></div>');
	$('.photoslide p').remove();
}

function photoSlideFirst(photoIndex,liNum) {

	if(photoIndex <= liNum) {
		photoEq = photoIndex-1;
		$('.photoslide li:eq(' + photoEq + ')').addClass('photoSlideCurrent');
		photoSlideHref(photoIndex,liNum);
	}
	else {
		$('.photoslide li:eq(0)').addClass('photoSlideCurrent');
		photoSlideHref(photoIndex,liNum);
	}
}

function photoSlideHref(photoIndex,liNum) {
	photoRight = photoIndex+1;
	photoLeft = photoIndex-1;
	//$('.photoSlideLeft a').attr('href','#' + photoLeft);
	//$('.photoSlideRight a').attr('href','#' + photoRight);
	if(photoIndex <= 1) {
		$('.photoSlideLeft a').attr('href','#' + liNum);
	}
	else {
		$('.photoSlideLeft a').attr('href','#' + photoLeft);
	}

	if(photoIndex >= liNum) {
		$('.photoSlideRight a').attr('href','#1');
	}
	else {
		$('.photoSlideRight a').attr('href','#' + photoRight);
	}
}

function photoSlideAction(liNum) {
	var photoSet = $('.photoslide li');
	var newHash;


	$('.photoSlideLeft a').click(function(event) {
		event.preventDefault();
		var photoCurrent = $('.photoslide .photoSlideCurrent');
		photoIndex = photoSet.index($(".photoSlideCurrent"));
		if (photoIndex <= 0) {
			newHash = liNum;
			window.location.hash = "#" + newHash;
			photoCurrent.stop(true,true).fadeOut('fast',function() {
				photoCurrent.removeClass('photoSlideCurrent');
				photoIndex = liNum - 1;
				$('.photoslide li:eq(' + photoIndex + ')').addClass('photoSlideCurrent').fadeIn('fast');
				photoIndex = liNum;
				photoSlideHref(photoIndex,liNum);
			});
		}
		else {
			newHash = photoIndex;
			window.location.hash = "#" + newHash;
			photoCurrent.stop(true,true).fadeOut('fast',function() {
				photoCurrent.removeClass('photoSlideCurrent');
				photoIndex--;
				$('.photoslide li:eq(' + photoIndex + ')').addClass('photoSlideCurrent').fadeIn('fast');
				photoIndex++;
				photoSlideHref(photoIndex,liNum);
			});
		}
	});
	$('.photoSlideRight a').click(function(event) {
		event.preventDefault();
		var photoCurrent = $('.photoslide .photoSlideCurrent');
		photoIndex = photoSet.index($('.photoSlideCurrent'));
		photoIndex++;
		if (photoIndex >=liNum) {
			newHash = 1;
			window.location.hash = "#" + newHash;
			photoCurrent.stop(true,true).fadeOut('fast',function() {
				photoCurrent.removeClass('photoSlideCurrent');
				$('.photoslide li:eq(0)').addClass('photoSlideCurrent').fadeIn('fast');
				photoIndex = 1;
				photoSlideHref(photoIndex,liNum);
			});
		}
		else {
			newHash = photoIndex + 1;
			window.location.hash = "#" + newHash;
			photoCurrent.stop(true,true).fadeOut('fast',function() {
				photoCurrent.removeClass('photoSlideCurrent');
				$('.photoslide li:eq(' + photoIndex + ')').addClass('photoSlideCurrent').fadeIn('fast');
				photoIndex++;
				photoSlideHref(photoIndex,liNum);
			});
		}
	});
}

function getHash() {
	var hash = "";
	hash = window.location.hash;
	hash = hash.substr(1);
	return parseInt(hash,10);
}






// Slide to hide/unhide functionality for forms
function formHide(regcount) {
	if ($('.hide-control-' + regcount).text() == "hide") {
		$('.formHide.regcount-' + regcount).css('display','none');
	}
	
	$('.formHideControl a.regcount-' + regcount).click(function(event) {
		event.preventDefault();
		$('.formHide').not('.formHide.regcount-' + regcount).slideUp('fast');
		$('.formHide.regcount-' + regcount).slideToggle('fast');
	});
}

function couponHide(regcount) {
  if ($('#prepurchaseyes-' + regcount).attr('checked') != true) {
    $('.couponselect-' + regcount).css('display','none');
  }
  if ($('#prepurchaseno-' + regcount).attr('checked') != true) {
    $('.coupondeselect-' + regcount).css('display','none');
  }
  
  $('#prepurchaseyes-' + regcount).click(function() {
    $('.coupondeselect-' + regcount).slideUp('fast',function() {
      $('.couponselect-' + regcount).slideDown('fast');
    });
  });
  $('#prepurchaseno-' + regcount).click(function() {
    $('.couponselect-' + regcount).slideUp('fast',function() {
      $('.coupondeselect-' + regcount).slideDown('fast');
    });
  });
}






// Client side validation for forms
function validateForm(regcount) {
	validateInit('form-tip');
	//$('input').after('<span class="form-tip" style="display: none;"></span>');
	//$('textarea').after('<span class="form-tip" style="display: none; vertical-align: top;"></span>');

	validateInput('#mb_name-' + regcount,'your name'); //Name field
	validateInput('#email-' + regcount,'so i can contact you; it will not be shared.'); //Email field
	validateInput('#phone-' + regcount,'so i can contact you'); //Phone field
	validateInput('#time-' + regcount,'choose your class time'); //Time field
	validateInput('#subject-' + regcount,'email subject'); //Subject field
	validateInput('#regcomments-' + regcount,"anything else you'd like for me to know?"); //Comments field
	validateInput('#message-' + regcount,"what's on your mind?"); //Message field
}

function validateInit(identifier) {
	$('input').after('<span class="' + identifier + '" style="display: none;"></span>');
	$('textarea').after('<span class="textarea-tip ' + identifier + '" style="display: none; vertical-align: top;"></span>');
	$('select').after('<span class="' + identifier + '" style="display: none;"></span>');
}

function validateInput(input,tip) {
	var inputTip = $(input + ' + .form-tip');
	input = $(input);

	input.focus(function() {
		if(inputTip.hasClass('require')) {
			inputTip.stop(true, true).fadeOut('fast', function() {
				$(this).removeClass('require').addClass('tip').html(tip).fadeIn('fast');
			});
		}
		else {
			inputTip.stop(true, true).addClass('tip').html(tip).fadeIn('fast');
		}
	});

	input.blur(function() {
		if (input.hasClass('required') && input.val() == "") {
			inputTip.stop(false, true).fadeOut('fast', function() {
				$(this).removeClass('tip').addClass('require').html('required').fadeIn('fast');
			});
		}
		else {
			inputTip.stop(false, true).fadeOut('fast');
		}
	});
}




function searchShowInit() {
  $('.search').css('display','none');
}

function searchShowClick() {
  $('.searchIcon').click(function(event) {
    event.preventDefault();
    if($(this).hasClass('active')) {
      $('.search').slideUp('fast');
      $(this).removeClass('active');      
    }
    else {
      $('.search').slideDown('fast');
      $(this).addClass('active');
    }
  });
}

function searchShow() {
  searchShowInit();
  searchShowClick();
}
