/* --------------------------------------------------------------------------------------------------------
AUTHOR:			Ryan Marincovich (ryan@bicycletheory.com)
Date Created:   2011.03.31
Client:         Bicycle Theory
File Purpose:	Common Javascript File
-------------------------------------------------------------------------------------------------------- */


// check to run on first load to see if we need to be small
var checkWindowFirstLoad = function() {
	if (windowTotalWidth < 1024) {
		window.location = "/change-size.php?strSiteSize=small";
	}
}

// check to window size
var checkWindowSize = function() {
	//alert('[2] windowTotalWidth = ' + windowTotalWidth);
	intCurrentWidth = self.innerWidth || document.documentElement.offsetWidth || document.body.clientWidth;
	//alert('[3] windowTotalWidth = ' + windowTotalWidth + '\n\nintCurrentWidth = ' + intCurrentWidth + '\n\nself.innerWidth = ' + self.innerWidth + '\ndocument.documentElement.offsetWidth = ' + document.documentElement.offsetWidth + '\ndocument.body.clientWidth = ' + document.body.clientWidth);
	var strSiteSize = '';
	// modify width of site based on window
	if (windowTotalWidth >= 1024) {
		if (intCurrentWidth < 1024) {
			strSiteSize = 'small';
			//alert('[4a] windowTotalWidth = ' + windowTotalWidth + '\nintCurrentWidth = ' + intCurrentWidth + '\nchanging to ' + strSiteSize);
			window.location = "/change-size.php?strSiteSize=" + strSiteSize;
		}
	} else {
		if (intCurrentWidth >= 1024) {
			strSiteSize = 'wide';
			//alert('[4b] windowTotalWidth = ' + windowTotalWidth + '\nintCurrentWidth = ' + intCurrentWidth + '\nchanging to ' + strSiteSize);
			window.location = "/change-size.php?strSiteSize=" + strSiteSize;
		}
	}
}


/* ---------- 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='+strAddThisID+'" 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="100" height="25" 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=bicycletheoryawk"></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');
	overlayVideoPlayerObj = document.getElementById('videoPlayer');
	overlayAudioPlayerObj = document.getElementById('audioPlayer');
	
	// 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/bty/galleria.bty.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="/resources/image-galleries-detail.php?intResourceID=' + intResourceID + '">Details</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="/resources/image-galleries.php">View All Photo Galleries</a>';
	overlayGalleryAddThisObj.innerHTML = buildAddThisButton(strDetailURL, strGalleryTitle + ' (' + strClientName + ')');
	
	// 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("bty", { height: 540 });
			}
		}
		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 = '';
}

// function to open video overlay
function videoPlayerOpen() {
	scroll(0, 0);
	overlayMainObj.style.visibility = 'visible';
	overlayVideoPlayerObj.style.visibility = 'visible';
}

// define function to start playing a video in a div obj without reloading the page
function openVideoDetail(intResourceID, strVideoTitle, strFlashSRC, strDetailURL) {
	// open the overlay
	videoPlayerOpen();
	
	// check bool for detail links
	var strLinkNewWin = '';
	/*if (boolParentURLNewWin) {
		strLinkNewWin = ' target="_blank"';
	}*/
	
	// get frame objects
	var objVideoFrame = document.getElementById('videoFrame');
	overlayVideoTitleObj = document.getElementById("videoTitle");
	overlayVideoLinksObj = document.getElementById("videoLinks");
	overlayVideoPlayerAddThisObj = document.getElementById('videoPlayerAddThis');
	overlayVideoTitleObj.innerHTML = strVideoTitle;
	overlayVideoLinksObj.innerHTML = '<a href="/resources/videos-detail.php?intResourceID=' + intResourceID + '"' + strLinkNewWin + '>Details</a> &nbsp;|&nbsp; <a href="/resources/videos.php"' + strLinkNewWin + '>View All Videos</a>';
	overlayVideoPlayerAddThisObj.innerHTML = buildAddThisButton(strDetailURL, strVideoTitle + ' (' + strClientName + ')');
	// init html var and fill with content
	var strHTML = '';
	strHTML += '<div style="text-align:center; margin-bottom: 10px; display:none;">Playing: ' + strFlashSRC + '</div>';
	if (strFlashSRC.substr(0, 3) == 'yt=') {
		// youtube video
		strYouTubeID = strFlashSRC.substring(3, strFlashSRC.length);
		strHTML += '<object width="640" height="480">';
		strHTML += '	<param name="movie" value="http://www.youtube.com/v/' + strYouTubeID + '&hl=en_US&fs=1&autoplay=1&"></param>';
		strHTML += '	<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>';
		strHTML += '	<embed src="http://www.youtube.com/v/' + strYouTubeID + '&hl=en_US&fs=1&autoplay=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="480"></embed>';
		strHTML += '</object>';
	} else {
		// venyou-uploaded video
		strHTML += '<script type="text/javascript">';
		strHTML += '	buildFlashMovie(640, 480, \'' + strFlashSRC + '\', 9, \'\', "videoFrame");';
		strHTML += '<\/script>';
		strHTML += '<noscript>';
		strHTML += '	<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="640" height="480" title="Video Player">';
		strHTML += '		<param name="movie" value="' + strFlashSRC + '" />';
		strHTML += '		<param name="quality" value="high" />';
		strHTML += '		<param name="wmode" value="transparent" />';
		strHTML += '		<embed src="' + strFlashSRC + '" quality="high" wmode="transparent" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="640" height="480"></embed>';
		strHTML += '	</object>';
		strHTML += '</noscript>';
	}
	// set video frame html
	objVideoFrame.innerHTML = strHTML;
	// eval all script that was set in html
	var x = objVideoFrame.getElementsByTagName("script");
	for (var i=0; i < x.length; i++) {
		eval(x[i].text);
	}
}

// function to stop a video (clears innerHTML of video frame)
function stopVideo() {
	overlayMainObj.style.visibility = 'hidden';
	overlayVideoPlayerObj.style.visibility = 'hidden';
	var objVideoFrame = document.getElementById('videoFrame');
	objVideoFrame.innerHTML = '';
}

// function to open audio overlay
function audioPlayerOpen() {
	scroll(0, 0);
	overlayMainObj.style.visibility = 'visible';
	overlayAudioPlayerObj.style.visibility = 'visible';
}

// function to play audio file through flash player controls
function openAudioDetail(intResourceID, strAudioTitle, strSource, strDetailURL) {
	audioPlayerOpen();
	var objAudioFrame = document.getElementById('audioFrame');
	var overlayAudioTitleObj = document.getElementById('audioTitle');
	overlayVideoLinksObj = document.getElementById("audioLinks");
	overlayAudioPlayerAddThisObj = document.getElementById('audioPlayerAddThis');
	overlayAudioTitleObj.innerHTML = strAudioTitle;
	overlayVideoLinksObj.innerHTML = '<a href="/resources/audio-detail.php?intResourceID=' + intResourceID + '">Details</a> &nbsp;|&nbsp; <a href="/resources/audio.php">View All Audio</a>';
	overlayAudioPlayerAddThisObj.innerHTML = buildAddThisButton(strDetailURL, strAudioTitle + ' (' + strClientName + ')');
	// init html var and fill with content
	var strHTML = '';
	strHTML += '<div style="text-align:center; margin-bottom: 10px; display:none;">Playing: ' + strSource + '</div>';
	strHTML += '<script type="text/javascript">';
	strHTML += '	buildFlashMovie(300, 80, \'' + strSource + '\', 9, \'\', "audioFrame");';
	strHTML += '<\/script>';
	strHTML += '<noscript>';
	strHTML += '	<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="300" height="80" title="Audio Player">';
	strHTML += '		<param name="movie" value="' + strSource + '" />';
	strHTML += '		<param name="quality" value="high" />';
	strHTML += '		<param name="wmode" value="transparent" />';
	strHTML += '		<embed src="' + strSource + '" quality="high" wmode="transparent" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="300" height="80"></embed>';
	strHTML += '	</object>';
	strHTML += '</noscript>';
	// set container html
	objAudioFrame.innerHTML = strHTML;
	// eval all script that was set in html
	var x = objAudioFrame.getElementsByTagName("script");
	for (var i=0; i < x.length; i++) {
		eval(x[i].text);
	}
}

// function to open audio player error message (for IE6)
function openAudioError(strAudioTitle, strFilename) {
	audioPlayerOpen();
	var objAudioFrame = document.getElementById('audioFrame');
	var overlayAudioTitleObj = document.getElementById('audioTitle');
	overlayAudioTitleObj.innerHTML = strAudioTitle;
	// init html var and fill with content
	var strHTML = '';
	strHTML += 'Sorry, streaming audio is not compatible with older versions of Internet Explorer (version 6 and earlier).<br /><br />';
	strHTML += 'If possible, we recommend upgrading your browser (try <a href="http://www.getfirefox.com" target="_blank">FireFox</a>).';
	// set container html
	objAudioFrame.innerHTML = strHTML;
}

// function to stop audio (clears innerHTML of audio frame)
function stopAudio() {
	overlayMainObj.style.visibility = 'hidden';
	overlayAudioPlayerObj.style.visibility = 'hidden';
	var objAudioFrame = document.getElementById('audioFrame');
	objAudioFrame.innerHTML = '';
}

