jQuery.fn.simpleImageRollover = function(preload) {
    
    if (preload) {
        this.each(function() {
            var i = new Image;
            i.src = this.src;
        });
    }
    
    this.hover(
        function() { this.src = this.src.replace(/\.(\w+)$/, '_h.$1'); },
        function() { this.src = this.src.replace(/_h\.(\w+)$/, '.$1'); }
    );

}

$(function() {
    
    $('.rollover').simpleImageRollover(true);
	
	//$('.climate-facts').cycle({
	//	fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	//});
	
	var frame = $('#photo-frame');
	if (frame.length) {
		
		var timer = null;
		var active = false;
		
		function reset() { 
			if (timer) window.clearInterval(timer);
			timer = window.setInterval(function() { go(1); }, 3000);
		}
		
		var slideshow = $('#home-page-slideshow', frame);
		if (slideshow.length) {
			reset();
		}
		
		function go(inc) {
			if (active) return;
			active = true;
			if (inc > 0) {
				$('a:last', slideshow).animate({opacity: 0}, function() {
					$(this).remove().css('opacity', 1).prependTo(slideshow);
					active = false;
				});
			} else if (inc < 0) {
				$('a:first', slideshow).remove().insertBefore($('a:last', slideshow));
				$('a:last', slideshow).animate({opacity: 0}, function() {
					$(this).remove().css('opacity', 1).insertBefore($('a:last', slideshow));
					active = false;
				});
			}
		}
		
		$('.actuators a', frame).click(function(event) {
			event.stopPropagation();
			if (active) return;
			var target = 'slideshow-item-' + parseInt(this.innerHTML);
			if ($('a:last', slideshow)[0].id == target) return;
			$('#' + target).remove().prependTo(slideshow);
			go(-1);
		});
		
	}

	$('#photo-frame').click(function() {
		window.location = $('#home-page-slideshow a:last', this)[0].href;
		return false;
	});
	
	var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);

	//
	// Asset Icons
	
	typeMap = { pdf: 'pdf', wmv: 'movie', mpg: 'movie', mov: 'movie',
                    wav: 'audio', mp3: 'audio', mp4: 'movie', ppt: 'presentation',
                    doc: 'document', rtf: 'document', txt: 'document', gif: 'image',
                    jpg: 'image', jpeg: 'image', png: 'image' };
	
	$('#page-content a[href]').each(function() {
		if ($(this).parents('#player-layout').length) return;
		var ext = $(this).attr('href').split('.').pop();
		if (typeMap[ext]) {
			$(this).addClass('asset-link').addClass('asset-link-' + typeMap[ext]);
		}
	});
	
	$('form.poll-form').submit(function() {
      if ($('input:checked', this).length == 0) {
        alert("Please select an option before voting");
        return false;
      }
    });
	
	//
	// Faces of Lent
	
	var setupGallery = function() {
			var gallery = $('.faces-layout .face-gallery').get(0);
			var ul 		  = $('ul', gallery);
			var size    = $('.faces-layout .face-gallery li').size();
			var pages   = Math.floor(size/8) + ((size % 8) == 0 ? 0 : 1);
			var current = 1;

			if(pages == 1) {
				$('.faces-layout .face-gallery .more').hide();
			} else {
				$('.faces-layout .face-gallery .more').click(function() {

					if(current == pages) {
						ul.animate({left:0});
						current = 1;
					} else {
						ul.animate({left:current*-416});
						current++;
					}

					return false;
				});
			}

			$('img', gallery).hover(function() {
				$(this).addClass('hover');
			}, function() {
				$(this).removeClass('hover');
			});

			$('img', gallery).click(function() {
				$('img', gallery).removeClass('active');
				$(this).addClass('active');
				$('.face-title').html($(this).attr('title'));
				$('.face-description').html($(this).next('.description').html());
			});

		};

		setupGallery();
	
});

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}
