$(document).ready(function() {

if($(".spotlight").length > 0){
	$(".spotlight:even").css("clear", "both");
}

if($(".reports dl").length > 0){
	var doc = $(".reports dl dt a[href$='.doc']");
	var pdf = $(".reports dl dt a[href$='.pdf']");
	var xls = $(".reports dl dt a[href$='.xls']");
	
	$(doc).parents("dl").addClass("doc");
	$(pdf).parents("dl").addClass("pdf");
	$(xls).parents("dl").addClass("xls");
}

if($(".downloads dl").length > 0){
	var doc = $(".downloads dl dt a[href$='.doc']");
	var pdf = $(".downloads dl dt a[href$='.pdf']");
	var xls = $(".downloads dl dt a[href$='.xls']");
	
	$(doc).parents("dl").addClass("doc");
	$(pdf).parents("dl").addClass("pdf");
	$(xls).parents("dl").addClass("xls");
}


if($(".related-downloads ul").length > 0){
	var doc = $(".related-downloads ul li a[href$='.doc']");
	var pdf = $(".related-downloads ul li a[href$='.pdf']");
	var xls = $(".related-downloads ul li a[href$='.xls']");
	
	$(doc).addClass("doc");
	$(pdf).addClass("pdf");
	$(xls).addClass("xls");
}

if($("#tweet").length > 0){
	getTwitters('tweet', { 
	  id: 'ncteireland', 
	  count: 3, 
	  enableLinks: true, 
	  ignoreReplies: true, 
	  clearContents: true,
	  template: '%text%<br /><span>%time%</span>'
	});
}

	var addthis_config = {"data_track_clickback":true};	   
    var delay = 100;
    
    function hideMenuSidebar() {
        if (!$('.sidebar .custom_button').data('in') && !$('.sidebar .hover_menu').data('in') && !$('.sidebar .hover_menu').data('hidden')) {
            $('.sidebar .hover_menu').fadeOut('fast');
            $('.sidebar .custom_button').removeClass('active');
            $('.sidebar .hover_menu').data('hidden', true);
        }
    }
    
    $('.sidebar .custom_button, .sidebar .hover_menu').mouseenter(function() {
        $('.sidebar .hover_menu').fadeIn('fast');
        $('.sidebar .custom_button').addClass('active');
        $(this).data('in', true);
        $('.sidebar .hover_menu').data('hidden', false);
    }).mouseleave(function() {
        $(this).data('in', false);
        setTimeout(hideMenuSidebar, delay);
    }); 
	
	function hideMenuFullwidth() {
        if (!$('.fullwidth-buttons .custom_button').data('in') && !$('.fullwidth-buttons .hover_menu').data('in') && !$('.fullwidth-buttons .hover_menu').data('hidden')) {
            $('.fullwidth-buttons .hover_menu').fadeOut('fast');
            $('.fullwidth-buttons .custom_button').removeClass('active');
            $('.fullwidth-buttons .hover_menu').data('hidden', true);
        }
    }
    
    $('.fullwidth-buttons .custom_button, .fullwidth-buttons .hover_menu').mouseenter(function() {
        $('.fullwidth-buttons .hover_menu').fadeIn('fast');
        $('.fullwidth-buttons .custom_button').addClass('active');
        $(this).data('in', true);
        $('.fullwidth-buttons .hover_menu').data('hidden', false);
    }).mouseleave(function() {
        $(this).data('in', false);
        setTimeout(hideMenuFullwidth, delay);
    }); 
	
	function hideMenuFooter() {
        if (!$('.footer .custom_button').data('in') && !$('.footer .hover_menu').data('in') && !$('.footer .hover_menu').data('hidden')) {
            $('.footer .hover_menu').fadeOut('fast');
            $('.footer .custom_button').removeClass('active');
            $('.footer .hover_menu').data('hidden', true);
        }
    }
    
    $('.footer .custom_button, .footer .hover_menu').mouseenter(function() {
        $('.footer .hover_menu').fadeIn('fast');
        $('.footer .custom_button').addClass('active');
        $(this).data('in', true);
        $('.footer .hover_menu').data('hidden', false);
    }).mouseleave(function() {
        $(this).data('in', false);
        setTimeout(hideMenuFooter, delay);
    }); 

if($("#follow-twitterapi")){
	twttr.anywhere(function (T) {
		T('#follow-twitterapi').followButton("ncteireland");
	});
}

if($(".downloads")){
	$(".downloads").hover(
	  function () {
		$(this).css("backgroundColor","#e4e8ee");
	  },
	  function () {
		$(this).css("backgroundColor","#e9ecf1");
	  }
	);
}

		   
 // main vertical scroll
$("#main").scrollable({	vertical: true }).navigator({navi:'.nitems',naviItem:'.nitem'});

$(".nav-scroller").scrollable({ vertical: true, items: '.nitems', next: '.__nnext', prev: '.__nprev' });

$(".scroller .nnext").bind("click", function() {
	var main = $("#main").data("scrollable");
	var nav = $(".nav-scroller").data("scrollable");

	var size = nav.getSize();
	var index = nav.getIndex();
	var stoppos = size - 3;
	
	if(index == stoppos){
		//alert("stop scrolling for stoppos = "+stoppos);
	} else {
		//alert("scroll away for stoppos = "+stoppos);
		nav.move(1);
	}
	
});
$(".scroller .nprev").bind("click", function() {
	var main = $("#main").data("scrollable");
	var nav = $(".nav-scroller").data("scrollable");
	
	var size = nav.getSize();
	var index = nav.getIndex();
	var stoppos = size - 3;
	
	nav.move(-1);
	
});

$(".nav-scroller .nitem:first").click();	


function yokey (){
	var nav = $(".nav-scroller").data("scrollable");
	var size = nav.getSize();
	var index = nav.getIndex();
	var stoppos = size - 3;
	
	if(index == stoppos){
		alert("stop scrolling for stoppos = "+stoppos);
	} else {
		alert("scroll away for stoppos = "+stoppos);
	}
	
}

$(".noticeboard").scrollable({ items: '.noticeboard-items', next: '.nbnext', prev: '.nbprev' }).navigator(".nbnavi");

$(".nbnext").click(function(event) {
  event.preventDefault();
});
$(".nbprev").click(function(event) {
  event.preventDefault();
});


if($("table")){
	$("table tr:even").addClass("even");
}

if($(".courses").length){
	$(".courses").tablesorter({cssHeader:'tableheader'}); 
}

if($(".spotlight").length > 0){
	$('.content .spotlight').equalHeightColumns();
}

if($(".subnav ul").length){
	$(".subnav ul ul").parent("li").addClass("active");	
}

if($(".content a")){
	$(".content a[href$='.pdf']").addClass("mini-pdf");
	$(".content a[href$='.doc']").addClass("mini-doc");
	$(".content a[href$='.docx']").addClass("mini-doc");
	$(".content a[href$='.xls']").addClass("mini-xls");
	$(".content a[href$='.xlsx']").addClass("mini-xls");
	$(".content a[href$='.txt']").addClass("mini-txt");
}

if($(".content li a")){
	
	$(".content li a.mini-pdf").parents("ul").addClass("icons");
	$(".content li a.mini-doc").parents("ul").addClass("icons");
	$(".content li a.mini-xls").parents("ul").addClass("icons");
	$(".content li a.mini-txt").parents("ul").addClass("icons");
}

}); //end document.ready



var bellc = { src: 'http://www.ncte.ie/media/bell-centennial.swf' };
var gothamb = { src: 'http://www.ncte.ie/media/gotham-book.swf' };
var gothamm = { src: 'http://www.ncte.ie/media/gotham-medium.swf' };
sIFR.activate(bellc);
sIFR.activate(gothamb);
sIFR.activate(gothamm);

sIFR.replace(gothamm, {
  selector: '.homebox h2',
  wmode: 'transparent',
  css: '.sIFR-root { color:#00254a; text-transform:uppercase; }',
  tuneHeight: '-5',
  forceSingleLine: true,
  filters: {
  DropShadow: { knockout: false,distance: 1,color: '#a8c2da',strength: 1 }
}});

sIFR.replace(bellc, {
  selector: '.pagetitle h1',
  wmode: 'transparent',
  css: '.sIFR-root { color:#00254a; letter-spacing: +1; }',
  tuneHeight: '-3',
  filters: {
  DropShadow: { knockout: false,distance: 1,color: '#a8c2da',strength:0.5 }
}});

sIFR.replace(gothamm, {
  selector: '.video-latest h3',
  wmode: 'transparent',
  css: '.sIFR-root { color:#00254a; }',
  tuneHeight: '-5',
  filters: {
  DropShadow: { knockout: false,distance: 1,color: '#a8c2da',strength: 1 }
}});

sIFR.replace(gothamm, {
  selector: '.noticeboard h2',
  wmode: 'transparent',
  css: '.sIFR-root { color:#00254a; }',
  tuneHeight: '-5',
  filters: {
  DropShadow: { knockout: false,distance: 1,color: '#a8c2da',strength: 1 }
}});

sIFR.replace(gothamm, {
  selector: '.spotlight h3',
  wmode: 'transparent',
  css: '.sIFR-root { color:#00254a; }',
  tuneHeight: '-3',
  filters: {
  DropShadow: { knockout: false,distance: 1,color: '#a8c2da',strength: 0.5 }
}});

sIFR.replace(gothamm, {
  selector: '.reports h2',
  wmode: 'transparent',
  css: '.sIFR-root { color:#00254a; }',
  tuneHeight: '-3'
});

sIFR.replace(gothamm, {
  selector: '.course-sorting h3',
  wmode: 'transparent',
  css: '.sIFR-root { color:#00254a; }',
  tuneHeight: '-3',
  filters: {
  DropShadow: { knockout: false,distance: 1,color: '#a8c2da',strength: 0.5 }
}

});

/**
 * Set all passed elements to the same height as the highest element.
 * 
 * Copyright (c) 2010 Ewen Elder
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * @author: Ewen Elder <glomainn at yahoo dot co dot uk> <ewen at jainaewen dot com>
 * @version: 1.0
**/ 
(function($){$.fn.equalHeightColumns=function(e){var J,a;e=$.extend({},$.equalHeightColumns.defaults,e);a=$(this);J=e.height;$(this).each(function(){if(e.children)a=$(this).children(e.children);if(!e.height){if(e.children){a.each(function(){if($(this).height()>J)J=$(this).height()})}else{if($(this).height()>J)J=$(this).height()}}});if(e.minHeight&&J<e.minHeight)J=e.minHeight;if(e.maxHeight&&J>e.maxHeight)J=e.maxHeight;a.animate({height:J},e.speed);return $(this)};$.equalHeightColumns={version:1.0,defaults:{children:false,height:0,minHeight:0,maxHeight:0,speed:0}}})(jQuery);


/* 
 * X Comms functions for swapping in and out videos
 * supports flowplayer videos and external embeds - e.g. YouTube, Vimeo
 */
$.fn.displayVideoDetails = function () {
	var videotitle = this.find("p a").text();	
	var videodate = this.find(".video-date").text();	
	var videoinfo = this.find(".video-info").text();
	
	$(".video-details").find("h2").text(videotitle);
	$(".video-details").find(".video-date strong").text(videodate);
	$(".video-details").find(".video-info").text(videoinfo);	
};

$.fn.embedFlowPlayerVideo = function() {
	//clear any previously viewed external videos and show the flowplayer div
	$("#external-player").remove();
	$("#flow-player").css("display", "block");
	
	//embed flowplayer
	player.play(this.find("p a").attr("href"));
	this.displayVideoDetails();		
	
	return false;
};

$.fn.embedExternalVideo = function(videoUrl, width, height) {
	//clear any previously viewed external videos and hide the flowplayer div
	var flowPlayerVid = $("#flow-player");		
	$("#external-player").remove();		
	flowPlayerVid.css("display", "none");
		
	var htmlOutput = '<div class="video-player" id="external-player"><iframe width="' + width + '" height="' + height + '" src="' + videoUrl + '" frameborder="0" allowfullscreen></iframe></div>';
	$(htmlOutput).insertBefore(flowPlayerVid);
};

var setLocationHash = function(newHashValue) {
	var newHref = window.location.href;	
	if (newHref.indexOf("#") > 0) {
		newHref = newHref.substring(0, newHref.indexOf("#"));
	}
	
	window.location.href = newHref + newHashValue;
}

var formatPreviewUrl = function (url) {
	//necessary hack - T4 preview handler doesn't like &amp; in URLs
	if(url.indexOf('SiteManager?ctfn=') >= 0) {
		return url.replace(/&amp;/g, "&");
	} else {
		return url;
	}
};

$(document).ready(function() {	
	if($(".video a").length > 0){  
		$(".video a").click(function (e) {
			var selectedVideo = $(this).parents(".video");			
			var videoUrl = selectedVideo.find("input.video-embed-url").val();
			
			if(videoUrl) {
				//if there is a hidden input called video-embed-url...
				selectedVideo.embedExternalVideo(videoUrl, 480, 320);
			} else {
				selectedVideo.embedFlowPlayerVideo();
			}	
			
			selectedVideo.displayVideoDetails();
			
			//update the URL to show the deep-link
			setLocationHash("#" + selectedVideo.attr("id"));
			
			//prevent the file being downloaded directly
			e.preventDefault();
			
			//scroll the user up to the video display
			setTimeout(function() {
			  if (location.hash) {
				window.scrollTo(0, 169);
			  }
			}, 1);
		});		
		
		//by default, select the first video
		var defaultVideo = $(".video a:first");
		
		//if there is a video ID in the URL on page load, set that as the default
		if(window.location.href.indexOf('#') > 0) {
			var hash = window.location.href.substring(window.location.href.indexOf('#'));
			if(hash) {
				defaultVideo = $(hash + ' p a');
			}
		}
		
		//set default video
		defaultVideo.trigger("click");
		
		//add a "div class clear" after each 5th video
		var videoBoxes = $("#all-videos div.video");
		for(var v=1;v<=videoBoxes.length;v++) {
			//alert("v=" + v + "   answer=" + (v % 4) + "");
			if(v % 5 == 0) {
				$("<div class=\'clear\'></div>").insertAfter(videoBoxes[v - 1]);
			}
		}
	}
	
	//show latest video on homepage
	if($("#home-latest-video").length) {
		var videoInput = $("#flow-player input[class='video-embed-url']");
		if(videoInput && videoInput.val()) {
			var videoUrl = videoInput.val();
			
			$("#latest-video-home").embedExternalVideo(videoUrl, 270, 170);
		}
	}
});
