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

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

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;
}
		
function videoVerticalAlignImages() {
	videoHeight = 150;
	
	$('.video img').each(function() {
		var imgHeight = $(this).height();
		var marginTop = (videoHeight - imgHeight) / 2;
		$(this).css('margin-top', marginTop);
	})
}

jQuery(document).ready(function() {
	if (isiPad()) {
		$('#videos .center').css({position:'absolute',right:'20px'});
	}

	$(".sliderIn img").click(function(evt) {
		evt.preventDefault();
		if ($(this).css('opacity') != 1) {
			clearTimeout(timeoutID);
			$('#slide').addClass('slideOff');
			var newBg = '#' + $(this).attr('class');

			$('.sliderIn img').css('opacity', 0.7);
			$(this).css('opacity', 1);

			$("#fotoblock img").fadeOut();
			$('#caption').remove();

			if ($(newBg).attr('data-background') != '') {
				$('body').animate({'background-color':'#'+$(newBg).attr('data-background')}, 400);
			}
			$(newBg).fadeIn("slow");
			if ($(newBg).attr('alt').length > 0) {
				$('body').append('<div id="caption">'+$(newBg).attr('alt')+'</div>');
			}
		}
	});

	// Start / Stop Slideshow.
	$('#slide').live('click', function(evt) {
		evt.preventDefault();
		if ( ! $(this).hasClass('slideOff')) {
			clearTimeout(timeoutID);
			$(this).addClass('slideOff');
		} else if ($('#fotoblock img').length > 1) {
			id = $('#fotoblock img:visible').attr('id').substring(4, 7);
			galleryCurrent = id;
			gallerySlideshow();

			$(this).removeClass('slideOff');
		}
	});

	$('#nav a[href="#"]').click(function(event) {
		$(this).next().toggle();
		return false;
	});
	if (menuItemBox) {
		$('#nav a').each(function() {
			$(this).css('background-color', $(this).attr('data-color'));
		});
	
		$('#nav a').hover(function() {
			//$(this).attr('data-color', $(this).css('background-color'));
			$(this).animate({'background-color':$(this).attr('data-rollover')},200);
		},function() {
			$(this).animate({'background-color':$(this).attr('data-color')},200);
		});
	
		$('#nav a.selected').css('background-color', $('#nav a.selected').attr('data-rollover')).attr('data-color', $('#nav a.selected').attr('data-rollover'));
	
		$('#nav li li a.selected').parent().parent().show().siblings('a').css('background-color',$('#nav a.selected').attr('data-rollover')).attr('data-color', $('#nav a.selected').attr('data-rollover'));
	}
	
	var numVideos = $('.video').length;
	if ($('#videos').length) {
		if ($(window).height() < 550) {
			videoNavigation(3);
		} else {
			videoNavigation(6);
		}
		//top = (top > 0) ? top : 0;
		//$('.center').css('top', top +'px');
		videoVericalCenter();
		
		if ( $.browser.msie && $.browser.version == 7.0 ) {
			videoVerticalAlignImages();
		}

		$(window).resize(function() {
			if ($(window).height() < 550) {
				videoNavigation(3);
			} else {
				videoNavigation(6);
			}

			$('#videos').css('height', $(window).height());
			videoVericalCenter();
		});
		
		if (numVideos == 1) {
			$('#videos .center').width(170);
		}
		
		if (numVideos == 2) {
			$('#videos .center').width(360);
		}
	}

	function videoNavigation(inc) {
		$('.video').hide();
		$('#video-controls').remove();
		$('#videos .clear').remove();
		
		var count = 1;
		$('#videos .video').each(function() {
			if(count % 3 == 0) {
				$(this).after('<div class="clear"></div>');
			}
			count++;
		});
		
		$('.center').append('<div class="clear"></div>');

		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;
			});
		}
		
		var count = 1;

		$('.group1').css('display', 'inline-block');
		$('#video-controls a.group1').attr('id', 'controlOn');
	}

	if ($('#textBlock').length) {
		$(window).resize(function() {
			resizeText();
		});
		resizeText();
	}

	function resizeText() {
		$('#textBlock').css('height', ($('body').height()-160)+'px');
	}

	// Organise Homepage images.
	if ($('#fotoblock').length == 1) {
		positionImages();

		$(window).resize(function() {
			positionImages();
		});

		// Home Page Slideshow
		if ( ! $('#fotoblock').hasClass('no-slide')) {
			timeoutID = 0;
			homeCurrent = 0;
			homeImages = $('#fotoblock img');
			if (homeImages.length > 1) {
				homeSlideshow();
			} else {
				if ($('#fotoblock img').length) {
					loadFirstImage('#fotoblock img');
	 			}
			}
		} else {
			if ($('#fotoblock img').length > 0 && $('#photoSlider').length == 0) {
				$('#fotoblock img:first-child').show();
				$('#slider img:first-child').css('opacity', 1);
				if ($('#fotoblock img:first-child').attr('alt').length > 0) {
					$('body').append('<div id="caption">'+$('#fotoblock img:first-child').attr('alt')+'</div>');
				}
			}
		}
	}

	// Centers Background Images on the Homepage
	function positionImages() {
		var imgWidth, imgHeight;
		var docWidth = $(window).width();
		var docHeight = $(window).height();
		/*
		if ($('.sliderIn').length == 1) {
			var docHeight = docHeight - $('.sliderIn').height();
		}*/

		$('#fotoblock img').each(function() {
			var imgWidth = $(this).attr('width');
			var imgHeight = $(this).attr('height');
			
			if (docHeight > imgHeight) {
				$(this).css('margin-top', ((docHeight - imgHeight) / 2)+'px');
				$(this).css('margin-left', ((docWidth - imgWidth) / 2)+'px');
			} else {
				$(this).css('margin', 0);
				var ratio = docHeight / imgHeight;
				imgHeight = docHeight;
				imgWidth = Math.round(imgWidth * ratio);
				
				if (imgWidth < docWidth) {
					$(this).css('margin-left', ((docWidth - imgWidth) / 2)+'px');
				}
				
				$(this).css({
					'width' : imgWidth,
					'height' : imgHeight
				});
			}
			
			/*
			if (docWidth > imgWidth) {
				$(this).css('margin-top', ((docHeight - imgHeight) / 2)+'px');
				$(this).css('margin-left', ((docWidth - imgWidth) / 2)+'px');
			} else {
				$(this).css('margin', 0);
				var ratio = docWidth / imgWidth;
				imgWidth = docWidth;
				imgHeight = Math.round(imgHeight * ratio);
				$(this).css({
					'width' : imgWidth,
					'height' : imgHeight
				});
			}*/
		});
	}

	if ($("#photoSlider"))  {
		if ( ! isiPad()) {
			$('#photoSlider').animate({'margin-bottom' : '-150px'}, 1000);
		} else {
			$('#photoSlider').css({'height' : '0'});
			$('#photoSlider').before('<div id="toggleThumbs">Show Thumbnails</div>');
		}
		
		$('#toggleThumbs').click(function() {
			if ($('#photoSlider').height() == 0) {
				$('#photoSlider').animate({'height' : '127px'}, 1000, function() {
					$('#toggleThumbs').html('Hide Thumbnails');
					$('#toggleThumbs').animate({'bottom' : '147px'}, 400);
				});
			} else {
				$('#toggleThumbs').animate({'bottom' : '23px'}, 200, function() {
					$('#toggleThumbs').html('Show Thumbnails');
					$('#photoSlider').animate({'height' : '0'}, 1000);
				});
			}
		});
		
		var windowHeight = $(window).height();
		var targetArea = windowHeight - 127 - 21 - 20; // Height - Footer - Thumbs - Error Margin
		$(document).mousemove(function(e) {
			if (e.pageY > targetArea) {
				$('#photoSlider').stop().animate({'margin-bottom' : 0}, 200);
			} else {
				$('#photoSlider').stop().animate({'margin-bottom' : '-150px'}, 200);
			}
		});

		timeoutID = 0;
		galleryCurrent = 0;
		galleryImages = $('#fotoblock img');
		galleryThumbs = $('#photoSlider img');
		if (galleryImages.length > 1 && slideshowStart) {
			gallerySlideshow();
		} else {
			$('#slide').addClass('slideOff');
			
			if ($('#fotoblock img').length) {
				loadFirstImage('#fotoblock img');
 			}
		}

		$("#photoSlider").css("overflow", "hidden");
		var slider=0;
		var sliderOriginal=0;
		$(".sliderIn img").each(function() {
			slider += $(this).width()+3;
		})
		slider = slider-$("#photoSlider").width()+31;
		sliderOriginal = slider;

		if (slider > 0) {
			$('#move #right').removeClass('off').addClass('on');
		}

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

		$("#left").click(function(){
			if ( ! $(this).hasClass('off')) {
				slider = slider + 300;
				if (slider<sliderOriginal) {
					$(".sliderIn").animate({"left": "+=300px"});
				} else {
					$(".sliderIn").animate({"left": "31px"});
					slider=sliderOriginal;
				}
				if (slider>0) {
					$("#right").removeClass("off");
				}
				if (slider==sliderOriginal) {
					$("#left").addClass("off");
				}
			}
			return false;
		});
	};

	$('#passwordForm').submit(function() {
		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');
					if ( ! $('#password label.error').length) {
						$('#password').after('<label for="password" generated="true" class="error" style="display: block; ">This Password is incorrect.</label>');
					}
				} else {
					window.location.replace('proofing/login.php?gallery'+msg);
				}
			}
		});
		return false;
	});

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

	$("#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;
	});

	/**
     * Contact Page
	 */
	$('#contactForm').submit(function(evt) {
		$('div.error').removeClass('error');

		if ($('#contactForm .required').length) {
			$('.required').each(function() {
				if ($(this).val() == '') {
					$(this).parent().addClass('error');
					if ( $(this).parent().find('label').length == 1) {
						$(this).after('<label generated="true" class="error" style="display: block; ">This field is required.</label>');
					}
				}
			});

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

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

function videoVericalCenter() {
	var centerHeight = ($('.center').height()) ? $('.center').height() : 170;
	var top = (($(window).height() - centerHeight) / 2) - 23;
	top = (top > 0) ? top : 0;
	$('.center').css('padding-top', top +'px');
}

// Home page Slideshow
function homeSlideshow() {
	// Specify Next Image
	var totalImages = homeImages.length;

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

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

	// Switch Image
	$(homeImages).fadeOut();
	if ($(homeImages[homeCurrent]).attr('data-background') && $(homeImages[homeCurrent]).attr('data-background') != '') {
		$('body').animate({'background-color':'#'+$(homeImages[homeCurrent]).attr('data-background')}, 400);
	}
	$(homeImages[homeCurrent]).fadeIn();

	// Switch Caption
	if ($('#caption').length) {
		$('#caption').fadeOut();
		$('#caption').remove();
	}

	if ($(homeImages[homeCurrent]).attr('alt')) {
		$('body').append('<div id="caption">'+$(homeImages[homeCurrent]).attr('alt')+'</div>');
	}

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

	clearTimeout(timeoutID);
	timeoutID = setTimeout("homeSlideshow()", slideshowTimer);
}
		
function loadFirstImage(imgs) {
	if ($(imgs).first().attr('data-loaded')) {
		$(imgs).first().fadeIn();
	} else {
		timeoutID = setTimeout(function(){loadFirstImage(imgs)}, 50);
	}
}

// Home page Slideshow
function gallerySlideshow() {
	// Specify Next Image
	var totalImages = galleryImages.length;
	var current = galleryCurrent;

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

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

	// Switch Image
	if ($('#fotoblock img:visible').length) {
		$('#fotoblock img:visible').fadeOut(500, function() {
			$(galleryThumbs).css('opacity', 0.7);
			$(galleryThumbs[current]).css('opacity', 1);
			if ($(galleryImages[current]).attr('data-background') != '') {
				$('body').animate({'background-color':'#'+$(galleryImages[current]).attr('data-background')}, 400);
			}
			$(galleryImages[current]).fadeIn();
		});
	} else {
		$(galleryThumbs).css('opacity', 0.7);
		$(galleryThumbs[current]).css('opacity', 1);
		if ($(galleryImages[current]).attr('data-background') != '') {
			$('body').animate({'background-color':'#'+$(galleryImages[current]).attr('data-background')}, 400);
		}
		$(galleryImages[current]).fadeIn();
	}

	// Switch Caption
	if ($('#caption').length) {
		$('#caption').fadeOut();
		$('#caption').remove();
	}

	if ($(galleryImages[current]).attr('alt')) {
		$('body').append('<div id="caption">'+$(galleryImages[current]).attr('alt')+'</div>');
	}

	if (galleryCurrent == (galleryImages.length - 1)) {
		galleryCurrent = 0;
	} else {
		galleryCurrent++;
	}

	clearTimeout(timeoutID);
	timeoutID = setTimeout("gallerySlideshow()", slideshowTimer);
}
