/*
* FileName: contextmenu.js
* Author: Madan Kumar
* This program is wirtten for Elance project 
* "Simple portable AJAX/JavaScript for a Deals Site"
*
* All rights are reserved to the buyer Siddarth[sproline]
*/

//Initialise global variables. 

var currentTarget = null;

// global variable which holds the context menu

// to cache the windows current user opened
var couponWindows = new Array();

// window.onload = InitContext; 

function InitContext() { 
	
	// Create a hidden textarea for the simulating clipboard behaviour
	var textarea = document.createElement("textarea");
	textarea.id = "holdtext";
	textarea.style.display = "none";
	document.body.appendChild(textarea);

	function addBodyMouseOverEvent(aEventFunction){
		var oldonmouseover = document.body.onmouseover;
 
		if (typeof document.body.onmouseover != 'function'){
			document.body.onmouseover = aEventFunction;
		} else {
			document.body.onmouseover = function(){
				oldonmouseover();
				aEventFunction(); 	
			}
		}
	}
	addBodyMouseOverEvent(ContextMouseOver);
}


// call from the onMouseOver event, passing the event if standards compliant 
function ContextMouseOver(event) { 
	//if (_noContext || _mouseOverContext) return;

	// IE is evil and doesn't pass the event object 
	if (event == null) event = window.event;

	// we assume we have a standards compliant browser, but check if we have IE 
	var target = event.target != null ? event.target : event.srcElement;

	// only show the context menu if the mouse is hovered on HTML elements with
	// with className either "coupon_number_clicked" or "coupon_number_unclicked"
	// and a hyperlink has been clicked (the code can be made more selective) 

	if ((target.className.toLowerCase() == "elanceprj_coupon_number_clicked" || 
		target.className.toLowerCase() == "elanceprj_coupon_number_unclicked" )) 
		currentTarget = target; 
	
}


// Copy the coupon id to clipboard and activate site for the coupon and
// cache the window details in a array object.
function copyCodeAndOpen(siteURL){ 
	// get the currentTarget global variable
	var target = currentTarget;

	// get the id of the target for caching the window
	var targetId = target.id

	// check if the targetId is null or not if null set the default 
	// targetId to 'popupwindow'
	if (targetId == null){
		targetId = 'popupwindow';
	}
	
	if (!checkTargetClickedPreviously(target)){

		// open the site in a new window and change the style
		win = window.open(siteURL, targetId);	

		if(!win){
			alert("Popup blocker is enabled. Please disable popup blocker for this site on your browser.");
			return false;
		}

		target.innerHTML = "";
		var couponLabel = document.createElement("div");
		couponLabel.className = "elanceprj_coupon_label";
		couponLabel.innerHTML = "Coupon Code:";

		var couponNumber = document.createElement("div");
		couponNumber.className = "elanceprj_coupon_number";
		couponNumber.innerHTML = targetId;

		target.appendChild(couponLabel);
		target.appendChild(couponNumber);

		target.className = "coupon_number_clicked";

		// change the label on the context menu
		//$('aContextNav').innerHTML = 'Coupon copied';

		// cache the event in the global couponWindows array object
		recordClickEvent(targetId, siteURL, win);
	}
	// Copy the couponId to clipboard
	holdtext.innerText = targetId;
	Copied = holdtext.createTextRange();
	Copied.execCommand("RemoveFormat");
	Copied.execCommand("Copy");

	document.focus();
}

function recordClickEvent(targetId, siteURL, aWindow){
	// check if global variable couponWindows is initialised if not 
	// initialise with new Array object.
	if (couponWindows == null){
		couponWindows = new Array();
	}
	var existingWindow = getWindow(targetId);
	if (existingWindow == null)	{
		// create a new entry in the array with window details
		couponWindows[couponWindows.length] = {"window" : aWindow,
											"couponId" : targetId,
											"siteURL" : siteURL,
											"couponClicked" : true,
											"couponCopied" : true }
	}else{
		// update existing window details
		existingWindow.window = aWindow;
		existingWindow.couponId = targetId;
		existingWindow.siteURL = siteURL;
		existingWindow.couponClicked = true;
		existingWindow.couponCopied = true;
	}

}
/* called from checkTargetClickedPreviously and recordClickEvent
 * to check/retrieve whether any popup window 
 * already opened for the same details
 * return true if already opened previously.
 */
function getWindow(targetId){
	if (couponWindows != null){
		for (i=0; i < couponWindows.length ; i++){
			// check for the window details exist previously with same id
			if (couponWindows[i].couponId == targetId)
			{
				return couponWindows[i];
			}
		}
	}
}

/* called from ContextShow to check whether any popup window 
 * already opened for the same details
 * return true if already opened previously or else return false
 */
function checkTargetClickedPreviously(target){
	if (couponWindows != null){
		var win = getWindow(target.id)
		if (win != null){
			// check whether couponClicked and the window is active currently
			return win.couponClicked && !win.window.closed
		}else{
			return false;
		}
	}
}


// this is simply easier on the eyes and fingers 
function $(id) { 
	return document.getElementById(id); 
}


