$(window).load(function() {

// --------- MAIN NAV FADE IN/OUT (don't ask... that was a headache)
// --------- Safari was buggy with doc.ready regarding CSS so we do a window.load instead
	$('#nav a:not(.selected)').css('color', '#Bfbfbf');
	$('#nav li ul').css('display', 'inline');
	$('#nav li:not(.selected) ul a:not(.selected)').css('color', '#fff');
	$('#nav a').hover(
		function () {
			if (!$(this).hasClass('selected')) {
				if ($(this).is(':animated')) {
					$(this).stop().animate({color: '#4a5666'}, 1);
				} else {
					$(this).css({color: '#Bfbfbf'}).animate({color: '#4a5666'}, 1);
				}
			}
		},
		function () {
			if (!$(this).hasClass('selected')) {
				if ($(this).is(':animated')) {
					$(this).stop().animate({color: '#Bfbfbf'}, 800);
				} else {
					$(this).css({color: '#4a5666'}).animate({color: '#Bfbfbf'}, 800);
				}
			}
		}
	);
	$('#nav li').hover(
		function () {
			if (!$(this).hasClass('selected')) {
				if ($(this).find('ul a').is(':animated')) {
					$(this).find('ul a').stop().animate({color: '#Bfbfbf'}, 1);
				} else {
					$(this).find('ul a').css({color: '#fff'}).animate({color: '#Bfbfbf'}, 1);
				}
			}
		},
		function () {
			if (!$(this).hasClass('selected')) {
				if ($(this).find('ul a').is(':animated')) {
					$(this).find('ul a').stop().animate({color: '#fff'}, 800);
				} else {
					$(this).find('ul a').css({color: '#Bfbfbf'}).animate({color: '#fff'}, 800);
				}
			}
		}
	);

});

$(document).ready(function() {

// --------- GLOBAL

	// open external links in new window
	$('a:not([@href*="minimaldesign.net"])').click(function () {
		window.open($(this).attr('href'));
		return false;
	});
	
// --------- PORTFOLIO
	// border color animation on portfolio thumbnails
	// project info slide up/down
	$('.slider li').hover(
      function () {
        $(this).children('a').css({borderColor: '#Bfbfbf'}).animate({borderColor: '#4a5666'}, 500);
        $(this).find('.description').slideDown(250, 'easeInExpo');
      }, 
      function () {
        $(this).children('a').animate({borderColor: '#Bfbfbf'}, 700);
        $(this).find('.description').slideUp(500, 'easeOutQuad');
      }
    );
	
	// ADD PORTFOLIO NAV TO PAGE
	// Got to prioritize esthetic over accessibility unfortunatly...
	// so following line now implemented via CSS to prevent "content flash"
	// $('.portfolio .slider').css("overflow-x", "hidden");
	// hide the portfolio descriptions
	$('.slider .description').hide();
	// disable access to individual portfolio page (only there for search results)
	$('.portfolio a.img_link').click(function() {
		window.open($(this).parent().find('.visit a').attr('href'));
		return false;
	});
	// get total portfolio items
	var items = $('.slider').find('li').length;
	// get # of pages
	var total_pages = Math.ceil(items/3);
	// create pagination + local nav
	var nav = '<ul id="pagination" display="width: ' + (14*total_pages) + 'px;">';
	for (var page = 0; page < total_pages; page++) {
		nav += '<li><a href="#page_0' + page + '">' + page + '</a></li>';
	}
	nav += '</ul><ul id="local_nav"><li class="prev"><a href="#">previous</a></li><li class="next"><a href="#">next</a></li></ul>';
	$('.slider').after(nav);
	
	// PORTFOLIO NAV FUNCTIONALITY - PAGINATION
	$('#pagination li:first a').addClass('selected');
	$('#pagination li a').click(function() {
		$('#pagination li a').removeClass('selected');
		$(this).addClass('selected');
		// get the portfolio item # to sroll to from end of pagination href*3
		var scroll_to = ($(this).attr('href')).slice(-1)*3;
		$('.slider').scrollTo('li:eq(' + scroll_to + ')', 1000, {axis:'x', easing:'easeInOutQuint'});
		// set prev/next to .off at end of pagination
		if (scroll_to == 0) { // if 1st page
			$('#local_nav .prev').addClass('off');
		} else {
			$('#local_nav .prev').removeClass('off');
		}
		// alert(scroll_to + ' and ' + items);
		if (items-scroll_to < 4) { // if 3 or less item left it's last page
			$('#local_nav .next').addClass('off');
		} else {
			$('#local_nav .next').removeClass('off');
		}
	});

	// PORTFOLIO NAV FUNCTIONALITY - PREV/NEXT

	// default state of prev is "off"
	$('#local_nav .prev').addClass('off');
	// PREV
	$('#local_nav .prev').click(function() {
		$('.slider').scrollTo('-=828', 1000, {axis:'x', easing:'easeInOutQuint'});
		// remove current .selected and add to next one if there's one
		if($('#pagination li a.selected').parent().prev().length) {
			$('#pagination li a.selected')
			.removeClass('selected')
			.parent().prev().children().addClass('selected');
		}
		// if there's no more pagination after the .selected page, can't click on next...
		if (!$('#pagination li a.selected').parent().prev().length) {
			$(this).addClass('off');
		}
		// just in case, we .next active
		$('#local_nav .next').removeClass('off');
	});
	
	// NEXT
	$('#local_nav .next').click(function() {
		$('.slider').scrollTo('+=828', 1000, {axis:'x', easing:'easeInOutQuint'});
		// remove current .selected and add to next one if there's one
		if($('#pagination li a.selected').parent().next().length) {
			$('#pagination li a.selected')
			.removeClass('selected')
			.parent().next().children().addClass('selected');
		}
		// if there's no more pagination after the .selected page, can't click on next...
		if (!$('#pagination li a.selected').parent().next().length) {
			$(this).addClass('off');
		}
		// just in case, we make .prev active
		$('#local_nav .prev').removeClass('off');
	});

// --------- INFORMATION
	$('.information dt').addClass('closed');

	$('.information dt.closed a').hover(
		function () {
			if ($(this).parent().hasClass('closed')) {
				if ($(this).is(':animated')) {
					$(this).stop().animate({color: '#4a5666'}, 1);
				} else {
					$(this).css({color: '#Bfbfbf'}).animate({color: '#4a5666'}, 1);
				}
			}
		},
		function () {
			if ($(this).parent().hasClass('closed')) {
				if ($(this).is(':animated')) {
					$(this).stop().animate({color: '#Bfbfbf'}, 800);
				} else {
					$(this).css({color: '#4a5666'}).animate({color: '#Bfbfbf'}, 800);
				}
			}
		}
	);

	$('.information dt').click(function() {
		var answer = $(this).next();
		if (answer.is(':hidden')) {
			if ($('.information dd').is(':visible')) {
				// if an answer's already shown, slide it up
				$('.information dd:visible').slideUp(80, function() { answer.slideDown(350, 'easeInOutSine'); });
				$('.information dt').addClass('closed').removeClass('open');
				$(this).removeClass('closed').addClass('open');
			} else {
				answer.slideDown(350, 'easeInOutSine');
				$(this).removeClass('closed').addClass('open');
			}
		}
		// make sure the previously selected goes back to grey
		$('.information dt.closed a').css('color', '#Bfbfbf');
		return false;
	});

	// --------- CONTACT - Google map
	$('.view_map a').click(function() {
		var map = $('#google_map');
		if (map.hasClass('hidden')) {
			map.removeClass('hidden');
			map.css({display: 'none', visibility: 'visible', position: 'relative'});
			map.slideDown(250, 'easeOutQuad');
			$(this).html('Hide map')
		} else {
			map.addClass('hidden');
			map.slideUp(500, 'easeOutQuad');
			$(this).html('View map')
		}
		return false;
	});

// --------- FORMS VALIDATION
	// add minimum word count method
	jQuery.validator.addMethod("minWords", function(value, element, params) { 
		return this.optional(element) || value.match(/\b\w+\b/g).length >= params; 
	}, "Please enter at least {0} words.");
	// custom validation for anti-spam question
	jQuery.validator.addMethod("spam_bot", function(value, element) {
		return this.optional(element) || /^green$/.test(value);
	}, "Wrong color");
	
// --------- CONTACT FORM 
	// validate signup form on keyup and submit
	$("#c_form").validate({
		rules: {
			name: { required: true, minlength: 2 },
			email: { required: true, email: true },
			message: { required: true, minWords: 5 },
			bot: { required: true, spam_bot: true }
		},
		messages: {
			name: {
				required: "Required Field",
				minlength: "2 letters minimum"
			},
			email: "Valid email required",
			message: {
				required: "Message required",
				minWords: "5 words minimum"
			},
			bot: {
				required: "Answer Required"
			}
		}
	});
	
// --------- MAILING LIST FORM 
	// validate mailing list form on keyup and submit
	$("#mailing_list").validate({
		rules: {
			'cm-name': { required: true, minlength: 2 },
			'cm-dlluiu-dlluiu': { required: true, email: true },
			'cm-fo-jrditj': { required: true }
		},
		messages: {
			'cm-name': {
				required: "Required Field",
				minlength: "2 letters minimum"
			},
			'cm-dlluiu-dlluiu': "Valid email required",
			'cm-fo-jrditj': {
				required: "You must select at least one"
			}
		}
	});

// --------- COMMENT FORM
	// validate signup form on keyup and submit
	$("#cmt_form").validate({
		rules: {
			author: { required: true, minlength: 2 },
			email: { required: true, email: true },
			comment: { required: true, minWords: 3 },
			bot: { required: true, spam_bot: true }
		},
		messages: {
			author: {
				required: "Required Field",
				minlength: "2 letters minimum"
			},
			email: "Valid email required",
			comment: {
				required: "Message required",
				minWords: "3 words minimum"
			},
			bot: {
				required: "Answer Required"
			}
		}
	});
	
// --------- ARTICLES + DOWNLOADS TITLE
	$('.entry h2 a').mouseover(
		function () {
			if (!$(this).is(':animated')) {
				$(this).css({color: '#D3D3D3'}).animate({color: '#4a5666'}, 1000, 'easeOutQuad');
			}
		}
	);


// --------- ARTICLES SPECIFIC STUFF
	$('.rollover img+img').hide();
	$('.rollover img').hover(
		function () {
			$(this).parent().addClass($(this).attr('src'));
			$(this).attr('src', $(this).next().attr('src'));
		},
		function () {
			$(this).attr('src', $(this).parent().attr('class'));
		}
	);

// --------- KEYBOARD ACCES SWITCH ON/OFF FOR ALPHA ARCHIVES
	$('.nav_switch').click(function() {
		$('.nav_switch').toggleClass('on');
		if ($('.nav_switch').hasClass('on')) {
			link_title = 'Keyboard Access <strong>On</strong>';
		} else {
			link_title = 'Keyboard Access <strong>Off</strong>';
		}
		$('.nav_switch a').html(link_title);
		return false;
	});

// --------- ARCHIVES FADE IN/OUT
	$('.tag_title a, #archives_pagination a').hover(
		function () {
			if (!$(this).parent().hasClass('selected')) {
				if ($(this).is(':animated')) {
					$(this).stop().animate({color: '#4a5666'}, 1);
				} else {
					$(this).css({color: '#Bfbfbf'}).animate({color: '#4a5666'}, 1);
				}
			}
		},
		function () {
			if (!$(this).parent().hasClass('selected')) {
				if ($(this).is(':animated')) {
					$(this).stop().animate({color: '#Bfbfbf'}, 800);
				} else {
					$(this).css({color: '#4a5666'}).animate({color: '#Bfbfbf'}, 800);
				}
			}
		}
	);

});

// --------- KEYBOARD NAV FOR ALPHA ARCHIVES
$(document).keydown(function(event) {
	// turn keyboard access on/off via esc (27) key
	if (event.keyCode == 27) {
		$('.nav_switch').toggleClass('on');
		if ($('.nav_switch').hasClass('on')) {
			link_title = 'Keyboard navigation is currently <strong>ON</strong>';
		} else {
			link_title = 'Keyboard navigation is currently <strong>OFF</strong>';
		}
		$('.nav_switch a').html(link_title);
	}
	// if keyboard access is on and only for alpha archives
	if ($('.nav_switch').hasClass('on') && $('body').hasClass('alpha')) {
		// we check key pressed and assign value to uri
		var key_pressed = String.fromCharCode(event.keyCode).toLowerCase();
		var uri = 'http://minimaldesign.net/articles/archives/alpha/' + key_pressed;
		// get nav options in an string
		var nav_content = [];
		var a = $('#archives_pagination li a').get();
		for (var i = 0; i < a.length; i++) {
			nav_content.push(a[i].innerHTML);
		}
		nav_content = nav_content.join('');
		// then we check if key pressed is availble as nav option
		if (nav_content.search(key_pressed) != -1) {
			$('#tip').hide();
			window.location = uri;
			return false;
		}
	}
});