isActive = true; 
			
function onBlur() {
	isActive = false; 
};
function onFocus(){
	isActive = true; 
};

if (/*@cc_on!@*/false) { // check for Internet Explorer
	document.onfocusin = onFocus;
	document.onfocusout = onBlur;
} else {
	window.onfocus = onFocus;
	window.onblur = onBlur;
}

// Loaded
function loaded(el) {
	$(el).attr('data-loaded', 1);
}

function isiPad(){
    return navigator.userAgent.match(/iPad/i) != null;
}

// Ensures Images are always centered in the available space.
function resizeImages(forceLeft) {
	var width, height, newWidth, newHeight, ratio, targetWidth, targetHeight, newLeft, newTop;
	$('#rightBlock #fotoblock img').each(function() {
		// Target
		targetWidth = $('#fotoblock').width();
		targetHeight = $('#fotoblock').height()-20;

		// Image Width
		if (this.currentStyle && $(this).attr('width') == 0) {
			ieW = this.currentStyle.width;
			ieH = this.currentStyle.height
			width = ieW.substring(0, (ieW.length - 2));
			height = ieH.substring(0, (ieH.length - 2));
		} else {
			width = $(this).attr('width');
			height = $(this).attr('height');
		}

		newWidth = width;
		newHeight = height;

		// Check Width
		if (width > targetWidth) {
			ratio = targetWidth / width;
			newWidth = targetWidth;
			newHeight = Math.round(ratio * newHeight);
		}

		// Check Height
		if (newHeight > targetHeight) {
			ratio = targetHeight / newHeight;
			newHeight = targetHeight;
			newWidth = Math.round(ratio * newWidth);
		}

		// Calculate Left
		if ( ! forceLeft) {
			newLeft = ((targetWidth - newWidth) / 2) + 'px';
		}

		newTop = ((targetHeight - newHeight) / 2)+'px';

		// Apply
		$(this).css({
			'width' : newWidth+'px',
			'height' : newHeight+'px',
			'left': newLeft,
			'top': newTop
		});
	});
}

function videoVerticalCenter() {
	var centerHeight = ($('.center').height()) ? $('.center').height() : 170;
	var top = (($('#whiteBlockIn').height() - centerHeight) / 2);
	top = (top > 0) ? top : 0;
	$('.center').css('top', top +'px');
}

// Slideshow
function slideShow() {
	// Specify Next Image
	var totalImages = homeImages.length;
	var current = homeCurrent;

	if ( ! window.isActive) {
		clearTimeout(timeoutID);
		timeoutID = setTimeout("slideShow()", 1000);
		return;
	}

	if ( ! $(homeImages[current]).attr('data-loaded')) {
		clearTimeout(timeoutID);
		timeoutID = setTimeout("slideShow()", 200);
		return;
	} else {
		clearTimeout(timeoutID);
	}

	// If there is a caption - slide that out first.
	if ($('#caption').length) {
		clearTimeout(timeoutID);
		$('#caption').stop().animate({bottom:'-150px'}, 500, function() {
			$('div#caption').remove();
			return slideShow();
		});
		return;
	}

	// Switch Image
	if ($('#fotoblockIn img:visible').length) {
		$('#fotoblockIn img:visible').fadeOut(500, function() {
			$(homeImages[current]).fadeIn(500, function() {
				if ($(homeImages[current]).attr('alt')) {
					$('div#fotoblockIn').append('<div id="caption">'+$(homeImages[current]).attr('alt')+'</div>');
					$('div#caption').animate({bottom:0}, 500);
				}
			});
		});
	} else {
		$(homeImages[current]).fadeIn(500, function() {
			if ($(homeImages[current]).attr('alt')) {
				$('div#fotoblockIn').append('<div id="caption">'+$(homeImages[current]).attr('alt')+'</div>');
				$('div#caption').animate({bottom:0}, 500);
			}
		});
	}

	if (homeCurrent == (homeImages.length - 1)) {
		homeCurrent = 0;
	} else {
		homeCurrent++;
	}
	
	clearTimeout(timeoutID);
	if (totalImages > 1) {
		timeoutID = setTimeout("slideShow()", slideshowTimer);
	}
}
		
function videoVerticalAlignImages() {
	videoHeight = 150;
	
	$('.video img').each(function() {
		var imgHeight = $(this).height();
		var marginTop = (videoHeight - imgHeight) / 2;
		$(this).css('margin-top', marginTop);
	})
}

// Slideshow
function gallerySlideShow(photo, go) {
	// Specify Next Image
	var totalImages = homeImages.length;
	var current = (photo != undefined) ? photo : homeCurrent;

	if ( ! window.isActive) {
		clearTimeout(timeoutID);
		timeoutID = setTimeout("gallerySlideShow("+photo+")", 1000);
		return;
	}

	if ( ! $(homeImages[current]).attr('data-loaded')) {
		clearTimeout(timeoutID);
		timeoutID = setTimeout("gallerySlideShow("+photo+")", 200);
		return;
	} else {
		clearTimeout(timeoutID);
	}

	// If there is a caption - slide that out first.
	if ($('#caption').length) {
		$('#caption').animate({bottom:'-150px'}, 500, function() {
			$('#caption').remove();
			return gallerySlideShow(photo);
		});
		return;
	}

	// Switch Image
	if ($('#fotoblockIn img:visible').length) {
		$('#fotoblockIn img:visible').fadeOut(500, function() {
			$(homeImages[current]).fadeIn(500, function() {
				if ($(homeImages[current]).attr('alt').length > 0) {
					$('#fotoblockIn').append('<div id="caption">'+$(homeImages[current]).attr('alt')+'</div>');
					$('#caption').animate({bottom:0}, 500);
				}
			});

			// Manipulate thumbs
			$("#photoSlider a, #photoSlider img").removeClass('current');

			id = $(homeImages[current]).attr('id');
			$('a[rel="'+id+'"]').addClass('current');
		});
	} else {
		$(homeImages[current]).fadeIn(500, function() {
			if ($(homeImages[current]).attr('alt').length > 0) {
				$('#fotoblockIn').append('<div id="caption">'+$(homeImages[current]).attr('alt')+'</div>');
				$('#caption').animate({bottom:0}, 500);
			}
		});

		// Manipulate thumbs
		$("#photoSlider a, #photoSlider img").removeClass('current');

		id = $(homeImages[current]).attr('id');
		$('a[rel="'+id+'"]').addClass('current');
	}

	if ( photo == undefined ) {
		if (homeCurrent == (homeImages.length - 1)) {
			homeCurrent = 0;
		} else {
			homeCurrent++;
		}

		clearTimeout(timeoutID);
		if (totalImages > 1) {
			timeoutID = setTimeout("gallerySlideShow()", slideshowTimer);
		}
	} else {
		if (photo == (homeImages.length - 1)) {
			homeCurrent = 0;
		} else {
			homeCurrent = photo+1;
		}

		clearTimeout(timeoutID);
		timeoutID = 0;
		$('#startStop').html('START');
	}
}

function showSliderControls() {
	var imageCount = $('#sliderPack img').length;
	var sliderHeight = ((58 + 10) * (imageCount / 3)) + 60; // Thumb Height + Padding * Image Rows + space for controls

	if (imageCount < 2) {
		$('#move #startStop').hide();
	}


	if (($('#leftBlock').height() - 220 - 60) > sliderHeight) {
		$('#move #left, #move #right').css('visibility', 'hidden');
	} else {
		$('#move #left, #move #right').css('visibility', 'visible');
	}
}

jQuery(document).ready(function(){
	// Resize On Start Up
	$('.fifty').css('width', '50%');
	resizeImages($('.alignLeft').length);
	if ($(window).height() > $('#whiteBlock').height()) {
		$('#whiteBlock').css('margin-top', (($(window).height() - $('#whiteBlock').height() + 20) / 2)+'px');
	}

	// Resize on Resize
	$(window).resize(function() {
		$('.fifty').css('width', '50%');
		if ($('#rightBlock img').length) {
			resizeImages($('.alignLeft').length);
		}
		if ($(window).height() > $('#whiteBlock').height()) {
			$('#whiteBlock').css('margin-top', (($(window).height() - $('#whiteBlock').height() + 20) / 2)+'px');
		}
	});

	// Gallery Controls
	if ($('#sliderPack').length) {
		showSliderControls();
		$(window).resize(function() {
			showSliderControls();
		});
	}

	$('#startStop').click(function() {
		if ($(this).html() == 'STOP') {
			clearTimeout(timeoutID);
			timeoutID = 0;
			$(this).html('START');
		} else {
			gallerySlideShow();
			$(this).html('STOP');
		}

		return false;
	});

	// Get the slideshow running.
	if ($('.slide').length && $('#rightBlock img').length > 1) {
		timeoutID = 0;
		homeCurrent = 0;
		homeImages = $('#rightBlock img');
		slideShow();
	} else {
		if ( ! $('#photoSlider').length) {
			$('#fotoblock img').first().fadeIn();
		}
	}

	/**
     * Password Page
	 */
	$('#passwordForm').submit(function(event) {
		event.preventDefault();
		var password = $('input[name="password"]').val();
		$.ajax({
			type: "POST",
			url: $(this).attr('data-cms') + "/sproof_dbaccess.php",
			data: "password="+password,
			success: function(msg){
				if (msg.length == 0) {
					$('#password').addClass('error');
				} else {
					window.location.replace('proofing/login.php?gallery'+msg);
				}
			}
		});
		return false;
	});

	$('#password').change(function() {
		$('#password').removeClass('error');
		$('label.error').remove();
	});
	/**
     * End
	 */


	/**
     * Main Menu
	 */
	$('#nav li a').click(function() {
		if ($(this).attr('href') == '#') {
			$(this).parent().find('ul').toggle(50);
			if (isiPad() && $(this).parent().find('ul:visible').length) {
				$(this).css({
					'background' : $(this).attr('data-background'),
					'color' : $(this).attr('data-rollover')
				});
			}

			return false;
		}
	});

	// Prepare Backgrounds
	$('#nav a:not(#nav li li a)').each(function() {
		$(this).prepend('<div class="bg" style="background-color:' + $(this).attr('data-background') + ';display:none"></div>');
		var span = $(this).find('span').html();
		$(this).find('span').css('visibility', 'hidden');
		$(this).prepend('<span style="position:absolute">'+span+'</span>');
	});

	if ( ! isiPad()) {
		$('#nav a:not(#nav li li a)').hover(function() {
			$(this).find('div').fadeIn(300);
			$(this).css('color', $(this).attr('data-rollover'));
		}, function() {
			if ( ! $(this).hasClass('selected') && ! $(this).parent().find('ul').is(':visible')) {
				$(this).find('div').fadeOut(300);
				$(this).css('color', $(this).attr('data-color'));
			}
		});

		$('#nav li li a').css('color', '#333');
		$('#nav li li a').hover(function() {
			//$(this).css('color', $(this).attr('data-rollover'));
			$(this).css('color', '#000');
		}, function() {
			if ( ! $(this).hasClass('selected')) {
				//$(this).css('color', $(this).attr('data-color'));
				$(this).css('color', '#333');
			}
		});
	}

	$('#nav a.selected').css('color', $('#nav a.selected').attr('data-rollover')).find('div').show();

	var parent = $('#nav li').has('.selected').find('a').first();
	if (parent.length == 1) {
		$(parent).addClass('selected').css('color', $('#nav a.selected').attr('data-rollover')).find('div').show();
	}

	//$('#nav li li a.selected').css('color', $('#nav li li a.selected').attr('data-rollover'));
	$('#nav li li a.selected').css('color', '#000');

	/**
     * Menu
	 */
	if ($('.sliderIn').length) {
		timeoutID = 0;
		homeCurrent = 0;
		homeImages = $('#fotoblockIn img');
		if ( slideshowStart) {
			gallerySlideShow();
		} else {
			gallerySlideShow(0);
		}
	}

    $(".sliderIn a").click(function(){
		if ($(this).attr("class") != "current") {
			gallerySlideShow($('#'+$(this).attr('rel')).index());
			clearTimeout(timeoutID);
		} else {
			clearTimeout(timeoutID);
		}
		return false;
    });

    if ($("#photoSlider")) {
		$("#photoSlider").css("overflow", "hidden");
		var slider=0;
		var sliderOriginal=0;

		$("#left").click(function(){
			var imageCount = $('#sliderPack img').length;
			slider = ((57 + 10) * Math.ceil(imageCount / 3)) - $("#photoSlider").height();
			if ($('.sliderIn').css('top') != 'auto') {
				var pos = $('.sliderIn').css('top');
				slider = slider - pos.substring(1, (pos.length-2));
			}

			//slider = $(".sliderIn").height()-$("#photoSlider").height();
			sliderOriginal = slider;

			if ( ! $(this).hasClass('off')) {
				if (slider>100) {
					$(".sliderIn").animate({"top": "-=100px"});
					slider = slider - 100;
				} else {
					$(".sliderIn").animate({"top": "-="+slider+"px"});
					slider = 0;
				};
			}
			if (slider==0) {
				$("#left").addClass("off");
			};
			if (slider>0) {
				$("#right").removeClass("off");
			};
			return false;
		});

		$("#right").click(function(){
			var imageCount = $('#sliderPack img').length;
			slider = ((58 + 10) * (imageCount / 3)) - $("#photoSlider").height();
			if ($('.sliderIn').css('top') != 'auto') {
				var pos = $('.sliderIn').css('top');
				slider = slider - pos.substring(1, (pos.length-2));
			}
			//slider = $(".sliderIn").height()-$("#photoSlider").height();
			sliderOriginal = slider;

			slider = slider + 100;
			if (slider < sliderOriginal) {
				$(".sliderIn").animate({"top": "+=100px"});
			} else {
				$(".sliderIn").animate({"top": "0px"});
				slider=sliderOriginal;
			};
			if (slider > 0) {
				$("#left").removeClass("off");
			};
			if (slider == sliderOriginal) {
				$("#right").addClass("off");
			};
			return false;
		});
	};

	/**
     * Video Page
	 */
	if ($('#videos').length) {
		// Correct Video Width
		var videoCount = $('#videos .video').length;
		var center = $('#videos .center');
		
		if ( $.browser.msie && $.browser.version == 7.0 ) {
			videoVerticalAlignImages();
		}

		if (videoCount == 1) {
			$(center).css('width', '170px');
		} else if(videoCount == 2) {
			$(center).css('width', '340px');
		}

		if ($(window).height() < 520) {
			$('#videos .clear').remove();
			if ($('#videos').width() < 340) {
				videoNavigation(2);
			} else {
				var count = 1;
				var videoCount = $('#videos .video').length;
				videoNavigation(3);
				$('#videos .video').each(function() {
					if(count % 3 == 0 || count == videoCount) {
						$(this).after('<div class="clear"></div>');
					}
					count++;
				});
			}
		} else {
			$('#videos .clear').remove();
			videoNavigation(6);
			var count = 1;
			var videoCount = $('#videos .video').length;
			$('#videos .video').each(function() { 
				if(count % 3 == 0 || count == videoCount) {
					$(this).after('<div class="clear"></div>');
				}
				count++;
			});
		}
		
		videoVerticalCenter()

		$(window).resize(function() {
			if ($(window).height() < 520) {
				$('#videos .clear').remove();
				if ($('#videos').width() < 340) {
					videoNavigation(2);
				} else {
					videoNavigation(3);
					var count = 1;
					var videoCount = $('#videos .video').length;
					$('#videos .video').each(function() {
						if(($(this).index() + 1) % 3 == 0 || count == videoCount) {
							$(this).after('<div class="clear"></div>');
						}
						count++;
					});
				}
			} else {
				$('#videos .clear').remove();
				videoNavigation(6);
				var count = 1;
				var videoCount = $('#videos .video').length;
				$('#videos .video').each(function() {
					if(($(this).index() + 1) % 3 == 0 || count == videoCount) {
						$(this).after('<div class="clear"></div>');
					}
					count++;
				});
			}
			$('#videos').css('height', $('#rightBlock').height());

			videoVerticalCenter();
		});
	}

	function videoNavigation(inc) {
		$('.video').hide();
		$('#video-controls').remove();

		var i = 0;
		var currInc = 1;
		$('.video').each(function() {
			$(this).attr('class', 'video');

			if (i == (inc * currInc)) {
				currInc++;
			}

			$(this).addClass('group'+currInc);
			i++;
		});

		if (currInc > 1) {
			var videoControls = '<div id="video-controls">';
			for(e=1; e <= currInc; e++) {
				videoControls += '<a href="#" class="group'+e+'">'+e+'</a>';
			}
			videoControls += '</div>';
			$('#videos .center').append(videoControls);

			$('#video-controls a').click(function(){
				$('#video-controls a').attr('id', '');
				$(this).attr('id', 'controlOn');
				$('.video').hide();
				$('.'+$(this).attr('class')).css('display', 'inline-block');
				return false;
			});

			$('#video-controls a:first-child').attr('id', 'controlOn');
		}

		$('.group1').css('display', 'inline-block');
	}

	$("#videos a").click(function() {
		if ($(this).hasClass('youtube')) {
			$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'			: $(this).attr('data-width'),
				'height'		: $(this).attr('data-height'),
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
					'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});

			return false;
		}

		if ($(this).hasClass('vimeo')) {
			$.fancybox({
				'width' : parseInt($(this).attr('data-width')),
				'height' : parseInt($(this).attr('data-height')),
				'href' : 'http://player.vimeo.com/video/' + $(this).attr('data-video'),
				'autoScale' : true,
				'title'	: this.title,
				'transitionIn' : 'none',
				'transitionOut' : 'none',
				'type' : 'iframe',
				'padding' : 0
			});

			return false;
		}

		return false;
	});
	/**
     * End
	 */
    
    /**
     * Contact Page
	 */
	$('#contactForm').submit(function(evt) {
		$('.error').removeClass('error');

		if ($('#contactForm .required').length) {
			$('.required').each(function() {
				if ($(this).val() == '') {
					$(this).parent().addClass('error');
				}
			});

			if ($('.error').length) {
				evt.preventDefault();
			}
		}
	});

	$('#contactForm .required').change(function() {
		if ($(this).val() == '') {
			$(this).parent().addClass('error');
		} else {
			$(this).parent().removeClass('error');
		}
	});
	/*
     * End
	 **/
});
