//JavaScript

//hero rotator variables
var delay = 3750;
var fadeFuration = 750;
var heroCount = "";
var currentGalleryImage = "";
var currentImage = 1;

function changeFocus(text, state, obj){
	var val = $(obj).val();
	switch(state){
		case 'focus':
			if(val == text){
				$(obj).val('');
			}
		break;
		case 'blur':
			if(val == ""){
				$(obj).val(text);
			}
		break;
	}
}

function rotateHero(){
	var next = (currentImage == heroCount) ? 1: currentImage+1;
	$('div.wrap-hero div.image'+next).css('z-index', '0');
	$('div.wrap-hero div.image'+currentImage).css('z-index', '1');
	$('div.wrap-hero div.image'+next).css('display', 'block');
	$('div.wrap-hero div.image'+currentImage).fadeOut(fadeFuration);
	currentImage = next;
}

$(document).ready(function(){

	heroCount = $('div.wrap-hero div').length;
	galleryCount = $('.jqmWindow div.full').length;

	//initiate hero rotation
	var rotator = setInterval('rotateHero()', delay)
	
	//get width of nav so it can be centered programmatically
	var navWidth = $('ul#nav_main').width();
	var navParentWidth = $('ul#nav_main').parent().width();
	var offset = (navParentWidth-navWidth)/2;
	$('ul#nav_main').css('float', 'none');
	$('ul#nav_main').css('left', offset+'px');
	$("ul#nav_main li:last-child").addClass("last");
	//reveal the nav once it's been centered
	$('ul#nav_main').fadeIn(500);
	
	$('a').click(function(event){
		event.preventDefault();
		target = $(this).attr('href').replace("/index.php", "");
		if($(this).attr('target') == "_blank"){
			window.open(target);			
		}else{
			window.location = target;
		}
	});
	
	$(window).load(function(){	
		//if the page has a gallery, center the images within the thumbnail divs
		$('div.gallery div.image p img').each(function(){
			
			width = $(this).width();
			height = $(this).height();
			
			if(width > height){
				$(this).height(75);
			}else{
				$(this).width(75);
			}
			
			
			/*
			width = $('div.gallery div.image p img ').width();
			height = $('div.gallery div.image p img ').height();
			
			if(width > 75){
				offset = (width - 75)/2;
				$(this).css('margin-left', '-'+offset+'px');
			}
			*/
		});
	});
	
	//gallery click listeners
	$('div.gallery div.image').click(function(){
		target = $(this).attr('class').split(' ')[0];
		$('.jqmWindow div.full').css('display', 'none');
		$('.jqmWindow div.full-'+target).css('display', 'block');
		currentGalleryImage = parseInt(stripAlphaChars(target));
		resizeGallery(target);
		$('div.full-gallery').jqmShow();
	});
	
	$('div.full-gallery div.btn-close').click(function(){
		$('div.full-gallery').jqmHide();
	});
	
	$('.jqmWindow div.controls div.btn-arrow-left').click(function(){
		if(currentGalleryImage == 1){
			next = galleryCount;
		}else{
			next = currentGalleryImage - 1;
		}
		$('.jqmWindow div.full-image'+currentGalleryImage).fadeOut();
		resizeGallery('image'+next);
		$('.jqmWindow div.full-image'+next).fadeIn();
		currentGalleryImage = next;
	});
	
	$('.jqmWindow div.controls div.btn-arrow-right').click(function(){
		if(currentGalleryImage == galleryCount){
			next = 1;
		}else{
			next = currentGalleryImage + 1;
		}
		$('.jqmWindow div.full-image'+currentGalleryImage).fadeOut();
		resizeGallery('image'+next);
		$('.jqmWindow div.full-image'+next).fadeIn();
		currentGalleryImage = next;
	});
});

function submitForm(formId){
	switch(formId){
		case 'newsletterForm':
			form = $('#newsletterForm');
			//form.action = "http://papercutinteractive.createsend.com/t/r/s/aothu/";
			form.submit();	
		break;
	}
}

function stripAlphaChars(string) {  
    string = string.replace(/[^0-9]/g, ''); 
    return string; 
}

function resizeGallery(target){
	imageHeight = $('.jqmWindow div.full-'+target+' img').height();
	imageWidth = $('.jqmWindow div.full-'+target+' img').width();
	$('.jqmWindow').css('height', imageHeight+'px');
	$('.jqmWindow').css('width', imageWidth+'px');
	$('.jqmWindow div.controls').css('height', imageHeight+'px');
	$('.jqmWindow div.controls').css('width', imageWidth+'px');
	
	portalHeight = $(window).height();
	portalWidth= $(window).width();
	if(portalHeight > imageHeight){
		offset = (portalHeight - imageHeight)/2;
		$('.jqmWindow').css('top', offset+'px');
	}
	if(portalWidth > imageWidth){
		offset = (portalWidth - imageWidth)/2;
		$('.jqmWindow').css('left', offset+'px');
	}
	
}
