if(!rarebrick) var rarebrick = new Object();

rarebrick.W;
rarebrick.H;

rarebrick.wRef = 1300;
rarebrick.hRef = 700;
rarebrick.scaleX = 1;
rarebrick.scaleY = 1;
rarebrick.scale = 1;

rarebrick.init = function() {	
	rarebrick.onresize();
	
	rarebrick.photoTimer = setTimeout('rarebrick.initPhotos()', 500);
	
	jQuery('#navWrap div.nav1 a').each(rarebrick.fixOffsiteLinks);
	jQuery('div.footer ul a').each(rarebrick.fixOffsiteLinks);
	jQuery('a[href^="javascript:"]').each(rarebrick.fixJSLinks);
}

rarebrick.fixJSLinks = function() {
	var a = jQuery(this);
	var href = a.attr('href').replace('javascript:','');
	a.attr('href','#').click(function() {eval(href);return false;} );
}

rarebrick.addOnresize = function(func) {
	if(!rarebrick.onresizeFuncs) rarebrick.onresizeFuncs = new Array();
	rarebrick.onresizeFuncs.push(func);
}

rarebrick.onresize = function() {
	rarebrick.calcSizes();
	
	rarebrick.resizeContent();
	rarebrick.adjustFontSize();
	
	if(rarebrick.currentPhoto) jQuery(rarebrick.currentPhoto).each(rarebrick.resizePhoto);
	
	if(rarebrick.onresizeFuncs) {
		for(var i=0; i<=rarebrick.onresizeFuncs.length; i++) {
			try {
				rarebrick.onresizeFuncs[i]();
			} catch(e) {}
		}
	}
}

rarebrick.adjustFontSize = function() {
	var size = rarebrick.scaleY;
	if(size<.7) size = .7;
	if(size>1) size = 1;
	size = Math.round(size*100);
	jQuery('body').css('font-size', size+'%');
}

rarebrick.calcSizes = function() {
	rarebrick.W = jQuery('#wrap').width();
	rarebrick.H = jQuery('#wrap').height() - jQuery('#footerWrap').height();
	
	rarebrick.scaleX = rarebrick.W / rarebrick.wRef;
	rarebrick.scaleY = rarebrick.H / rarebrick.hRef;
	rarebrick.scale = (rarebrick.scaleX + rarebrick.scaleY) / 2;
	
	rarebrick.resizePhotosWrap();
}

rarebrick.resizePhotosWrap = function() {
	jQuery('#photosWrap').css('height', rarebrick.H+'px').css('width', rarebrick.W+'px');
}

rarebrick.getNav1Width = function() {
	return jQuery('#navWrap div.nav1Wrap').width();
}
rarebrick.getNav2Width = function() {
	return jQuery('#navWrap div.nav2Wrap').width();
}
rarebrick.getNav3Width = function() {
	return jQuery('#navWrap div.nav3Wrap').width();
}
rarebrick.getContentWidth = function() {
	return jQuery('#contentWrap').width();
}

rarebrick.specialsTimer = 0;
rarebrick.specialsIsOn = false;

rarebrick.currentSpecial = null;
rarebrick.allSpecials = null;
rarebrick.specialsOpen = false;
rarebrick.showSpecial = function(which) {
	//alert('showSpecial: ' + which);
	if(!which && rarebrick.specialsOpen) return;
	
	var newSpecial;
	
	if(!which && !rarebrick.currentSpecial) {
		newSpecial = jQuery("#specials div:first-child");
	} else {
		newSpecial = jQuery("#specials div."+which);
		if(rarebrick.currentSpecial && jQuery(rarebrick.currentSpecial).hasClass(which)) return;
	}
	
	jQuery('#specials div.specialMenu ul li').removeClass('on');
	
	if(rarebrick.currentSpecial) {
		//clear current first
		jQuery("#specials").slideToggle(200, rarebrick.showSpecialNow).fadeOut(700);
		rarebrick.currentSpecial = newSpecial;
		jQuery('#specials div.specialMenu ul li.'+which).addClass('on').blur();
	} else {
		//first time shown
		rarebrick.allSpecials = jQuery("#specials div.special");
		rarebrick.currentSpecial = newSpecial;
		rarebrick.showSpecialNow();
		jQuery('#specials div.specialMenu ul li:first-child').addClass('on');
	}
	
	jQuery('#footerWrap').mouseout(rarebrick.specialsOut).mouseover(rarebrick.specialsOver);
}

rarebrick.showSpecialNow = function() {
	rarebrick.specialsOpen = true;
	jQuery(rarebrick.allSpecials).hide();
	jQuery(rarebrick.currentSpecial).show();
	//var h = jQuery(rarebrick.currentSpecial).height();
	jQuery("#specials").show();
	jQuery("#specials div.special").each(rarebrick.adjustSpecialBox);
	jQuery("#specials").hide().slideToggle(500).each(rarebrick.specialsOver);
	clearTimeout(rarebrick.specialsTimer);
}

rarebrick.specialsOver = function() {
	clearTimeout(rarebrick.specialsTimer);
	rarebrick.specialsIsOn = true;
	//alert('over');
}
rarebrick.specialsOut = function() {
	clearTimeout(rarebrick.specialsTimer);
	rarebrick.specialsTimer = setTimeout('rarebrick.specialsOutNow()', 1500);
}
rarebrick.specialsOutNow = function() {
	jQuery("#specials").show().slideToggle(500);
	rarebrick.specialsOpen = false;
	rarebrick.currentSpecial = null;
	clearTimeout(rarebrick.specialsTimer);
}

rarebrick.adjustSpecialBox = function() {
	var imgW = jQuery(this).find('img').first().outerWidth();
	if(!imgW) return;
	var ulW = jQuery("#specials div.specialMenu ul").first().outerWidth();
	//alert('w: ' + ulW);
	var newW = rarebrick.W - ulW - 20;
	jQuery(this).css('width', newW+'px');
	newW = newW - imgW - 10;
	jQuery(this).find('div.specialDescript').css('width', newW+'px');
}


rarebrick.allPhotos = null;
rarebrick.currentPhoto = null;
rarebrick.currentPhotoNum = -1;
rarebrick.prevPhoto = null;
rarebrick.photoTimer = 0;
rarebrick.autoPlay = false;
rarebrick.slideDelay = 4;

rarebrick.initPhotos = function() {
	rarebrick.allPhotos = jQuery('#photosWrap .photos img');
	
	if(rarebrick.allPhotos.length > 1) rarebrick.initPhotoControls();
	rarebrick.loadNextPhoto();
	rarebrick.nextPhoto();
}

rarebrick.loadNextPhoto = function() {
	//alert('next');
	try {
		var loadNum = rarebrick.currentPhotoNum+1;
		//if(document.location.search.indexOf('debug=true')!=-1) rarebrick.appendDebug('loadPhoto: ' + loadNum);
		var nextPhoto = rarebrick.allPhotos[loadNum];
		//if(document.location.search.indexOf('debug=true')!=-1) rarebrick.appendDebug('loadPhoto: ' + nextPhoto.src);
		if(nextPhoto.src.indexOf('lores/') != -1) nextPhoto.src = nextPhoto.src.replace('lores/', '');
	} catch(e) {
		//if(document.location.search.indexOf('debug=true')!=-1) rarebrick.appendDebug('rarebrick.loadNextPhoto error');
		//alert('error: ' + e);
	}
}

rarebrick.initPhotoControls = function() {
	var html = '<div id="photoControls">';
	html += '<img src="'+rarebrick.baseURL+'/images/photoNext.png" id="photoNext" />';
	html += '<img src="'+rarebrick.baseURL+'/images/photoDiv.png" id="photoDiv" />';
	html += '<img src="'+rarebrick.baseURL+'/images/photoPrev.png" id="photoPrev" />';
	html += '</div>';
	jQuery('#wrap').append(html);
	jQuery('#photoNext').click(rarebrick.nextPhoto);
	jQuery('#photoPrev').click(rarebrick.prevPhoto);
}

rarebrick.nextPhoto = function() {
	clearTimeout(rarebrick.photoTimer);
	rarebrick.prevPhoto = rarebrick.currentPhoto;
	
	rarebrick.currentPhotoNum++;
	if(rarebrick.currentPhotoNum >= rarebrick.allPhotos.length) rarebrick.currentPhotoNum = 0;
	rarebrick.currentPhoto = rarebrick.allPhotos[rarebrick.currentPhotoNum];
	
	// ||true added for IE9, which still apparently sucks	
	if(rarebrick.currentPhoto.complete || true) jQuery(rarebrick.currentPhoto).each(rarebrick.photoFade);
	else jQuery(rarebrick.currentPhoto).bind('load', rarebrick.photoFade);
}
	

rarebrick.prevPhoto = function() {
	//alert('prev');
	clearTimeout(rarebrick.photoTimer);
	rarebrick.prevPhoto = rarebrick.currentPhoto;
	
	rarebrick.currentPhotoNum--;
	if(rarebrick.currentPhotoNum < 0) rarebrick.currentPhotoNum = rarebrick.allPhotos.length-1;
	rarebrick.currentPhoto = rarebrick.allPhotos[rarebrick.currentPhotoNum];
		
	if(rarebrick.currentPhoto.complete) jQuery(rarebrick.currentPhoto).each(rarebrick.photoFade);
	else jQuery(rarebrick.currentPhoto).bind('load', rarebrick.photoFade);
}
	

rarebrick.photoFade = function() {
	var delay = 0;
	if(rarebrick.prevPhoto) {
		delay = 1000;
		jQuery(rarebrick.prevPhoto).fadeOut(delay);
	}
	jQuery(rarebrick.currentPhoto).each(rarebrick.resizePhoto).delay(delay).fadeIn(2000);
	var delay = rarebrick.slideDelay*1000;
	if(!delay) delay = 4000;
	//alert('autoPlay: ' + rarebrick.autoPlay + '  delay: ' + delay);
	if(rarebrick.autoPlay==true) rarebrick.photoTimer = setTimeout('rarebrick.nextPhoto()', delay);
	rarebrick.loadNextPhoto();
}

rarebrick.resizePhoto = function() {
	//if(!this.complete) return;
	var W = jQuery('#photosWrap').width();
	var H = jQuery('#photosWrap').height();
	
	var _x = 50;
	var _y = 50;
	if(this.className) {
		var classes = this.className.split(' ');
		for(var i=0; i<classes.length; i++) {
			if(classes[i].substring(0,2) == '_x') _x = classes[i].substring(2,classes[i].length);
			if(classes[i].substring(0,2) == '_y') _y = classes[i].substring(2,classes[i].length);
		}
	}
	
	var imgW = jQuery(this).data('w');
	if(!imgW) {
		imgW = jQuery(this).width();
		jQuery(this).data('w', imgW);
	}
	var imgH = jQuery(this).data('h');
	if(!imgH) {
		imgH = jQuery(this).height();
		jQuery(this).data('h', imgH);
	}
	
	//if(document.location.search.indexOf('debug=true')!=-1) rarebrick.appendDebug(jQuery(this).attr('src'));
	
	var _xScale = W / imgW;
	var _yScale = H / imgH;
	var _scale;
	
	if(jQuery(this).hasClass('showAll')) _scale = (_xScale < _yScale) ? _xScale : _yScale;
	else _scale = (_xScale > _yScale) ? _xScale : _yScale;
		
	var newW = (imgW * _scale);
	var newH = (imgH * _scale);
	
	jQuery(this).css('width', newW+'px');
	jQuery(this).css('height', newH+'px');
	
	var newX = (W - newW) * _x / 100;
	var newY = (H - newH) * _y / 100;
		
	jQuery(this).css('left', newX+'px');
	jQuery(this).css('top', newY+'px');
}


rarebrick.fixOffsiteLinks = function() {
	if(jQuery(this).attr('href').indexOf(rarebrick.baseURL) != -1) return;
	jQuery(this).attr('target', '_blank');
}

rarebrick.appendDebug = function(p) {
	jQuery('#debug').show();
	var elem = document.getElementById('debug');
	if(!elem) return;
	elem.innerHTML = p + '<br/>' + elem.innerHTML;
}

rarebrick.resizeContent = function() {
	jQuery('#contentWrap').css('height', rarebrick.H+'px');
}

rarebrick.addId = function(elem) {
	if(elem.id) return;
	var id = ('id' + Math.random()).replace('.', '');
	this.id = id;
	return id;
}
rarebrick.evalCode = function(code) {
	eval(code);
}

rarebrick.emailsignup = function() {
	var dx=590;
	var dy=400;
	var url = 'http://visitor.constantcontact.com/d.jsp?m=1102619973943&p=oi&go=Go';

	var w = window.open(url, 'emailPop', 'width=' + dx + ',height=' + dy + ',status=no,resizable=yes,menubar=yes,location=no,scrollbars=yes,toolbar=no');
	w.focus();
}

rarebrick.popup = function(url, dx, dy) {
	if(!dx) dx=590;
	if(!dy) dy=400;

	var w = window.open(url, 'emailPop', 'width=' + dx + ',height=' + dy + ',status=no,resizable=yes,menubar=yes,location=no,scrollbars=yes,toolbar=no');
	w.focus();
}

rarebrick.addPromo = function() {
	var endDate = new Date(2011,11,26);
	var now = new Date();
	if(now > endDate) return;
	var href = "javascript:rarebrick.popup('https://www.superinn.com/webview1.asp?dk=Union_Street_Inn&rd=gc')";
	jQuery('#wrap').append(jQuery('<a id="xmasPromo" href="'+href+'" />').append('<img src="http://unioninn.com/images/xmasPromo.png" />').hide().delay(2000).fadeIn(3000));
}


jQuery('document').ready(rarebrick.init);
window.onresize = rarebrick.onresize;
