
var innerWidth= 320;
var innerHeight= 440;



//the following function will read the text file and will return the response Text as string.
function readTextFile(pPath)
{
    var file = pPath;
    var allText = "nil"
    var rawFile = new XMLHttpRequest();
    rawFile.overrideMimeType("text/plain; charset=x-user-defined");
    rawFile.open("GET", file, true);
    rawFile.onreadystatechange = function ()
    {
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {
                allText = rawFile.responseText;
                console.log("path for text file is ", pPath);
                console.log("allText file is ", allText);
                // alert(allText);
                // var myArray = new Array()
                // myArray = allText.split(',');
                setSoccerPlacementString(allText)
            }
        }
    }
    
    rawFile.send();
    
}

function setScreenScale()
{   
    
    console.log("total width and height of the window ", Crafty.viewport.width, Crafty.viewport.height)
    
   var scale = window.innerWidth / Crafty.viewport.width;
        
        scale = window.innerWidth / Crafty.viewport.width;
            //make sure it's not bigger than the height
        if (scale * Crafty.viewport.height > window.innerHeight) {
            scale = window.innerHeight / Crafty.viewport.height;
            }
        var stageStyle = Crafty.stage.elem.style;
            stageStyle.transformOrigin = stageStyle.webkitTransformOrigin = stageStyle.mozTransformOrigin = "0 0";
            stageStyle.transform = stageStyle.webkitTransform = stageStyle.mozTransform = "scale(" + scale + ")";
             
    
    
    
    // if(Crafty.viewport.height > 400 && Crafty.viewport.width > 320) {
//         console.log("width and height placem", Crafty.viewport.width/2-320/2, Crafty.viewport.height/2-480/2)
//         console.log("x and y is ", Crafty.viewport.x, Crafty.viewport.y)
//         var lScaleX = Crafty.viewport.width/320
//         var lScaleY = Crafty.viewport.height/480
//         var lScale
//         if(lScaleX < lScaleY) { lScale = lScaleX } else { lScale = lScaleY }
// //         if(Crafty.viewport.width > lScale*320) {
//       //   var lScaleX = Crafty.viewport.width/320
//         console.log("scale is ", lScale, lScaleX, lScaleY);
//         Crafty.viewport.scale(lScale)
//         Crafty.viewport.x = -(Crafty.viewport.width/2-320/2)*lScale  //-320/lScale
//         Crafty.viewport.y =  -(Crafty.viewport.height/2-480/2)*lScale  //-480/lScale
//         console.log("x and y is ", Crafty.viewport.x, Crafty.viewport.y)
// //         }
//     }
    
   if(Crafty.viewport.width==320) {
// 
    } else  {
      // Crafty.viewport.scale(1.7)
//       Crafty.viewport.x = -110
//       Crafty.viewport.y = -200
        console.log("zooming");
//         Crafty.viewport.zoom(1.2, Crafty.viewport.width/2, Crafty.viewport.height/2,1)
    }
    
    
    /*
    var scaler = function() {
			var current = { w: Crafty.viewport.width, 
					  h: Crafty.viewport.height };
			
			  // This will reset the scale when it adjusts the canvas element
			  // dimensions, so do it right before setting the scale we want.
			  Crafty.viewport.reload();
			
			  if ( current.w > current.h ) {
			    // Landscape, scale to fit height
			    Crafty.viewport.absoluteScale( current.h / Screen.h );
			  } else {
			    // Portrait, scale to fit width
			    Crafty.viewport.absoluteScale( current.w / Screen.w )
			  }
			};
			
			// Avoid drawing entities before we're ready to scale.
			Crafty.removeEvent( Crafty, window, 'resize', Crafty.viewport.reload );
			
			// Adjust scale to fit inside resized window.
			Crafty.addEvent( Crafty, window, 'resize', scaler );
			
			// Run the scaler after scene changes, since they reset the scale.
			Crafty.bind( 'SceneChange', scaler );*/
    
        
}

//the following function will convert all floating point numbers to integers example 3.99 to 3
function floatToInt(value) {
    // console.log("float to int", value|0)
    return value|0 
}

function deleteImages(pList) {

    console.log("88888888888 deleted images ")
    if(pList) {
        for (var i=0; i< pList.length; i++) {
            if(pList[i]) {
                pList[i].destroy();
                // console.log("Destroyed ", i);
            }
        }
    }
}

function hideImages(pList,pResult) {
  if(pList) {
    for (var i=0; i < pList.length; i++) {
        pList[i].alpha=pResult;
    }
  }
}

// var levelName = Crafty.e("2D, DOM, number"+lLevelId).attr({x:level_hud._x+48, y:puzzle_hud._y+33});

var pTimerText1, pTimerText2

function createNumber(lNumber, pX, pY) {
    // console.log("lnumber is ", lNumber)
    if(pTimerText1 != null || pTimerText2 != null ) {
        pTimerText1.destroy(); pTimerText2.destroy();
    }
    var lNumber1, lNumber2
    lNumber1=lNumber%10; lNumber=Math.floor(lNumber/10)
    lNumber2=lNumber%10;
    // console.log("the two numbers are ", lNumber2, lNumber1)
    pTimerText1 = Crafty.e("2D, DOM, number"+lNumber2).attr({x:pX+23, y:pY+33});
    pTimerText2 = Crafty.e("2D, DOM, number"+lNumber1).attr({x:pX+45, y:pY+33});

}

function setTimerText(pBool) {
    if (pTimerText1 == undefined) {
        //code
        return;
    }
    pTimerText1.visible = pBool;
    pTimerText2.visible = pBool;
}


// for alert
function createAlertScreen(){
    
         
 var hurryUp = Crafty.e("2D, DOM, Image").image('images/hurryup.png');
    hurryUp.x = Crafty.viewport.width/2-260/2; hurryUp.y = Crafty.viewport.height/2-85/2;

 setTimeout(function(){
     
        
    },700);
}

