// Popup Menus



$(document).ready(function(){


	var currentPosition = 0;
	var slideWidth = 560;
	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>').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="controlStyle control" id="leftControl"><a href="#">Clicking moves left</a></span>')
	.append('<span class="controlStyle control" id="rightControl">Clicking moves right</span>');

	// Hide left arrow control on first load
	manageControls(currentPosition);



	// Create event listeners for .controls clicks
	$('.control').bind('click', function(){
		// Determine new position
		if ($(this).attr('rel')) {
			currentPosition = parseInt($(this).attr('rel')) - 1;
			animate = 0;
		} else {
			currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
			animate = 1;
		}
		
		// Hide / show controls
		manageControls(currentPosition);

		// Move slideInner using margin-left
		if (animate == 0) {
			$('#slideInner').css({
				'marginLeft' : slideWidth*(-currentPosition)
			}).fadeIn('normal');
		} else {
			$('#slideInner').animate({
				'marginLeft' : slideWidth*(-currentPosition)
			}, 200);
		}

		return false;
	});

	// Create event listeners for slidecontainer hover
	$('#rightControl,#leftControl,#slidesContainer').hover(function(){
		$('#rightControl').addClass('arrowRightOver');
		$('#leftControl').addClass('arrowLeftOver');
	}, function(){
		$('#rightControl').removeClass('arrowRightOver');
		$('#leftControl').removeClass('arrowLeftOver');
	});

	// manageControls: Hides and Shows controls depending on currentPosition
	function manageControls(position){
		if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() }
		if(position==numberOfSlides-1){ $('#rightControl').hide() } else{ $('#rightControl').show() }
		(currentPosition == 0) ? $("#rightControl").addClass("rightControlNext") : $("#rightControl").removeClass("rightControlNext")
	}
	

	//Email Obfuscation
	$(".replaceAt").replaceWith("@");
  	$(".obfuscate").each(function () {
  		$(this).attr("href", "mailto:"+$(this).text());
  	});


	//Form Default Values
	$(".contactform input[type=text], .contactform textarea").bind('focus', function(){
		var currentValue = $(this).attr("value");
		var defaultValue = $(this).attr("defaultValue");
		if (currentValue == defaultValue) $(this).attr({ value: ""});
	});
	$(".contactform input[type=text], .contactform textarea").bind('blur', function(){
		var currentValue = $(this).attr("value");
		var defaultValue = $(this).attr("defaultValue");
		if (currentValue == '') $(this).attr({ value: defaultValue});
	});
	


	$(".fancybox a").fancybox({
		'zoomSpeedIn': 300,
		'zoomSpeedOut': 300
	});
	

});
