// File createrd for achieveing drag drop across disparate technologies using Javascript mouse tooltips - Saurabh Vig

var dragapproved=false
var mousedown=false
var imageDrag=false
var briefCaseList="";
var dragInfo="";
var dragName="";
var tooltipBoxWidth=200;
var tooltipBoxHeight=10;
var dragImageHeight=32;
var dragImageWidth=32;
var z,x,y

var DDMemberID=0;
var DDShowID=0;

function move(e){

if (mousedown)
{
dragapproved=true;
//TagToTip('draggingdiv');
if(imageDrag)
{
	z.style.pixelLeft=temp1+event.clientX-x
	z.style.pixelTop=temp2+event.clientY-y
}
return false;
}
}

function drags_org(e){

// IE is retarded and doesn't pass the event object 
if (e == null) 
	e = window.event; 
// IE uses srcElement, others use target 
var target = e.target != null ? e.target : e.srcElement;
if (target.className=="drag"){
	//alert("3");//mousedown=true
//z=event.srcElement
//imageDrag=true
//temp1=z.style.pixelLeft
//temp2=z.style.pixelTop
//x=event.clientX
//y=event.clientY
drags(target.name);

document.body.focus(); 
// prevent text selection in IE 
document.onselectstart = function () { return false; }; 
// prevent IE from trying to drag an image 
target.ondragstart = function() { return false; };
return false;
}
}

function drags(name){
{

mousedown=true
dragInfo=name
var nameArr = name.split("---");
dragName=nameArr[1];
//var tooltip = "Dragging...  " + nameArr[1];
var tooltip = nameArr[1];



//alert(window.event)
	

var image = "";
if (nameArr[0]=="PDF")
{
	image="images/iconPDF.jpg"
}
if (nameArr[0]=="WORD")
{
	image="images/iconWORD.jpg"
}
if (nameArr[0]=="PPT")
{
	image="images/iconPPT.jpg"
}
if (nameArr[0]=="Video")
{
	image="images/iconVIDEO.jpg"
}

//x=event.clientX
//y=event.clientY
ShowDraggingDiv(image, 0, 0, tooltip);
//Tip(tooltip)
TagToTip('draggingdiv');
//alert("test");
//document.body.style.cursor="im_orghv.cur";

//z=document.getElementById('draggingdiv');
//imageDrag=true
//temp1=z.style.pixelLeft
//temp2=z.style.pixelTop
//return false:

}

}
function drops(e)
{
//alert ("Mouse Up");
mousedown=false;
if (dragapproved==true)
{
	dragapproved=false
	var MyBriefcase = document.getElementById('briefcase')
	var divLeft = parseInt((MyBriefcase.style.left.split("px"))[0]);
	var divWidth = parseInt((MyBriefcase.style.width.split("px"))[0]);
	var divTop = parseInt((MyBriefcase.style.top.split("px"))[0]);
	var divHeight = parseInt((MyBriefcase.style.height.split("px"))[0]);
	var divRight = divLeft + divWidth
	var divBottom = divTop + divHeight
		//alert(document.getElementById('draggingdiv').style.height);

	/*
	var	tipLeft = event.clientX;
	var tipTop = event.clientY;
	var tipRight = tipLeft + tooltipBoxWidth;
	var tipBottom = tipTop + tooltipBoxHeight;
	tt_x, tt_y, tt_w, tt_h
	*/
	//alert(tipLeft +"  " + tipTop+"  " +  tipRight +"  " + tipBottom);
	//alert(tt_x +"  " + tt_y+"  " +  tt_w +"  " + tt_h);

	var	tipLeft = tt_x;
	var tipTop = tt_y;
	var tipRight = tipLeft + tt_w;
	var tipBottom = tipTop + tt_h;
	//alert(tt_x +"  " + tt_y+"  " +  tt_w +"  " + tt_h);

	//if(((divLeft <= event.clientX)&&(event.clientX <= divRight))&& ((divTop <= event.clientY)&&( event.clientY<= divBottom)))
	if(((((tipTop<divTop)&&(tipBottom>divTop))||((tipTop>divTop)&&(tipTop<divBottom)))&&(((tipLeft<divLeft)&&(tipRight>divLeft))||((tipLeft>divLeft)&&(tipLeft<divRight)))))
	{
		if(briefCaseList=="")
		{
			briefCaseList = dragInfo;
			alert(dragName + " added to briefcase");
			if(imageDrag)
			{
				//z.style.pixelLeft=temp1
				//z.style.pixelTop=temp2
				hiddenFloatingDiv('draggingdiv');
				imageDrag=false
			}
		}
		else
		{
			var BriefCaseArr = briefCaseList.split(";")
			var addBrochure = true
			for (var i=0; i < BriefCaseArr.length; i++) 
			{
				if (BriefCaseArr[i]==dragInfo)
				{
					addBrochure = false
				}
				
			}
			if(addBrochure)
			{
				briefCaseList = briefCaseList +";"+ dragInfo;	
				alert(dragName + " added to briefcase");
				if(imageDrag)
				{
					//z.style.pixelLeft=temp1
					//z.style.pixelTop=temp2
					hiddenFloatingDiv('draggingdiv');
					imageDrag=false
				}
			}
			else
			{
				if(imageDrag)
				{
					//z.style.pixelLeft=temp1
					//z.style.pixelTop=temp2
					hiddenFloatingDiv('draggingdiv');
					imageDrag=false
				}
				alert(dragName + " is already present in your briefcase")
			}
		}
		 
	}
	else
	{
		if(imageDrag)
		{
			//z.style.pixelLeft=temp1
			//z.style.pixelTop=temp2
			hiddenFloatingDiv('draggingdiv');
			imageDrag=false
		}
		alert("Cannot Drop here. Drag again to the briefcase.")
	}

	
	
}
dragInfo="";
dragName="";
//UnTip();
document.body.style.cursor="";

}

function addtoBriefCase(fileName)
{
	//alert("Adding");
		if(briefCaseList=="")
		{
			briefCaseList = fileName;
			//alert(fileName + " added to briefcase");
			//Added by Ankur to add Briefcase Item in DB
			addBriefcaseItem(fileName);
		}
		else
		{
			var BriefCaseArr = briefCaseList.split(";")
			var addBrochure = true
			for (var i=0; i < BriefCaseArr.length; i++) 
			{
				if (BriefCaseArr[i]==fileName)
				{
					addBrochure = false
				}
				
			}
			if(addBrochure)
			{
				briefCaseList = briefCaseList +";"+ fileName;	
				//alert(fileName + " added to briefcase");
				//Added by Ankur to add Briefcase Item in DB
				addBriefcaseItem(fileName);
			}
			else
			{
				//alert(fileName + " is already present in your briefcase")
			}
		}
		
}

//Added by Ankur to add Briefcase Item in DB
function addBriefcaseItem(fileName)
{
		var ItemArr = fileName.split("---");
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
			alert ("Your browser does not support AJAX!");
			return false;
		}
		//var url="../eShow/eshow-project/apps/eShowApp/modules/eShowModule/templates/briefcaselogSuccess.php";
		var url="briefcaselog";

		//Added on 13 April
		//url=url+"?MemberID=31&ContentID="+ItemArr[2]+"&Operation=added&ShowID=1";
		url=url+"?MemberID="+MemberIDHTML+"&ContentID="+ItemArr[2]+"&Operation=added&ShowID="+ShowIDHTML;
		//alert(url);
		xmlHttp.onreadystatechange=stateChangedUser;
		xmlHttp.open("GET",url,true);
		if (xmlHttp.overrideMimeType) {
		
					xmlHttp.overrideMimeType('text/xml');
			}
		xmlHttp.send(null);

}

function stateChangedUser() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		//alert("Content has been added in DB. ID: "+xmlHttp.responseText);
		return true;
	}
}

function getBriefCaseData()
{
	alert ("Briefcase contains... " + briefCaseList)
}

function ShowbriefCase()
{

	//var ItemArr = fileName.split("---");
		//alert(document.getElementById("GlobalMemberID").value);
		//alert(document.getElementById("GlobalShowID").value);
	if(LoginPage == 1 && MemberIDHTML == 0)
	{
		alert("Please Login to access the your Briefcase.");
	}
	else
	{

		if(document.body.style.cursor == 'default')
			document.body.style.cursor = 'wait';
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
			alert ("Your browser does not support AJAX!");
			return false;
		}
		var url="listbriefcase";

		//Added on 13 April
		//url=url+"?MemberID=31&ShowID=1";
		url=url+"?MemberID="+MemberIDHTML+"&ShowID="+ShowIDHTML;
		//alert(url);
		xmlHttp.onreadystatechange=stateChangedList;
		xmlHttp.open("GET",url,true);
		if (xmlHttp.overrideMimeType) {
		
					xmlHttp.overrideMimeType('text/xml');
			}
		xmlHttp.send(null);
	}
		
}

function stateChangedList() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		//alert(xmlHttp.responseText);
		
		//Added on 20th April
		CreateBriefcaseDiv();

		var BriefcaseItemListDB = xmlHttp.responseText.split("<");
		briefCaseList = BriefcaseItemListDB[0];
		var strToWrite="";
		//strToWrite="<table><tr><th width=120 align='left'>File Name</th><th width=120 align='left'>File Type</th><th align='left'>Content ID</th></tr>";
		var BriefCaseArr = briefCaseList.split(";");
		var addBrochure = true;
		strToWrite+="<tr><td>";
		for (var i=0; i < BriefCaseArr.length-2; i++) 
		{
			var briefcaseItemArr = BriefCaseArr[i].split("---")
	/*		strToWrite+="<tr>";
			strToWrite+="<td>";
			strToWrite+=briefcaseItemArr[1];
			strToWrite+="</td>";
			strToWrite+="<td>";
			strToWrite+=briefcaseItemArr[0];
			strToWrite+="</td>";
			strToWrite+="<td>";
			strToWrite+=briefcaseItemArr[2];
			strToWrite+="</td>";
			strToWrite+="</tr>";*/
			strToWrite+='<div style="display:block;visibility:visible;" id="div'+briefcaseItemArr[2]+'"><table width="100%" border="1px" cellpadding="0" cellspacing="0"><tr>'+'<td style="background-color: #B0C4DE;"><table width="100%" border="1px" cellpadding="0" cellspacing="0" bordercolor="#b1b5c0">'+
			  '<tr>'+ '<td width="4%"><input type="checkbox" name="checkboxItem'+i+'" id="checkboxItem'+i+'" value="'+briefcaseItemArr[2]+'"/></td>'+ '<td width="23%" style="color:#0b1249; ">&nbsp;'+briefcaseItemArr[3]+'</td>'+
				 '   <td width="56%" style="color:#000000; ">&nbsp;'+briefcaseItemArr[0]+'</td>'+ ' <td width="17%" style="color:#000000; "><div align="center">'+briefcaseItemArr[1]+'</div></td>'+
				  '</tr>'+ '</table></td>'+'</tr>'+'<tr>'+' <td style="height:5px;"></td>'+'</tr></table></div>';
			
		}
		strToWrite+="</td></tr>";
		if(document.getElementById('dimmer')==null)
			buildDimmerDiv();
		document.getElementById('dimmer').style.visibility="visible";
		
		document.getElementById('briefcasediv').style.width = 650;
		document.getElementById('briefcasediv').style.height = 450;
		document.getElementById('briefcasediv').style.left = 30;
		document.getElementById('briefcasediv').style.top = 100;

		/*addHeader = '<table style="width:' + "700" + 'px" class="floatingHeader">' +
					'<tr><td ondblclick="void(0);" onmouseover="over=true;" onmouseout="over=false;" style="cursor:move;height:18px">' + "Your Briefcase contains..." + '</td>' + 
					'<td style="width:18px" align="right"><a href="javascript:hiddenFloatingDiv(\'' + "briefcasediv" + '\');void(0);">' + 
					'<img alt="Close..." title="Close..." src="images/close.jpg" border="0"></a></td></tr></table>';*/
		
		var ItemCountArr = BriefCaseArr[BriefCaseArr.length-2].split("---");
		//var ItemCountArr = new Array(0, 0, 0, 0, 0);

		addHeader = '<table width="650" border="0" align="center" cellpadding="0" cellspacing="0" class="floatingHeader">'+
					'  <tr>'+
					'    <td style=" background-image: url(/eShow/eshow-project/web/images/briefcase_05.jpg); background-repeat: repeat-x; height: 29px; border-bottom: 1px solid #848aa2;"><table width="100%" border="0" cellpadding="0" cellspacing="0">'+
					'      <tr>'+
			'<td width="5%"><div align="center"><img src="/eShow/eshow-project/web/images/briefcase_03.jpg" alt="icon" width="22" height="29" /></div></td>'+
			'<td width="86%" style=" font-size: 15px; font-weight: bold; color:#0b1249; ">Your Briefcase</td>'+
			'<td width="1%"><img src="/eShow/eshow-project/web/images/briefcase_08.jpg" alt="closeicon" width="9" height="9" /></td>'+
			'<td width="8%"><a href="javascript:hiddenFloatingDiv(\'' + "briefcasediv" + '\');void(0);">Close</a></td>'+
		  '</tr>'+
		'</table></td>'+
	  '</tr>'+
	  '<tr>'+
		'<td><table width="100%" border="0" cellpadding="0" bgcolor="#f2f2fa">'+
		  '<tr>'+
		   ' <td style="color:#000000; ">Your Briefcase Contains</td>'+
		  '</tr>'+
		  '<tr>'+
		   ' <td><table width="100%" border="0" cellpadding="0">'+
			'  <tr>'+
			 '   <td width="18%" style="color:#000000; ">&nbsp;'+ItemCountArr[3]+' Videos</td>'+
			 '   <td width="18%" style="color:#000000; ">'+ItemCountArr[0]+' Documents</td>'+
			 '   <td width="18%" style="color:#000000; ">'+ItemCountArr[1]+' Images</td>'+
			  '  <td width="38%" style="color:#000000; ">'+ItemCountArr[4]+' Web Pages</td>'+
			  '</tr>'+
			'</table></td>'+
		  '</tr>'+
		  '<tr>'+
		   ' <td><table width="100%" border="0" cellpadding="0">'+
			'  <tr>'+
			 '   <td width="8%">&nbsp;</td>'+
			  '  <td width="74%">&nbsp;</td>'+
			   ' <td width="18%"><img src="/eShow/eshow-project/web/images/briefcase_13.jpg" alt="Delete" width="104" height="27" style="cursor:pointer" onClick="deletefromBriefcase();"/></td>'+
			  '</tr>'+
			'</table></td>'+
		  '</tr>'+
		  '<tr>'+
		   ' <td><table width="100%" border="0" cellpadding="0">'+
			strToWrite +
			'</table></td>'+
		  '</tr>'+
		'</table></td>'+
	  '</tr>'+
	'</table>';


		//document.getElementById('briefcasediv').innerHTML= addHeader + strToWrite;
		document.getElementById('briefcasediv').innerHTML= addHeader;

		document.getElementById('briefcasediv').className = 'dimming';
		document.getElementById('briefcasediv').style.background="#f2f2fa";
		document.getElementById('briefcasediv').style.visibility="visible";

		if(document.body.style.cursor = 'wait')
		  document.body.style.cursor = 'default';
		
		return true;
	}
}

function deletefromBriefcase()
{
	document.body.style.cursor = 'wait';
    for(var i = 0 ;i<100;i++)
	{
	    var elements = document.getElementById('checkboxItem'+i);
		if(elements!=null){
			if(elements.checked)
			{
				//document.getElementById("div"+elements.value).style.visibility = "hidden";
				xmlHttp1=GetXmlHttpObject();
				if (xmlHttp1==null)
				{
					alert ("Your browser does not support AJAX!");
					return false;
				}
				var url="briefcaselog";

				//Added on 13 April
				//url=url+"?MemberID=31&ShowID=1&ContentID="+elements.value+"&Operation=deleted";
				url=url+"?MemberID="+MemberIDHTML+"&ShowID="+ShowIDHTML+"&ContentID="+elements.value+"&Operation=deleted";
				//alert(url);
				xmlHttp1.onreadystatechange=stateChangedDelete;
				xmlHttp1.open("GET",url,true);
				if (xmlHttp1.overrideMimeType) {
				
							xmlHttp1.overrideMimeType('text/xml');
					}
				xmlHttp1.send(null);
			}
		}
    }
}

function stateChangedDelete() 
{ 
	if (xmlHttp1.readyState==4)
	{ 
		//alert("Content has been deleted");
		ShowbriefCase();
		return true;
	}
}

function ShowDraggingDiv(image, left, top, tiptext)
{
	var strToWrite="";
	strToWrite="<img src='"+image+"' class='drag' height='"+dragImageHeight+"' width='"+dragImageWidth+"'><br>";
	strToWrite= strToWrite+ tiptext;
	
	document.getElementById('draggingdiv').style.left = left;
    document.getElementById('draggingdiv').style.top = top;
	document.getElementById('draggingdiv').style.height= tooltipBoxHeight;
	document.getElementById('draggingdiv').style.width= tooltipBoxWidth;

	document.getElementById('draggingdiv').innerHTML= strToWrite;

	//document.getElementById('briefcasediv').className = 'dimming';
	//document.getElementById('draggingdiv').style.visibility="visible";
}


function hiddenFloatingDiv(divId) 
{
	document.getElementById(divId).innerHTML = "";
	document.getElementById(divId).style.visibility='hidden';
	if(document.getElementById('dimmer')==null)
		buildDimmerDiv();
	document.getElementById('dimmer').style.visibility = 'hidden';
	if(divId == "briefcasediv")
	{
		var olddiv = document.getElementById(divId);
		document.body.removeChild(olddiv);
	}
}

function buildDimmerDiv()
{
    //document.write('<div id="dimmer" class="dimmer" style="width:'+ window.screen.width + 'px; height:' + window.screen.height +'px;position:absolute"></div>');
	var dv = document.createElement('div'); // create dynamically div tag
	dv.setAttribute('id',"dimmer");       //give id to it
	dv.setAttribute('class',"dimmer");       
	var IE=document.all;
	var iXPos;
	var iYPos;
	if (!IE)
	{
		iXPos = (self.innerWidth / 2) - 115;
		iYPos = (self.innerHeight / 2) - 100;
	}
	else
	{
		iXPos = ((document.body.clientWidth) / 2) - 115;
		iYPos = ((document.body.clientHeight) / 2) - 100;
	}
	dv.style.position="absolute";
	dv.style.width=window.screen.width+"px";
	dv.style.height=window.screen.height+"px";
	document.body.appendChild(dv);
}

function openNewWindow(url)
{
	window.open(url);
}


document.onmousedown=drags_org;
document.onmousemove=move;
document.onmouseup=drops;


//document.onmousemove=move
//buildDimmerDiv();

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e)
		{
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	  }
	return xmlHttp;
}

function dropinBriefcase()
{

}

function HideDate()
{
	if(document.getElementById('date')!=null)
	{
		document.getElementById('date').style.visiblity = "hidden";
		document.getElementById('date').style.display = "none";

	}
}

function CreateBriefcaseDiv()
{
	if(document.getElementById('briefcasediv')==null)
	{
		var dv = document.createElement('div'); // create dynamically div tag
		dv.setAttribute('id',"briefcasediv");       //give id to it
		dv.style.position="absolute";
		dv.style.width="650px";
		dv.style.height="480px";
		dv.style.backgroundColor="#545454";
		dv.style.visibility = "hidden";
		dv.style.border = "1px solid #848aa2";
		dv.style.overflow = "auto";
		document.body.appendChild(dv);
	}
}