// CRM Effects Library

var generalEffectSpeed = 800;

// Slide a div down and display
function SlideElementDown(div)
{	
	div = "#" + div;
	$(div).slideDown(generalEffectSpeed);	
}

// Slide a div up and hide
function SlideElementUp(div)
{
	div = "#" + div;
	$(div).slideUp(generalEffectSpeed);
}

// Remove a dive from displaying
function CloseElement(div)
{
	div = "#" + div;
	//$(div).css("display", "none");
	$(div).fadeOut(generalEffectSpeed/2).IeAAFix();
}

// Redisplay a dive
function OpenElement(div)
{
	div = "#" + div;
	//$(div).css("display", "block");
	$(div).fadeIn(generalEffectSpeed).IeAAFix();
}

function ieOpacityFix(div) {
	if ( $.browser.msie ) {
		$(div).get(0).style.removeAttribute('filter');
	}
}

function doFade(div, speed) {
	
	if($("#" + div).is(":hidden")) {	
		$("#" + div).fadeIn(speed);
	} else { $("#" + div).fadeOut(speed); }
	
}

//Attach a listener to catch mouse clicks
function attachListener() 
{
	if (document.layers) 
	{
		document.captureEvents(Event.MOUSEUP);
	}
	
	document.onmouseup = hide;

}

/*
Hide - Used to parse mouse clicks to determine if they are outside the message and if so hide it
*/
function hide(e) 
{	
    var i = 1;
    var x = "";
    
    //if no event retrieve (needed???)
    if(!e)
    { 
        e = window.event;
    }
    
    //IE Branch - catchs top most element (p or a, etc)
    //therefore need to loop up through the tree and determine if the element is a child of the messageContent
    if(e.srcElement)
    {
        var curElement = e.srcElement;
	    while (curElement.parentElement != null) {
		    if (curElement.id.match("messageContent")) {
		        x = curElement.id;
		    }
		    curElement = curElement.parentElement;
	    }
    }
    else //Mozilla, Firefox (everything else?) Returns main container, no need to loop
    {
	    x = e.target.id;
    }
    
    //Check if retrieved element ID is a messageContent DIV, if found check that it is visible.
    //visibile call doFade and hide
    if(!x.match("messageContent"))
    {
        $('.messageCont:visible').fadeOut(500);
    }
}

/*
Select Text from within a given element in the page.
Element must have a unique ID
*/
function selectText(elementID)
{
    var object = document.getElementById(elementID);
    var newRange;
    
    //Clear currently highlighted text
    removeCurrentSelection();
    
    // Test for IE or Mozila javascript
    if(document.all) //Using document.all - IE
    {
        //Create new range and find required selection
        newRange = document.body.createTextRange();
        newRange.findText(object.innerHTML);
        
        //Visually highlight text
        var selection = newRange.select();    
    }
    else //Using W3 Standard - Mozilla/Firefox/Everything else (hopefully)
    {
        //Create new range and add node containing content
        newRange = document.createRange();
        newRange.selectNodeContents(object);
        
        //Transfer range to window selection
        var selection = window.getSelection();
        if(selection.addRange) 
        {
            window.getSelection().addRange(newRange);
        }
    }
    selection = null;
}

//Remove current text selection
function removeCurrentSelection() 
{
    
    if(document.all)
    {
        document.selection.empty;
    }
    else
    {
        window.getSelection().removeAllRanges();
    }
}


/* Function to load the Customers/Companies content into the middle div in the contact section of the crm */
function LoadPageInDiv(div, url)
{		
	//$("#" + div).hide();
	$("#LoadingDiv").css({visibility: "visible"});
	
	if(url == "AutoLoadUrl") { // If a user is returning to this page and had previous loaded content, load it up automatically
		$("#" + div).load( $("#AutoLoadUrl").html(), function() {
			$("#" + div).show();
			$("#LoadingDiv").css({visibility: "hidden"});
		});
	} else { 
		$("#" + div).load(url, function() { // Callback function to display the div once the content is successfully loaded
			$("#" + div).show();
			$("#LoadingDiv").css({visibility: "hidden"});
		}); 
	}
	
	return false;
	
	// Hide the div where the content will be loaded
	//$("#LoadingDiv").show(); // Show the div with the loading image to make sure the users knows something is happening
}

var foshiz = 0; // Stores the status of input elements with focus, used with the helpdesk TechSearchIcon... or in short fo shiz!

/* 
	Setup the main banner

	This is done by checking which tab is currently active and then using the inner content to build the path to the image.
	
 */
function setupHeader() {
	
	var localPath = $("#CRMImagePath").attr("value"); // Need the local path
	
	// Emboss Header setup - create the path the image file and assign to the banner image which has the id of EmbossHeader
	$('#EmbossHeader').attr( "style", 'background-image:url("' + localPath +  'Emboss_' + $('td.TabActiveFill > a').html() + '.gif' + '");' );
	
}

/* Setup for all of the left hand side icon menu functions
   Must be executed others the menus wont work  */

$(document).ready(function()
{
	var speedTabMenu = 200; // Tab menu speed
	var speedIcon = 600; // Icon fade speed
	var speedTip = 150; // Tool tip speed
	var localPath = $("#CRMImagePath").attr("value"); // Locall path variable for dynamic images
	
	/* Tab Hovers */
	$(".CRMTabs a").each(function(e) {
		// Assign hover functions to tab links
		$(this).hover(function() {
			$(this).animate({color: "#D01530"}); // Turn to red
		   },
		   function () {
		    $(this).animate({color: "#1A1A1A"}); // Turn back to black
		});
	});
	
	/* Left Menu Icon setup */
	
	$(".LeftIcon").each(function(e) {
		
		// Set the original height value to enable hover function to access it and reset the css height if the slide animation is ended before it completes
		if(($(this).find(".LeftIconSubDIV").width()) != null) {
			$(this).attr("origWidth", $(this).find(".LeftIconSubDIV").width()); // The place the height/width is stored is a custom attribute on the icon which the subdiv is assocaited with.
		} else { $(this).attr("origWidth", 0 ); } // If no sub content just set the original height to 0 (could possibly be omitted for speed?)
		
		// Assign Hover functions to the icons tab menus
		$(this).hover(function() {	
				if(this.id != "TechSearchIcon") { // If this is a normal icon menu
					$(this).find(".LeftIconSubDIV").css("overflow", "visible"); // Make sure the overflow is visible so tool tips will be displayed
				}
				$(this).find(".IconImage > a > img").fadeTo({queue: false, duration: speedIcon}, "1"); // Fade in the Main Icon over image
				$(this).find(".LeftIconSubDIV").width(parseInt($(this).attr("origWidth"))).animate({width: "show"}, {queue: false, duration: speedTabMenu, easing: "easeInOutCirc"}); // Slide in the icon menu				
			},
			function () {
				// This if statement checks to see if the icon that is being hovered over is the TechSearch Icon with input fields. This needs to be handled seperatly so as to not hide the menu if an input field has focus
				if(this.id == "TechSearchIcon" ) { 
					if(foshiz == 1) { // An input field has focus, dont hide this menu
						return;
					} else {
						if(foshiz == 0 && $("#LeftIconSubTechSearchDIV").is(":visible")) { // The input field does not have focus anyone but if still visible. It needs to be hidden
							$("#TechSearchIcon .IconImage > a > img").fadeTo({queue: false, duration: speedIcon}, "0"); // Fade out the Main Icon over image
							$("#LeftIconSubTechSearchDIV:visible").css("overflow", "hidden").animate({width: "hide"}, {queue: false, duration: speedTabMenu, easing: "easeInOutCirc"}); // Slide out the icon menu	(hide)
						}
					}
				}
				$(this).find(".IconImage > a > img").fadeTo({queue: false, duration: speedIcon}, "0"); // Fade out the Main Icon over image
			  	$(this).find(".LeftIconSubDIV").css("overflow", "hidden").animate({width: "hide"}, {queue: false, duration: speedTabMenu, easing: "easeInOutCirc"}); // Slide out the icon menu (hide)
			});
		});
	
	
	// Effects for the small sub icons in the left hand side icon menu. These hover effects subetly change the background colour and then display the tooltip associated.
	$(".LeftIconSub").hover(function() {
	   $(this).animate({backgroundColor:"#777777"}); // Bg color animation
	   $(this).find(".ButtonTip").show(); // Fade out the tooltip
	   },
	   function() {
		   $(this).animate({backgroundColor:"#484848"}); // Bg color animation
		   $(this).find(".ButtonTip").hide(); // Fade out the tooltip
	   });
	   
	// This function is used in the helpdesk to set the variable to indicate if an input field is in focus
	$('#LeftIconSubTechSearchDIV :input').focus(function() {
									foshiz = 1;
									});
	// Set the variable back to false when field is not in focus
	$('#LeftIconSubTechSearchDIV :input').blur(function() {
									foshiz = 0;
									});
	
	// Hovers for Top Icons used to jump between display, edit, permissions pages
	$('.tab_icon').each(function(e) { // Loop through each icon by selecting all icons witht he tab_icon class
		
		// Assign a hover function to each icon found
		$(this).hover(function() {
			$("#" + this.id + " > a > img").fadeTo({queue: false, speed: speedIcon}, "1"); // Fade in to full opacity
		},
		function () {
			$("#" + this.id + " > a > img").fadeTo({queue: false, speed: speedIcon}, "0"); // Fade out to 0 opacity but stay displayed to ensure the background icon stays the correct size
		});
	 });
	
	// Fade out pop up message display div
	$('div.Message').fadeOut(5000);
	
	$("div.BackButton").hover(function() {
		$(this).find("img").fadeTo({queue: false, speed: speedIcon}, "1"); // Fade in to full opacity
	},
	function () {
		$(this).find("img").fadeTo({queue: false, speed: speedIcon}, "0"); // Fade in to full opacity
	});	
	
	// Setup events for Input Info Box pics
	$("img.InputInfoIcon").each(function(e) {
		
		$(this).hover(function() {
			$(this).next("div.InputInfoBox").css("top", $(this).offset().top - $(this).next("div.InputInfoBox").height() );
			$(this).next("div.InputInfoBox").css("left", $(this).offset().left + $(this).width() );
			$(this).next("div.InputInfoBox").show();
		}, function() {
			$(this).next("div.InputInfoBox").hide();
		});
		
	});
	
});


