 
var inner_content = '';
var album_id = '';

/**
* Displays the Film Detail overlay
* @param String the url to the film detail
* @param String the ID of the tab you want opened (default is Summary)
*/
function showDetailOverlay(DETAIL_URL,evt,TAB) {
	
	// turn off all the form elements
	$('filterForm').style.display = 'none';
	// turn off the sponsors flash
	$('sponsors').style.display = 'none';

	new Ajax.Updater('overlayBox', DETAIL_URL, {method: 'get'});
    

    // get page height
    psize = getPageSize();
    // set up the page
    $('ajaxOverlay').style.left = '0';
    $('ajaxOverlay').style.top = '0';
    $('ajaxOverlay').style.height = psize[1] + 'px';
    $('ajaxOverlay').style.width = psize[0] + 'px';
    $('ajaxOverlay').className = 'greyedOut'; 
    
    // set up the overlayBox
    box_width = 740;
    $('overlayBox').style.width = box_width + 'px';
    $('overlayBox').style.left = (psize[0]/2) - (box_width/2) - 100;
    

    // figure out the Y position
    if (tempY < 500) {
    	top_pos = tempY - 200;
    $('ajaxOverlay').style.height = (psize[1] + 200) + 'px';
    } else if (tempY > (psize[1] - 500)) {
    	top_pos = psize[1] - 800;
    $('ajaxOverlay').style.height = (psize[1] + 500) + 'px';
    } else {
    	top_pos = tempY - 200;
    $('ajaxOverlay').style.height = (psize[1] + 200) + 'px';
    }
    
    $('overlayBox').style.top = top_pos;
  
    $('ajaxOverlay').style.display = 'block';
    Effect.Appear('overlayBox',{duration: '0.7', afterFinish: defaultTab});

}

/**
* AJAX helper function to show the default tab when the overlay loads
*/
function defaultTab(OBJ) {
	ChooseTab('mainLeftSummary', '');
}

/**
* AJAX helper function to load the flash after fade in for IE ActiveX patent bug
*/
function loadFlash() {
	$('overlayFlash').innerHTML = '';
	$('overlayFlash').innerHTML = inner_content;
}

/**
* shows the overlay (for Photo Gallery and Video Gallery)
* @param String GALLERY_URL the url to the XML
* @param String LINKS_URL the url to the related links HTML
* @param int ALBUM_ID the id of the album or the brightcove video ID
* @param String GALLERY_TYPE the gallery type ('video','photo')
*/
function showOverlay(GALLERY_URL,LINKS_URL,ALBUM_ID,GALLERY_TYPE) {

    // first test flash version
	if(Tribeca.Flash >= 8){
		if (GALLERY_TYPE == 'video') {
	  		if(is_mac){
				openPopupGallery(GALLERY_URL,LINKS_URL,ALBUM_ID,GALLERY_TYPE);
			} else {
				inner_content = GenerateEmbededFlash('http://media.tribecafilm.com/designvideo/video.swf?myVar='+ GALLERY_URL + '&video_id='+ ALBUM_ID,457,425,8,true,false,false,false,'#ffffff');
			}
		} else {
			if(is_mac){
				openPopupGallery(GALLERY_URL,LINKS_URL,ALBUM_ID,GALLERY_TYPE);
			} else {
				// set maximum number of chars for the send to friend
				setMaxLength();
				// set alum ID
				album_id = ALBUM_ID;
				// first populate the related links
				new Ajax.Updater('relatedLinks',LINKS_URL, {method: 'get'});
		  		inner_content = GenerateEmbededFlash('http://media.tribecafilm.com/designvideo/polaroid_gallery.swf?myVar='+ GALLERY_URL + '&photo_id='+ ALBUM_ID,457,500,8,true,false,false,false,'#ffffff');
			}
		}
	} else {
	  var error_msg = "To view this page, you will need to download Adobe Flash Player 8 or higher."
		  error_msg+= '<br /><br /><a href="http://www.adobe.com/products/flashplayer/" target="_blank">Download it now</a>.';
		inner_content = '<div class="flasherror">' + error_msg + '</div>';
	}

	if (!is_mac) {
	    // get div height
	    dh = $('mainCol').offsetHeight;
	    // get page height
	    psize = getPageSize();
	    $('ajaxOverlay').style.height = (dh + 100) + 'px';
	    $('ajaxOverlay').className = 'greyedOut'; 
	    $('ajaxOverlay').style.display = 'block';
	    $('overlayFlash').innerHTML = '';
	    $('overlayBox').style.left = ((psize[0] / 2) - 300) + 'px';
	    Effect.Appear('overlayBox',{duration: '0.7', afterFinish: loadFlash}); 
	}

} // end showOverlay()

function openPopupGallery(GALLERY_URL,LINKS_URL,ALBUM_ID,GALLERY_TYPE) {
	if (GALLERY_TYPE == 'video') {
		POPUP_URL = 'videos_popup.html?xml='+GALLERY_URL+'&related='+LINKS_URL+'&id='+ALBUM_ID+'&gallery_type='+GALLERY_TYPE;
		gallery_popup = window.open(POPUP_URL,'gallerypopup','width=600,height=500,top=100,left=100,scrollbars=yes');
		gallery_popup.focus();
	} else {
		POPUP_URL = 'photos_popup.html?xml='+GALLERY_URL+'&related='+LINKS_URL+'&id='+ALBUM_ID+'&gallery_type='+GALLERY_TYPE;
		gallery_popup = window.open(POPUP_URL,'gallerypopup','width=600,height=700,top=100,left=100,scrollbars=yes');
		gallery_popup.focus();
	}

} // end openPopupGallery()


function showPopupGallery(GALLERY_URL,LINKS_URL,ALBUM_ID,GALLERY_TYPE) {

    // first test flash version
	if(Tribeca.Flash >= 8){
		if (GALLERY_TYPE == 'video') {
	  		inner_content = GenerateEmbededFlash('http://media.tribecafilmfestival.org/designvideo/rollback.swf?myVar='+ GALLERY_URL + '&video_id='+ ALBUM_ID,457,480,8,true,false,false,false,'#ffffff');
		} else {
			// set maximum number of chars for the send to friend
			setMaxLength();
			// set alum ID
			album_id = ALBUM_ID;
			// first populate the related links
			new Ajax.Updater('relatedLinks',LINKS_URL, {method: 'get'});
	  		inner_content = GenerateEmbededFlash('http://media.tribecafilm.com/designvideo/polaroid_gallery.swf?myVar='+ GALLERY_URL + '&photo_id='+ ALBUM_ID,457,500,8,true,false,false,false,'#ffffff');
		}
	} else {
	  var error_msg = "To view this page, you will need to download Adobe Flash Player 8 or higher."
		  error_msg+= '<br /><br /><a href="http://www.adobe.com/products/flashplayer/" target="_blank">Download it now</a>.';
		inner_content = '<div class="flasherror">' + error_msg + '</div>';
	}

loadFlash();

} // end showPopupGallery()


/**
* closes the overlay
*/
function closeOverlay(DETAIL) {
	if (is_mac) {
		window.close();
	} else {
		// turn form elements back on
		if ($('filterForm')) {
			$('filterForm').style.display = 'block';
		}
		// turn flash sponsor back on
		if ($('sponsors')) {
			$('sponsors').style.display = 'block';
		}
		
		$('overlayBox').style.display = 'none';
		$('ajaxOverlay').style.display = 'none';
		
		$('overlayBox').innerHTML = '';
	}
} // end closeOVerlay()


/**
* sets the maximum length of characters in a textarea
* just set maxlength= for the text area
*/
function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = $('counter');
	counter.className = 'counter';
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			
			counter.relatedElement = x[i];
			counter.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength');
			//x[i].parentNode.insertBefore(counter,x[i].nextSibling);
			x[i].relatedElement = counter.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		} // end if maxlength
	} // end for i
} // end function setMaxLength()

/**
* checks the maximum length of characters in a textarea
*/
function checkMaxLength() {
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength) {
		this.relatedElement.className = 'toomuch';
	} else {
		this.relatedElement.className = '';
	}
	this.relatedElement.firstChild.nodeValue = currentLength;
	// not innerHTML
} // end checkMaxLength()

/**
* send to a friend form checking and sending function
*/
function SendaFriend() {
  var url = 'send_a_friend.php';
  failed = 0;
  var email_match = /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/i;
  
  // test email addresses
  if (!email_match.test($('from_email').value)) {
    alert("Please enter a valid email address for you");
    failed = 1;
  } // end from_email
  if (!email_match.test($('to_email').value)) {
    alert("Please enter a valid email address for your friend");
    failed = 1;
  } // end from_email
  // test email message
  if ($('email_content').value == 'Message (200 characters)') {
    alert('Please enter a message');
    failed = 1;
  }

  
  if (failed > 0) {
    // failed
    return false;
  } else {
    // passed, now, let's send
    poststring = 'album_id=' + album_id + '&from_email=' + $('from_email').value + '&to_email=' + $('to_email').value + '&email_content=' + $('email_content').value;
    new Ajax.Request(url,{ method: 'post', parameters: poststring, onSuccess: SendSuccess });
    // we're returning false in both cases
    return false;
  }
} // end SendaFriend()

// some support functions

/**
* AJAX helper function for success on send to a friend
*/
function SendSuccess(OBJ) {
  $('sendToFriend').innerHTML = OBJ.responseText;
} // end SendSuccess()

/**
* getPageSize()
* Core code from - quirksmode.org
* Edit for Firefox by pHaez
* @return array with page width, height and window width, height
*/
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

