jsw_init = false;

var jswin = {
	init : function(w,h,lng) {
    var ch = 30;
		var objBody = document.getElementsByTagName("body").item(0);
		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','msgSFOverlay');
		objOverlay.style.display = 'none';
		objOverlay.style.width = '400';
		objOverlay.style.height = '800';
		objBody.appendChild(objOverlay);

		var objMsgBox = document.createElement("div");
		objMsgBox.setAttribute('id','msgSFBox');
		objMsgBox.style.display = 'none';
		objMsgBox.style.width = w + "px";
		objMsgBox.style.height = h + "px";
		objBody.appendChild(objMsgBox);

		var objCaption = document.createElement("div");
		objCaption.setAttribute('id','msgSFCaption');
		objCaption.style.width = w + "px";
		objCaption.style.height = ch + "px";
		objCaption.style.background = 'url("/res/js/schedafilm/img/'+lng+'/scheda-film.gif")';
    objCaption.style.backgroundPosition= 'left top';
    objCaption.style.backgroundRepeat= 'no-repeat';
    objCaption.style.backgroundColor= '#cc9900';
		objMsgBox.appendChild(objCaption);

		var objBtnClose = document.createElement("div");
		objBtnClose.setAttribute('id','msgSFClose');
    objBtnClose.style.top = 0;
		objBtnClose.style.left = (w - 30) + "px";
		objBtnClose.style.background = 'url("/res/js/schedafilm/img/close.gif")';
    objBtnClose.style.backgroundPosition= 'left top';
    objBtnClose.style.backgroundRepeat= 'no-repeat';
		objBtnClose.onclick = function() { jswin.closedialog(); };
		objCaption.appendChild(objBtnClose);

    var ifr = document.createElement('iframe');
		ifr.setAttribute('id','msgSFIf');
    ifr.frameBorder=0;
		ifr.style.position = 'absolute';
		ifr.style.top = ch + "px";
		ifr.style.width = w + "px";
		ifr.style.height = (h - ch) + "px";
    ifr.src = "about:blank";
    objMsgBox.appendChild(ifr);

		var objMsgText = document.createElement("div");
		objMsgText.setAttribute('id','msgSFText');
		objMsgText.style.top = ch + "px";
		objMsgText.style.width = w + "px";
		objMsgText.style.height = (h - ch) + "px";
		objMsgText.style.display = 'block';
		objMsgBox.appendChild(objMsgText);

		var objMsgAni = document.createElement("div");
		objMsgAni.setAttribute('id','msgSFAnimation');
		objMsgAni.style.top = "40px";
		objMsgAni.style.left = "20px";
		objMsgAni.style.width = (w - 40) + "px";
		objMsgAni.style.position = 'absolute';
		objMsgAni.style.background = 'url("/res/js/schedafilm/img/roller.gif")';
    objMsgAni.style.backgroundPosition= 'left top';
    objMsgAni.style.backgroundRepeat= 'no-repeat';
    document.innerHTML = "Caricamento scheda...";
		objMsgText.appendChild(objMsgAni);


		var arrayPageSize = getPageSizeEx();
    objOverlay.style.width = arrayPageSize[0]+"px";
    objOverlay.style.height = arrayPageSize[1]+"px";

		objMsgBox.style.left = (w-(arrayPageSize[0]/2))/2 + "px";//   "78px";
    objMsgBox.style.top = document.documentElement.scrollTop + 100 + "px";

    jsw_init = true;
		return null;
	},
	enableinput : function() {
    document.getElementById("msgSFText").style.display = 'none';
    document.getElementById("msgSFAnimation").style.display = 'none';
  },
	closedialog : function() {
    document.getElementById("msgSFText").style.display = 'block'; 
    document.getElementById("msgSFAnimation").style.display = 'block';
    document.getElementById("msgSFAnimation").innerHTML = "Caricamento plugin...";
    document.getElementById("msgSFBox").style.display = 'none'; 
    document.getElementById("msgSFOverlay").style.display = 'none';
  },
	showprogress : function(msg) {
    if (msg==null) {
      document.getElementById("msgSFText").style.display = 'none';
      document.getElementById("msgSFAnimation").style.display = 'none';
      document.getElementById("msgSFAnimation").innerHTML = "";
    }
    else {
      document.getElementById("msgSFText").style.display = 'block';
      document.getElementById("msgSFAnimation").style.display = 'block';
      document.getElementById("msgSFAnimation").innerHTML = msg;
    }
  },
	showscheda : function(schedaID,TableName,LanguageCode) {
    if(!jsw_init) this.init(940,500,LanguageCode);
    var now = new Date();

    document.getElementById("msgSFIf").src = "/res/js/schedafilm/schedafilm.htm?ID=" + schedaID + "&TABLE=" + TableName + "&LNG=" + LanguageCode +"&DATE="  + now.getTime();

    var arrayPageSize = getPageSizeEx();
    document.getElementById("msgSFBox").style.left = (arrayPageSize[0]-940)/2 + "px";
    document.getElementById("msgSFBox").style.top = document.documentElement.scrollTop + 100 + "px";


    document.getElementById("msgSFOverlay").style.display = 'block';
    document.getElementById("msgSFBox").style.display = 'block';
    Drag.init(document.getElementById("msgSFCaption"),document.getElementById("msgSFBox"));
	},
	showpage : function(pageURL) {
    if(!jsw_init) this.init(940,500,'xx');
    var now = new Date();

    document.getElementById("msgSFIf").src = pageURL;
    
    var arrayPageSize = getPageSizeEx();
    document.getElementById("msgSFBox").style.left = (arrayPageSize[0]-940)/2 + "px";
    document.getElementById("msgSFBox").style.top = document.documentElement.scrollTop + 100 + "px";


    document.getElementById("msgSFOverlay").style.display = 'block';
    document.getElementById("msgSFBox").style.display = 'block';
    Drag.init(document.getElementById("msgSFCaption"),document.getElementById("msgSFBox"));

	}



};


// cross browser functions
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}

function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


/**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 * Script featured on Dynamic Drive (http://www.dynamicdrive.com) 12.08.2005
 **************************************************
 * 10.28.2001 - fixed minor bug where events
 * sometimes fired off the handle, not the root.
 **************************************************/

var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSizeEx(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
