
/*
	Browser detection method
	
	The primary intented purpose is to make it possible to work around HTML rendering issues
	with IE, where the visible positioning of elements would otherwise be off (and it can't
	easily be fixed with CSS).
	
	Sometimes - particularly when addressing cosmetic issues - the only reasonable approach 
	to browser detection is to ask the client what it is - as that's an instance where trying 
	to determine the browser based on available methods simply makes no sense, hence this script.
	
	It checks if it looks like it's really Opera, to save it from the 'feature' of it earnestly
	pretending to be Internet Explorer (because of course it renders dynamic content quite
	differently).
*/
var browser = new browserCheck();

function browserCheck() {

        var browserIdentString, i;

        this.userAgent = navigator.userAgent;
        this.Opera      = false;
        this.Safari     = false;
        this.IE         = false;
        this.Mozilla    = false;

        // Check for Opera first, as it typically impersonates other browsers,
        // such as MSIE, even though it's not really compatible
        if ((i = this.userAgent.indexOf("Opera")) >= 0) {
                this.Opera = true;
                return;
        }

        if ((i = this.userAgent.indexOf("Safari")) >= 0) {
                this.Safari = true;
                return;
        }

        if ((i = this.userAgent.indexOf("MSIE")) >= 0) {
                this.IE = true;
                return;
        }

        // Check for generic 'Mozilla' browsers last. Not all will work,
        // (there are too many to test) but the majority will be just fine.
        if ((i = this.userAgent.indexOf("Mozilla")) >= 0) {
                this.Mozilla = true;
                return;
        }

        // If we end up here, then the user is out of luck, and we need
        // to let them know the web app almost certainly won't work.
        //
        // We should really give them the option to tell us what browser they 
        // are using. but for now I'm just going to assume it's a re-branded
        // MSIE as that's by far the most likely statistically.
        this.IE = true;
        return false;
}

// Called every time any page is loaded
window.onload = function()
{

	if (browser.IE)	 {
		$("#navigationTrailContainer").corner("8px");
		$("#mainContent").corner("16px");
		$("#sidebar").corner("16px");	
	}
	
	// Attach resize event to the window
	window.onresize = function() {
		Resize();
	};
	
	// Handle clicks anywhere on the page
	document.onclick = function(e) {
		OnClick(e);
	};
	
}

/* Calling it in line forces IE to load TinyMCE properly when "back" button is hit in MSIE,
 * because when redisplaying a previously loaded page MSIE does not invoke onload() again.
 * Specifing a delay allows some browsers (Safari, MSIE) time to finish rendering the page,
 * so that the resizing is done by TinyMCE only once the page has finished rendering.
 */
$(document).ready(function() {
	// setTimeout("LoadTinyMCE()",500);
});


function LoadTinyMCE() {
	tinyMCE.init({
		// General options
		mode : "specific_textareas",
		editor_selector : "simpleRichTextArea",
		theme : "simple",

		plugins : "safari,pagebreak,style,save,paste,preview",
		plugin_preview_width : "500",
		plugin_preview_height : "600",

		theme_advanced_buttons1 : "cut,copy,pastetext,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,|,bullist,numlist,|,outdent,indent,|,formatselect,fontselect,fontsizeselect,forecolor,backcolor,code",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		
		setup: function(ed) {
    
    	// Force Paste-as-Plain-Text
   	 	ed.onPaste.add( function(ed, e, o) {
        	ed.execCommand('mcePasteText', true);
        	return tinymce.dom.Event.cancel(e);
    		});
		}
		
	});		
}

// Called whenever a page is resized
function Resize() {

}

// Handle click events anywhere on the page
function OnClick(e) {

}

function HideElement(elementName) {
	document.getElementById(elementName).style.visibility =  'hidden';
	document.getElementById(elementName).style.display =  'none';
}

function ShowElement(elementName) {
	document.getElementById(elementName).style.display =  'block';
	document.getElementById(elementName).style.visibility =  'visible';
}

function CreateElement(objectId, parentObjectId) {
	parentObject = null;

		var newObject = document.createElement('div');
		newObject.setAttribute('id',objectId);
		
		// If parentObjectId is null, add to page body. 
		if (parentObjectId == null) {
			var body = document.getElementsByTagName("body").item(0);
			body.appendChild(newObject, body.firstChild);
		} else if (document.getElementById(parentObjectId)) {	
			parentObject = document.getElementById(parentObjectId);
			parentObject.appendChild(newObject);
		} else {
			// If parentObjectId is not null, and does not point to a valid element
			return false;
		}
		
		return true;

}

function DeleteElement(objectId, parentObjectId) {
	parentObject = null;
	if (document.getElementById(parentObjectId)) {	
		parentObject = document.getElementById(parentObjectId);
		if (document.getElementById(objectId)) {		
			parentObject.removeChild(document.getElementById(objectId));	
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

function SetClass(objectId, classString) {
	// Usage: classString can be one or more CSS classes (space seperated)
	if (document.getElementById(objectId)) {
		document.getElementById(objectId).className = classString;
		return true;
	} else {
		return false;
	}
}

function AddEvent(objectId,eventName,eventHandler) {
	if (document.getElementById(objectId)) {
		object = document.getElementById(objectId);
		if (browser.IE) {
			// IE uses attachEvent() and common event names like 'mouseover' become 'onmouseover'
			eventName = 'on'+eventName;
			object.attachEvent(eventName, eventHandler);
		} else {
			// Firefox and Safari both use addEventListener()
			object.addEventListener(eventName, eventHandler, true);
		}
		return true;
	} else {
		return false;
	}
}

function RemoveEvent(objectId,eventName,eventHandler) {
	if (document.getElementById(objectId)) {
		object = document.getElementById(objectId);
		if (browser.IE) {
			// IE uses detachEvent()
			object.detachEvent("on"+eventName, eventHandler);
		} else {
			// Firefox and Safari both use removeEventListener()
			object.removeEventListener(eventName, eventHandler, true);
		}
		return true;
	} else {
		return false;
	}
}

/* Cookie handling, based on http://www.quirksmode.org/js/cookies.html
 * 
 * NB: If 'days' is set to 0 then the resulting cookie is a session cookie,
 * if it is a negative value, the cookie is expired (i.e. deleted) immediately.
 */
 
function SetCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function GetCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function DeleteCookie(name) {
	SetCookie(name,"",-1);
}


function ToggleCheckBox(checkboxId) {
	if (document.getElementById(checkboxId).checked == true) {
		document.getElementById(checkboxId).checked = false;
	} else {
		document.getElementById(checkboxId).checked = true;
	}
}
 jQuery.fn.extend({
   check: function() {
     return this.each(function() { this.checked = true; });
   },
   uncheck: function() {
     return this.each(function() { this.checked = false; });
   }
 });


function GetMouseX(event) {
	if (!event)
		event = window.event;
	
	if (event.pageX) {
		return event.pageX; 
	} else if (event.clientX) {
		return event.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft);
	} else {
		return 0;
	}
}

function GetMouseY(event) {
	if (!event)
		event = window.event;
		
	if (event.pageY) {
		return event.pageY;
	} else if (event.clientY) {
		return event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	} else {
		return 0;
	}

}