//fix for ie7+ jquery ajax handler
$.ajaxSetup({
	xhr: function(){
		if ($.browser.msie) {
			return new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			return new XMLHttpRequest();
		}
	}
});


function randomTestimonialIndex(num, currentIndex)
{
	var randomIndex = currentIndex;
	if(num > 1)
	{
		while(randomIndex == currentIndex)
		{
			randomIndex = Math.floor(Math.random() * num);
			
		}
	}
	
	return randomIndex;
}

function changeTestimonial(testimonial)
{
	var sideBarContent = $('#sidebar_testimonial p a');

	sideBarContent.fadeOut('slow', function(){
		if(testimonial.exerpt.length > 0) {
			sideBarContent.find('span.text').html(testimonial.exerpt);
		}else{
			sideBarContent.find('span.text').html(testimonial.testimonial);
		}

		sideBarContent.find('span.customer').html(' By: ' + testimonial.customer_name);
		sideBarContent.find('span.location').html(' from ' + testimonial.customer_location);

		$(this).fadeIn('slow');
	});
}

function showMessage(message){
  $('#caption_1').html(message);
  $('#freeShipping p').html(message);
}

var itemData = ["Now! Everything Ships Free" , "Scroll Down To Begin Shopping", "Free Shipping on Everything!" ];
var i = 0;
var interval = 5000;

function idTimer(list, i) {
  if (!(i >= 0)) {
     i= 0;
  }
  setTimeout((function(msg){
    if (i == (list.length - 1)){
        i = 0;
    } else {
        i++;
    }
    return function(){
      if(i < list.length){
        idTimer(list, i);
      }
      showMessage(msg);
    }
  })(list[i]), interval);
}

$(document).ready(function()
{

	var keyCodeEnter = 13;

    $('#caption_1').html(itemData[0]);
    $('#freeShipping p').html(itemData[0]);
    idTimer(itemData);

    //search
    $('#g_search_submit').click(function() {
        var query = $('#g_search').val();
        if (query.length == 0) {
            return false;
        }
        else {
            _gaq.push(['_trackPageview', '/search/?query=' + query]);
        }
    });
    $('#g_search').keyup(function(e) {
        if (e.keyCode == keyCodeEnter) {
            var query = $('#g_search').val();
            _gaq.push(['_trackPageview', '/search/?query=' + query]);
        }
    });
    $('#c_search_submit').click(function() {
        var query = $('#c_search').val();
        if (query.length == 0) {
            return false;
        }
        else {
            _gaq.push(['_trackPageview', '/search/?query=' + query]);
        }
    });
    $('#c_search').keyup(function(e) {
        if (e.keyCode == keyCodeEnter) {
            var query = $('#g_search').val();
            _gaq.push(['_trackPageview', '/search/?query=' + query]);
        }
    });

    //email
    $('#emailBox, #from').focus(function() {
        if ($(this).val().toLowerCase() == 'your email address') {
            $(this).val('');
            $(this).css('color', 'black');
        }
    });
    $('#emailBox, #from').blur(function() {
        if ($(this).val() == '') {
            $(this).val('your email address');
            $(this).css('color', 'gray');
        }
    });
    $('#emailButton').click(function() {
        var query = $('#emailBox').val();
        if (query != 'your email address') {
            alert('sign up!');
        }
    });

    //   Position Reverse Captch Outside Screen (Hidden for now!)
    $('input.spam').each(function() {
        $(this).parent().css('display', 'none');
        $(this).parent().prev().css('display', 'none');
    });


	/***** disables submit button of all forms on page after first click *****/
	$('form').submit(function(){
		// On submit disable its submit button
        var button = $('input[type=submit]', this);
        var prevText = button.val();

        button.attr('disabled', 'disabled');
        if (button.attr('id') != 'g_search_submit' && button.attr('id') != 'c_search_submit') {
            button.val('Submitting...');
        }
        else {
            button.attr('style', 'font-size: 0.8em;');
            button.val('Searching');
        }
        window.setTimeout(function() {
            button.removeAttr('disabled').val(prevText);
        }, 3000);
	});

	
	/***** Testimonials ****/
	//TODO:: put these in a class
	var testimonialIndex = -1;
	//only do something if there are testimonials
	if ((typeof(testimonials) != 'undefined') && (testimonials.length > 0))
	{
		//if therre is only 1 just show it and do nothing else
		if(testimonials.length == 1)
		{
			changeTestimonial(testimonials[0]);
		}
		else if(testimonials.length > 1)
		{
			// randomize the initial testimonial
			testimonialIndex = randomTestimonialIndex(testimonials.length, testimonialIndex);
			changeTestimonial(testimonials[testimonialIndex]);

			//set a interval for testimonials to cycle through
			setInterval(function(){
				testimonialIndex = randomTestimonialIndex(testimonials.length, testimonialIndex);
				changeTestimonial(testimonials[testimonialIndex]);
			}, 20000);
		}
	}


});

