// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var featureFadeDelay = 8000;
var featureRotateTimeout = null;

function rotateFeatures(nextIndex) {
    clearTimeout(featureRotateTimeout);
    var list_items = $$('#features .feature');
    var nav_items = $$('#features-nav .nav');
	
    if (list_items.length != nav_items.length) return false;
    if (list_items.length < 1 || nav_items.length < 1) return false;

	if (list_items.length == 1) {
		list_items[0].setAttribute('style', '');
    	nav_items[0].className = 'nav nav-1 selected nav-1-selected';
	} else if (list_items.length < 1) {
		return false;
	} else {
		if (typeof(nextIndex) != 'undefined') {
	        nextFeature(nextIndex)
	    } else {
	        nextFeature();
	    }

	    featureRotateTimeout = setTimeout(function () { rotateFeatures(); }, featureFadeDelay);
	}
}

function nextFeature(feature_id) {
    var list_items = $$('#features .feature');
    var nav_items = $$('#features-nav .nav');
    
    var currentIndex = null;
    
	if (typeof(feature_id) == 'undefined') {
		nextIndex = feature_id
	} else {
		var nextIndex = 0;

		// map feature ID to nextIndex
		for (var i = 0; i < list_items.length; i++) {
			temp = list_items[i].getAttribute('id').split('_');
			id = temp[temp.length - 1];
			if (id == feature_id) {
				nextIndex = i;
				break;
			}
		}
	}
    
    // hide all selected
	for (var i = 0; i < list_items.length; i++) {
        if (list_items[i].className == 'feature selected') {
            if (!currentIndex) currentIndex = i;
            if (typeof(nextIndex) == 'undefined' || i != nextIndex) {
                Effect.Fade(list_items[i], { duration: 0.3 });
            }
        }

        list_items[i].className = 'feature';
        nav_items[i].className = 'nav nav-' + (i + 1);
    }
   
    if (currentIndex == null) {
        currentIndex = list_items.length - 1;
    }
    if (typeof(nextIndex) == 'undefined') {
        nextIndex = currentIndex + 1;
        if (nextIndex > (list_items.length-1)) nextIndex = 0;
    }
   
    list_items[nextIndex].className = 'feature selected';
   	nav_items[nextIndex].className = 'nav selected nav-' + (nextIndex + 1) + ' nav-' + (nextIndex + 1) + '-selected';
	
    Effect.Appear(list_items[nextIndex], { duration: 0.4 })
}


/* Very good, very popular, unobtrusive event loader
 * that executes functions on page load.
 * - by JS guru Simon Willison.
 * http://simonwillison.net/2004/May/26/addLoadEvent/
 */

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function show_n_hide ( show, hide ) {
    Effect.Fade(hide)
    Effect.Appear(show)
}

function swap_em ( show, hide ) {
    Element.hide ( hide )
    Element.show ( show )
}
function disappear ( hide ) {
    Effect.Fade(hide)    
}

function rate_showcase (rating, showcase_id){
    var url = '/showcases/rating/?id=' + showcase_id + '&rating=' + rating;
    new Ajax.Request(url, {
        onSuccess: function(transport){
            var notice = $('notices');
            notice.update(transport.responseText)
        }
    })

}


// usage: document.write(calculateTimeDifference('2/6/2008 18:00', ' until next workshop', ' since last workshop'));
function calculateTimeDifference(targetDate, untilString, sinceString) {
    var daysLeft = null;
    var hoursLeft = null;
    var timeLeftAsString = 'unknown';
    var today = new Date();
    
    targetDate = new Date(targetDate);
    
    daysLeft = Math.round(Math.abs(
        ((today.getTime() - today.getHours()*3600000 - today.getMinutes()*60000 - today.getSeconds()*1000) -
         (targetDate.getTime() - targetDate.getHours()*3600000 - targetDate.getMinutes()*60000 - targetDate.getSeconds()*1000)
        ) / 86400000
    ));
    hoursLeft = Math.abs(today.getHours() - targetDate.getHours());
    
    if (daysLeft == 0) {
        timeLeftAsString = '' + hoursLeft + ' hour' + (hoursLeft == 1 ? '' : 's');
    } else {
        timeLeftAsString = '' + daysLeft + ' day' + (daysLeft == 1 ? '' : 's');
    }
    timeLeftAsString += (targetDate > today ? untilString : sinceString);
    
    return timeLeftAsString;
}


var galleryTimeout = null;
function advanceGallerySlideshow(galleryName, delay) {
    $(galleryName + '_next_button').onclick();
    galleryTimeout = setTimeout(function() { advanceGallerySlideshow(galleryName, delay); }, delay);
}



var autoGallerySize = [];
function changeAutoGalleryImage(galleryName, index) {
    var target = galleryName + '_image_' + index;
    var caption = galleryName + '_caption_' + index;
    if (!$(target)) return false;
    
    // var queue = Effect.Queues.get('global');
    // queue.each(function(e) { e.cancel() });
    
    $$('.' + galleryName + '_image').each(function (el) {
        if (el.id != target && el.style.display != 'none') Effect.Fade(el, { duration: 0.6 });
    });
    // $$('.' + galleryName + '_thumb').each(function (el) {
    //     if (el.id == galleryName + '_thumb_' + index) {
    //         el.style.borderColor = '#EA5B34';
    //         el.onmouseover = null;
    //         el.onmouseout = null;
    //     } else {
    //         el.style.borderColor = '#FBEDC4';
    //         el.onmouseover = function () { el.style.borderColor = '#EA5B34' };
    //         el.onmouseout = function () { el.style.borderColor = '#FBEDC4' };
    //     }
    // });
    
    Effect.Appear(target, { duration: 0.6 });
    
    // if($(caption).innerHTML != '') {
    //     $(galleryName + '_caption').style.display = 'block';
    //     $(galleryName + '_caption').innerHTML = $(caption).innerHTML;
    // } else {
    //     $(galleryName + '_caption').style.display = 'none';
    // }
    
    var prevIndex = index == 0 ? autoGallerySize[galleryName] - 1 : index-1;
    var nextIndex = index == autoGallerySize[galleryName] - 1 ? 0 : index+1;
    $(galleryName + '_prev_button').onclick = function () { changeAutoGalleryImage(galleryName, prevIndex); return false };
    $(galleryName + '_next_button').onclick = function () { changeAutoGalleryImage(galleryName, nextIndex); return false };
}

function advanceAutoGallery(galleryName, delay) {
    $(galleryName + '_next_button').onclick();
    setTimeout(function() { advanceAutoGallery(galleryName, delay); }, delay);
}


function autoResizeGalleryCaptionDiv(galleryName) {
    captions = $$('.hidden-caption');
    masterDiv = $(galleryName + '_caption');
    masterDiv.style.display = 'block';
    
    var max = 0;
    
    captions.each(function (caption) {
        masterDiv.innerHTML = caption.innerHTML;
        max = Math.max(max, parseInt(masterDiv.scrollHeight));
    });
    
    masterDiv.style.height = '' + max + 'px';
    masterDiv.innerHTML = captions[0].innerHTML;
}
