// JavaScript Document


// Home Page Gallery Variables

var duration = 850;
var transition = 10000;
var easing = 'easeInOutCubic';
var totalItems = 5;
var imgWidth = 1002;

var inactiveClass = 'cloneGallery';
var animating = false;
var timeout;


$(document).ready(function(){
	// Common functions
	clearClick();
	setBrowserClass();
	cssTweaks();
	if($('body').hasClass('home')) {
		init_home();
	}
	
	// Init Process Page
	if($('a').hasClass('process_btn_lg')) {
		init_process();
	}
		
	// PNG support for IE6
	if(isIE(6)){
		$(document).pngFix();
	}
});

$(window).resize(function() {
	if($('body').hasClass('home')) {
		alignGallery();
	}
});

(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = (ph - ah) / 2;
	if($('body').hasClass('ie7')) {
		$(this).css('top', mh);
	} else {
		$(this).css('margin-top', mh);
	}
	});
};
})(jQuery);

function cssTweaks(){
	$('blockquote').each(function(i,elem){ $(elem).find('p:last').css('margin',0); });
	$('ol li').wrapInner('<span></span>');
	$('hr').wrap('<div class="hr" />');
}

// Home Page Gallery Functions

function init_home() {
	appendTo('', true);
	returnHomeBindings();
	init_alignGallery();
	automatedSlide();
}

function appendTo(dir, inClass) {
	if(dir == 'start') {
		var html = $('#gallery .gallery_item:last').html();
		var rel = $('#gallery .gallery_item:last').attr('rel');
		$('#gallery').prepend('<div rel="' + rel + '" class="gallery_item">' + html + '</div>');
		$('#gallery .gallery_item:last').remove();
	} else if (dir == 'end') {
		var html = $('#gallery .gallery_item:first').html();
		$('#gallery').append('<div class="gallery_item">' + html + '</div>');
		$('#gallery .gallery_item:first').remove();
	}

	if(inClass) {
		if($('.'+inactiveClass).length == 0) {
			var newHTML = '';
			
			$('#gallery .gallery_item').each(function() {
				var html = $(this).html();
				var rel = $(this).attr('rel');
				newHTML += '<div rel="' + rel + '" class="gallery_item '+ inactiveClass +'">' + html + '</div>';
			});
			
			$('#gallery').css('width', (imgWidth * (totalItems * 3)));	
			$('#gallery').append(newHTML);
			$('#gallery').append('<div class="clear"></div>');
			$('#gallery').prepend(newHTML);
			alignGallery();
		}
	}
}

function goToSlide(num, arrowNav) {
	clearTimeout(timeout);
	animating = true;
	$('#gallery_overlay').css('z-index','800');
	removeHomeBindings();
	if(num > totalItems) {
		if(arrowNav === true) {
			$('#gallery .gallery_item').each(function() {
				$(this).removeClass('current');
				if($(this).attr('rel') == '1') {
					$(this).addClass('current');	
				}
			});
			$('#gallery_nav .gallery_nav_item').each(function() {
				if($(this).hasClass('current')) {
					$(this).removeClass('current');	
				}
				var rel2 = $(this).attr('rel');
				if(rel2 == 1) {
					$(this).addClass('current');	
				}
			});
			$('#gallery').animate({
				marginLeft: '-=' + imgWidth
			}, duration, easing, function() {
				$('#gallery').animate({
					marginLeft: '+=' + (imgWidth * totalItems)
				}, 1, 'linear', function() {
					$('.' + inactiveClass).each(function() {
						$(this).removeClass('current');
					});
					animating = false;
					returnHomeBindings();
					automatedSlide();
					$('#gallery_overlay').css('z-index','550');
				});
			});
			
		}
		return false;
	} else if(num < 1) {
		if(arrowNav === true) {
			$('#gallery .gallery_item').each(function() {
				$(this).removeClass('current');
				if(parseInt($(this).attr('rel')) == totalItems) {
					$(this).addClass('current');	
				}
			});
			$('#gallery_nav .gallery_nav_item').each(function() {
				if($(this).hasClass('current')) {
					$(this).removeClass('current');	
				}
				var rel2 = $(this).attr('rel');
				if(rel2 == totalItems) {
					$(this).addClass('current');	
				}
			});
			$('#gallery').animate({
				marginLeft: '+=' + imgWidth
			}, duration, easing, function() {
				$('#gallery').animate({
					marginLeft: '-=' + (imgWidth * (totalItems))
				}, 1, 'linear', function() {
					$('.' + inactiveClass).each(function() {
						$(this).removeClass('current');
					});
					animating = false;
					returnHomeBindings();
					automatedSlide();
					$('#gallery_overlay').css('z-index','550');
				});
			});
			
		}
		return false;
	}
	$('#gallery_nav .gallery_nav_item').each(function() {
		if($(this).hasClass('current')) {
			$(this).removeClass('current');	
		}
		var rel2 = $(this).attr('rel');
		if(rel2 == num) {
			$(this).addClass('current');	
		}
	});
	var lo = $('#logo').offset().left;	
	$('#gallery .current').removeClass('current');
	$('#gallery .gallery_item').each(function() {
		var rel = $(this).attr('rel');
		if(rel == num && ($(this).hasClass(inactiveClass) === false)) {
			$(this).addClass('current');	
		}
	}); 
	var co = $('#gallery .current').offset().left;
	var diff = co - lo;
	
	$('#gallery').animate({
		marginLeft: '-=' + (diff + 21)
	}, duration, easing, function() {
		animating = false;
		returnHomeBindings();
		automatedSlide();
		$('#gallery_overlay').css('z-index','550');
	});
}

function slideLeft() {
	var numc = $('#gallery .current').attr('rel');
	goToSlide(parseInt(numc) - 1, true);
}

function slideRight() {
	var numc = $('#gallery .current').attr('rel');
	goToSlide(parseInt(numc) + 1, true);
}

function returnHomeBindings() {
	$('#gallery_nav .gallery_nav_item').each(function() {
		$(this).bind('click', function() {
			var rel = $(this).attr('rel');
			goToSlide(rel, false); return false;
		});
	});
	$('#gallery_btn_prev a').bind('click', function() { slideLeft(); return false; });
	$('#gallery_btn_next a').bind('click', function() { slideRight(); return false; });
	alignGallery();
	if($('body').hasClass('ie7')) {
		var href = $('#gallery .current a').attr('href');
		var target = $('#gallery .current a').attr('target');
		$('#gallery_overlay').html('<a href="' + href + '" target="' + target + '"></a>');
	}
}

function removeHomeBindings() {
	$('#gallery_nav .gallery_nav_item').each(function() {
		$(this).unbind('click');
		$(this).bind('click', function() { return false; });
	});
	$('#gallery_btn_prev a').unbind('click');
	$('#gallery_btn_prev a').bind('click', function() { return false; });
	$('#gallery_btn_next a').unbind('click');
	$('#gallery_btn_next a').bind('click', function() { return false; });
	$('#gallery_overlay').html('');
}

function automatedSlide() {
	timeout = setTimeout('slideRight();',transition);
}

function alignGallery() {
	if(animating === false) {
		$('#gallery').css('marginLeft','0px');	
		var lo = $('#logo').offset().left;
		//var goDist = $('#gallery .current').offset().left;
		var rel = parseInt($('#gallery .current').attr('rel'));
		var goDist = (imgWidth * totalItems) + (imgWidth * (rel - 1));			
		var d = lo - goDist;
		if(d != 0) {
			$('#gallery').css('marginLeft',(d - 21)+'px');	
		}
	}
}

function init_alignGallery() {
	var lo = $('#logo').offset().left;
	$('#gallery').css('marginLeft', - ((totalItems * imgWidth) + 21) + lo +'px');
	if($('body').hasClass('ie7')) {
		var href = $('#gallery .current a').attr('href');
		var target = $('#gallery .current a').attr('target');
		$('#gallery_overlay').html('<a href="' + href + '" target="' + target + '"></a>');
	}
}

// Process Page Functions

function init_process() {
	//alert("This is the Process Page");
	$(".scroll").click(function(event){
		//prevent the default action for the click event
		event.preventDefault();

		//get the full url - like mysitecom/index.htm#home
		var full_url = this.href;

		//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
		var parts = full_url.split("#");
		var trgt = parts[1];

		//get the top offset of the target anchor
		var target_offset = $("#"+trgt).offset();
		var target_top = target_offset.top;

		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 750, easing);
	});
}

// Technologies Page Functions

function init_tech() {
	var height = 0;
	$('.tech td p').each(function() {
		var this_height = $(this).innerHeight();
		if(this_height > height) {
			height = this_height;	
		}
	});
	$('.tech td p').css('height',175);
}

// Clients Page Functions

function init_clients() {
	$('.client_testimonial div').vAlign();
}
