/*
Copyright (c) 2006,
WebWM Sourceforge Developers
Jorge Eduardo Torres <torrmal at users.sourceforge.net>
Jorge Eduardo Cardona <jorgeecardona at users.sourceforge.net>
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the WebWM project nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

////////////////////////////////////////////////////////////////
////////////////		MovyJS V.1.1.date
////////////////	MovyJS is part of the WebWM framework
////////////////---USE:
////////////////	Include this .js on your html.
////////////////	if your want something to move:
////////////////	Add any id and set GlobalObjectID variable to that id on onmousedown event
////////////////---Example:
////////////////	<div id='mine' onmousedown='GlobalObjectID="mine";'>something</div>
////////////////
////////////////	That's it!!  WebWM simple, very simple.......
////////////////
////////////////---Warning:
//////////////// Works better with XHTML
//////////////// <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
/////////////////////////////////////////////////////////////////
/*
/////////////////////////////////////
Change-Log
--------------------
Version 1.1.20070505
2007-05-05	Everithing changed to OO model
		by: Jorge Torres.
--------------------
/////////////////////////////////////

/////////////////////////////////////
Version 1.1 GOALS
-----------------
-Obkect Oriented			OK!
-Auto ProtectionGrid Load,innerHTML	OK!
-Object add content method using AJAX	OK!
-Object Frame
-Object Resize				OK!
*/



/////////////////////////////////////////////////////////////////
//Platform detection
var IE = document.all?true:false;
var NS = !IE;
var AjaxObject=GetXmlHttpObject();
if(!AjaxObject) window.alert('Ajax not supported by your browser');

function GetXmlHttpObject()
{
	var xmlHttp=null;
	if(NS){
		return xmlHttp=new XMLHttpRequest();
	}
	else{
		if(xmlHttp=new ActiveXObject("Msxml2.XMLHTTP")) return xmlHttp;
		return xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}

}
/////////////////////////////////////////////////////////////////

if (window.innerWidth == undefined)
	window.innerWidth=document.body.offsetWidth;
if (window.innerHeight == undefined)
	window.innerHeight=document.body.offsetHeight ;







////////////////////////////////////////////////////////////////
//
var LastZIndex=1;	//ZINDEX Global counter

////////////////////////////
// TObject is the TObject class constructor
// A = new TObject(PARAM1)
// PARAM1 -> WEBElement ID
function TObject(){
	if(arguments.length == 0) return false;
	this.ID=arguments[0];
	this.Static=false; //Is this a not-moveable object?					TODO

	this.Box=document.getElementById(this.ID);

	this.DeltaX=0;
	this.DeltaY=0;
	if(!this.Box) return false;
	this.Index=this.Box.style.zIndex;

}

TObject.prototype.SetBox=function (ID){
	this.ID=ID;
	this.Box=document.getElementById(this.ID);
	if(!this.Box) return false;
}
TObject.prototype.GetFromStyle=function (prop){
	if(!this.Box) return false;
	if (this.Box.currentStyle)
		var Resp = this.Box.currentStyle[prop];
	else if (window.getComputedStyle)
		var Resp = document.defaultView.getComputedStyle(this.Box,null).getPropertyValue(prop);
	if(Resp=='auto') Resp=0;
	return Resp;
}
////////////////////////////
// X method
// A.X(PARAM1)
// PARAM1 -> null,then return style.left(x position)
// PARAM1 -> n, THEN style.left(x position) = n
TObject.prototype.X= function(){
	if(!this.Box) return false;
	if(arguments.length >= 1){
		if(arguments[0]){//window.alert(arguments[0]);
		this.Box.style.position="absolute";
		this.Box.style.left=arguments[0]+'px';
		this.Box.style.zIndex=this.Index;}
	}
	return parseInt(this.GetFromStyle("left"));//this.Box.style.left);
}
////////////////////////////
// Y method
// A.Y(PARAM1)
// PARAM1 -> null,then return style.top(Y position)
// PARAM1 -> n, THEN style.top(Y position) = n
TObject.prototype.Y= function(){
	if(!this.Box) return false;
	if(arguments.length >= 1){
		if(arguments[0]){
		this.Box.style.position="absolute";
		this.Box.style.top=arguments[0]+'px';
		this.Box.style.zIndex=this.Index;
		}
	}
	return parseInt(this.GetFromStyle("top"));//this.Box.style.top);
}
////////////////////////////
// Show method
// MAKE A.ID Visible
TObject.prototype.Show= function(){
	if(!this.Box) return false;
	this.Box.style.visibility="visible";
	this.Box.style.zIndex=this.Index;
}
////////////////////////////
// Hide method
// MAKE A.ID hidden
TObject.prototype.Hide= function(){
	if(!this.Box) return false;
	this.Box.style.visibility="hidden";
}
////////////////////////////
// SetXY method
// change A.ID position to X,Y
TObject.prototype.SetXY=function (X,Y){
	if(!this.Box) return false;
	this.X(X);
	this.Y(Y);
}
////////////////////////////
// SetXY method
// Move A.ID position to X,Y based on Deltas
TObject.prototype.Move=function (DeltaX,DeltaY){
	if(!this.Box) return false;
	var X=(AX=this.X())?parseInt(DeltaX)+AX:parseInt(DeltaX);
	var Y=(AY=this.Y())?parseInt(DeltaY)+AY:parseInt(DeltaY);
	this.SetXY(X,Y);
}

TObject.prototype.Width=function(){
	if(!this.Box) return false;
	if(arguments.length >= 1)
		return this.Box.style.width=arguments[0];
	//if(this.Box.style.width) return this.Box.style.width;
	//window.alert(this.Box.width);
	return this.GetFromStyle("width");//this.Box.width;
}

TObject.prototype.Height=function(){
	if(!this.Box) return false;
	if(arguments.length >= 1)
		return this.Box.style.height=arguments[0];
	//if(this.Box.style.height) return this.Box.style.height;
	return this.GetFromStyle("height");//this.Box.height;

}

TObject.prototype.SetHW=function(H,W){
	if(!this.Box) return false;
	this.Height(parseInt(H)+'px');
	this.Width(parseInt(W)+'px');
}

TObject.prototype.Maximize=function(){
	if(!this.Box) return false;
	if (window.innerWidth == undefined)
		window.innerWidth=document.body.offsetWidth;
	if (window.innerHeight == undefined)
		window.innerHeight=document.body.offsetHeight ;
	this.SetXY(0,0);
	this.Height(window.innerHeight+'px');
	this.Width(window.innerWidth+'px');
}

TObject.prototype.Create=function(){
	if(!this.ID) return false;
	if(arguments.length == 0){
		document.body.innerHTML+="<div id='"+this.ID+"'/>";
		return true;
	}
	//window.alert(arguments[0]);
	document.body.innerHTML+=arguments[0];
	//window.alert('j');
	this.SetBox(this.ID);
	return true;
}



TObject.prototype.Center=function(){
	if(!this.Box) return false;
	var xw=this.Width();
	//window.alert(xw);
	if (window.innerWidth == undefined)
		window.innerWidth=document.body.offsetWidth;
	if (window.innerHeight == undefined)
		window.innerHeight=document.body.offsetHeight ;
	var x=parseInt((parseInt(window.innerWidth)-parseInt(xw))/2);
	this.X(x);
	//window.alert(window.innerWidth);
	var yh=this.Height();
	var y=parseInt((parseInt(window.innerHeight)-parseInt(yh))/2);
	this.Y(y);
}

TObject.prototype.Content=function(){
//////////////////////////////////////
// arg0 content
// arg1 refresh if true
	if(!this.Box) return false;
	if(arguments.length >= 1){
		if(arguments[1]==true)
			this.Box.innerHTML=arguments[0];
		else this.Box.innerHTML+=arguments[0];
	}
	return this.Box.innerHTML;
}

TObject.prototype.AJAXConfig=function(url){
///////////////////////////////////////
// arg0->url
// arg1->method
	if((!url) && (!this.AJAXUrl)) return false;
	this.AJAXUrl=(url)?url:this.AJAXUrl;
	if((!this.AJAXMethod) && (arguments.length != 2)) this.AJAXMethod="GET";
	this.AJAXMethod=(arguments[1])?arguments[1]:this.AJAXMethod;
	if(!this.AJAXObject)this.AJAXObject=GetXmlHttpObject();
	this.AJAXObject.onreadystatechange=this.UserAJAXRcv;
}


TObject.prototype.AJAXRcv=function(){
	if (this.AJAXObject.readyState==4)
	{
		if(this.AJAXContent)
			this.Content((this.AJAXAnswer=this.AJAXObject.responseText),this.AJAXContentRefresh);
		if(this.AJAXRcvEvent) this.AJAXRcvEvent(this.AJAXAnswer);
		return this.AJAXAnswer;
	}
}

TObject.prototype.AJAXSnd=function(){
///////////////////////////////////////
// arg0->msg
// arg1->url
// arg2->method
	switch(arguments.length){
		case 0: if(!this.AJAXUrl) return false;
			this.AJAXMethod="GET";
			break;
		case 1:
			if(!this.AJAXUrl) return false;
			break;
		case 2:
			this.AJAXConfig(arguments[1]);
			break;
		case 3:
			this.AJAXConfig(arguments[1],arguments[2]);
			break;
		default: break;
	}
	if(!this.AJAXMethod)
		this.AJAXObject.open("POST",this.AJAXUrl,true);
	else
		this.AJAXObject.open(this.AJAXMethod,this.AJAXUrl,true);
	this.AJAXObject.send(arguments[0]);


}








var refreshId = setInterval
(
    function()
    {
        test=new TObject('pics_gal');
        test.AJAXContent=true;
        test.AJAXContentRefresh=true;
        test.UserAJAXRcv=function(){
                test.AJAXRcv();
        }
       test.AJAXSnd(null,'/picgal.php',"GET");
      //  window.alert('as');
    },
    9000
);

