// HOME PAGE SLIDESHOW
$(document).ready(function(){
    $('#slideshow').cycle({
		fx: 'fade',
		pause: true,
		timeout: 5000
	});
	
	//labelify SMS page
	$('.labelify').labelify();
	
	function state_checker(input){ // This is the pattern we're looking for: 1-XXX-XXX-XXXX
		var pattern = /^1\-\d{3}\-\d{3}\-\d{4}$/; // Grab the value of the input field passed as an argument.
		var str = $(input).val(); // This is the element that indicates the current state of the validation. This example uses a check/x sprite.
		var state = $('span.state');
		
		if (str.length > 14){ // If the string is longer than a standard phone number...
			str = str.substr(0,14); // ... shorten it and update the string variable...
			$(input).val(str); // ... and update the input field.
		}
		
		if (pattern.test(str)){ // Test the string against the regular expression. Will return true or false. If true...
			state.addClass('valid'); // Switch the x to a check via CSS.
			$('input[type="submit"]').attr('disabled', ''); // Enable the submit button.
		} else {
			state.removeClass('valid'); // If false, keep x visible (or switch check if for some reason it's visible).
			$('input[type="submit"]').attr('disabled', 'disabled'); // Disable the submit button.
		}
	}
	
	$('input[name="phone_number"]').keyup(function(){ // Run this function every time a keyboard button is pressed. Keyup is more reliable that keydown or keypress.
		state_checker(this);
	});
	
});

// ROUNDED CORNERS
$(document).ready(function(){
	$('#bottom .block').add('.home-block').corner();
});

// SCROLLER / MARQUEE
$(document).ready(function(){
  $jScroller.add("#scroller-container","#scroller","left",5,true);
  $jScroller.start();
});
$(document).ready(function(){
	ticker = $('#scroller').html();
	$('#scroller').append(ticker,ticker,ticker,ticker,ticker,ticker,ticker,ticker,ticker,ticker);
	$('#scroller span').blink();
});

// CALCULATOR
function exchange(id){
	var inputVal = jQuery('#'+id).val().replace(',','')*1;
	if(isNaN(inputVal) || inputVal === undefined || inputVal === null){
		inputVal = 0;
	}
	exchangeBase = inputVal;
	jQuery('.currencyOutput').each(function(){
		var myRate = jQuery(this).attr('r');
		var myVal = exchangeBase*myRate;
		jQuery(this).text(myVal.toFixed(4));
	});
}

jQuery(document).ready(function(){
	jQuery('#currencyInput').keyup(function(){
		exchange('currencyInput');
	});
});

// BULLETINS PAGE
$(document).ready(function(){
	$('.scrollable').scrollable({
		circular: true
	});
});

// LINKS PAGE  EXPAND COLLAPSE
$(document).ready(function() {

	// $("#faq div.answer").hide();
	$(".answer").hide();
	$("div.question").click(function() {
	
		// Get associated div
		var answerDiv = $(this).next('div.answer');
		
		if($(answerDiv).is(':hidden')){
			//alert('hidden');
			$(".answer:visible").slideUp({
				duration: 400,
				easing: 'easeInOutSine'});
			$(answerDiv).slideDown({
				duration: 400,
				easing: 'easeInOutSine'});						
		}else{
			$(answerDiv).slideUp({
			duration: 400, 
			easing: 'easeInOutSine'});
			//alert('visable');		
		}				
											
	});

});

// TELL-A-FRIEND FORM
function addForm() {
	$('#dite-amie-form').ajaxForm(function() {
		$('#send-to-friend #success').slideDown();
	}); 
}

$(document).ready(function(){
	$('.boxy').boxy();
	$('.boxy').click(function(){
		setTimeout('addForm();',1000);
	});

	jQuery('#diteAmie').click(function(){
		setTimeout('diteAmie();',3000);
	});
	
	jQuery('#tellFriend').click(function(){
		setTimeout('tellFriend();',3000);
	});
});

function diteAmie(){
	$('#dite-amie-form').validationEngine({
		promptPosition: "topRight",
		ajaxSubmit: true,
		ajaxSubmitFile: "lib/dite-amie.php"
	});

}

function tellFriend(){
	$('#dite-amie-form').validationEngine({
		promptPosition: "topRight",
		ajaxSubmit: true,
		ajaxSubmitFile: "lib/tell-friend.php"
	});

}
