﻿	/*
	************************************************************************

	Default scripts
		General scripts

	@Homepage:		http://www.internotredici.com
	@Author:			Fabrizio Menghini Calderón <fabrizio@internotredici.com>
	@License:		Creative Commons Attribution-Noncommercial-Share Alike 
						3.0 Unported License
	@Copyright:		2009 (c) Interno Tredici. 
						Some rights reserved.

	************************************************************************
	*/





	function ShowLayer(whichLayer)
	{
		if(document.getElementById)
		{
			// this is the way the standards work
			var style2 = document.getElementById(whichLayer).style;
			style2.display = "block";
		}
		else if (document.all)
		{
			// this is the way old msie versions work
			var style2 = document.all[whichLayer].style;
			style2.display = "block";
		}
		else if (document.layers)
		{
			// this is the way nn4 works
			var style2 = document.layers[whichLayer].style;
			style2.display = "block";
		}
	}

	function HideLayer(whichLayer)
	{
		if(document.getElementById)
		{
			// this is the way the standards work
			var style2 = document.getElementById(whichLayer).style;
			style2.display = "none";
		}
		else if (document.all)
		{
			// this is the way old msie versions work
			var style2 = document.all[whichLayer].style;
			style2.display = "none";
		}
		else if (document.layers)
		{
			// this is the way nn4 works
			var style2 = document.layers[whichLayer].style;
			style2.display = "none";
		}
	}

	/*
	========================================================================
		JQuery ready function
	========================================================================
	*/

	$(document).ready
	(
		function()
		{
			// Opening ready function


			// Change the hr into separators
			$("hr").replaceWith("<div class=\"separator\"></div>");

			// IE fixes
			$(".large:first").css("margin-left","0");
			$(".contentpage .large:even").css("margin-left","0");
			$(".contentpage .large:odd").css("margin-right","0");

			//$(".homepage .large:nth-child(2)").css("margin-left","0");
			if(document.all)
			{
				//$(".homepage .doormat:nth-child(3n+0)").css("margin-left","0");
				//$(".homepage .doormat:nth-child(3n+1)").css("margin-left","0");
				//$(".homepage .doormat:nth-child(3n+2)").css("margin-right","0");
				//$(".contentpage .doormat:nth-child(2n+1)").css("margin-left","0");
				//$(".contentpage .doormat:nth-child(2n+0)").css("margin-right","0");

				$("#main_content .call_to_actions h1:first-child").css("margin-top",0);
				$("#main_content .call_to_actions h2:first-child").css("margin-top",0);
				$("#main_content .call_to_actions h3:first-child").css("margin-top",0);
/*
				$("#main_content .call_to_actions p:last-of-type").css("margin-bottom",0);
				$("#main_content .call_to_actions ul:last-of-type").css("margin-bottom",0);
				$("#main_content .call_to_actions ol:last-of-type").css("margin-bottom",0);*/
			}


			/*
			==============================================
				Hover button
			==============================================
			*/

			/* Check if SiteManager preview is used */
			var current_url = document.location.href;
			if(current_url.indexOf("SiteManager?ctfn=publish") == -1)
			{
				/* Change the hover button on mouse hover */
				$(".hover_button").hover ( function () 
					{
						if($(this).attr("src"))
						{
							/* Remove the png extension from the image name */
							var image_normal = $(this).attr("src"); 
							image_normal = image_normal.replace(/_hover.png/,"");
							image_normal = image_normal.replace(/.png/,"");
							image_normal = image_normal + "_hover.png";

							$(this).attr("src",image_normal); 
						}
					}, function () 
					{ 
						if($(this).attr("src"))
						{
							/* Remove the "_hover.png" substring from the image name */
							var image_hover = $(this).attr("src"); 
							image_hover = image_hover.replace(/_hover.png/,"");
							image_hover = image_hover.replace(/.png/,"");
							image_hover = image_hover + ".png";

							$(this).attr("src",image_hover); 
						}
					} 
				);


				/* Change the hover button on focus */
				$(".hover_button").focus ( function () 
					{
						if($(this).attr("src"))
						{
							/* Remove the png extension from the image name */
							var image_normal = $(this).attr("src"); 
							image_normal = image_normal.replace(/_hover.png/,"");
							image_normal = image_normal.replace(/.png/,"");
							image_normal = image_normal + "_hover.png";

							$(this).attr("src",image_normal); 
						}
					} 
				);

				/* Change the hover button on blur */
				$(".hover_button").blur ( function () 
					{ 
						if($(this).attr("src"))
						{
							/* Remove the "_hover.png" substring from the image name */
							var image_hover = $(this).attr("src"); 
							image_hover = image_hover.replace(/_hover.png/,"");
							image_hover = image_hover.replace(/.png/,"");
							image_hover = image_hover + ".png";

							$(this).attr("src",image_hover); 
						}
					} 
				);

				/* Unbind the functions */
				$(".hover_button").click ( function () 
					{ 
						if($(this).parent().lenght > 0)
						{
							var parent_href = $(this).parent().attr("href");
							if(parent_href.charAt(0) != "#")
								$(".hover_button").unbind();
						}
					} 
				);
			}



			// Fix the link on the hero image
			// Get the image path and put as background for the link
			$('.hero_image').each(function(index)
				{
					var hero_image = $(this).children("img").attr("src");
					$(this).children("a").css("background-image","url("+ hero_image +")");

					/* Hover for the hero graphic */
					$(this).children("a").hover ( function () 
						{
							if($(this).prev().attr("src"))
							{
								/* Remove the png extension from the image name */
								var image_normal = $(this).prev().attr("src");
								image_normal = image_normal.replace(/_hover.jpg/,"");
								image_normal = image_normal.replace(/.jpg/,"");
								image_normal = image_normal + "_hover.jpg";

								$(this).css("background-image","url("+ image_normal +")");
							}
						}, function () 
						{ 
							if($(this).prev().attr("src"))
							{
								/* Remove the "_hover.png" substring from the image name */
								var image_hover = $(this).prev().attr("src");
								image_hover = image_hover.replace(/_hover.jpg/,"");
								image_hover = image_hover.replace(/.jpg/,"");
								image_hover = image_hover + ".jpg";

								$(this).css("background-image","url("+ image_hover +")");
							}
						} 
					);

				});
			/*
			==============================================
				Rotating banners
			==============================================
			*/

			if($("#rotating_gallery").length > 0)
			{
				$('#rotating_gallery div:first').css("display","block");
				$('#rotating_gallery').cycle (
					{
						fx: 'fade',
						pause: true,
						timeout: 4000
					} );
			};
			if($(".rotating_one_column").length > 0)
			{
				$('.rotating_one_column p:first').css("display","block");
				$('.rotating_one_column').cycle (
					{
						fx: 'fade',
						pause: true,
						timeout: 4000
					} );
			};
			


			/*
			==============================================
				Fix the height in doormats
			==============================================
			*/	

			var max_height = 0;
			$(".large .doormat_content").each
			(
				function (i)
				{
					if ($(this).height() > max_height) 
						max_height = $(this).height();
						
				}
			);
			$(".large .doormat_content").height(max_height);
			


			/*
			==============================================
				Iframe height resize
			==============================================
			*/
			if ($('#locateiframe').length) {
				var $iframe = $("#locateiframe");
				function resize_iframe() {
  				document.domain = "casl.ucd.ie"
  				var current_height = $iframe.css("height");
  				if(current_height != $iframe[0].contentWindow.document.body.scrollHeight) {
    				$iframe.css("height", $iframe[0].contentWindow.document.body.scrollHeight);
  				}
				}
				resize_iframe();
				//setInterval("resize_iframe();", 100);
			}
			


			/*
			==============================================
				People page
			==============================================
			*/	

			if($("#main_content #rms-main").length > 0)
			{
				/* Remove styles */
				$("#main_content #rms-main").removeAttr("style");
				$("#main_content #rms-centre").removeAttr("style");

				/* Add the tabs navigation */
				$("#main_content").prepend("<div id=\"tabs\"></div>");

				/* Add the tabs to the tabs div */
				$("#tabs").append($("#profileNavigation").html());
				$("#main_content #profileNavigation").remove();

				/* Format the tabs */ 
				$("#tabs ul").attr("id","tab-list");
				$("#tabs ul").addClass('clearfix');

				/* Tabs content */
				$("#tabs").append("<div id=\"tab-content\"></div>");

				/* Set the current page */
				$("#tabs ul li a.current").parent().addClass('selected');
				$("#tabs ul li a.current").parent().prepend("<span>" + $("#tabs ul li a.current").text() + "</span>");
				$("#tabs ul li a.current").remove();

				/* Add the project info */
				$("#main_content").prepend("<div id=\"project-intro\" class=\"clearfix\"></div>");

				/* Convert the title in H1 */
				$("#main_content #rms-centre h2").text();
				$("#main_content").prepend("<h1>" + $("#main_content #rms-centre h2").text() + "</h1>");
				$("#main_content #rms-centre h2").remove();

				/* Remove unused features */
				$("#HIDE_PROJECTS").remove();
				$("#SHOW_PROJECTS").remove();

				/* Remove scripts */
				$("#rms-centre script").remove();
				$("#rms-centre noscript").remove();

				/* Copy the content into the content tabs */
				var person_content = $("#rms-centre").html();
				$("#tabs #tab-content").html(person_content);

				/* Remove table widths */
				$("#tabs #tab-content table").removeAttr("width");

				/* Format the people home page */
				if($("#tabs #tab-content #rms-right").length > 0)
				{
					var rightColumn = $("#rms-right").html();
					$("#rms-right").remove();

					var leftColumn = $("#tabs #tab-content").html();
					$("#tabs #tab-content").empty();

					/* Add a new table */
					$("#tabs #tab-content").prepend("<table id=\"personTable\" style=\"width: 100%;\"><tr><td id=\"personLeft\"></td><td id=\"personRight\" style=\"width: 180px; vertical-align: top;\"></td></tr></table>");

					$("#tab-content #personLeft").prepend(leftColumn);
					$("#tab-content #personRight").prepend(rightColumn);
				}

				/* Remove the old content */
				$("#rms-centre").remove();
			}

			// Closing ready function
		}
	);

