//
// Define Global Variables
var iconScrollOK = true;
var slideCount;
var slideshowInt = 5000;
var n = 0;
var z = 0;
var zz = 1;
var shrink;
var shrinkBool;
var collapse;
var collapseBool;
//
// Define Functions
function shiftIcons(dir) {
	var pos = Math.floor(parseFloat($('#content-span').css('left').split('px')[0]));
	var inc = 0;
	var max = ($('#content-span').width() - 798) * -1;
	
	switch(dir) {
	case 'left' :
		if(pos<0) {
			inc = 88;
			if(pos+inc>0) inc=inc - (pos+inc);
		}
		break;
	case 'right' :
		if(pos>max) {
			inc = -88;
			if(pos+inc<max) inc=(max-pos);
		}
		break;
	}
	
	$('#content-span').stop().animate({left:pos+inc},500);
}
function loadIcons(iconNum) {
	var iconMax = $('.icon').length - 1;
	
	if (iconNum <= iconMax) {
		$('#icon-'+ iconNum).animate({opacity:1},50, function() {
			iconNum++;
			loadIcons(iconNum);
		});
	}
}
function animateText(head,str,headCount,strCount,headStripped,strStripped) {
	if (headCount >= 0) {
		$('#content-body>h2').prepend(headStripped[headCount]);
	}
	if (strCount >= 0) {
		
		$('#content-body>p').prepend(strStripped[strCount] + ' ');
	}
	$('#content-body').animate({opacity:1},20,function() {
		if (headCount >= 0 || strCount >= 0) {
			headCount--;
			strCount--;
			animateText(head,str,headCount,strCount,headStripped,strStripped);
		} else {
			$('#content-body>p').html(str);
		}
	});
}
function fadeSlide() {
	if(slideCount > 1) {
		var target = '#slideshow-' + slideCount;
		$(target).fadeOut('slow',function() {
			slideCount--;
			//fadeSlide(slideCount);
		});
	} else {
		slideCount = $('.slideshow').length;
		var target = '#slideshow-' + slideCount;
		$(target).fadeIn('slow',function() {
			$('.slideshow:hidden').show();
			//fadeSlide(slideCount);
		});
	}
}
function collapseTable(value) {
	var target = $('#tr' + zz + ' .hidden');
	//
	// animate to left
	var w = $(target)[0].width - 80;
	if (w > 1) {
		$(target).css('width',w);
	} else {
		shrinkBool = false;
	}
	
	
	var myC = (shrinkBool) ? 1 : 5;
	
	// Speed up after animate finished
	for (i=0; i<myC; i++) {
		// remove cells
		target = $('#tr' + zz + ' .hidden');
		$(target[z]).hide();
		// cycle rows
		if (zz == 3) {
			zz = 1;
			z++;
		} else {
			zz++;
		}
	}
	
	if($('.hidden:visible').length == 0) {
		collapseBool = false;
	}

	
	if (!collapseBool && !shrinkBool) {
		clearInterval(collapse);
		cascade(value);
	}
}
function cascade(value) {
	n++;
	switch(n) {
	case 1 :
		$('#content-span a').not('.'+ value).parents('td').addClass('hidden').animate({opacity:0},500,function() {
			//alert('hit');
		});
		$('#logo').animate({opacity:1}, 500, function() {
			cascade(value);
		});
		break;
	case 2 :
		if ($('.hidden').children('a.'+ value).length > 0) {
			$('a.'+ value).parents('.hidden').css({'opacity':1}).fadeIn('slow').removeClass('hidden').children('img').css({"width":80});
		}
		$('#logo').animate({opacity:1}, 500, function() {
			cascade(value);
		});
		break;
	case 3 :
		var target = $('#content-span a').not('.'+ value).children('img');
		shrinkBool = true;
		collapseBool = true;
		collapse = setInterval(function() { collapseTable(value) },1);
		break;
	case 4 :
		$('.flasher').css({'opacity':1}).animate({opacity:0},500);
		break;
	}
}
function filter(value) {
	n=0;z=0;zz=1;
	if ($('#content-span a').not('.'+ value).length > 0) {
		cascade(value);
	} else {
		$('#content-span a.'+ value).parents('td').show().removeClass('hidden').css({'opacity':1}).children('img').css({'width':80});
	}
}
//
// Define Document Ready
$(document).ready(function() {
	if($('.item').length > 0) $('#content-item-0, #content-link-0, #content-caption-0').show();
	//
	// Fade-in icons and animate text
	if(!location.href.split('opt=')[1]) {
		var rex = /<(?:.|\s)*?>/g;
		var head = $('#content-body>h2').html();
		var str = $('#content-body p').html();
		if (head != null && head != "") {
			var headStripped = head.replace(rex,"").split('');
			var headCount = headStripped.length - 1;
		}
		if (str != null && str != "") {
			var strStripped = str.replace(rex,"").split(' ');
			var strCount = strStripped.length - 1;
		}
		$('#content-body>h2, #content-body>p').text('');
		animateText(head,str,headCount,strCount,headStripped,strStripped);
		loadIcons(0);
	} else {
		$('.icon').animate({opacity:1},50);
	}
	
	//
	// Highlight icons
	$('.overlay').mouseenter(function() {
		$('.overlay').css({'background-position':'0px 80px'});
		$(this).css({'background-position':'0px 0px'});
	});
	
	//
	// Scroll icons
	$('#content-scroll>div').mouseenter(function() {
		if(iconScrollOK) {
			iconScrollOK = false;
			$(this).css({'background-position':'0px 0px'});
			//
			// Scroll and set interval
			var dir = this.id.split('-')[1];
			shiftIcons(dir);
			var iconSlide = setInterval(function() { shiftIcons(dir) },550);
			//
			// Clear interval
			$(this).mouseleave(function() {
				$(this).css({'background-position':'0px 20px'},200);
				clearTimeout(iconSlide);
				iconScrollOK = true;
			});
		}
	});
	
	//
	// Navigate client items
	$('#content-nav>div').mouseenter(function() {
		$(this).css({'background-position':'0px 20px'});
		$(this).mouseleave(function() {
			$(this).css({'background-position':'0px 0px'},200);
		});
	});
	$('#content-nav>div').click(function() {
		var dir = $(this).attr('id').split('-')[1];
		var num = $('.item').length - 1;
		var current = $('.item:visible').attr('id').split(/[\-,]+/);
		current = parseFloat(current[current.length - 1]);
		
		var ok = ((dir == 'left' && current > 0) || (dir == 'right' && current < num)) ? true : false;
		
		switch(dir) {
		case 'left' :
			if (current > 0) {
				current = current - 1;
			}
			break;
		case 'right' :
			if (current < num) {
				current = current + 1;
			}
			break;
		}
		
		if (current == num) {
			$('#scroll-right').css({'opacity':0,'cursor':'default'});
		} else if (current == 0) {
			$('#scroll-left').css({'opacity':0,'cursor':'default'});
		} else {
			$('#scroll-right,#scroll-left').css({'opacity':1,'cursor':'pointer'});
		}
		
		if (ok) {
			$('.item:visible, .caption:visible, .link:visible').fadeOut(400,function() {
				$('#content-item-' + current + ',#content-link-' + current + ',#content-caption-' + current).fadeIn(400);
			});
		}
	});
	
	//
	// Contact checkboxes
	$('.contact-check').click(function() {
		var val = $(this).attr('id').split('-')[1];
		$('input[name^="contact"]').removeClass('required');
		$('input[name="contact-'+ val +'"]').addClass('required');
		$('input[name="contact"]').attr('value',val);
		$('.contact-check').css('background-position','0px 19px');
		$(this).css('background-position','0px 0px');
	});
	
	//
	// Form validation
	$('form').submit(function() {
		var error = false;
		var errorMsg = "";
		var rexEmail = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		var rexPhone = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
		$('.required').each(function() {
			var val = $(this).attr('value');
			if (val != "") {
				if ($(this).hasClass('email') && !rexEmail.test(val)) {
					error = true;
					errorMsg+= "Email address is invalid<br />";
				}
				if ($(this).hasClass('phone') && !rexPhone.test(val)) {
					error = true;
					errorMsg+= "Phone number is invalid<br />";
				}
			} else {
				if ($(this).hasClass('name')) {
					errorMsg+= "Please enter your name<br />";
				}
				if ($(this).hasClass('contact')) {
					errorMsg+= "Please choose a contact method<br />";
				}
				if ($(this).hasClass('email')) {
					errorMsg+= "Please enter your email address<br />";
				}
				if ($(this).hasClass('phone')) {
					errorMsg+= "Please enter your phone number<br />";
				}
				error = true;
			}
		});
		if (error) {
			$('#error-message').html(errorMsg + "<br />");
			return false;
		} else {
			return true;
		}
	});
	
	//
	// News slideshow
	slideCount = $('.slideshow').length;
	if($('.slideshow').length > 0) var slideTimer = setInterval(function() { fadeSlide(); },slideshowInt);
	
	if ($('.scrollpane').length > 0) $('.scrollpane').jScrollPane();
	
	$('#content-drop').hover(function() {
		$(this).addClass('over');
		$(this).mouseleave(function() {
			$(this).removeClass('over');
		});
	});
	$('#content-drop:not(".down")').live('click',function() {
		$('select#drop-alt').focus();
		if ($(this).hasClass('down')) {
			$(this).removeClass('down');
		} else {
			$('li.selected').addClass('over');
			$(this).addClass('down');
		}
	});
	$('#content-drop.down li').live('mouseover', function() {
		if ($('li.over').length > 0) $('#content-drop li.over').removeClass('over');
		$(this).addClass('over');
	});
	$('#content-drop li.over').live('click',function() {
		var value = $(this).attr('filter');
		//alert(value);
		$('#drop-alt').attr('value',value);
		$('#content-drop.down').removeClass('down');
		$('#content-drop li.selected').removeClass('selected');
		$(this).addClass('selected').removeClass('over');
		filter(value);
		$('.icon').each(function() {
			var href = $(this).parent('a').attr('href').split('&filter=')[0];
			$(this).parent('a').attr('href', href +'&filter='+ value);
		});
	});
	$('#drop-alt').change(function() {
	 	var value = $(this).attr('value').split('#').join('');
	 	$('#content-drop li.selected').removeClass('selected');
	 	$('#content-drop li:contains("'+ value +'")').addClass('selected');
	 	$('#content-drop').removeClass('down');
	 	$('#content-drop').removeClass('over');
	 	filter(value);
		$('.icon').each(function() {
			var href = $(this).parent('a').attr('href').split('&filter=')[0];
			$(this).parent('a').attr('href', href +'&filter='+ value);
		});
	});
	
	//
	// vLoad handler
	$('.video').vLoad();
});
