/*
	Author: mcads.co.uk
*/

var titleChanged = false;
var bgChanged = false;

// header dead for contact page
var myHeaders = new Array();

myHeaders["#headerEdinburgh"] = "Directions from Edinburgh";
myHeaders["#headerGlasgow"] = "Directions from Glasgow";
myHeaders["#headerPerth"] = "Directions from Perth";
myHeaders["#headerAddress"] = "Address";

function ChangeTitle()
{
	if (titleChanged == false)
  {
  	document.getElementById("bannerImage").src = "images/logoClean.jpg";
  	titleChanged = true
  }
	else
  {
  	document.getElementById("bannerImage").src = "images/logo.jpg";
  	titleChanged = false
  }
}

function ChangeBackground(imageID, newImage)
{
	document.getElementById(imageID).src = newImage
	/*if (bgChanged == false)
  {
  	document.getElementById(imageID).src = newImage
  	//document.getElementById("contentBackground").style.display = "none"
  	bgChanged = true
  }
	else
  {
  	document.getElementById(imageID).style.display = "block"
  	//document.getElementById("contentBackground").style.display = "block"
  	bgChanged = false
  }*/
}

function ChangeHeader (newHeaderFile)
{
	document.getElementById("headerImage").src = newHeaderFile;
}

function ChangeBackgroundColour (newColor)
{
	document.getElementById("headerDiv").style.background = newColor
}


function ReplacePane (paneName)
{
	// THIS NEEDS THE DATA SCRIPT FILE TO FUNCTION
	document.getElementById("displayPane").innerHTML = myData[paneName]
}


// setup events for index and arch design pages
$(document).ready(function() {
	// check only a and li tags then check for the id prefix
	/*$('a, li').filter('[id^=MCADS_EVENT_]').hover(function() {
		ReplacePane($(this).attr('id').substring(12));
	});*/
	
	$('[id^=EXPANDJS_TITLE_]').click(function() {
		var curID = $(this).attr('id').substring(15)
		
		// close all elements
		$('[id^=EXPANDJS_CONTENT_]').css("display", "none");
		
		// open current content element
		$('#EXPANDJS_CONTENT_' + curID).css("display", "");
	
		var arrowNormal = "images/DirectionArrow.png";
		var arrowDown = "images/DirectionArrowDown.png";
		
		// make all images into deafult mode
		$('[id^=EXPANDJS_ARROW_]').attr("src", arrowNormal);
		
		// add arrow to current element ID
		$('#EXPANDJS_ARROW_' + curID).attr("src", arrowDown);
		
		// all css styling off
		$('[id^=EXPANDJS_HEADER_]').css('font-weight', '');
		
		// css styling on current element
		$('#EXPANDJS_HEADER_' + curID).css('font-weight', 'bold');
	});
	
	$('#bannerButton').click(function() {
		ChangeTitle();
	});
	
	$('#backgroundButton').click(function() {
		ChangeBackground("contentBackImage", "images/contentBack.jpg");
	});
	
	$('#backgroundButton2').click(function() {
		ChangeBackground("contentBackImage", "images/contentBack2.jpg");
	});
	
	$('#backgroundButton3').click(function() {
		ChangeBackground("contentBackImage", "images/spacer.gif");
	});
});

// check for any events passed via page variables and call them
$(document).ready(function() {
	if(document.getElementById("SetupPane") != undefined) {
		if(document.getElementById("SetupPane").value != "") {
			ReplacePane (document.getElementById("SetupPane").value.substring(12));
		}
	}
});


// highlight current page menu link
$(document).ready(function() {
	HighlightCurrentMenuLink();
});

function HighlightCurrentMenuLink() {
	// SETUP THE STYLE NAMES HERE
	var menuStyle = 'a.menuLink';
	var highlightStyle = 'highlightLink';
	
	$(menuStyle).each(function(index) {
		if(getPageName($(this).attr('href')) == getPageName()) {
			$(this).addClass(highlightStyle);
		}
	});
}

function getPageName(pageURL) {
	var pageName = pageURL;
	if(pageURL === undefined) {
  	pageName = $(location).attr('href');
  }
  pageName = pageName.split("#")[0];
  pageName = pageName.split("?")[0];
  pageName = pageName.substring(pageName.lastIndexOf("/") + 1, pageName.length);

  // SETUP THE INDEX FILE NAME HERE
  if(pageName == "") {
  	pageName = "index.html";
  }
  return pageName;
}




// data for rooms + rates tooltips

var myTooltips = new Array();


// redundant images, not comments!
myTooltips["Image01"] = "images/Room 1 1 Full.jpg";
myTooltips["Image02"] = "images/Room 1 2 Full.jpg";
myTooltips["Image03"] = "images/Room 2 1 Full.jpg";
myTooltips["Image04"] = "images/Room 2 2 Full.jpg";
myTooltips["Image05"] = "images/Room 3 1 Full.jpg";
myTooltips["Image06"] = "images/Room 3 2 Full.jpg";
myTooltips["Image07"] = "images/Room 4 1 Full.jpg";
myTooltips["Image08"] = "images/Room 4 2 Full.jpg";
myTooltips["Image09"] = "images/Room 5 1 Full.jpg";
myTooltips["Image10"] = "images/Room 5 2 Full.jpg";
myTooltips["Image11"] = "images/Room 6 1 Full.jpg";
myTooltips["Image12"] = "images/Room 6 2 Full.jpg";
myTooltips["Image13"] = "images/interior 1 full.jpg";
myTooltips["Image14"] = "images/Interior 2 Full.jpg";
myTooltips["Image15"] = "images/exterior 1 full.jpg";
myTooltips["Image16"] = "images/Exterior 2 Full.jpg";
myTooltips["Image17"] = "images/Dining 1 Full.jpg";
myTooltips["Image18"] = "images/Dining 2 Full.jpg";
myTooltips["Image19"] = "images/";
myTooltips["Image20"] = "images/";



myTooltips["Review1"] = "images/Comments 1.jpg";
myTooltips["Review2"] = "images/Comments 2.jpg";
myTooltips["Review3"] = "images/Comments 3.jpg";
myTooltips["Review4"] = "images/Comments 4.jpg";
myTooltips["Review5"] = "images/Comments 5.jpg";
myTooltips["Review6"] = "images/Comments 6.jpg";
myTooltips["Review7"] = "images/Comments 7.jpg";

myTooltips["DirectionExpand"] = "Click to Expand";
myTooltips["DownloadMap"] = "To download; Right Click and Save as...";

/*
	Jquery Dynamic Tooltips
	Version: 0.1
	Author: Timothy Cowan
*/

var dynamicTips_tooltipOffsetX = 15;
var dynamicTips_tooltipOffsetY = 15;
var avalibleHeight = 400;
var marginOffset = 50;
var cursorInUpperScreen = true;
var delayTime = 0;

$(document).ready(function() {
	//$('#DYNATOOLTIP').stop().fadeTo(0, 0);
	//$('#DYNATOOLTIP').css("visibility", "visible");
	/*$('body').hover(function(curEvent) {
		$('#lol').text(curEvent.pageX + ", " + curEvent.pageY);
	}, function() {
		// hide tooltip
		//$('#DYNATOOLTIP').stop(true, true).fadeOut(100);
	});*/
	
	
	// update the delay time if the variable is present on the page
	delayTime = $('[id^=DYNATIP_VAR_DELAY]').val();
	
	// image version
	$('[id^=DYNATIP_Im_]').hover(function() {
		document.getElementById("DYNATOOLTIPIMAGE").src = myTooltips[$(this).attr('id').substring(11)];
		
		
		// show tooltip		
		$('#DYNATOOLTIP').css("display", "none");
		$('#DYNATOOLTIP').css("visibility", "visible");
		$('#DYNATOOLTIP').stop(true, true).delay(delayTime).fadeIn(200);
	}, function() {
		// hide tooltip
		$('#DYNATOOLTIP').stop(true, true).fadeOut(100);
	});
	
	// text version
	$('[id^=DYNATIP_Tx_]').hover(function() {
		$('#DYNATOOLTIP').text(myTooltips[$(this).attr('id').substring(11)]); // old text version
		// show tooltip
		$('#DYNATOOLTIP').css("visibility", "visible");
	}, function() {
		// hide tooltip
		$('#DYNATOOLTIP').css("visibility", "hidden");
	});
	
	// start mousemove event checking to update tooltip position
	$('[id^=DYNATIP_]').mousemove(function(curEvent){
		// get element width
		var tooltipWidth = $('#DYNATOOLTIP').outerWidth(false);
		var tooltipHeight = $('#DYNATOOLTIP').outerHeight(false);
		
		var mousePosX = curEvent.pageX;
		var mousePosY = curEvent.pageY;
		
		// if the space between the edge of the document and the mouse is smaller than the width, move it inwards
		// solves the problem of not seeing the tooltip near the edge of the page
		var winWidth = $(window).width();
		var winHeight = $(window).height();
		
		var tooltipReposX = 0; // default value, not used unless off page
		var tooltipReposY = 0; // default value, not used unless off page
		
		
		if(winWidth - tooltipWidth < mousePosX + dynamicTips_tooltipOffsetX) {
			tooltipReposX = -tooltipWidth - (dynamicTips_tooltipOffsetX * 2);
		}
		/*if(mousePosY < dynamicTips_tooltipOffsetY * -1) {
			tooltipReposY = (dynamicTips_tooltipOffsetY * -1) + 10
		}*/
		
		
		// put the tool tip on the left side if on the right side of the screen
		if(winWidth / 2 < mousePosX + dynamicTips_tooltipOffsetX) {
			tooltipReposX = -tooltipWidth - (dynamicTips_tooltipOffsetX * 2);
		}
		/*if(mousePosY < dynamicTips_tooltipOffsetY * -1) {
			tooltipReposY = (dynamicTips_tooltipOffsetY * -1) + 10
		}*/
		
		
		// raise the tooltip if too low down
		if(winHeight / 2 < mousePosY + dynamicTips_tooltipOffsetY) {
			tooltipReposY = -tooltipHeight - (dynamicTips_tooltipOffsetY * 2);
			cursorInUpperScreen = false;
		}
		/*if(mousePosY < dynamicTips_tooltipOffsetY * -1) {
			tooltipReposY = (dynamicTips_tooltipOffsetY * -1) + 10
		}*/
		
		//$('#lol').text("Too low: " + tooltipReposY + ", winHeight: " + winHeight + ", mousePosY: " + (mousePosY + dynamicTips_tooltipOffsetY));
		
		// upper edge stop - tooltip is flipped up so we only care about upper edge
		if(tooltipReposY + mousePosY - dynamicTips_tooltipOffsetY - marginOffset < 0) {
			var lolNum = tooltipReposY + mousePosY - dynamicTips_tooltipOffsetY - marginOffset;
			if(winHeight / 2 < mousePosY + dynamicTips_tooltipOffsetY) {
				var temp = tooltipReposY;
				tooltipReposY += (mousePosY - tooltipHeight - dynamicTips_tooltipOffsetY - marginOffset) * -1;
				//$('#lol').text("Upper Edge Hit: " + "Old Repos Value: " + lolNum + ", New Repos Value: " + tooltipReposY + ", tooltipHeight: " + tooltipHeight + ", winHeight: " + winHeight + ", mousePosY: " + (mousePosY + dynamicTips_tooltipOffsetY));
			}
		}
		
		// lower edge stop - tooltip is going down from cursor so we only care about lower edge
		if(mousePosY + tooltipHeight + dynamicTips_tooltipOffsetY + marginOffset > winHeight) {
			if(winHeight / 2 >= mousePosY + dynamicTips_tooltipOffsetY) {
				tooltipReposY += winHeight - (tooltipHeight + mousePosY + marginOffset);
				//$('#lol').text("Lower Edge Hit: " + tooltipReposY + ", tooltipHeight: " + tooltipHeight + ", winHeight: " + winHeight + ", mousePosY: " + (mousePosY + dynamicTips_tooltipOffsetY));
			}
		}
		
		
		
		// get new positions
		var newTooltipPosX = curEvent.pageX + dynamicTips_tooltipOffsetX + tooltipReposX;// - tooltipWidth;
		var newTooltipPosY = curEvent.pageY + dynamicTips_tooltipOffsetY + tooltipReposY;// - tooltipHeight;
		$('#DYNATOOLTIP').css("left", newTooltipPosX);
		$('#DYNATOOLTIP').css("top", newTooltipPosY);
	});
});




// Slideshow Gallery Jquery

$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */

	var totWidth = 0;
	var positions = new Array();

	$('#slideshow_slides .slideshow_slide').each(function(i){
		/* Loop through all the slides and store their accumulative widths in totWidth */
		positions[i]= totWidth;
		totWidth += $(this).width();

		/* The positions array contains each slide's commulutative offset from the left part of the container */

		if(!$(this).width())
		{
			alert("Please, fill in width & height for all your images!");
			return false;
		}
	});

	$('#slideshow_slides').width(totWidth);

	/* Change the cotnainer div's width to the exact width of all the slides combined */

	$('#slideshow_menu ul li a').click(function(e){

		/* On a thumbnail click */
		$('li.slideshow_menuItem').removeClass('act').addClass('inact');
		$(this).parent().addClass('act');

		var pos = $(this).parent().prevAll('.slideshow_menuItem').length;
		
		/* Start the sliding animation */
		$('#slideshow_slides').stop().animate({marginLeft:-positions[pos]+'px'}, 450);
		
		/* Prevent the default action of the link */
		e.preventDefault();
	});

	/* On page load, mark the first thumbnail as active */
	$('#slideshow_menu ul li.slideshow_menuItem:first').addClass('act').siblings().addClass('inact');
});
