
function copyright_popup() {
	var message = 'Professional portraits are copyright protected.  Images on the Olan Mills Image Center are low resolution, intended for electronic viewing and sharing only. Please visit the Olan Mills Shop at http://shop.olanmills.com/ to purchase additional prints.';
	alert(message);
	return true;
}

if(!window['om']) {
	window['om'] = {};
}

var om = window['om'];

om.browserSucks = (jQuery.browser.msie && jQuery.browser.version < 7);

om.rotateImageCenter = function(){
	var items = $("#gal-images .gal-items .item");
	if(!items.length) { return; }
	
	function cleanURL(src){
		var parts = src.split("?");
		var cleanQuery = [];
		if(typeof parts[1] != 'undefined'){
			queries = parts[1].split("&");
			for(var i = 0; i < queries.length; i++){
				cur = queries[i].split("=")[0];
				if((cur != "rotate") && (cur != "v")){
					cleanQuery.push(queries[i]);
				}
			}
		}

		if(cleanQuery.length > 0){
			result = parts[0] + "?" + cleanQuery.join("&");
		} else {
			result = parts[0];
		}
		return result;
	}
	
	function addQuery(src, q){
		var parts = src.split("?");
		var query = [];
		if (typeof parts[1] != 'undefined'){
			query = query.concat(parts[1].split("&"));
		}
		query.push(q);
		result = parts[0] + "?" + query.join("&");
		
		return result;
	}
	
	function rotateImage(image, dir){
		var src = cleanURL($(image).attr("href"));
		var command = addQuery(src, "rotate=" + dir + "&callback=?");
		$.getJSON(command, function(data){
				res = addQuery(src, "v=" + new Date().getTime());
				$(image).attr("href", res);
				$(image).find("img").attr("src", res);
		});
	}
	
	items.each(function(){
		var that = $(this);
		
		that.find('.rotate_cw').click(function(e){
			e.preventDefault();
			var image_wrap = that.find(".image");
			rotateImage(image_wrap, "cw");
		});
		
		that.find('.rotate_ccw').click(function(e){
			e.preventDefault();
			var image_wrap = that.find(".image");
			rotateImage(image_wrap, "ccw");
		});
		
	});
};

om.signupPlaceholders = function(){
	$("input[placeholder]").each(function(){
		if($(this).val() == ""){
			$(this).val($(this).attr("placeholder"));
			$(this).focus(function(){
				if($(this).val() == $(this).attr("placeholder")) {
					$(this).val("");
				}
			});
			$(this).blur(function(){
				if($(this).val() == ""){
					$(this).val($(this).attr("placeholder"));
				}
			});
		}
	});
	
	$('form').submit(function(e){
		$('input[placeholder]').each(function(){
			if($(this).val() == $(this).attr("placeholder")){
				$(this).val("");
			}
		});
	});
};

om.locations = function() {
	if(!$('html#map').size()) { return; }
	
	var locations = $('#sb.locations > ul li');
	var map = $('#map-container');
	locations.each(function() {
		var location = $(this);
		var link = location.find('.title a');
		link.click(function(c) { c.preventDefault(); });
		location.click(function() {
			//	If we didn't come from web appointments set studio in background
			if (!om.u.match(/appointments/)) {
				$.ajax({
					type: "GET",
					url: "/locations/svc_set_studio.asp",
					data: "s=" + $(this).attr("id"),
					dataType: "text",
					success: function() {
						if (om.u != '') {
							top.location = om.u;
						}
					}
				});
			}
		

		
			if(map.find('div.location-info').size()) {
				//alert('remove');
				map.find('div.location-info').animate({'bottom':'0px'},500, function() { map.find('div.location-info').remove(); });
				var timeout = 550;
			} else {
				var timeout = 0;
			}
				
			om.map_overlay_visible = $(this).attr("id");
			setTimeout(function(){
				var info = $(jQuery('<div class="location-info"></div>'));
				info.html(location.html());
				info.css({'bottom':'-300px'}).appendTo(map).animate({'bottom': '0'},500);
			},timeout);
				
			if (typeof(om.mapMarkers) != "undefined" && typeof(om.mapMarkers[$(this).attr("id")]) != "undefined") {
				GEvent.trigger(om.mapMarkers[$(this).attr("id")], "click");
			}
			
		});
	});
};

om.gallery = function() {
	if(!$('body.gallery').size()) { return; }
	var animating = false;
	$('#main > .gallery_wrapper > .gallery-image > img').wrap($(jQuery('<div id="gallery-image" class="loading loading-light"></div>')));
	om.gallery.target = $('#gallery-image');

	

	var thumbs = $('a[rel=gallery]');
	
	var container = $(jQuery('<div class="thumbs-container"></div>'));
	container.prependTo($('#main'));
	container.html(thumbs.parent('div.thumbs'));
	
	var slider = $(jQuery('<div class="slider" style="left: 0"></div>'))
	slider.width(96 * thumbs.size());
	
	slider.appendTo(thumbs.parent());
	thumbs.each(function() {
		$(this).appendTo(slider);
	});
	
	var prev = $(jQuery('<a href="#prev" class="prev disabled">Previous</a>'));
	var next = $(jQuery('<a href="#next" class="next">Next</a>'));
	
	container.prepend(prev);
	container.append(next);
	
	prev.add(next).each(function() {
		var link = $(this);
		link.click(function(c) {
			c.preventDefault();
			if(slider.is(':animated')) { return; }
			var newX = parseInt(slider.css('left'),10) - (link.hasClass('prev') ? (0 - 576) : 576);
			prev.add(next).removeClass("disabled");
			if(newX >= 0) {
				newX = 0;
				prev.addClass("disabled");
			} else if(newX <= (0 - slider.width()) + 576) {
				newX = (0 - slider.width()) + 576;
				next.addClass("disabled");
			}
			slider.animate({
				'left': newX
			},1000);
		});
	});
	
	$(".gallery-image").click(function(c){
		c.preventDefault();
		if(animating) {return;}
		animating = true;
		n = thumbs.filter('.current').next();
		if(n.length){
			loadImage(n);
			if(!visible(n.position().left)){
				next.click();
			}
		} else {
			loadImage(thumbs.first());
			slider.animate({'left':0});
			next.removeClass("disabled");
			prev.addClass("disabled");
		}
	});
	
	
	function loadImage(thumb){
		if(thumb.hasClass('current')) { 
			animating = false;
			return; 
		}
		thumbs.removeClass('current');
		thumb.addClass('current');
		var currentImage = om.gallery.target.find('img');
		currentImage.fadeOut('normal',
			function() {
				var temp = new Image();
				temp.onload = function() {
					$(temp).css({'display':'none'});
					if(temp.width < temp.height) {
						om.gallery.target.parents('.gallery-image').removeClass('gallery-image-landscape').addClass('gallery-image-portrait');
					} else {
						om.gallery.target.parents('.gallery-image').removeClass('gallery-image-portrait').addClass('gallery-image-landscape');
					}
					
					om.gallery.target.append($(temp))
					$(temp).fadeIn('normal',
						function() {
							currentImage.remove();
							animating = false;
						}
					);
				}
				temp.src = thumb.attr('href');
			}
		)
	}
	
	
	function visible(pixel){
		rangeStart = Math.abs($(".slider").position().left);
		rangeEnd = rangeStart + 566;
	 	if(pixel < rangeEnd && pixel >= rangeStart){
			return true;
		} else {
			return false;
		}
	}
	
	thumbs.each(function() {
		var thumb = $(this);
		thumb.click(function(c) {
			c.preventDefault();
			if(animating) {return;}
			animating = true;
			loadImage(thumb);
		});
	});

	thumbs.eq(0).click();
};

(function($){
	$.fn.omHeroshots = function(options){
		var defaults = {
			heroWrapper:     ".heroes",
			heroClass:      ".heroshot",
			heroControl:    ".hero_control",
	 		duration: 8 
		};
	
		var options = $.extend(defaults, options);
		this.each(function(){
			var obj = $(this);
			var animating = false;
			var slides = $(options.heroClass, obj);
			var count = slides.length;
			var width = $(options.heroClass, obj).width();
			$(options.heroWrapper, obj).css({position: 'relative', width: count*width});
			var last = count-1;
			var current = 0;
			var timeout;  		//Yeah, we'll need this to keep track of such.
			
			
			//Our heroshot callouts can vary in height but need to be vertically centered.
			//Calls for some JS action
			//$(options.heroClass, obj).find(".callout").each(function(index, nObj){
				//$(nObj).css({top: $(nObj).parent().height()/2 - $(nObj).height()/2});
			//});
			
			// Build a direct nav structure
			var nav = $("<div class='hero_control' style='position: absolute; z-index: 10;'></div>");
			nav.append("<div class='left_cap'></div>");
			holder = $("<div class='control_holder'></div>");
			for (var i = 0; i < count; i++) {
				nav_item = $("<div class='hero_indicator' style='cursor: pointer; float: left;' id='hs_"+ i +"'>"+ i + "</div>");
				holder.append(nav_item);
				nav_item.click({index: i}, function(e){
					if(animating) {return;}
					animate2(e.data.index, true);
				});
			}
			nav.append(holder);
			nav.append("<div class='right_cap'></div>");
			$(this).parent().append(nav);
			var that = $(this);
			nav.css({
				top: that.position().top + that.height() - 10,
				left: (that.width()/2) - (nav.width()/2)  + that.position().left
			});
			
			
			function setSelected(slide){
				$(".hero_indicator").removeClass("selected");
				if(slide !== undefined){
					$("#hs_" + slide).addClass("selected");
				} else {
					$("#hs_0").addClass("selected");
				}
			}
			
			function animate2(newSlide, manual){
				if(animating) {return;}
				animating = true;
				var previous = current;
				current = (newSlide !== undefined) ? newSlide : ((previous >= last) ? 0 : current + 1);
				if(timeout) clearTimeout(timeout);
				curSlide = $(slides.eq(previous));
				newSlide = $(slides.eq(current));
				slideForward = ((current - previous > 0) || (previous === last && current === 0 && manual !== true )) ? true : false;
				if (slideForward){
					cssDirection = "-=";
					cssPos = width;
				} else {
					cssDirection = "+=";
					cssPos = 0-width;
				}
				newSlide.css({left: cssPos, "z-index": 10});
				newSlide.animate({left:  cssDirection + width}, 1600);
				curSlide.animate({left:  cssDirection + width}, 1600, function(){
						curSlide.css({left: 0, "z-index": 0});
						setSelected(current);
						animating = false;
						timeout = setTimeout(function(){
							animate2();
						}, options.duration * 1000);
				});
			}
			
			//function animate(newSlide){
			//	var previous = current;
			//	//Let's figure out which one we're going to.
			//	current = (newSlide !== undefined) ? newSlide : ((previous >= last) ? 0 : current + 1);
			//	if(timeout) clearTimeout(timeout);
			//	
			//	//Now, some maths
			//	var difference = Math.abs(previous - current);
			//	var speed = 1600 * difference;
			//	nLeft = (current * width * -1);
			//	$(options.heroWrapper, obj).animate( {left: nLeft}, speed);
			//	setSelected(current);
			//	timeout = setTimeout(function(){
			//		animate();
			//	}, options.duration * 1000);
			//}
			
			setSelected(0);
			slides.first().css({"z-index": 10});
			timeout = setTimeout(function(){
				animate2();
			}, options.duration * 1000);
			
		});
	};
})(jQuery);


$(function(){
	om.lightbox = function() {
		if(typeof ajax == 'undefined') { ajax = false; }
		if(typeof html == 'undefined') { html = ''; }
		if(!ajax) { om.lightbox.links = $('a[rel*=lightbox]') } else if(typeof html == 'object') { om.lightbox.links = html.find('a[rel*=lightbox]'); } else { return; }
		if(!om.lightbox.links.size()) { return; }
		if(!$('#overlay').size()) { $('body').append($(jQuery('<div id="overlay" style="display: none"></div>'))); }
		om.lightbox.overlay = $('#overlay');
		om.lightbox.overlay.click(function() { om.lightbox.closeLightbox(); });
		if(!$('#popup').size()) { $('body').append($(jQuery('<div id="popup" class="lightbox" style="display: none"></div>'))); }
		om.lightbox.popup = $('#popup');
		om.lightbox.closeLightbox = function() {
			om.lightbox.popup.fadeOut('normal', function(){ om.browserSucks ? om.lightbox.overlay.hide() : om.lightbox.overlay.fadeOut('normal') });
			om.lightbox.popup.fadeOut('normal', function() {
				om.lightbox.target.find('img').remove();
			});
		}

		if(!om.lightbox.popup.find('a.close').size()) {
			om.lightbox.close = $(jQuery('<a class="close" href="#close">Close</a>'));
			om.lightbox.close.click(function(c){ c.preventDefault(); om.lightbox.closeLightbox(); });
			om.lightbox.close.appendTo(om.lightbox.popup);
		}

		if(typeof om.lightbox.target == 'undefined') {
			om.lightbox.target = $(jQuery('<div class="content-wrapper"></div>'));
			om.lightbox.popup.append(om.lightbox.target);
		}

		om.lightbox.links.each(function(i){
			var link = $(this);
			link.click(function(c){
				c.preventDefault();
				link.blur();
				om.lightbox.links.removeAttr('current');
				link.attr({'current': 'current'});
				var img = new Image();
				img.onload = function() {
					$(img).appendTo(om.lightbox.target);
					om.browserSucks ? om.lightbox.overlay.show() : om.lightbox.overlay.fadeIn('normal');
					var new_css = {'width': img.width,'height': img.height,'margin-left': (0 - Math.round(img.width / 2)) + 'px','margin-top': (0 - Math.round(img.height / 2)) + 'px'};
					om.lightbox.popup.css(new_css);
					om.lightbox.popup.fadeIn('normal');
					if(ajax) { om.lightbox.overlay.find('.loading').remove() };
				}
				img.src = link.attr('href');
			})
		})
		om.lightbox.links.filter('[current]').size() ? null : $(om.lightbox.links[0]).attr({'current':'current'});
	}
	
	
	om.signupPlaceholders();
	om.gallery();
	om.rotateImageCenter();
	om.locations();
	om.lightbox();
	$(".hero").omHeroshots();
	$('a').click(function() { $(this).blur(); });
});

