
/*
function initTabs(elem_id) {
	$('.tabs', elem_id).tabs( {selected:0, fx:{opacity:'toggle', duration:500} } ).find(' > ul a').bind('click', function (){																																	  
		$(this).parent().addClass('ui-state-active').siblings().removeClass('ui-state-active'); // switch tab style quicker
		$('.mask').animate({'height':$(this.hash).height()}, {queue:false, easing:'easeOutQuart', duration:900});
		$(this.hash).addClass('selected').siblings().removeClass('selected'); // $('.mask > div').removeClass('selected').filter(this.hash).addClass('selected');
		return false;
	});
}
*/

$(document).ready(function() {  // $(function(){
															 
	var container = $("#outer_wrap");
	// container.find("a[target='_blank']").css("border", "5px solid red").css("padding", "3px"); //
	container.find('a[class="new_window"]').attr({target:'_blank', title:'This link will open in a new window'}); // Concatenate to existing title attribute value (new window) etc
	container.find('a[href^="mailto:"]').attr({title:'Send an e-mail using your default mail client'});
	// container.find("img[alt!='*']img[alt!='']").css("border", "1px solid red").css("padding", "3px");
	
	/*
	$('[href$="pdf"]), [href$="doc"], [href$="xls"]').live("click", function(){ // get the link attribute and find the filename of the PDF 
	   var pdfLink = $(this).attr("href"), pdfLocation = pdfLink.substr(pdfLink.lastIndexOf("/")).replace("/","") ; 
	   _gaq.push(['_trackPageview', pdfLocation]); 
	});
	*/
	
	// TODO: Combine with above? - No point searching through all the links again
	// - Though what if class new_window is missing?
	
	var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;
	var baseHref = '';
	
	if ($('base').attr('href') != undefined) baseHref = $('base').attr('href');
	$('a').each(function() {
		var href = $(this).attr('href');
		// External link
		if (href && (href.match(/^https?\:/i)) && (!href.match(document.domain))) {
			 $(this).click(function() {
				  var extLink = href.replace(/^https?\:\/\//i, '');
				  _gaq.push(['_trackEvent', 'External', 'Click', extLink]);
				  if ($(this).attr('target') != undefined && $(this).attr('target').toLowerCase() != '_blank') {
						setTimeout(function() { location.href = href; }, 200);
						return false;
				  }
			 });
		}
		// E-mail link
		else if (href && href.match(/^mailto\:/i)) {
			 $(this).click(function() {
				  var mailLink = href.replace(/^mailto\:/i, '');
				  _gaq.push(['_trackEvent', 'Email', 'Click', mailLink]);
			 });
		}
		// File
		else if (href && href.match(filetypes)) {
			 $(this).click(function() {
				  var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
				  var filePath = href;
				  _gaq.push(['_trackEvent', 'Download', 'Click-' + extension, filePath]);
				  if ($(this).attr('target') != undefined && $(this).attr('target').toLowerCase() != '_blank') {
						setTimeout(function() { location.href = baseHref + href; }, 200);
						return false;
				  }
			 });
		}
	});

	
	// Zebra tables
   $("#main_content .content table.zebra tbody tr:nth-child(even)").addClass("odd");
	
	initTabs = function(elem_id) {
		$(elem_id).tabs( {selected:0, fx:{opacity:'toggle', duration:500} } ).find('.tab_nav a').bind('click', function (){
			$(this).parent().toggleClass("ui-state-selected");
			$('.mask', elem_id).stop(true, true).animate({'height':$(this.hash).height()}, {queue:false, easing:'easeOutQuart', duration:900});
			$(this.hash).addClass('selected').siblings().removeClass('selected');
			return false;
		});
	}
	
   /*--- Top Panel ---*/
	topPanel = function(elem, url) { // function topPanel() {
		$(elem).load(url, "", function(responseText, textStatus, XMLHttpRequest) {
         if (textStatus == 'error') {
			   return; // Problem loading data, leave link as is
         } else {
			   $("#toggleButton").live('click', function(){
			      if ($("#top_panel").is(":hidden")) { // or hasClass?
			         $("#top_panel").slideDown({easing:'easeOutQuart', duration:1000}); //$("#top_panel").slideDown(1000);
			         $(this).replaceWith("<a id=\"toggleButton\" class=\"active\" href=\"#\">Close Shortcuts</a>");
						return false;
			         // $.cookie('showTop', 'expanded');
					} else {
						$("#top_panel").slideUp({easing:'easeOutQuart', duration:1000}); //$("#top_panel").slideUp(900);
						$(this).replaceWith("<a id=\"toggleButton\" href=\"#\">Shortcuts</a>");
						// $.cookie("showTop", null);
					}
					return false;
				});
			}
      });
		/*
		var showTop = $.cookie('showTop');
		if (showTop != null) {
         $("#top_panel").show();
         $("#toggleButton").addClass("active");
		}
		*/
	};
	
   /*--- Top Level Megadropdown ---
   $(window).bind('resize load', function() {

      $("li.mega").each(function(i) {
         var panel = $(this).find("div.panel");
         var panel_width = panel.width();
			var panel_padding = parseFloat(panel.css("padding-left"),2)*2;
         var position = $(this).position();
         var li_offset_left = position.left;
			var nav_padding = parseFloat($("#nav_main > div").css("padding-right"), 2); // '2)*2;' // deduct padding on nav menu
			
			// TODO: Maybe add exception...
         // if .three_col align to right, else if .two_col flip left
			
			if ((li_offset_left + panel.width()) >= ($('#container').width()) - nav_padding) { // if it's outwith the boundary of it's containing element, re-position it.
            var right_offset = - (li_offset_left + panel.width() - $('#container').width());
            var posX = right_offset - (panel_padding + nav_padding);
			} else {
				var posX = 0;
			}
			panel.css("left",(posX) + "px");
			}

		);
	});
	*/

  

	/*
	$url = $(location).attr('href');
	if ($('$url^="http://www.churchofscotland.org.uk/"')) { // If not church domain
		$("a").each(
			
			function(intIndex) {
				$(this).bind (
					"click",
					function(){
					   alert("Link index: " + intIndex);
					}
				);
		   }
			
			//function() {
			//   $(this).attr('target', '_blank');
			//}
			
		);
	}*/
	// $("a:not([href^='http://www.churchofscotland.org.uk/'])").attr("target", "_blank");
	

});

$(window).ready(function() {
	
	$("input.sq-form-field", "#search_site").focus(function () {
			$("#nav_top h1").stop().animate({width: '684px'}, {queue:false, easing:'easeOutQuart', duration:500}), // easeOutElastic (800)
			$(this).stop().animate({width: '262px'}, {queue:false, easing:'easeOutQuart', duration:500}).blur(function() { // easeOutElastic
			$(this).stop().animate({width: '172px'}, {queue:false, easing:'easeOutQuart', duration:400}), // easeOutQuart
			$("#nav_top h1").stop().animate({width: '774px'}, {queue:false, easing:'easeOutQuart', duration:400});
		});
	});
	
	toggle = function($trigger) {
		$($trigger).click(function () {
			$div = $(this).next("li .toggle_wrap").css("width", $(this).parent().width()); // $div = $(this).next("li > div").wrapInner('<div class="toggle_wrap">').css("width", $(this).parent().width());
			if ($div.is(':hidden')) {
				$(this).parent().addClass("active");
				$div.stop(true, true).animate({height: 'toggle', opacity: 'toggle' }, {queue:false, easing:'easeOutQuart', duration:800});
			} else {
				$(this).parent().removeClass("active");
				$div.stop(true, true).animate({height: 'toggle', opacity: 'toggle' }, {queue:false, easing:'easeOutQuart', duration:800});
			}
			return false;
		});
	}
		
	
	/*			 
	function addMega(){
		var li = $(this);
		li.addClass("hovering");
		li.children("a").animate({backgroundColor:"#ccc", color:"#000"}, 400); // $('a', this).ani...
		$(li.find("div")).fadeIn(350);
	}

	function removeMega(){
		var li = $(this);
		li.removeClass("hovering");
		li.children("a").removeAttr("style"); // Quick fix, setting transparency doesn't work (removes css a:hover bgcolor) :(  OR: $(this).children("a").attr("style","");
		$(li.find("div")).fadeOut(200);
	}
								 
	var megaConfig = {
		interval: 400, // 400
		sensitivity: 8, // 4
		over: addMega,
		timeout: 250, // 500
		out: removeMega
	};
	
	$("li.mega").hoverIntent(megaConfig);
	$("li.mega div.panel").hide();
	*/
	
	// Init top panel
   // topPanel('#top_panel', 'top.panel.remote.php #remote_content');
	topPanel('#top_panel', 'http://www.churchofscotland.org.uk/site_tools/shortcuts/ #remote_content');
	
});




