/* MAIN NAV */
var ul_width = 0;
var tweetCount = 0;
var t;
$(document).ready(function()
{	
	
	
	$('input:text').bind('focus',function()
	{
		$(this).val('');
		
	});
	
	$('#main_nav ul li.has_sub_nav').each(function()
	{
		ul_width = 0;
		$('ul', this).css('display', 'block');	
		$('.sub_nav li',this).each(function()
		{
			if($.browser.msie){
				ul_width += $(this).width() + 20;
			}else{
				ul_width += $(this).width() + 16;
			}
		});
		
			$('ul', this).width(ul_width+8);
		
		$('ul', this).css('display', 'none');
	});


	$('#main_nav ul li.has_sub_nav').bind('mouseenter',function()
	{
		$('ul', this).css('display', 'block');
		
	});
	
	$('#main_nav ul li.has_sub_nav').bind('mouseleave', function()
	{
		$('ul', this).delay('2000').hide();
		
	});
	
	$('.previous').css('visibility','hidden');
	$('.previous').click(function(){
		
		tweetCount--;
		
		$('#tweetbox').animate({
    		'left' : "+=230px"
		});
		
		if(tweetCount==0){
			$('.previous').css('visibility','hidden');
		}
		if(tweetCount<9){
			$('.next').css('visibility','visible');
		}
	});
	
	$('.next').click(function(){
		
		tweetCount++;
		
		$('#tweetbox').animate({
    			'left' : "-=230px"
			});
		
		if(tweetCount==9){
			$('.next').css('visibility','hidden');

		}
		if(tweetCount>0){
			$('.previous').css('visibility','visible');
		}
	});
	
	$("#customForm").validate({
		errorLabelContainer: $(".error_ls"),
		wrapper:"li",
        rules: {
          firstname_field: {required:true },
          lastname_field: {required:true },
          city_field: { required: true },
          adress_field: { required: true },
          province: { required: true },
          zip_field: { required: true, maxlength: 3 },
          zip2_field: { required: true, maxlength: 3 },
          phone_field: { required: true, maxlength: 3 },
          phone2_field: { required: true, maxlength: 3 },
          phone3_field: { required: true, maxlength: 4 },
          email_field:{required: true, email: true},
          email_conf_field:{equalTo: "#email_field"},
          question_field:{required: true},
          confirm_field:{required: true},
          rules_field:{required: true},
          recaptcha_response_field:{required:true}
        }
    });
   
	$("#falseSubmit").click(function() {
		
		if($("#customForm").valid()){
			validateCaptcha();
			
		}else{
			window.scrollTo(0, $("#customForm").position().top);
			return false;
		}
	});
	
	$("html").scrollTop(0);
});

function validateCaptcha()
	{
		challengeField = $("input#recaptcha_challenge_field").val();
		responseField = $("input#recaptcha_response_field").val();
		//console.log(challengeField);
		//console.log(responseField);
		//return false;
		var call = $.ajax({
			type: "POST",
			url: "../test/",
			data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
			async: false
			
		});

		html = call.responseText;
		
		//console.log( html );
		if(html == "success") {
			//Add the Action to the Form
			//$("form").attr("action", "http://action/to/the/form_handler");
			//Indicate a Successful Captcha
	
			// Uncomment the following line in your application
			$("#customForm").submit();
		} else {
			Recaptcha.reload();
			return false;
		}
	} 

function show_img(src,txt) 
{
    $('#photo_show').attr('src',src);
    $('#photo_desc').html('<br/>'+txt);
}

function parseTwitterDate(text) {
var newtext = text.replace(/(\+\S+) (.*)/, '$2 $1')
var date = new Date(Date.parse(newtext)).toLocaleDateString();
var time = new Date(Date.parse(newtext)).toLocaleTimeString();
return date + ' - ' + time;
}

/* PRELOAD IMAGES */
function preload(arrayOfImages) 
{
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
    });
}

/* Preload every mouse hover animated gifs */
preload([
	'images/bt_arrow_pink.png'
]);
