﻿/*
* Copyright 2010, Nicholas Chua
* Author's Website: http://www.whooshpro.com
* Copyright Remark: Free to use as long as copyright is left intact
*
* Date: 11 Feb 2011
*/

//-----------------
//Global Variables
//-----------------

var wp_prt_msg_html =
	'<div class="wp_prt_msg">' +
	'<b>Image Copyright Statement</b>' +
	'<br />' +
	'All users of this website are reminded that any redistribution or copying of copyrighted material requires the permission of Keppel Corporation. Please email <a href="mailto:keppelgroup@kepcorp.com?Subject=Permission request for copyrighted material on Keppel Corporation Website">keppelgroup@kepcorp.com</a> to obtain permission prior to any use. Thank you.' +
	'</div>';
var wp_prt_ov_html = '<img class="wp_prt_ov" />';
var wp_prt_ov_src = 'utility/protect/protect.gif';

var wp_prt_msg = null;
var wp_prt_ov = null;

//-----------------
//Global Functions
//-----------------

//Show Protect Msg at current mouse page position
function wp_prt_msg_show(e) {
	wp_prt_msg.css({ 'top': e.pageY + 'px', 'left': e.pageX + 'px' }).show();
	return false; //cancel default contextmenu
}

//Transparent Overlay Protect
function wp_prt_mouseover(e) {
	var t = e.target;
	$(t).parent().append(wp_prt_ov)
	//margin offset
	var mT = parseInt($(t).css('margin-top')); mT = isNaN(mT) ? 0 : mT;
	var mR = parseInt($(t).css('margin-right')); mR = isNaN(mR) ? 0 : mR;
	var mB = parseInt($(t).css('margin-bottom')); mB = isNaN(mB) ? 0 : mB;
	var mL = parseInt($(t).css('margin-left')); mL = isNaN(mL) ? 0 : mL;
	//alert(mT + ' ' + mR + ' ' + mB + ' ' + mL)
	//border offset
	var bT = parseInt($(t).css('border-top-width')); bT = isNaN(bT) ? 0 : bT;
	var bR = parseInt($(t).css('border-right-width')); bR = isNaN(bR) ? 0 : bR;
	var bB = parseInt($(t).css('border-bottom-width')); bB = isNaN(bB) ? 0 : bB;
	var bL = parseInt($(t).css('border-left-width')); bL = isNaN(bL) ? 0 : bL;
	//alert(bT + ' ' + bR + ' ' + bB + ' ' + bL)
	//padding offset
	var pT = parseInt($(t).css('padding-top')); pT = isNaN(pT) ? 0 : pT;
	var pR = parseInt($(t).css('padding-right')); pR = isNaN(pR) ? 0 : pR;
	var pB = parseInt($(t).css('padding-bottom')); pB = isNaN(pB) ? 0 : pB;
	var pL = parseInt($(t).css('padding-left')); pL = isNaN(pL) ? 0 : pL;
	//alert(pT + ' ' + pR + ' ' + pB + ' ' + pL)
	//overlay url
	wp_prt_ov.attr('src', wp_prt_ov_src);
	//overlay sizing + offset
	wp_prt_ov.width($(t).width() + mR + mL + bR + bL + pR + pL);
	wp_prt_ov.height($(t).height() + mT + mB + bT + bB + pT + pB);
	//overlay positioning
	wp_prt_ov.css({ 'top': $(t).position().top + 'px' });
	wp_prt_ov.css({ 'left': $(t).position().left + 'px' });
	//overlay show; activating overlay
	wp_prt_ov.show();
}

//-----------------
//Document Ready
//-----------------

$(document).ready(function() {

	//protection message init
	wp_prt_msg = $(wp_prt_msg_html);
	$('body').append(wp_prt_msg);

	//protection overlay init
	wp_prt_ov = $(wp_prt_ov_html);
	$('body').append(wp_prt_ov);

	//protection message hiding
	$(document).click(function() {
		wp_prt_msg.hide();
	});

	//mouse over document elements action
	$(document).mouseover(function(e) {
		var t = e.target;
		//protect image or element with .wp_prt, and not protection overlay .wp_prt_ov, and more exclusions...
		if ((t.nodeName == 'IMG' || $(t).is('.wp_prt')) &&
					!$(t).is('.wp_prt_ov') && //protection overlay
					//with class of
					!$(t).is('[class~="ui-datepicker-trigger"]') && //date picker icon
					!$(t).parent().is('[class~="arrow_links"]') && //home arrow
					//src containing
					!$(t).is('[src*="icon_"]') && //general icons
					//src ending with
					!$(t).is('[src$="printer_logo.gif"]') && //printer icon
					!$(t).is('[src$="arrowDown.gif"]') && //double arrow down
					!$(t).is('[src$="bullet_right.gif"]') && //breadcrumb arrow
					!$(t).is('[src$="button_red.gif"]')//home banner rotator button
				) {
			//add more exclusions conditions above if necessary 
			//e.g. display components images that may conflict and not want to protect:
			//	- calendar date picker icon

			var p = $(t).parent().get(0);
			//if element is not link anchored
			if (p.nodeName == 'A' && p.href != null) {
				$(t).unbind("contextmenu"); $(t).bind("contextmenu", wp_prt_msg_show);
			}
			else {
				wp_prt_mouseover(e); //overlay
				wp_prt_ov.unbind("contextmenu"); wp_prt_ov.bind("contextmenu", wp_prt_msg_show);
			}
		}
	});

});



