var slider;
var maxColumns = 3;
var currPg = 1;
var firstSlide = false;
var controlsRemoved = true;
var colCount = 0;

newsletterGoHover = function() {
	if ($(this).attr('src').search('-down') > -1) {
		$(this).attr('src', '/img/go.jpg');
	}
	else {
		$(this).attr('src', '/img/go-down.jpg');
	}
};

kbNavDown = function (e) {
	var scrollWidth = $(document).width()-$(window).width();
	var scrollHeight = $(document).height()-$(window).height();
	if (slider) {
		if (!controlsRemoved) {
			if ($(document.activeElement).attr('id') != 'NewsletterEmail') {
				if (e.keyCode == 37 && ($(document).scrollLeft() <= 0)) {
					$('#slide-prev img').attr('src', '/img/slide-prev.png');
					slider.goToPreviousSlide();
				}
				else if (e.keyCode == 39 && ($(document).scrollLeft() >= scrollWidth)) {
					$('#slide-next img').attr('src', '/img/slide-next.png');
					slider.goToNextSlide();
				}
			}
		}
	}
	
	var scrollElem = ($.browser.msie || $.browser.mozilla || $.browser.opera) ? 'html' : 'body';
	if (e.keyCode == 38 && $(document).scrollTop() > 0) {
		if (!$(scrollElem).hasClass('scrolling')) {
			$(scrollElem).addClass('scrolling');
			$(scrollElem).animate({
				scrollTop: $(document).scrollTop()-100
			}, 700, null, function() {
				$(scrollElem).removeClass('scrolling');
			});
		}
		e.preventDefault();
	}
	else if (e.keyCode == 40 && $(document).scrollTop() < scrollHeight) {
		if (!$(scrollElem).hasClass('scrolling')) {
			$(scrollElem).addClass('scrolling');
			$(scrollElem).animate({
				scrollTop: $(document).scrollTop()+100
			}, 700, null, function() {
				$(scrollElem).removeClass('scrolling');
			});
		}
		e.preventDefault();
	}
};
kbNavUp = function (e) {
	if (slider) {
		if (!controlsRemoved) {
			if ($(document.activeElement).attr('id') != 'NewsletterEmail') {
				if (e.keyCode == 37) {
					$('#slide-prev img').attr('src', '/img/slide-prev-inactive.png');
				}
				else if (e.keyCode == 39) {
					$('#slide-next img').attr('src', '/img/slide-next-inactive.png');
				}
			}
		}
	}
};

projectImageHover = function() {
	if ($(this).attr('hover-fade') == 0.7) {
		$(this).animate(
			{opacity: 1})
			.attr('hover-fade', 1);
	}
	else {
		$(this).animate(
			{opacity: 0.7})
			.attr('hover-fade', 0.7);
	}
};

navDropdownIn = function() {
	var currItem = $(this).attr('class').replace('drop-down drop-down-', '').replace(' drop', '');
	if (!$('#nav .drop-down-'+currItem).hasClass('sliding')) {
		$('#nav .drop-down-'+currItem).addClass('sliding').slideDown(400, function() {
			$(this).removeClass('sliding');
		});
	}
};
navDropdownOut = function() {
	var currItem = $(this).attr('class').replace('drop-down drop-down-', '').replace(' drop', '');
	$('#nav .drop-down-'+currItem).slideUp(400);
};

var newsletterEmailDefault = 'Enter your email to receive our newsletter...';
newsletterFocus = function() {
	var currVal = $('#NewsletterEmail').val();
	if (currVal !== undefined && (currVal.length <  1 || currVal == newsletterEmailDefault)) {
		$('#NewsletterEmail').val('');
	}
};
newsletterUnfocus = function() {
	var currVal = $('#NewsletterEmail').val();
	if (currVal !== undefined && currVal.length < 1) {
		$('#NewsletterEmail').val(newsletterEmailDefault);
	}
};
newsletterActivateGo = function() {
	if (jQuery.trim($(this).val()).length > 0) {
		$('#NewsletterForm .submit input').addClass('active');
	}
	else {
		$('#NewsletterForm .submit input').removeClass('active');
	}
};

doColumnResize = function() {
	$('#columns .column').each(function() {
		if ($(this).attr('custom-width')) {
			var totalWidth = $('#columns').width();
			var thisWidth = parseInt(totalWidth * ($(this).attr('custom-width') / 100));
			$(this).css('width', thisWidth);
		}
		
		if ($(this).children('hr.titlebar').length > 0) {
			$(this).css({
				position: 'relative',
				top: '-1.5em'
			});
		}
		
		var widgets = $(this).find('img, iframe, embed, object');
		if (widgets.length > 0 && !$(this).attr('custom-width')) {
			var parent = $(this);
			var maxWidth = 0;
			var maxHeight = 0;
			var colWidth = 0;
			var colHeight = 0;
			widgets.each(function() {
			    if (!$(this).hasClass('twitter-share-button')) {
				colWidth = parent.width();
				colHeight = parent.height();
				parent.css('width', 'auto');
				if ($(this).attr('width') && $(this).attr('width') > maxWidth) {
					maxWidth = $(this).attr('width');
				}
				else if ($(this).width() > maxWidth) {
					maxWidth = $(this).width();
				}
				if ($(this).attr('height') && $(this).attr('height') > maxHeight) {
					maxHeight = $(this).attr('height');
				}
				else if ($(this).height() > maxWidth) {
					maxHeight = $(this).height();
				}
				if ($(this).attr('height')) $(this).css('height', '100%');
				if ($(this).attr('width')) $(this).css('width', '100%');
			    }
			});
			
			if (maxWidth) {
				$(this).css('width', maxWidth).attr('resized-width', maxWidth);
				$(this).children('.column-content').css('width', maxWidth).attr('resized-width', maxWidth);
			}
			
			if (maxHeight) {
				$(this).css('height', maxHeight).attr('resized-height', maxHeight);
				$(this).children('.column-content').css('height', maxHeight).attr('resized-height', maxHeight);
			}
			
			$(this).find('.placeholder').hide();
		}
	});
};

doSlider = function(reset) {
	var startSlide = 0;
	var colsWidth = 0;
	var oldMaxCols = maxColumns;
	maxColumns = 0;
	var maxPg = 0;
	$('#columns .column').each(function(e) {
		colsWidth = colsWidth + $(this).outerWidth();
		if (colsWidth < parseInt($(window).width()-110)) {
			maxColumns = maxColumns+1;
		}
		else {
			return false;
		}
	});
	if (maxColumns <= 0) {
		maxColumns = oldMaxCols;
	}
	maxPg = Math.ceil(colCount-maxColumns);
	if (colCount == maxColumns || !Modernizr.cssgradients) {
		$('#content-fade').hide();
	}
	if (reset != undefined && slider) {
		startSlide = slider.getCurrentSlide();
		slider.destroyShow();
		doColumnResize();
	}
	slider = $('#columns').bxSlider({
		controls: false,
		displaySlideQty: maxColumns+1,
		moveSlideQty: 1,
		speed: 600,
		infiniteLoop: false,
		onFirstSlide: function() {
			firstSlide = true;
		},
		onPrevSlide: function() {
			lastColSlide = colCount-1;
			if (slider.getCurrentSlide() <= 0 && firstSlide) {
				slider.goToSlide(lastColSlide);
				if (Modernizr.cssgradients) $('#content-fade').fadeOut();
			}
			else {
				if (Modernizr.cssgradients && $('#content-fade').is(':hidden')) {
					$('#content-fade').fadeIn();
				}
			}
			firstSlide = false;
		},
		onNextSlide: function() {
			var maxPg = Math.ceil(colCount-maxColumns);
			lastColSlide = colCount-1;
			if (slider.getCurrentSlide() > lastColSlide) {
				firstSlide = true;
				slider.goToSlide(0);
				if (Modernizr.cssgradients && $('#content-fade').is(':hidden')) {
					$('#content-fade').fadeIn();
				}
			}
			else if (slider.getCurrentSlide() >= maxPg || maxPg == 1) {
				if (Modernizr.cssgradients) $('#content-fade').fadeOut();
				firstSlide = false;
			}
			else {
				if (Modernizr.cssgradients && $('#content-fade').is(':hidden')) {
					$('#content-fade').fadeIn();
				}
				firstSlide = false;
			}
		}
	});
	if ($('#slide-next, #slide-prev').length < 1 && colCount > maxColumns) {
		var slideArrows = '<div id="slide-next"><a href="/"><img src="/img/slide-next-inactive.png" alt="" /></a></div><div id="slide-prev"><a href="/"><img src="/img/slide-prev-inactive.png" alt="" /></a></div>';
		$('#content').prepend(slideArrows);
		if (Modernizr.cssgradients) $('#content-fade').show();
		controlsRemoved = false;
		
		$('#slide-prev a').click(function(e) {
			slider.goToPreviousSlide();
			e.preventDefault();
		}).hover(function() {
			if ($(this).children('img').attr('src').search('-inactive') > -1) {
				$(this).children('img').attr('src', '/img/slide-prev.png');
			}
			else {
				$(this).children('img').attr('src', '/img/slide-prev-inactive.png');
			}
		});
		$('#slide-next a').click(function(e) {
			slider.goToNextSlide();
			e.preventDefault();
		}).hover(function() {
			if ($(this).children('img').attr('src').search('-inactive') > -1) {
				$(this).children('img').attr('src', '/img/slide-next.png');
			}
			else {
				$(this).children('img').attr('src', '/img/slide-next-inactive.png');
			}
		});
	}
	else if (colCount <= maxColumns) {
		$('#slide-next, #slide-prev').remove();
		controlsRemoved = true;
		startSlide = 0;
	}
	
	slider.goToSlide(startSlide, false, true);
	doFade();
};

doFade = function() {
	if (colCount <= maxColumns || !Modernizr.cssgradients) {
		$('#content-fade').hide();
	}
};

$(document).ready(function() {
	$('#content').mask('Loading...', 500);
	if ($.browser.msie && $.browser.version.substr(0, 1) <= 7) {
	    $('#content .column hr').replaceWith('<div class="hr"></div>');
	}
	colCount = $('#ColumnCount').val();
	$('#nav .drop').mouseover(navDropdownIn).mouseleave(navDropdownOut);
	$(document).keydown(kbNavDown).keyup(kbNavUp);
	$('#NewsletterEmail').focusin(newsletterFocus).focusout(newsletterUnfocus);
	$('#NewsletterEmail').keyup(newsletterActivateGo);
	$('#NewsletterForm').submit(newsletterFocus);
	$('img.project-image').hover(projectImageHover);
	$('#NewsletterForm .submit input').hover(newsletterGoHover);
	newsletterUnfocus();
});
$(window).load(function() {
	doColumnResize();
	doSlider();
	$('#content').unmask();
	$('body').focus();
});

supportsOrientationChange = "onorientationchange" in window,
	orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";

if (window.addEventListener) {
	window.addEventListener(orientationEvent, function() {
		doSlider(true);
	}, false);
}
else if (window.attachEvent) {
	window.attachEvent(orientationEvent, function() {
		doSlider(true);
	}, false);
}
