$(function() {

	// Start of JQuery Code

  // ================
  // = Form Styling =
  // ================
  
  $("input[type=file]").filestyle({ 
       image: "/images/site_images/choose_file.png",
       imageheight : 16,
       imagewidth : 72,
       width : 100
   }).addClass('styled');
  
  // ==================
  // = Search Effects =
  // ==================
  
  $('.search input.text').attr("readonly", true);
  
  $('.search input.text').focus(function(){ 
   if($(this).attr('readonly') == true){
     $(this).val(''); 
     $(this).removeClass("readonly");
          $(this).removeAttr("readonly");
   }
  });
  
  
  // ============
  // = User Nav =
  // ============
  
  $('#user_nav .wrapper').css({left:93, top:40});
  
  // $('#user_nav .login_button').toggle(function(){
  //   $('#user_nav .wrapper').animate({top:0, left:0}, 1000);
  // },function(){
  //   $('#user_nav .wrapper').animate({top:40, left:93}, 1000);
  // });
  
  $('#user_nav .login_button').toggle(function(){
   $('#user_nav .wrapper').animate({top:0, left:133}, 500);
   $('#user_nav .wrapper').animate({top:0, left:0}, 500);
  },function(){
   $('#user_nav .wrapper').animate({top:0, left:133}, 500);
   $('#user_nav .wrapper').animate({top:40, left:93}, 500);
  });


  // =================
	// = Portfolio bar =
	// =================
	
	$('#scroll_nav ul').jScrollPane({scrollbarWidth:5, scrollbarMargin:10});
	
	function scroll_up(){
		$('#scroll_nav ul')[0].scrollBy(-6);
	}
	
	function scroll_down(){
		$('#scroll_nav ul')[0].scrollBy(6);
	}
	
	var intervalID;

	$('#top').hover(function(){
		intervalID = setInterval(scroll_up,50);
		$(this).addClass('on');
	},function(){
		intervalID  = clearInterval(intervalID);
		$(this).removeClass('on');
	});
	
	$('#bottom').hover(function(){
		intervalID = setInterval(scroll_down,50);
		$(this).addClass('on');
	},function(){
		intervalID  = clearInterval(intervalID);
		$(this).removeClass('on');
	});

  $('#scroll_nav li a').hover(function(){
    $(this).addClass('hover');
    $('#scroll_nav .header').text($(this).children('.ad_image').attr('alt'));
    $(this).parent().siblings('li').find('.color').fadeOut('slow');
    $(this).children('.color').fadeIn('slow');
  },function(){
    $(this).removeClass('hover');
    $('#scroll_nav .header').text($('#scroll_nav .selected').siblings('.ad_image').attr('alt'));
  });
    
  $('#scroll_nav ul').bind("mouseleave", function(){
    $('#scroll_nav .color').not('.selected').fadeOut('slow');
    $('#scroll_nav .selected').fadeIn('slow');
  });
  
  
  $('#scroll_nav li a').bind('click', function(){
    if($(this).find('.selected').length == 0){
      $('#scroll_nav').find('.color').removeClass('selected');
      $(this).children('.color').addClass('selected');
      $(this).children('.ajax_loader').effect("pulsate", { times:0 });
      $.get($(this).attr('href'), function(data){
        $('#content').fadeOut('slow', function(){
          $(this).html(data);
          $('#scroll_nav').find('.ajax_loader').fadeOut('fast');
          // $('.portfolio_spread .example_box .examples a').bind('click', example_click);
          $(this).fadeIn(1000);
          $('#content .copy').jScrollPane({scrollbarWidth:5, scrollbarMargin:10});
        });
      });
    }
	  return false;
	});
	
	
	
	
  
  // ====================
  // = Portfolio Spread =
  // ====================
  $('.portfolio_spread .copy').jScrollPane({scrollbarWidth:5, scrollbarMargin:10});

  $('.portfolio_spread .example_box .examples a').live('click', function(){
    $('.portfolio_spread .example_box .examples .active').removeClass('active');
    $(this).parent().addClass('active');
    image_caption = $('#' + $(this).attr('name')).children('h2').html();
    $('.portfolio_spread .example_name').fadeOut('fast', function(){
      $('.portfolio_spread .example_name').html(image_caption);
      $('.portfolio_spread .example_name').fadeIn('fast');
    });
    $('.portfolio_spread .example_box img.on').fadeOut('slow').removeClass('on');
    $('.portfolio_spread .example_box object.on').fadeOut('slow').removeClass('on');
    $("#" + $(this).attr('name')).children('img').addClass('on').fadeIn('slow');
    $("#" + $(this).attr('name')).children('object').addClass('on').fadeIn('slow');
    return false;
  });
  
  
  
  // ============
  // = Sortable =
  // ============
    
  $("#sortable").sortable({ 
      handle: '.handle',
      update: function () { 
        $("#order").val($('#sortable').sortable('serialize').replace(/[^\d\&]*/g, "").replace(/[\&]/g, ",")); 
      } 
    }); 
    
  $("#sortable").disableSelection();

  
  // ==============
  // = Flash Fade =
  // ==============
  $('.flash>div').effect('highlight', {}, 1000)
  $('.flash').animate({opacity: 1}, 4000).fadeOut(4000);
  $('.flash').toggle(function(){
    $(this).stop();
    $(this).css('opacity', 1);
  },function(){
    $(this).fadeOut(2000);
  });
  
  
  // ================
  // = Delayed Show =
  // ================
  $('.delayed').css('opacity', '0');
  $('.delayed').show();
  $('.delayed').animate({opacity: 1}, 3000);
  
  
  // ======================
  // = Controls Drop Down =
  // ======================
  $('li .controls').wrap('<div class="controls_wrapper"></div>');
  $('li .controls').hide();
  $('li .controls').each(function(){
    $(this).after('<div class="controls"></div>');
    $(this).children('a').appendTo($(this).siblings('div.controls'));
  });
  $('li span.controls').remove();
  $('li .controls').hide();
  $('.controls_wrapper').prepend('<div class="controls_label">Actions</div>');
  $('.controls_wrapper').after('<div class="clear"></div>');
  $('.controls_wrapper').parent('li').css('overflow', 'visible');
  // $('.controls_wrapper').bind('mouseenter', function(){
  //   $(this).children('.controls').slideDown('slow');
  // });
  // $('.controls_wrapper').bind('mouseleave', function(){
  //   $(this).children('.controls').slideUp('slow');
  // });
  $('.controls_label').toggle(function(){
    $(this).siblings('.controls').slideDown('slow');
  }, function(){
    $(this).siblings('.controls').slideUp('slow');
  });
  

	// End of JQuery Code

});