
/**
 *  Set active button
 */
function setCPDrawAction(id) 
{
	document.getElementById("b"+selectedTool).src = "./static/html_images/bt_"+selectedTool + ".png";
	
	document.getElementById("b"+id).src = "./static/html_images/bt_sel_"+id + ".png";
	selectedTool = id;
}

function onUpdateLevelsTable(response)
{
    if(response[0] == 0) //Means that this is the first time ever the user visits this site.
    {
      //addBorder(startWallXCoords, startWallYCoords, endWallXCoords, endWallYCoords);
      //server.AddLevel(levelpackTitle, phoneID, 1,
      //                holeXCoords, holeYCoords, startWallXCoords, startWallYCoords, endWallXCoords, endWallYCoords, 
      //                startX, startY, goalX, goalY, onNewLevelSuccess);
    
      clearLevel();
      numberOfLevels = 1;
      maxLevelID = response[2];
      document.getElementById("levels").innerHTML = response[1];
    }
    else
    {
      //response is [int, str] where int is the number of levels and str is the html code
      document.getElementById("levels").innerHTML = response[1];
      numberOfLevels = response[0];
      maxLevelID = response[2];
    }
    
    
    //ChangeLevel needs 2 callbacks. First it updates it counter and on second it will reset into finished state.
    if(changingLevel == 1)
    {
      changingLevel++;    
    }
    else if(changingLevel == 2)
    {
      changingLevel = 0;
    }

}

function onMoveLevelSuccess(response)
{
    if(response[0] == 1)
    {
       levelNumber = response[1];
       server.GetLevelHTML(levelpackTitle, phoneID, response[1], onUpdateLevelsTable);    
    }
}

function newLevel()
{
    if(numberOfLevels < 10)
    {
        canvasUpdated = 1;
        uploadLevel(1);
    	
    	clearLevel();
    		
    	newLevelId =  maxLevelID+1;	
        server.AddLevel(levelpackTitle, phoneID, newLevelId,
                        [], [], startWallXCoords, startWallYCoords, endWallXCoords, endWallYCoords, -1, -1, -1, -1, onNewLevelSuccess);

        levelNumber = newLevelId;
    }
}

function deleteLevel(levelID,levelSelected)
{
    if (numberOfLevels > 1)
    {
        server.RemoveLevel(levelpackTitle, phoneID, levelID, levelSelected, onRemoveLevelSuccess);    
    }  
}

function moveLevelUp(levelID)
{
    if (numberOfLevels > 1)
    {
        if(canvasUpdated)
        {
            idToBeMoved = levelID;
            server.AddLevel(levelpackTitle, phoneID, levelID, holeXCoords, holeYCoords, startWallXCoords, startWallYCoords, endWallXCoords, endWallYCoords, startX, startY, goalX, goalY, onMoveUpAfterSave);
        }
        else
        {
            server.MoveLevelUp(levelpackTitle, phoneID, levelID, onMoveLevelSuccess);    
        }
    }  
}

function onMoveUpAfterSave()
{
    server.MoveLevelUp(levelpackTitle, phoneID, idToBeMoved, onMoveLevelSuccess);
}


function onMoveDownAfterSave()
{
    server.MoveLevelDown(levelpackTitle, phoneID, idToBeMoved, onMoveLevelSuccess);    
}

function moveLevelDown(levelID)
{
    if (numberOfLevels > 1)
    {
        if(canvasUpdated)
        {
            idToBeMoved = levelID;
            server.AddLevel(levelpackTitle, phoneID, levelID, holeXCoords, holeYCoords, startWallXCoords, startWallYCoords, endWallXCoords, endWallYCoords, startX, startY, goalX, goalY, onMoveDownAfterSave);
        }
        else
        {
            server.MoveLevelDown(levelpackTitle, phoneID, levelID, onMoveLevelSuccess);    
        }
    }  
}

function addBorder(tmpStartWallXCoords, tmpStartWallYCoords, tmpEndWallXCoords, tmpEndWallYCoords)
{
    tmpStartWallXCoords.push(borderOffsetX + 0);
	tmpStartWallYCoords.push(borderOffsetY + 0);
	tmpEndWallXCoords.push(borderOffsetX + 320);
	tmpEndWallYCoords.push(borderOffsetY + 15);	  
	
	tmpStartWallXCoords.push(borderOffsetX + 0);
	tmpStartWallYCoords.push(borderOffsetY + 0);
	tmpEndWallXCoords.push(borderOffsetX + 15);
	tmpEndWallYCoords.push(borderOffsetY + 480);	  
	
	tmpStartWallXCoords.push(borderOffsetX + 305);
	tmpStartWallYCoords.push(borderOffsetY + 0);
	tmpEndWallXCoords.push(borderOffsetX + 320);
	tmpEndWallYCoords.push(borderOffsetY + 480);	  
	
	tmpStartWallXCoords.push(borderOffsetX + 0);
	tmpStartWallYCoords.push(borderOffsetY + 465);
	tmpEndWallXCoords.push(borderOffsetX + 320);
	tmpEndWallYCoords.push(borderOffsetY + 480);	  
}

function findPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}
  
function findPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

function showClearPopup()
{
    if(showLoginPopup != 1)
    {
        popupObj = document.getElementById("popup");
        popupObj.style.left = String(53 + findPosX(document.getElementById("canvDiv")))+"px"; 
        popupObj.style.top = String(240 + findPosY(document.getElementById("canvDiv")))+"px"; 
        popupObj.style.display ="block"; 
    }
}

function showIDPopup()
{
    showLoginPopup = 1;
    popupObj = document.getElementById("id_popup");
    popupObj.style.left = String(53 + findPosX(document.getElementById("canvDiv")))+"px"; 
    popupObj.style.top = String(240 + findPosY(document.getElementById("canvDiv")))+"px"; 
    devID = readCookie("DeviceID");
    if (devID != null)
      document.getElementById("inputbox").value=devID;  
    popupObj.style.display ="block"; 
}

function showWarningPopup()
{
    popupObj = document.getElementById("warning_popup");
    popupObj.style.left = String(53 + findPosX(document.getElementById("canvDiv")))+"px"; 
    popupObj.style.top = String(240 + findPosY(document.getElementById("canvDiv")))+"px"; 
    popupObj.style.display ="block"; 
}


function showSavePopup()
{
    popupObj = document.getElementById("save_popup");
    popupObj.style.left = String(53 + findPosX(document.getElementById("canvDiv")))+"px"; 
    popupObj.style.top = String(240 + findPosY(document.getElementById("canvDiv")))+"px"; 
    popupObj.style.display ="block"; 
}

function hideSavePopup()
{
    document.getElementById("save_popup").style.display="none";    
}

function hideClearPopup()
{
    document.getElementById("popup").style.display="none"; 
    
}

function PopupID_RemoveText()
{
    if (document.getElementById("inputbox").value == "Enter ID")
      document.getElementById("inputbox").value="";  
}

function createCookie(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 readCookie(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 eraseCookie(name) {
	createCookie(name,"",-1);
}

function hideIDPopup()
{
    showLoginPopup = 0;
    document.getElementById("id_popup").style.display="none"; 
    
}

function hideWarningPopup()
{
    document.getElementById("warning_popup").style.display="none";    
    showIDPopup();
}

function clearLevelSelected()
{
  clearLevel();   
  hideClearPopup(); 
  drawEverything();
}

function GetFireFox()
{
    window.open("http://getfirefox.com");  
}

function popup(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
window.open(href, windowname, 'resizable=no,width=440,height=365');
return false;
}



