var playing = true;

function isTouchScreen() {
	touchOS = ('ontouchstart' in document.documentElement) ? true : false;
	return touchOS;	
}

function playSlideshow() {
	var api = $(".scrollable").data("scrollable");
	api.play();
	$("#play-button").addClass("active-button");
	$("#pause-button").removeClass("active-button");
}

function stopSlideshow(pause) {
	var api = $(".scrollable").data("scrollable");
	if (pause) {
		api.pause();
	}
	else {
		api.stop();
		playing = false;
		$("#play-button").removeClass("active-button");
		$("#pause-button").addClass("active-button");
	}
}

$.fn.handleSwipes = function() {
  return this.each(function() {
    var api = $(this).data("scrollable");

    api.getRoot().addSwipeEvents()
       .bind('swipeleft', function() {
		 stopSlideshow();
         //api.next();
		 return false;
       })
       .bind('swiperight', function() {
	     stopSlideshow();
         //api.prev();
		 return false;
       });
  });
};

function submitContactForm() {
	$(".error-message").text("");
	$(".error-message").hide();
	$.ajax({
		async: false,
		type: 'POST',
		url: 'submit-form.php',
		data:$("#contact-form").serialize(),
		success:function(data, textStatus, jqXHR) {
			if (data.success) {
				$(".success-message").fadeIn(300);
				$("#contact-form").css("visibility","hidden");
			}
			else {
				$(".error-message").text(data.error);
				$(".error-message").show();
			}
		},
		error: function(jqXHR, textStatus, errorThrown) {
			$(".error-message").text("An error occured while trying to send your message. Please try again later.");
			$(".error-message").show();
		}
	});
}

$(document).ready(function() {
	$(".scrollable").scrollable({
		speed: SLIDESHOW_SPEED,
		//keyboard: true,
		circular: true,
		mousewheel: false,
		onSeek: function() {
			//alert("aaA");
		}
	}).autoscroll({ 
		autoplay: false,
		autopause: false,
		interval: SLIDESHOW_INTERVAL
	}).handleSwipes();;
	
	
	$(".slideshow-item").mousedown(function() {
		stopSlideshow();
	});
	
	$(".slideshow-item, #arrow-left, #arrow-right").hover(function() {
		stopSlideshow(true);
	},function() {
		if (playing)
			playSlideshow();
	});
	
	$("#arrow-left").click(function() {
		stopSlideshow();
		var api = $(".scrollable").data("scrollable");
		api.prev();
	});
	
	$("#arrow-right").click(function() {
		stopSlideshow();
		var api = $(".scrollable").data("scrollable");
		api.next();
	});
		
	$("#play-button").click(function() {
		playing = true;
		playSlideshow();
	});
	
	$("#pause-button").click(function() {
		stopSlideshow();
	});
	
	$("#slideshow-arrows-wrapper, .slideshow-item").hover(function() {
	
		$("#slideshow-arrows-wrapper").stopTime("hide-captions");
		$("#slideshow-arrows-wrapper").oneTime(300,"show-captions",function() {
			$(".slideshow-item .caption").fadeIn(300);
		});
	
		
	},function() {
		$("#slideshow-arrows-wrapper").stopTime("show-captions");
		$("#slideshow-arrows-wrapper").oneTime(300,"hide-captions",function() {
			$(".slideshow-item .caption").fadeOut(300);
		});
		
	});
	
	playSlideshow();
	
	if (isTouchScreen()) {
		$(".scrollable").css("top","0px");
		$("#slideshow-arrows-wrapper").hide();
	}
	
	//enable form default values
	$("*[placeholder]").defaultValue();
	
	//form submission
	$("#contact-form").submit(function() {
		submitContactForm();
		return false;
	});
	$(".submit-button").click(function() {
		submitContactForm();
		return false;
	});
	
	
	// menu functionality
	
	$("#featured-work-menu, #logo-button").click(function() {
		$("#right-side").animate({
			scrollTop:0
		},500);
		$(this).addClass("active");
		$("#company-info-menu").removeClass("active");
	});
	
	$("#company-info-menu").click(function() {
		$("#right-side").animate({
			scrollTop:811
		},500);
		$(this).addClass("active");
		$("#featured-work-menu").removeClass("active");
	});
	
	//start from top
	$("#right-side").scrollTop(0);
	
	//scroll event to change menu item
	$("#right-side").scroll(function() {
		
		//IE7 fix
		if ($.browser.msie  && parseInt($.browser.version) == 7) {
			//topScroll = 534 + ($("#right-side").scrollTop() / 20);
			//$(".scrollable").css("top","-"+topScroll+"px");
			$(".scrollable").css("top","0px");
			$(".scrollable").css("top","-534px");
			$("#slideshow-arrows-wrapper").css("top","1px");
			$("#slideshow-arrows-wrapper").css("top","0px");
		}
	
		if ($(this).scrollTop() >= 700) {
			$("#featured-work-menu").removeClass("active");
			$("#company-info-menu").addClass("active");
		}
		else {
			$("#featured-work-menu").addClass("active");
			$("#company-info-menu").removeClass("active");
		}
	});
	
	
	//fix scroller issue on big screens
	height = $("#right-side").height()-$("#company-content").height()-54;
	if (height > 0 ) {
		$("#fix-spacer").css("height",height+"px");
	}
	
	//clear errors on reset
	$(".reset-button").click(function() {
		$(".error-message").text("");
		$(".error-message").hide();
	});
	
	//show form on click on "click here"
	$("#open-form-link").click(function() {
		if ($("#contact-form").css("visibility") == 'hidden') {
			$("#contact-form").css("opacity","0");
			$("#contact-form").css("visibility","visible");
			$("#contact-form").animate({
				opacity: 1
			},500);
		}
	});
});
