// home.js for brudder
// functionality for homepage
// code by CN

/**********************************************************************
   Page initialization
 *********************************************************************/

function pageInit() {
   if (isDefined(document.getElementsByTagName)) {
      
      // about section expand / collapse
			var aboutExpand = document.getElementById('about-expand');
			if (aboutExpand) {
				aboutExpand.onclick = function() {
					document.getElementById('about').style.display = 'block';
					document.getElementById('about-expand').parentNode.style.display = 'none';
					document.getElementById('about-collapse').parentNode.style.display = 'block';
				}
			}
			var aboutCollapse = document.getElementById('about-collapse');
			if (aboutCollapse) {
				aboutCollapse.onclick = function() {
					document.getElementById('about').style.display = 'none';
					document.getElementById('about-collapse').parentNode.style.display = 'none';
					document.getElementById('about-expand').parentNode.style.display = 'block';
				}
			}
			
			// lists
      var list = document.getElementsByTagName('ul');
      for (var listIndex = list.length - 1; listIndex >= 0; listIndex--) {
         var listElement = list[listIndex];
         
         // video thumbnails
         if (listElement.className.indexOf('thumb-list') > -1) {
            var listPrefix = listElement.id.substring(0, listElement.id.lastIndexOf('-thumbs'))
            var flashPlayerId = listPrefix + 'playerflash';
            var itemDetailsId = listPrefix + '-item-detail';
            
            var listItems = listElement.getElementsByTagName('li');
            for (var listItemIndex = listItems.length - 1; listItemIndex >= 0; listItemIndex--) {
               var listItemElement = listItems[listItemIndex];
               
               // initialization: add vignette
               // comment out for IE 5-6 until we know if server supports .htc files with correct mime type
               if (!(/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32")) {
                 var vignette = document.createElement('img');
                 vignette.src = 'img/vignette.png';
                 vignette.className = 'thumb-vignette';
                 listItemElement.appendChild(vignette);
               }
               
               // initialization: store variables
               listItemElement.itemDetailsId = itemDetailsId;
               listItemElement.flashPlayerId = flashPlayerId;
               
               // onclick: load video
               listItemElement.onclick = function() {
                  var img = this.getElementsByTagName('img')[0];
									if (img.className == '' || img.className == 'current') {
										
										// remove current thumb on all other thumbs
										var sectionThumbs = this.parentNode.getElementsByTagName('li');
										for (var thumbIndex = sectionThumbs.length - 1; thumbIndex >= 0; thumbIndex--) {
											 sectionThumbs[thumbIndex].className = '';
										}
										
										// set this thumb as current
										this.className = 'current';
										
										// show item details area
										var itemDetails = document.getElementById(this.itemDetailsId);
										itemDetails.style.display = 'block';
										
										// show item details content
										getElementsByClassName('description', itemDetails, 'div')[0].innerHTML = getElementsByClassName('description', this, 'div')[0].innerHTML;
										
										// load video
										var imgPath = this.getElementsByTagName('img')[0].src;
										var imgFile = imgPath.substring(imgPath.indexOf('img/') + 4);
										var flvPath = '../flv/' + imgFile.substring(0, imgFile.lastIndexOf('_thumb.')) + '.flv';
										viewVideo(flvPath, this.flashPlayerId);
										
										// position and turn on masks
										if (this.parentNode.id != 'feature-thumbs') {
											var section = this.parentNode.parentNode;
											var topHeight = cumulativeOffset(section)[1];
											var bottomTop = parseInt(section.offsetHeight) + topHeight;
											 
											var mask1 = document.getElementById('mask1');
											mask1.style.height = topHeight + 'px';
											mask1.style.bottom = topHeight + 'px';
											mask1.style.display = 'block';
											
											var mask2 = document.getElementById('mask2');
											mask2.style.height = (parseInt(mask2.parentNode.offsetHeight) - bottomTop + 30) + 'px';
											mask2.style.top = bottomTop + 'px';
											mask2.style.display = 'block';
										}
										
										// IE sucks
										fixIeListBug(this.parentNode);
										
										// stop feature movie
										document.getElementById('featureplayerflash').sendEvent('stop');
										return false;
									}
									else {
										location.href = img.className;
									}
               };
            }
         }
      }
      
      // paragraphs
      var paras = document.getElementsByTagName('p');
      for (var paraIndex = paras.length - 1; paraIndex >= 0; paraIndex--) {
         var paraElement = paras[paraIndex];
         
         // collapse-row buttons
         if (paraElement.className.indexOf('collapse-row') > -1) {
            paraElement.firstChild.onclick = function() {
               var sectionIdRoot = this.id.substring(0, this.id.lastIndexOf('-'));
               
               // hide related item detail section
               document.getElementById(sectionIdRoot + '-item-detail').style.display = 'none';
               
               // remove current thumb on all thumbs
               var sectionThumbs = document.getElementById(sectionIdRoot + '-thumbs').getElementsByTagName('li');
               for (var thumbIndex = sectionThumbs.length - 1; thumbIndex >= 0; thumbIndex--) {
                  sectionThumbs[thumbIndex].className = '';
               }
               
               // turn off masks
               document.getElementById('mask1').style.display = 'none';
               document.getElementById('mask2').style.display = 'none';
               
               // IE sucks
               fixIeListBug(document.getElementById(sectionIdRoot + '-thumbs'));
               
               return false;
            };
         }
      }
      
      // masks
      var mask1 = document.createElement('div');
      mask1.className = 'mask';
      mask1.id = 'mask1';
      
      var mask2 = mask1.cloneNode(false);
      mask2.id = 'mask2';
      
      var body = document.getElementsByTagName('body')[0];
      body.appendChild(mask1);
      body.appendChild(mask2);
      
      // call global initialization
      globalPageInit()

      // load the first feature (if there are multiple features)
      if (document.getElementById('feature-thumbs')) {
         document.getElementById('feature-thumbs').getElementsByTagName('li')[0].onclick();
      }
      
      // set cookie that user has seen feature video
      // Cookie.set('seenFeature', 'true');
   }
}

// IE6- fix for thumbnails shifting to the left (set marginLeft to a different value and then correct after a pause)
function fixIeListBug(thumbsList) {
   var firstListItem = thumbsList.getElementsByTagName('li')[0];
   firstListItem.style.marginLeft = '18px';
   setTimeout("document.getElementById('" + thumbsList.id + "').getElementsByTagName('li')[0].style.marginLeft = '19px'", 100);
}

// call pageInit when document finishes loading (remove call to globalPageInit)
window.removeEvent(window, 'load', globalPageInit);
window.addEvent(window, 'load', pageInit);
