/* --------------------------------------------------------------------------------------------------------
AUTHOR:			Ryan Marincovich (ryan@bicycletheory.com)
Date Created:   2010.10.26
Client:         Anderson Race Management
File Purpose:	Common Javascript File
-------------------------------------------------------------------------------------------------------- */


/* ---------- OVERLAY AND MEDIA DATA AND FNS ------------------------------------------------ */
// function to generate an addthis button
var buildAddThisButton = function(strAltURL, strAltTitle) {
	// set alt params for a tag
	var strAltURLParam = '';
	var strAltTitleParam = ''
	if (strAltURL) strAltURLParam = ' addthis:url="' + strAltURL + '"';
	if (strAltTitle) strAltTitleParam = ' addthis:title="' + strAltTitle + '"';
	// set alt params for old mouseover that is needed to work in an overlay
	if (!strAltURL) strAltURL = '[URL]';
	if (!strAltTitle) strAltTitle = '[TITLE]';
	return '<a href="http://addthis.com/bookmark.php?v=250&amp;username=bicycletheoryarm" class="addthis_button"' + strAltURLParam + strAltTitleParam + ' onmouseover="return addthis_open(this, \'\', \'' + strAltURL + '\', \'' + strAltTitle + '\')" onmouseout="addthis_close()" onclick="return addthis_sendto()"><img src="/_images/buttons/share.gif" width="70" height="24" alt="Bookmark and Share" align="absmiddle" style="border:0" class="share"></a><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=bicycletheoryarm"></script>';
}

var isOverlayLoaded = false;

// build overlay objects (run onload)
function buildOverlay() {
	
	// global overlay elements
	overlayMainObj = document.getElementById('mainOverlay');
	overlayBodyObj = document.getElementById('container');
	overlayBGObj = document.getElementById('overlayBG');
	overlayGalleryObj = document.getElementById('photoGallery');
	
	// calc height
	windowHeight = overlayMainObj.offsetHeight;
	contentHeight = overlayBodyObj.offsetHeight;
	overlayBGObj.style.height = Math.max(windowHeight, contentHeight) + "px";
	
	// set var to note objects are built and ready
	isOverlayLoaded = true;
}

// function to open flash photo gallery
function galleryOpen() {
	scroll(0, 0);
	overlayMainObj.style.visibility = 'visible';
	overlayGalleryObj.style.visibility = 'visible';
}

// function to open photo gallery 
function photoGalleryOpen(intResourceID, strGalleryTitle, strDetailURL) {
	// open the overlay
	galleryOpen();
	
	// load galleria
	Galleria.loadTheme("/_js/aino-galleria-ee792e2/src/themes/classic/galleria.classic.js");
	
	// get frame objects
	var objGalleryContainer = document.getElementById('galleryOverlay');
	overlayGalleryTitleObj = document.getElementById("galleryTitle");
	overlayGalleryLinksObj = document.getElementById("galleryLinks");
	overlayGalleryAddThisObj = document.getElementById('photoGalleryAddThis');
	overlayGalleryTitleObj.innerHTML = strGalleryTitle;
	overlayGalleryLinksObj.innerHTML = '<a href="/photo-galleries-detail.php?intResourceID=' + intResourceID + '">Details</a>';
	overlayGalleryAddThisObj.innerHTML = buildAddThisButton(strDetailURL, strGalleryTitle + ' (Anderson Race Management)');
	
	// load in gallery html via ajax
	if (objGalleryContainer != null) {
		var xmlHttp = GetXmlHttpObject();
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4) {
				objGalleryContainer.innerHTML = xmlHttp.responseText;
				$("#galleryOverlay").galleria("classic", { height: 510 });
			}
		}
		xmlHttp.open("GET", "/_feeds/photo_gallery.php?intResourceID="+intResourceID, true);
		xmlHttp.send(null);
	}
}

// function to close flash photo gallery (clears innerHTML of video frame)
function closeGallery() {
	overlayMainObj.style.visibility = 'hidden';
	overlayGalleryObj.style.visibility = 'hidden';
	var objGallery = document.getElementById('galleryOverlay');
	objGallery.innerHTML = '';
}

// ---------- AJAX FUNCTIONS ------------------------------------------------

// load in twitter html
var loadTwitter = function() {
	var objTwitter = document.getElementById("feedTwitter");
	if (objTwitter != null) {
		var xmlHttp = GetXmlHttpObject();
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4) {
				objTwitter.innerHTML = xmlHttp.responseText;
			}
		}
		xmlHttp.open("GET", "/_feeds/twitter.php", true);
		xmlHttp.send(null);
	}
}

