/* Revision: 2009-05-31 17:01:46
 * Author: Matt Holford <m+a+t+t-@-helenDASHmarieDOTcom>
 */

/* Load appropriate dudes, or jQuery by default */
var HM_HAS_OPTS = false;
if ('object' == typeof(hmOpts)) {
	HM_HAS_OPTS = true;
	if ('boolean' == typeof(hmOpts.jquery) && true == hmOpts.jquery) {
		google.load('jquery', '1');
	}
	if ('boolean' == typeof(hmOpts.maps) && true == hmOpts.maps) {
		google.load('maps', '1');
	}
	if ('boolean' == typeof(hmOpts.prototype) && true == hmOpts.prototype) {
		google.load('prototype', '1.6.0.2');
	}
} else {
	google.load('jquery', '1'); // 1.2.6
}

google.setOnLoadCallback(function() {
	if (!HM_HAS_OPTS) {
		hmOpts = {
			jquery: true,
			prototype: false,
			maps: false
		};
	}
	hm.init(hmOpts);
});

function loadFrame() {
	$("div#containerFrame").show("slow");
}

hm = {
	library: false,
	
	workCats: ["identity", "interactive", "print"],
	workCurrent: "",
	
	createMapMarker: function(point, html) {
		var marker = new GMarker(point);
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html);
		});
		return marker;
	},
	
	debug: function(msg) {
		if ("undefined" != typeof(console)) {
			console.log(msg);
		}
	},
	
	init: function(opts) {
		if ("boolean" == typeof(opts.jquery) && true == opts.jquery) {
			this.library = "jquery";
		} else {
			this.library = "prototype";
		}
		if ("undefined" != typeof(opts.page) && "contact" == opts.page) {
			this.initContact();
		}
		this.initNav();
		this.initMore();
		
		/* Contact page's "Google Maps" link */
		$("#link-gmap").click(function() {
			hm.initMap();
			return false;
		});
	},
	
	initContact: function() {
		/* Set up accordion */
		$("#vertical-container").accordion({
			active: false,
			alwaysOpen: false
		});
	},
	
	initHome: function() {
		//this.initLightbox();
	},
	
	initLightbox: function() {
		var me = this;
		$("a.lightbox").lightBox({
			containerBorderSize: 0,
			imageBlank: '/js/jquery-lightbox-0.5/images/lightbox-blank.gif',
			imageBtnClose: '/images/spacer.gif', //'/js/jquery-lightbox-0.5/images/lightbox-btn-close.gif',
			imageBtnNext: '/images/nav/nav-lightbox-next.png',
			imageBtnPrev: '/images/nav/nav-lightbox-prev.png',
			imageLoading: '/js/jquery-lightbox-0.5/images/lightbox-ico-loading.gif',
			keyToPrev: '`',
			keyToNext: '=',
			txtImage: ''
		});
	},
	
	/* Init Google map */
	initMap: function() {
		if (GBrowserIsCompatible()) {
			$("#wrapper-google-maps").css("display", "block");
			var map = new google.maps.Map2(document.getElementById("container-google-maps"));
			
			var point = new GLatLng(40.69741288774269, -73.98231983184814);
			var html = '<div class="gmaps-marker"><p>Our neighborhood is called RAMBO: Right After the Manhattan Bridge Overpass.  (We didn\'t make it up.) <a class="link" href="http://maps.google.com/maps?f=q&hl=en&geocode=&q=2+Prince+St,+Brooklyn,+NY&sll=40.679128,-73.981241&sspn=0.010496,0.015707&ie=UTF8&z=16&iwloc=addr">Bigger map</a></p></div>';
			var marker = this.createMapMarker(point, html);
			map.addOverlay(marker);
			map.setCenter(new google.maps.LatLng(40.700178463402345, -73.98010969161987), 15);
			marker.openInfoWindowHtml(html);
		}
		
		return false;
	},
	
	initMore: function() {
		$("a.more").hover(
			function() {
				var w = $(this).width();
				$(this).html("<span class=\"more-over\" style=\"display:inline-block;width:" + w + "px;\" title=\"Click for more\">&gt;</span>");
			},
			function() {
				$(this).empty();
				$(this).html("more");
			}
		);
	},
	
	initNav: function() {
		$("#nav-global li a").mousedown(function() {
			if ("nav-global-blog" != $(this).parent().attr("id")) {
				$(this).parent().addClass("selected");
			}
		});
		
		var me = this;
		
		$("#nav-global-blog").click(function() {
			me.pop("http://blog.helen-marie.com", 940, 650);
			return false;
		});
	},
	
	initNews: function() {
		//this.initLightbox();
		
		/*
		$(".list-linkable li").each(function() {
			console.log("News id=" + $(this).attr("id"));
			var url = $("a.more", this).attr("href");
			if (typeof(url) != "undefined" && "" != url) {
				$("a.more,p.dateline,p.summary,h2", this).mouseover(function() {
					$(this).parent().toggleClass("on").toggleClass("off");
					$(this).click(function() {
						top.location = url;
					});
				});
			}
		});
		*/
	},
	
	initProject: function() {
		hm.loadScript('/js/show_layer.js');
		hm.loadScript('/js/jquery.easing.1.1.1.js');
		
		$(document).ready(function() {
			$("span#readON1").click(function() {
				$("div#imageA").fadeOut("fast");
				$("div#imageB").fadeOut("fast");
				$("div#layer-copy").fadeIn(400, function() {
					$("div#layer-copy").fadeIn(100);
				});
				return false;
			}); 
		
			$("a#imgSet1").click(function() {
				$("div#imageB").fadeOut("fast");
				$("div#layer-copy").fadeOut("fast");
				$("div#imageA").fadeIn(400, function() {
					$("div#imageA").fadeIn(100);
				});
				return false;
			}); 
			
			$("a#imgSet2").click(function() {
				$("div#imageA").fadeOut("fast");
				$("div#layer-copy").fadeOut("fast");
				$("div#imageB").fadeIn(400, function() {
					$("div#imageB").fadeIn(100);
				});
				return false;
			});
			
			$("a.close-copy").click(function() {
				$("div#layer-copy").fadeOut("fast");
				$("div#imageA").fadeIn(400, function() {
					$("div#imageA").fadeIn(100);
				});
				readON();
				return false;
			});
			
			$("a#imgSet1").click();
		});
	},
	
	initProjectNav: function() {
		hm.loadScript('/js/jquery.timers.js');
		$("#services-link").mouseover(function() {
			$("#services-coming-soon").removeClass("hide");
			$("#services-link").addClass("hide");
		});
		$("#services-coming-soon").mouseout(function() {
			$("#services-coming-soon").oneTime("5s", function() {
				$("#services-coming-soon").addClass("hide");
				$("#services-link").removeClass("hide");
			});
		});
	},

	initProjectSlides: function(container, nav) {
		var cDiv = "#" + container;
		var nDiv = "#" + nav;
		
		$.fn.cycle.defaults.timeout = 6000;
		$(function() {
			$(cDiv).before('<div id="' + nav + '" class="' + nav + '">').cycle({
				fx:     'fade',
				speed:  'fast',
				timeout: 0,
				pager:   nDiv
			});
		});
	},
	
	initServices: function() {
		/* Set up accordion */
		$("#vertical-container").accordion({
			active: false,
			alwaysOpen: false
		});
	},
	
	initWork: function() {
		var me = this;
		$("#wrapper-grid-work-nav a").click(function() {
			var parts = $(this).attr("id").split("-");
			me.transitionWorkLayer(parts[3]);
			return false;
		});
		$("#wrapper-grid-work-nav a:first").click();
	},
	
	loadScript: function(loc) {
		if (false == this.library || "jquery" == this.library) {
			$('head').append($('<script src="' + loc + '"></script>'));
		} else {
			$$('head')[0].insert('<script src="' + loc + '"></script>');
		}
	},
	
	pop: function(url) {
		var w = 600;
		var h = 400;
		
		if (arguments.length == 3) {
			w = arguments[1];
			h = arguments[2];
		}
		
		window.open(url, '_blank', 'width=' + w + ',height=' + h + ',scrollbars=yes,status=yes,resizable=yes,toolbar=yes,location=yes,menubar=yes');
		return false;
	},
	
	showWorkLayer: function(label) {
		$("#wrapper-work-" + label + " a").addClass("lightbox");
		$("#wrapper-work-" + label).fadeIn("fast");
		$("#grid-work-view-" + label).addClass("selected");
		this.workCurrent = label;
	},
	
	transitionWorkLayer: function(label) {
		var me = this;
		if ("" == this.workCurrent) {
			me.showWorkLayer(label);
			me.initLightbox();
		}
		else if (label != this.workCurrent) {
			$("#grid-work-view-" + this.workCurrent).removeClass("selected");
			$("#wrapper-work-" + this.workCurrent + " a").removeClass("lightbox");
			$("#wrapper-work-" + this.workCurrent).fadeOut("fast", function() {
				me.showWorkLayer(label);
				me.initLightbox();
			});
		}
	}
}

function checkInput(elm, text, mode) {
	if (elm.value == "" && mode == "off") {
		return text;
	}
	if (elm.value == text && mode == "on") {
		return "";
	}
	return elm.value;
}
