/*
 * MyTooCool mini framework
 * Copyright 2009 MyTooCool
 *   
 * see The GNU Public License (GPL)
 *
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */


var MiniWidgetClass = function (bkimg,effect,scrollb,container) {
this.bkimg=bkimg; //background image
this.effect=effect; //effect right left 0:no , 1:yes
this.scrollb=scrollb; //vertical scrollbar 0:no , 1:yes
this.container=container;
}

var MiniGadgetClass = function() {}

MiniWidgetClass.prototype.init=function() {

this.wrap=this.container;

this.wrap.style.overflow='hidden';

this.wimgo = document.createElement("img");
this.wimgo.src = this.bkimg;
this.wimgo.style.position = 'absolute';
this.wimgo.style.left='0px';
this.wimgo.style.width = '100%';
this.wimgo.style.height = '100%';
this.wimgo.style.zIndex='0';
this.wrap.appendChild(this.wimgo);


this.services = document.createElement("div");
this.services.className = 'mtcservices';
this.services.style.position = 'absolute';
this.services.style.width = '90%';
this.services.style.height = '90%';
this.services.style.top = '5%';
this.services.style.left=brow?'5%':'5%';
this.services.style.zIndex='-1';
this.wrap.appendChild(this.services);


this.divservices = document.createElement("div");
this.divservices.style.width = '100%';
this.divservices.style.height = '100%';
this.services.appendChild(this.divservices);

this.divgadgets = document.createElement("div");
this.divgadgets.style.styleFloat = 'left';
this.divgadgets.style.cssFloat = 'left';
this.divgadgets.style.width = '100%';
this.divgadgets.style.height = '100%';
this.divgadgets.style.overflowY = this.scrollb==1?'scroll':'hidden';
this.divgadgets.style.overflowX = 'hidden';
this.divservices.appendChild(this.divgadgets);

this.gadgets = document.createElement("div");
this.gadgets.style.styleFloat = 'left';
this.gadgets.style.cssFloat = 'left';
this.gadgets.style.display = 'block';
this.gadgets.style.width = '100%';
this.gadgets.style.height = '100%';
this.gadgets.style.marginTop = '0px';
this.gadgets.style.marginLeft = '0px';
this.divgadgets.appendChild(this.gadgets);

}

MiniGadgetClass.prototype.init=function(Widget) {

this.Widget=Widget;
this.mode0 = document.createElement("div");
this.mode0.style.styleFloat = 'left';
this.mode0.style.cssFloat = 'left';
this.mode0.style.width = '100%';
this.mode0.style.height = '100%';
}

MiniWidgetClass.prototype.AddWidget=function() {

this.Content=new MiniGadgetClass();

this.init();

this.Content.init(this);

}

MiniWidgetClass.prototype.SwitchContent=function(func) {

while (this.Content.mode0.firstChild) {
this.Content.mode0.removeChild(this.Content.mode0.firstChild);
}

this.gadgets.style.marginTop='0px';

func.call(this.Content);

}

MiniWidgetClass.prototype.ChContent=function() {

while (this.gadgets.firstChild) {
this.gadgets.removeChild(this.gadgets.firstChild);
}

this.gadgets.appendChild(this.Content.mode0);

this.services.style.zIndex=1;

this.width=this.services.offsetHeight;

this.x=this.width;

if (this.effect==1)
{
this.AffPage();
}

}

MiniWidgetClass.prototype.AffPage=function(sc) {

this.gadgets.style.marginLeft=this.width+'px';

this.j=0;
this.startEff=0;

if (typeof(this.t0)!="undefined") {clearInterval(this.t0);};

var funceff=this.EffLeft3.Delegate2(this,0.2);

this.t0=setInterval(funceff,10);

}

MiniWidgetClass.prototype.EffLeft3=function(second) {
var second=second;
var d = new Date();
var percenteff = 100;
var scmarg = this.x;

if ( this.startEff == 0 )
{
this.startEff = d.valueOf();
}

if (second)
{
percenteff = (((d.valueOf()-this.startEff)/1000)/second)*100;
} else {
clearInterval(this.t0);
this.startEff = 0;
}

scmarg=this.x*Math.exp(1-percenteff/100);
this.j++;

if (scmarg>=1)
{
this.gadgets.style.marginLeft=scmarg+'px';
} else {
this.gadgets.style.marginLeft='0px';
clearInterval(this.t0);
this.startEff = 0;
}
}

Function.prototype.Delegate2=function(obj,obj2) { var f=this;return function() { f.call(obj,obj2); }}

var initXML=function initXML(string)
{
if (window.ActiveXObject) {
var obj = new ActiveXObject("Microsoft.XMLDOM" );
obj.loadXML(string);}
else if (window.XMLSerializer) {
var  obj = (new DOMParser()).parseFromString(string,"text/xml" );
}
return obj;
}

var initXMLtoArray=function initXMLtoArray(myxml)
{
var G=new Array();
var res=[];
res=XMLtoArray(myxml.childNodes[0],G);
return res;
}

var XMLtoArray=function XMLtoArray(child,Myarr)
{
if ((child.nodeType==1))
{
for (var j=0;j<child.childNodes.length;j++)
{
if (((child.childNodes[j].nodeType==3)||(child.childNodes[j].nodeType==4))&&(child.childNodes[j].nodeValue!=null))
{
try {
Myarr=decodeURIComponent2(child.childNodes[j].nodeValue);
} catch(ee) {
Myarr=child.childNodes[j].nodeValue;
}

} else {
Myarr.push(XMLtoArray(child.childNodes[j],[]));
}
}
}
return Myarr;
}

var encodeURIComponent2 = function(val)
{
try {
var tmp=encodeURIComponent(val);
} catch(ee) {
var tmp=val;
}

var reg=new RegExp("(%5C)", "g");
tmp=tmp.replace(reg,"%2F");

return tmp;
}

var decodeURIComponent2 = function(val)
{
try {
var tmp=decodeURIComponent(val);
} catch(ee) {
var tmp=val;
}

return tmp;
}

var Browstype = function() { if (self.innerHeight) {return 0;} else {return 1;}}

var brow=Browstype();
