/* The Firefox is default*/
function openCenteredWindow(url, width, height) {
	if (!width) width = 800;
  	if (!height) height = 600;
  	var left = (screen.width/2) - width/2;
  	var top = (screen.height/2) - height/2;

  	window.open(url, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}
function openCenteredWindowWidthResizable(url, width, height) {
	if (!width) width = 800;
  	if (!height) height = 600;
  	var left = (screen.width/2) - width/2;
  	var top = (screen.height/2) - height/2;

  	window.open(url, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}
function openMultiWindow(url, width, height) {
	if (!width) width = 800;
  	if (!height) height = 600;
  	var left = (screen.width/2) - width/2;
  	var top = (screen.height/2) - height/2;

  	window.open(url, '', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}
function openWindowWithScrollBar(url, width, height, id){
	if (!width) width = 800;
  	if (!height) height = 600;
  	var left = (screen.width/2) - width/2;
  	var top = (screen.height/2) - height/2;
    if(!id ) {
        id ="popUpWin"
    }
 	window.open(url, id, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
    return false;
}

// close an opened window
function closeWindow() {
    window.close();
}

function showPixDetails(picture) {
    var url = "";
    if(picture) {
        url = picture.getAttribute("href");
    }
    var width = 800;
    var height = 600;
    var left = (screen.width/2) - width/2;
    var top = (screen.height/2) - height/2;
    
    window.open(url, '', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
    return false;
}
//Size to content script
//emulating the sizeToContent function for the bad guy IE
var heightDiff = 0;
var global = new Object();

global.SAFE_MARGIN_H = 20;
global.SAFE_MARGIN_W = 20;

// window sizing utils
function centerScreenIE() {
    var w = window.document.body.offsetWidth + 6;
    var h = window.document.body.offsetHeight + 25;
    var W = screen.availWidth;
    var H = screen.availHeight - 20;
    window.moveTo((W - w) / 2, (H - h) / 2);
}
function centerScreenGecko() {
    var w = window.outerWidth;
    var h = window.outerHeight;
    var W = screen.availWidth;
    var H = screen.availHeight - 20;
    window.moveTo((W - w) / 2, (H - h) / 2);
}
function centerScreen() {
    if(global.isGecko) {
        centerScreenGecko();
    } else {
        centerScreenIE();
    }
}

global.getContentElement = function () {
    if (document.documentElement.clientHeight) {
        return document.documentElement;
    }
    return document.body;
};
global.sizeToContentImpl = function(finalize) {
    if(!window.sizeToContent) {
        var anchor = document.createElement("div");
        document.body.appendChild(anchor);
        anchor.style.clear = "both";
        anchor.style.overflow = "hidden";
        anchor.style.height = "3px";
        anchor.style.backgroundColor = "red";
        anchor.style.styleFloat = "none";

        var w = anchor.offsetWidth;
        var h = anchor.offsetTop + 0;

        var deltaH = h - global.getContentElement().clientHeight;
        //alert("h = " + h + "\ndeltaH = " + deltaH + "\nclientHeight = " + global.getContentElement().clientHeight);
        var deltaW = 0
        window.resizeBy(deltaW, deltaH);
        deltaH = global.getContentElement().scrollHeight - global.getContentElement().clientHeight;
        deltaW = global.getContentElement().scrollWidth - global.getContentElement().clientWidth;
        //alert("document.body.scrollHeight = " + document.body.scrollHeight + "\ndeltaH = " + deltaH + "\nclientHeight = " + document.body.clientHeight + "\ndeltaW = " + deltaW);
        window.resizeBy(deltaW, deltaH);
        //alert("after resize");

        document.body.removeChild(anchor);
    } else {
      	window.sizeToContent();
    }
    if (finalize) {
        if (window.outerWidth) {
            var maxW = screen.availWidth - global.SAFE_MARGIN_H;
            var maxH = screen.availHeight - global.SAFE_MARGIN_W;
            
            var deltaH = 0;
            var deltaW = 0;

            var w = window.outerWidth;
            var h = window.outerHeight;

            deltaW = w > maxW ? maxW - w : 0;
            deltaH = h > maxH ? maxH - h : 0;
            window.resizeBy(deltaW, deltaH);                    

        }
        global.centerScreen();
    }
};
global.sizeToContent = function () {
    if(global.isGecko) {
        window.resizeTo(778, 595);
	    global.sizeToContentImpl();
    //	window.setTimeout('global.sizeToContentImpl(true);', 10);
	//    window.setTimeout('global.sizeToContentImpl(true);', 30);
        
    } else {
	    window.resizeTo(728, 535);
	    global.sizeToContentImpl();
	    window.setTimeout('global.sizeToContentImpl(true);', 10);
	    window.setTimeout('global.sizeToContentImpl(true);', 30);
	    
    }
};
global.centerScreenIE = function () {
    var w = window.document.body.offsetWidth + 6;
    var h = window.document.body.offsetHeight + 25;
    var W = screen.availWidth;
    var H = screen.availHeight - 20;
    window.moveTo((W - w) / 2, (H - h) / 2);
};
global.centerScreenGecko = function () {
    var w = window.outerWidth;
    var h = window.outerHeight;
    var W = screen.availWidth;
    var H = screen.availHeight - 20;
    window.moveTo((W - w) / 2, (H - h) / 2);
};
global.centerScreen = function () {
    if(window.outerWidth) {
        global.centerScreenGecko();
    } else {
        global.centerScreenIE();
    }
};

global.fitToContent = function(hoz_margin,vert_margin){
    window.resizeTo(document.body.scrollWidth + hoz_margin, document.body.scrollHeight + vert_margin + heightDiff);
    heightDiff = 0;
}

global.isGecko = (navigator.userAgent.indexOf("Gecko") > 0);



//Resize to content

function getRefToDivMod( divID, oDoc ) {
	if( !oDoc ) { oDoc = document; }
	if( document.layers ) {
		if( oDoc.layers[divID] ) { return oDoc.layers[divID]; } else {
			for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
				y = getRefToDivNest(divID,oDoc.layers[x].document); }
			return y; } }
	if( document.getElementById ) { return oDoc.getElementById(divID); }
	if( document.all ) { return oDoc.all[divID]; }
	return oDoc[divID];
}

function resizeWinTo( idOfDiv ) {
	var oH = getRefToDivMod( idOfDiv ); if( !oH ) { return false; }
	var x = window; x.resizeTo( screen.availWidth, screen.availWidth );
	var oW = oH.clip ? oH.clip.width : oH.offsetWidth;
	var oH = oH.clip ? oH.clip.height : oH.offsetHeight; if( !oH ) { return false; }
	x.resizeTo( oW + 200, oH + 200 );
	var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;
	if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }
	else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }
	else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }
	if( window.opera && !document.childNodes ) { myW += 16; }
	//second sample, as the table may have resized
	var oH2 = getRefToDivMod( idOfDiv );
	var oW2 = oH2.clip ? oH2.clip.width : oH2.offsetWidth;
	var oH2 = oH2.clip ? oH2.clip.height : oH2.offsetHeight;
//	x.resizeTo( oW2 + ( ( oW + 200 ) - myW ), oH2 + ( (oH + 200 ) - myH ) );
	
	x.resizeTo( oW2 = oW + ( ( oW + 200 ) - myW ), oH2 = oH + ( (oH + 200 ) - myH ) );
	var scW = screen.availWidth ? screen.availWidth : screen.width;
	var scH = screen.availHeight ? screen.availHeight : screen.height;
	x.moveTo(Math.round((scW-oW)/2),Math.round((scH-oH)/2));
}
