$(document).ready(function(){
	// user account slider
	var listWidth = parseFloat($('.slider li:first').css('width'));
	var listHeight = parseFloat($('.slider li:first').css('height'));
	var list = $('.slider li').length;
	$('.slider').css('width',listWidth * list);
	$('a.slider_nav').click(function(event){event.preventDefault();});
	
	$('.shift').click(function(){
		$('.slider li:first').animate({marginLeft: '-' + listWidth + 'px'},'normal');
	});
	$('.back').click(function(){
		$('.slider li:first').animate({marginLeft: '0px'},'normal');
	});
	$('a[href*="delete"]').click(function(event){
		var answer = confirm("Are you sure you want to delete this record from the database?");
		if (answer) {
		} else {
			event.preventDefault();
		}
	});
	
	// navigation slideIn
	var url = window.location.pathname,
		slideInTime = 450,
		delayTimer = 1000,
		delayed1 = null,
		delayed2 = null;
	if (delayed1) clearTimeout(delayed1);
		delayed1 = setTimeout(function(){ $('.expert').animate({opacity: 1},500,function(){
		
			if (delayed2) clearTimeout(delayed2);
			delayed2 = setTimeout(function(){ $('.main-nav').menuIn(slideInTime); },delayTimer); 
		
		}); },delayTimer);
	//console.log(url);
	
	// work page project fader
	
	var counter = 1;
	var fadeSpeed = 250; // fade transition speed in millisecond
	$list = $('#work-slide li'); // total list items = $list.length
	var workNav = '<div id="work-nav"><span class="prev end">Prev</span>|<span class="next">Next</span><span class="nav-num"><span class="cur">1</span>/<span class="total"></span></span></div>';
	$(workNav).insertBefore('#page-content');
	//$('#home-page #page-content').load('homeslide.htm');
	$('.total').text($list.length);
	$('#work-slide li').css({opacity:0}).filter(':first').addClass('shown').css({opacity:1});
	$('#work-slide li').css({position:'absolute',top:0,left:0}).not(':first').css({opacity:0});
	$('.next').click(function(){
		if(counter < $list.length) {
			$('#work-slide li').eq(counter).addClass('shown').animate({opacity:1},fadeSpeed,function(){
				$(this).prev().css({opacity:0}).removeClass('shown');
			});
			counter += 1;
			$('.cur').text(counter);
			$('.prev').removeClass('end');
			if (counter == $list.length) {
				$('.next').addClass('end');
			}
		}
	});
	$('.prev').click(function(){
		if(counter > 1) {
			$('#work-slide li').eq(counter-2).css({opacity:1}).next().animate({opacity:0},fadeSpeed,function(){
				$(this).removeClass('shown');
			}).end().addClass('shown');
			counter -= 1;
			$('.cur').text(counter);
			$('.next').removeClass('end');
			if(counter == 1) {
				$('.prev').addClass('end');
			}
		} 
	});
	
	var divHeight = $('.hide').outerHeight(true);
	
	$('.hide').css({height: 0});
	//console.log(divHeight);
		
	$('a.more').click(function(){
		var txt = $(this).text();
		if (txt == "read more") {
			$('.hide').animate({height: divHeight + "px" },'normal');
			$(this).text("hide");
			$(this).css("background-image", "url(../images/arrow_left_sml.png)");
		} else if (txt == "hide") {
			$('.hide').animate({height: 0},'normal');
			$(this).text("read more");
			$(this).css("background-image", "url(../images/arrow_right_sml.png)");
		}
		
		return false;
	});	
	
	// TESTIMONIAL IMAGE FADER
	var counter1 = 1,
		speed = 500,
		timer = 4000,
		fadeDelayer = null,
		$totalImg = $('.testimo-img-wrap ul li'),
		total = $totalImg.length;
	
	$('.testimo-img-wrap ul li:first').addClass('cur');
	
	if (fadeDelayer) clearInterval(fadeDelayer);
	fadeDelayer = setInterval(function(){ fadeStart(speed); },timer);
	//console.log("initial: " + counter1);
	function fadeStart(speed) {
		
		var curImg = $('.testimo-img-wrap ul li.cur'),
			nextImg;
		
		if (counter1 >= total) {
			counter1 = 0;
			nextImg = $('.testimo-img-wrap ul li:first');
		} else {
			nextImg = $('.testimo-img-wrap ul li').eq(counter1);
		}
		
		curImg.animate({opacity: 0},speed,function(){$(this).removeClass('cur');});
		nextImg.animate({opacity: 1},speed,function(){$(this).addClass('cur')});
		
		//console.log("before: " + counter1);
		counter1++;
		//console.log("after: " + counter1);

	}
	// CAPTION HOVER SLIDE UP EFFECT
	$('.post-img').each(function(){
		var cap = $(this).find('span');
		if (cap) {
			$(this).hover(function(){
				cap.stop().animate({bottom: '0px'}, 'fast');
			},function(){
				cap.stop().animate({bottom: '-35px'}, 'fast');
			});	
		}
	});
	
	// WORK CREDIT HOVER SLIDE UP
	$('#work-slide li').each(function(){
		var credit = $(this).parents('ul:first').find('.credit_wrap').css({bottom:'-35px', opacity:.9});
		if (credit) {
			$(this).hover(function(){
				credit.stop().animate({bottom: '0px'},'fast');
			},function(){
				credit.stop().animate({bottom: '-35px'},'fast');
			});
		}
	});
	
	
	
	// SLIDEDOWN MENU
	var wrapper = $('ul.work'),
		$section = $('ul.work li'),
		$handle = $('ul.work li h4'),
		$current = $('ul.work li ul').find('li.selected').parent().addClass('open'),
		$target = $('ul.work li ul').hide().filter('.open').show();

	$handle.each(function(index){
		$(this).click(function(event){
			var open = $('ul.work li ul.open'),
				pointer = $(this).siblings('ul');
			if (!pointer.hasClass('open')) {
				open.removeClass('open').slideUp('normal',function(){
					$('ul.work li ul').eq(index).slideDown().addClass('open');
				});
			} else {
				return;
			}
		
			event.preventDefault();
		});
	});
	
	// Slimbox customization
	$('a[rel^="lightbox"]')
		.slimbox({
			/* Put custom options here */
			//loop: false, // if TRUE, allows users to navigate between the first and last image. Default is FALSE.
			overlayOpacity: 0.8, // default is 0.8, (0.1 = lighter & 0.9 = darker) 
			overlayFadeDuration: 400, // default is 400
			resizeDuration: 400, // set to 1 to disable, default is 400
			//resizeEasing: "swing", // requires easing.js plugin, default is swing
			//easing: "easeOutElastic",
			initialWidth: 250, // default is 250
			initialHeight: 250, // default is 250
			imageFadeDuration: 400, // set to 1 to disable, default is 400
			captionAnimationDuration: 400, // set to 1 to disable, default is 400
			//counterText: "Image {x} sur {y}", // default is "Image {x} of {y}"
			closeKeys: [27, 70], // default is [27, 88, 67] which means Esc (27), x (88) and c (67)
			//previousKeys: [39, 78], // default is [37, 80] which means Left arrow (37) and p (80)
			nextKeys: [39, 83] // default is [39, 78] which means Right arrow (39) and n (78)
			}, null, function(el) {
				return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
	});
	
	// initiate markitup plugin
	$("textarea").markItUp(mySettings);
});

(function ($) {
	$.fn.menuIn = function(fadeInTime) {
		return this.animate({
			top: "165px",
			opacity: 1	
		}, fadeInTime);
	};
})(jQuery);

jQuery.fn.fadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle'}, speed, easing, callback);  
};


