// iPhone Listeners
	
window.addEventListener('load', setOrientation, false); 
window.addEventListener('orientationchange', setOrientation, false);

function setOrientation() {
  var orient = Math.abs(window.orientation) === 90 ? 'landscape' : 'portrait'; 
  var cl = document.body.className; 
  cl = cl.replace(/portrait|landscape/, orient);
  document.body.className = cl; 
}

window.addEventListener('load', function(){
	if (location.hash.length == 0) {
		setTimeout(scrollTo, 0, 0, 1);
	}
}, false);

// jQuery functions

// Slider script

slidesLength = $(".slides").length;
swipeInd = 0;
slideIndex = 0;

// Slideshow function

function slideShow() {
	var devWidth = $('#slider').width();
	var listPos = parseInt($('#slide_list').css('left'));
	var slidesLength = $(".slides").length;
	slideIndex = (listPos/devWidth)*(-1);
	slideIndex = slideIndex+1;
	if ( slideIndex == slidesLength ) {
		slideIndex = 0;
	}
	slideSwitcher(slideIndex);
	setTimeout(slideShow,5000);
}
	
// Slide switcher
	
function slideSwitcher(slideIndex) {
	var devWidth = $('#slider').width();
	var slidesLength = $('.slides').length;
	var listPos = parseInt($('#slide_list').css('left'));
	if ( slideIndex >= slidesLength ) {
		slideIndex = slidesLength - 1;
		$('#slide_list').animate({
			left:listPos-(devWidth/8)
		},150, function(){
			$(this).animate({
				left:listPos
			},150, function(){
				swipeInd = 0;
			})
		});
		return false;
	} else if ( slideIndex < 0 ) {
		slideIndex = 1;
		$('#slide_list').animate({
			left:listPos+(devWidth/8)
		},150, function(){
			$(this).animate({
				left:listPos
			},150, function(){
				swipeInd = 0;
			})
		});
		return false;
	}
	
	listPos = (devWidth*(slideIndex))*(-1);
	$("#slider_navigation li").removeAttr("class").eq(slideIndex).addClass("current");			
	$('#slide_list').animate({
		left:listPos
	},500,'easeOutExpo', function(){
		swipeInd = 0;
	});
	
}

// Swipe handler

function swipe(event, direction) {

	if ( swipeInd == 0 ) {
		
		swipeInd = 1;
		
		var devWidth = $('#slider').width();
		var slidesLength = $('.slides').length;
		var listPos = parseInt($('#slide_list').css('left'));
		slideIndex = (listPos/devWidth)*(-1);
		
		if ( direction == "left" ) {					
			slideIndex = slideIndex+1;
			slideSwitcher(slideIndex);
		}
		if ( direction == "right" ) {
			slideIndex = slideIndex-1;
			slideSwitcher(slideIndex);
		}
	}
	
}

swipeOptions = {
	swipe:swipe,
	threshold:80,
	allowPageScroll:'vertical'
}

$(document).ready(function(){

	// Slide_list resizing
	
	if ( $('#slider').length > 0 ) {
		var devWidth = $('#slider').width();
		var slidesLength = $('.slides').length;
		$('#slide_list').width(devWidth*slidesLength);
	}
	
	// Login popup
	$('#login_link').toggle(function(){
		$('#login_window').show();
	}, function(){
		$('#login_window').hide();
	});
	
	// Navigation popup
	$('#main_navigation_button').toggle(function(){
		$('#main_navigation .menu').show();
	}, function(){
		$('#main_navigation .menu').hide();
	});
	
	// Back2Top 4 iPhone
	$("#back2top").click(function(){
		setTimeout(scrollTo, 0, 0, 1);
	});
	
	// Form reset function
	
	function formReset() {
		window.location.reload();
	}
	
	// Forms validation script
	
	function sendMail(obj,ajaxurl) {
		var form = obj.parent().parent().parent();
		if (form.find(":input:not(button)[value=]:not").length > 0 ) {
			emptyInputName = form.find(":input[value=]:first").attr("name");
			missingText = form.find("label[for='" + emptyInputName + "']").text();
			form.find(".form_alert")
				.css("opacity","0")
				.show()
				.html("Type in your <strong>" + missingText + "</strong>")
				.animate({
					opacity:1
				},200);
			return false;
		}
		dataString = form.serialize() + '&action=ajax_sendmail';
		$.post(ajaxurl, dataString, function(response) {
			form.find("fieldset").animate({
				opacity:0.1
			},500,function(){
				$(this).find(".form_alert").replaceWith("");
				if($(this).parent().find(".success").length == 0) {
					$(this).parent().prepend(response);
				}
				$(this).parent()
						.find(".success")
						.hide()
						.css("opacity","0")
						.show()
						.animate({
							opacity:1
						}, 500, function(){
							var formTimeout = setTimeout(formReset,2000);
						});
			});
		});
	}
	
	$(".form_submit").click(function(){
		ajaxurl = $(this).parent().parent().parent().attr('action');
		sendMail($(this),ajaxurl);
		return false;
	});
	
	$("form :input:not(button)").focus(function(){
		$(this).parent().parent().find(".form_alert").animate({
			opacity:0
		},200,function(){
			$(this).hide();
		});
	});
	
	// Slider interactions 
	
	if ($("#slider").length > 0) {
	
		$(function() {			
			$("#slider").swipe( swipeOptions );	
		});
		
		$("#slider_controls .next").click(function(){
			var devWidth = $('#slider').width();
			var listPos = parseInt($('#slide_list').css('left'));
			slideIndex = (listPos/devWidth)*(-1);
			slideIndex = slideIndex+1;
			slideSwitcher(slideIndex);
			return false;
		});
		
		$("#slider_controls .prev").click(function(){
			var devWidth = $('#slider').width();
			var listPos = parseInt($('#slide_list').css('left'));
			slideIndex = (listPos/devWidth)*(-1);
			slideIndex = slideIndex-1;
			slideSwitcher(slideIndex);
			return false;
		});
		
		$("#slider_navigation li a").click(function(){
			slideIndex = $(this).parent().index();
			slideSwitcher(slideIndex);
			return false;
		});
		
	}
	
});
