$(document).ready(function() {

 var fadeDuration = 550; //time in milliseconds
 var menuElm = $('#slide_menu li').size();
 var menuSize = menuElm * 100;
 var menuSizeStart = 100-menuSize;
 $('#slide_menu').css({ marginLeft: -menuSize+100 });
 
      $('#slide_menu').hover(function() {
        $(this).animate({ marginLeft: 0 }, fadeDuration);
        $(this).children('span').show().animate({ left: -10 }, fadeDuration);
      }, function() {
        $(this).animate({ marginLeft: -menuSize+100 }, fadeDuration);
        $(this).children('span').animate({ left: -10 }, fadeDuration).fadeOut(fadeDuration);          
      });
	  
	  
  $("#slide_menu a").children('span').fadeTo("slow", 0.0); 

  $("#slide_menu a").hover(function(){
    $(this).children('span').fadeTo("slow", 1.0); // This should set the opacity to 100% on hover
  },function(){
    $(this).children('span').fadeTo("slow", 0.0); // This should set the opacity back to 60% on mouseout
});	  
    
  $(".portfolio_item img").hover(function(){
    $(this).parent().children('.description').fadeTo("slow", 1.0); // This should set the opacity to 100% on hover
  },function(){
    $(this).parent().children('.description').fadeTo("slow", 0.0); // This should set the opacity back to 60% on mouseout
});	  	
	
    /*var link = $(location).attr('href').split('#')[1];
	if(typeof(link) == 'undefined') { alert('aaa'); }
	else { alert(link); }*/
  
    $('a[rel*=facebox]').click(function(){
        var link = $(this).attr('href');

  	    $('.modal_window').slideDown(2000);
		
		if('#'+$('.active_page').attr('id')!=link) { 
		
		$('.active_page').slideUp(2000);
		$('.active_page').hide();
		$('.active_page').removeClass('active_page');
		
		$('div'+link).addClass('active_page');
        $('.active_page').slideDown(2000);
		$('.active_page').show();
       };		
     });

  var currentPosition = 0;
  var slideWidth = 500;
  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">Clicking moves left</span>')
    .append('<span class="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
	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() }
  }	

  $('a[rel="external"]').attr('target', '_blank'); 
  
});
