//object pages for tracking content scrolling
var pages = {
	names: new Array("home", "resources", "tech", "customers", "contact"),
	current_page: "home",
	prev_page: "contact",
	next_page: "resources",
	
	scrollPrev: function() {
		this.scrollPage(this.prev_page);
	},
	
	scrollNext: function() {
		this.scrollPage(this.next_page);
	},
	
	scrollPage: function(page) {
		$('#content').scrollTo("#" + page, 500, {onAfter: this.setSelectedTile(page)});
		this.updateScrollPages(page);
	},
	
	setSelectedTile: function(page) {
		$("div.marker").hide();
		$("a."+page+" + div.marker").show();
	},
	
	updateScrollPages: function(page) {
		if (page == this.names[this.names.length - 1])
			{
				this.prev_page = this.names[this.names.length - 2];
				this.current_page = this.names[this.names.length - 1];
				this.next_page = this.names[0];
			}
		else if (page == this.names[0])
			{
				this.prev_page = this.names[this.names.length - 1];
				this.current_page = this.names[0];
				this.next_page = this.names[1];
			}
		else
			{
				//index = this.names.indexOf(page);
				index = getPosition(this.names,page);
				this.prev_page = this.names[index - 1];
				this.current_page = this.names[index];
				this.next_page = this.names[index + 1];				
			}	
	}
};


//scroll functions for left/right arrows
function scrollPrev() {	pages.scrollPrev(); }
function scrollNext() {	pages.scrollNext(); }
function scrollPage(page) {	pages.scrollPage(page); }

//contact form funtions
function submitForm()
{
	hideErrorMessages();
	
	if (validateFields())
		{
			var formData = $('#contact_form').serialize();

			jQuery.post('contact.php', formData, function(data) {
				
				$('#form_guide').fadeOut();

				$('#contact_form').fadeOut('400',
					function() {
						$('#contact_form').html(data);
						$('#contact_form').fadeIn();
					});
				});
		}
}

function validateFields()
{
	var name =		$("input#name").val();
	var phone =		$("input#phone").val();
	var email =		$("input#email").val();
	var comments =	$("input#comments").val();
	
	var errors = false;
	
	if (name == "") { $('#name_error').fadeIn('slowly'); errors = true; }
	
	if ((phone == "") && (email == "")) { $('#contact_error').fadeIn('slowly'); errors = true; }
	
	if (comments == "") { $('#message_error').fadeIn('slowly'); errors = true; }
	
	if (errors)
		{
			return false;
		}
	else
		{
			return true;
		}
}

function hideErrorMessages()
{
	$('#message_error').fadeOut();
	
	$('#contact_error').fadeOut();
	
	$('#name_error').fadeOut();
}

function getPosition(arrayName,arrayItem)
{
    for(var i=0;i<arrayName.length;i++)
	{
       if(arrayName[i]==arrayItem)
            return i;
    }
}

//rollover function for <img> use class="rollover"
$(document).ready(function() {
  $("img.rollover").hover(function() {
    $(this).attr("src", $(this).attr("src").split(".").join("-hover."));
  }, function() {
    $(this).attr("src", $(this).attr("src").split("-hover.").join("."));
  });
});

//setup scroll content panels
$(document).ready(function() {
	$('#nav_bar').localScroll({
		target: '#content',
		duration: 500,
		axis:'x'
	});

	//determine if #hash is in effect in url
	var url = document.location.href;
	var hash = null;

/*
	if (url.indexOf('#') > 0)
		{ hash = url.slice(url.indexOf('#') + 1); }

	//set pages.current_page (default: home, otherwise #hash value)
	if (hash)
		{ pages.scrollPage(hash); }
	else
		{ pages.scrollPage('home'); }
		
*/	
		
	pages.scrollPage('home');		
		
});
