﻿function showDiv(divId)
{
    document.getElementById(divId).style.visibility = "visible";
}
function hideDiv(divId)
{
    document.getElementById(divId).style.visibility = "hidden";
}
function showModalPopup()
{
    document.getElementById('popupBackground').style.visibility = "visible";
    var popupContents = document.getElementById('popupContents');
    var padding = screen.width - 800;
    //popupContents.style.top = "60px";
    //popupContents.style.left = "" + (padding / 2) + "px";
    popupContents.style.visibility = "visible";
    return true;
}
function hideModalPopup()
{
    var popupContents = document.getElementById('popupContents');
    popupContents.style.visibility = "hidden";
    document.getElementById('popupBackground').style.visibility = "hidden";
    return true;
}

var sightingsTimer;
var scrollDex = 0;
var intervl = 10;

function scrollSightingsBackward(x)
{
    if (scrollDex > x)
    {
        if ((x + scrollDex) > -100)
        {
            intervl = -7;
        }
        if ((x + scrollDex) > -80)
        {
            intervl = -5;
        }
        if ((x + scrollDex) > -60)
        {
            intervl = -3;
        }
        if ((x + scrollDex) > -40)
        {
            intervl = -2;
        }
        else if ((x + scrollDex) > -20)
        {
            intervl = -1;
        }
        
        var actualInterval = Math.max(intervl, x + scrollDex)
        window.scrollBy(actualInterval, 0);
        scrollDex += actualInterval;
        sightingsTimer = setTimeout("scrollSightingsBackward(" + x + ")", 10);
    }
    else
    {
        clearTimeout(sightingsTimer);
        //alert(scrollDex);
        scrollDex = 0;
        intervl = 10;
        actualInterval = 0;
    }
}

function scrollSightings(x)
{
    if (scrollDex < x)
    {
        if ((x - scrollDex) < 100)
        {
            intervl = 7;
        }
        if ((x - scrollDex) < 80)
        {
            intervl = 5;
        }
        if ((x - scrollDex) < 60)
        {
            intervl = 3;
        }
        if ((x - scrollDex) < 40)
        {
            intervl = 2;
        }
        else if ((x - scrollDex) < 20)
        {
            intervl = 1;
        }

        var actualInterval = Math.min(intervl, x - scrollDex)
        window.scrollBy(actualInterval, 0);
        scrollDex += actualInterval;
        sightingsTimer = setTimeout("scrollSightings(" + x + ")", 10);
    }
    else
    {
        clearTimeout(sightingsTimer);
        //alert(scrollDex);
        scrollDex = 0;
        intervl = 10;
        actualInterval = 0;
    }
}

function findPos(obj){
var posX = obj.offsetLeft;var posY = obj.offsetTop;
while(obj.offsetParent){
alert(obj.offsetParent);
if(obj==document.getElementsByTagName('body')[0]){break}
else{
posX=posX+obj.offsetParent.offsetLeft;
posY=posY+obj.offsetParent.offsetTop;
obj=obj.offsetParent;
}
}
var posArray=[posX,posY]
return posArray;
}

function showCreateTaskMiniWindow(currentPanelId, subscriberId, alertId)
{
    var currentDiv = document.getElementById(currentPanelId);

    var newDiv = document.createElement("div");
    newDiv.setAttribute('id', 'createTaskMiniWindow');
    newDiv.setAttribute('class', 'modalBackgroundWhite');
    newDiv.setAttribute('style', 'width:400px;height:400px;position:fixed;z-index:999;top:0px;left:auto;right:auto');
    var innerHtml = '<p style="color:Black;font-weight:bold">What would you like to do?</p><input type="radio" name="createTaskFromSighting" value="cbt">Post a comment/article<br/><input type="radio" name="createTaskFromSighting" value="ct">Something else I\'ll specify below<br/><br />Notes:<br /><textarea rows="3" cols="25" name="notes"></textarea>';
    innerHtml += '<br /><input type="button" value="create task" onclick="alert(this.onclick);ctfs('+alertId+','+subscriberId+',\''+currentPanelId+'\');">&nbsp;<input type="button" value="cancel" onclick="hideCreateTaskMiniWindow()"><br/><br/><br/><br/><br/><br/>';
    newDiv.innerHTML = innerHtml;
    
    currentDiv.parentNode.insertBefore(newDiv, currentDiv);
    return false;
}

function hideCreateTaskMiniWindow()
{
    var taskWindow = document.getElementById('createTaskMiniWindow');
    taskWindow.parentNode.removeChild(taskWindow);
}

function ctfs(sightingId, subscriberId, divId)
{
    var postVars = "sightingId=" + sightingId + "&subscriberId=" + subscriberId + "&command=createTask&divId="+divId;
    //alert(postVars);
    loadXMLDoc("ajax/sightingTaskAgent.aspx", postVars, createTaskResponseHandler);
}

function deleteSighting(divId, subscriberId, sightingId)
{
    var postVars = "sightingId=" + sightingId + "&subscriberId=" + subscriberId + "&command=delete&divId="+divId;
    //alert(postVars);
    loadXMLDoc("ajax/sightingTaskAgent.aspx", postVars, deleteSightingResponseHandler);
}

// AJAX code
var xmlhttp;

function deleteSightingResponseHandler()
{
	if (xmlhttp.readyState == 4)
	{	
		if (xmlhttp.status == 200)
		{	// 200 = OK
		    var divId = xmlhttp.responseText;
            var divElem = document.getElementById(divId);
            scrollSightings(294);
            divElem.parentNode.removeChild(divElem);
		}
		else
		{
		    return false;
			alert("Problem retrieving XML data: " + xmlhttp.status);
		}
	}
}

function createTaskResponseHandler()
{
	if (xmlhttp.readyState == 4)
	{	
		if (xmlhttp.status == 200)
		{	// 200 = OK
		    hideCreateTaskMiniWindow();
		}
		else
		{
		    return false;
			alert("Problem retrieving XML data: " + xmlhttp.status);
		}
	}
}

function loadXMLDoc(url, postVars, responseHandler)
{
	xmlhttp=null;
	if (window.XMLHttpRequest)
	{
	    // code for all new browsers
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
	    // code for IE5 and IE6
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if (xmlhttp != null)
	{
		xmlhttp.onreadystatechange = responseHandler;
		if (postVars != null)
		{
			xmlhttp.open("POST",url,true);
			xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xmlhttp.send(postVars);				
		}
		else
		{
			xmlhttp.open("GET",url,true);
			xmlhttp.send(null);
		}
	}
	else
	{
		alert("Your browser does not support XMLHTTP.");
	}
}

