$(document).ready(function () {

	//transitions
	//for more transition, goto http://gsgd.co.uk/sandbox/jquery/easing/
	var style = 'easeOutExpo';
	var default_left = Math.round($('#menu li.selected').offset().left+50 - $('#menu').offset().left);
	var default_top = $('#menu li.selected').height()+5;

	//Set the default position and text for the tooltips
	$('#box').css({left: default_left, top: default_top});
	$('#box .head').html($('#menu li.selected').find('a').attr('rel'));				
	
	//if mouseover the menu item
	$('#menu li').hover(function () {
		
		left = Math.round($(this).offset().left+50 - $('#menu').offset().left);

		//Set it to current item position and text
		$('#box .head').html($(this).find('a').attr('rel'));
		$('#box').stop(false, true).animate({left: left},{duration:2500, easing: style});	

	
	//if user click on the menu
	}).click(function () {
		
		//reset the selected item
		$('#menu li').removeClass('selected');
		$('#menu li').addClass('not-selected');
		
		//select the current item
		$(this).addClass('selected');

	});
	
	//If the mouse leave the menu, reset the floating bar to the selected item
	$('#menu').mouseleave(function () {

		default_left = Math.round($('#menu li.selected').offset().left+50 - $('#menu').offset().left);

		//Set it back to default position and text
		$('#box .head').html($('#menu li.selected').find('a').attr('rel'));				
		$('#box').stop(false, true).animate({left: default_left},{duration:3500, easing: style});	
		
	});
	
	//FANCYBOX	
	$("a#theelastic").fancybox({
		'titleShow'		: true,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});
	/*$("a#portfolio").fancybox({
		'zoomOpacity'			: true,
		'overlayShow'			: false,
		'zoomSpeedIn'			: 500,
		'zoomSpeedOut'			: 500
	});*/
	
	//$("a.group").fancybox({
	//	'hideOnContentClick': false
	//});
	
	//CONTACT FORM
	$("#kontak").fancybox({
		'scrolling'		: 'no',
		'titleShow'		: true,
		'onStart'		: function() {
			$("#contact_error").hide();
		},
		'onClosed'		: function() {
			$("#contact_error").hide();
		}
	});
	
	$("#kontak-front").fancybox({
		'scrolling'		: 'no',
		'titleShow'		: true,
		'onStart'		: function() {
			$("#contact_error").hide();
		},
		'onClosed'		: function() {
			$("#contact_error").hide();
		}
	});
	
	$("#contact_form").bind("submit", function() {
		if ($("#contact_name").val().length < 1 || $("#contact_email").val().length < 1 || $("#contact_phone").val().length < 1) {
			$("#contact_error").show();
			$.fancybox.resize();
			return false;
		}
	
		$.fancybox.showActivity();
	
		$.ajax({
			type	: "POST",
			cache	: true,
			url		: "process_enquiry.php",
			data	: "contact_name=" + $("#contact_name").val() + "&contact_company=" + $("#contact_company").val() + "&contact_phone=" + $("#contact_phone").val()+ "&contact_email=" + $("#contact_email").val() + "&contact_enquiry=" + $("#contact_enquiry").val(),
			success: function(data) {
				$.fancybox(data);
			}
		});
	
		return false;
	});
	
	//HALAMAN CONTACT
	$('.error').hide();
	$("#kontak_form").bind("submit", function() {
		$('.error').hide();
		if ($("#contact_name").val().length < 1) {
			$("#contact_name_error").show();
			$("#contact_name").focus();
        	return false;
		}
		if ($("#contact_phone").val().length < 1) {
			$("#contact_phone_error").show();
			$("#contact_phone").focus();
        	return false;
		}
		if ($("#contact_email").val().length < 1) {
			$("#contact_email_error").show();
			$("#contact_email").focus();
        	return false;
		}
	
		$.fancybox.showActivity();
	
		$.ajax({
			type	: "POST",
			cache	: true,
			url		: "process_enquiry.php",
			data		: $(this).serializeArray(),
			success: function(data) {
				$.fancybox(data);
			}
		});
	
		return false;
	});
	
	//BROCHURE PARTNER
	$("#brochure_partner").fancybox({
		'scrolling'		: 'no',
		'titleShow'		: true,
		'onStart'		: function() {
			$("#brochure_error").hide();
		},
		'onClosed'		: function() {
			$("#brochure_error").hide();
		}
	});
	
	$("#brochure_form").bind("submit", function() {
		if ($("#brochure_name").val().length < 1 || $("#job_title").val().length < 1 || $("#phone").val().length < 1 || $("#email").val().length < 1 || $("#business_name").val().length < 1 || $("#business_website").val().length < 1) {
			$("#brochure_error").show();
			$.fancybox.resize();
			return false;
		}
	
		$.fancybox.showActivity();
	
		$.ajax({
			type	: "POST",
			cache	: false,
			url		: "process_brochure.php",
			data	: $(this).serializeArray(),
			success: function(data) {
				$.fancybox(data);
			}
		});
	
		return false;
	});
	
	//SERVICES SLIDER
	  var currentPosition = 0;
	  var slideWidth = 660;
	  var slides = $('.slide');
	  var numberOfSlides = slides.length;
	
	  // Remove scrollbar in JS
	  $('#slidesContainer').css('overflow', 'hidden');
	
	  // Wrap all .slides with #slideInner div
	  slides
		.wrapAll('<div id="slideInner"></div>')
		// Float left to display horizontally, readjust .slides width
		.css({
		  'float' : 'left',
		  'width' : slideWidth
		});
	
	  // Set #slideInner width equal to total width of all slides
	  $('#slideInner').css('width', slideWidth * numberOfSlides);
	
	  // Insert controls in the DOM
	  $('#slideshow')
		.prepend('<span class="control" id="leftControl">Klik geser ke kiri</span>')
		.append('<span class="control" id="rightControl">Klik geser ke kanan</span>');
	
	  // Hide left arrow control on first load
	  manageControls(currentPosition);
	
	  // Create event listeners for .controls clicks
	  $('.control')
		.bind('click', function(){
		// Determine new position
		currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
		
		// Hide / show controls
		manageControls(currentPosition);
		// Move slideInner using margin-left
		$('#slideInner').animate({
		  'marginLeft' : slideWidth*(-currentPosition)
		});
	  });
	
	  // manageControls: Hides and Shows controls depending on currentPosition
	  function manageControls(position){
		// Hide left arrow if position is first slide
		if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() }
		// Hide right arrow if position is last slide
		if(position==numberOfSlides-1){ $('#rightControl').hide() } else{ $('#rightControl').show() }
	  }
});