// Window opener functions  v1.0.6
// http://www.dithered.com/javascript/window/index.html
// code by Chris Nott (chris@NOSPAMdithered.com - remove NOSPAM)

/*******************************************************************************
Popup Window openers
*******************************************************************************/
var base_href='/';

var mouseX;
var mouseY;

function handleMouseMove(e) {
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    } 
    else {
        cursor.x = e.clientX + 
            (document.documentElement.scrollLeft || 
            document.body.scrollLeft) - 
            document.documentElement.clientLeft;
        cursor.y = e.clientY + 
            (document.documentElement.scrollTop || 
            document.body.scrollTop) - 
            document.documentElement.clientTop;
    }
    mouseX=cursor.x;
    mouseY=cursor.y;
    return false;
}


var winReference = null;
// Open a window at a given position on the screen
function openPositionedWindow(url, name, width, height, x, y, status, scrollbars, moreProperties, openerName) {
    
    // ie 4.5 and 5.0 mac - windows are 2 pixels too short; if a statusbar is used, the window will be an additional 15 pixels short
    var agent = navigator.userAgent.toLowerCase();
    if (agent.indexOf("mac") != -1 && agent.indexOf("msie") != -1 && (agent.indexOf("msie 4") != -1 || agent.indexOf("msie 5.0") != -1) ) {
        height += (status) ? 17 : 2;
    }
    
    // Adjust width if scrollbars are used (pc places scrollbars inside the content area; mac outside) 
    width += (scrollbars != '' && scrollbars != null && agent.indexOf("mac") == -1) ? 16 : 0;
    var properties = 'width=' + width + ',height=' + height + ',screenX=' + x + ',screenY=' + y + ',left=' + x + ',top=' + y + ((status) ? ',status' : '') + ',scrollbars' + ((scrollbars) ? '' : '=no') + ((moreProperties) ? ',' + moreProperties : '');
    return openWindow(url, name, properties, openerName);
}

// Open a window at the center of the screen
function openCenteredWindow(url, name, width, height, status, scrollbars, moreProperties, openerName) {
    var x, y = 0;
    if (screen) {
        x = (screen.availWidth - width) / 2;
        y = (screen.availHeight - height) / 2;
    }
    
    if (!status) status = '';
    if (!openerName) openerName = '';
    var reference = openPositionedWindow(url, name, width, height, x, y, status, scrollbars, moreProperties, openerName);
    return reference;
}

// Open a window at the center of the parent window
function openCenteredOnOpenerWindow(url, name, width, height, status, scrollbars, moreProperties, openerName) {
    var centerX = 0;
    var centerY = 0;
    if (window.screenX != null && window.outerWidth) {
    centerX = window.screenX + (window.outerWidth / 2);
    centerY = window.screenY + (window.outerHeight / 2);
    }
    else if (window.screenLeft) {
    if (document.documentElement) {
    centerX = window.screenLeft + (document.documentElement.offsetWidth / 2);
    centerY = window.screenTop + (document.documentElement.offsetHeight / 2);
    }
    else if (document.body && document.body.offsetWidth) {
    centerX = window.screenLeft + (document.body.offsetWidth / 2);
    centerY = window.screenTop + (document.body.offsetHeight / 2);
    }
    }
    
    if (centerX == 0) {
    openCenteredWindow(url, name, width, height, status, scrollbars, moreProperties, openerName);
    }
    var x = parseInt(centerX - (width / 2));
    var y = parseInt(centerY - (height / 2));
    if (!status) status = '';
    if (!openerName) openerName = '';
    var reference = openPositionedWindow(url, name, width, height, x, y, status, scrollbars, moreProperties, openerName);
    return reference;
}
// Open a full-screen window (different from IE's fullscreen option)
function openMaxedWindow(url, name, scrollbars, openerName) {
    var x, y = 0;
    var width  = 600;
    var height = 800;
    if (screen) {
    if (screen.availLeft) {
    x = screen.availLeft;
    y = screen.availTop;
    }
    width  = screen.availWidth - 6;
    height = screen.availHeight - 29;
    }
    var reference = openPositionedWindow(url, name, width, height, x, y, false, scrollbars, openerName);
    return reference;
}

// Open a full-chrome (all GUI elements) window
// This is like using a target="_blank" in a normal link but allows focussing the window
function openFullChromeWindow(url, name, openerName) {
    return openWindow(url, name, 'directories,location,menubar,resizable,scrollbars,status,toolbar');
}

// Open a sized full-chrome (all GUI elements) window 
function openSizedFullChromeWindow(url, name, width, height, openerName) {
    return openCenteredWindow(url, name, width, height, true, true, 'directories,location,menubar,resizable,toolbar', openerName)
}

// Core utility function that actually creates the window and gives focus to it
function openWindow(url, name, properties, openerName) {
    // ie4.x pc can't give focus to windows containing documents from a different domain
    // in this case, initially load a local interstisial page to allow focussing before loading final url
    var agent = navigator.userAgent.toLowerCase();
    if (agent.indexOf("msie") != -1 && parseInt(navigator.appVersion) == 4 && agent.indexOf("msie 5") == -1 && agent.indexOf("msie5") == -1 && agent.indexOf("win") != -1 && url.indexOf('http://') == 0) {
        winReference = window.open('', name, properties);
        
        setTimeout('if (winReference && !winReference.closed) winReference.location.replace("' + url + '")', 300);
    }
    else {
        winReference = window.open(url, name, properties);
    }
    
    // ie doesn't like giving focus immediately (to new window in 4.5 on mac; to existing ones in 5 on pc)
    setTimeout('if (winReference && !winReference.closed) winReference.focus()', 200);
    
    if (openerName) self.name = openerName;
    return winReference;
}

/*******************************************************************************
Modal Dialog controls
*******************************************************************************/
// Close a dialog
// Call from onunload event handler of any page that can create a dialog
function closeDialog(dialog) {
    if (dialog && dialog.closed != true) dialog.close();
}

// Close parent popup
// Call from onload event handler of any page that could be created from a dialog
function closeParentDialog() {
    if (top.opener && isWindowPopup(top.opener)) {
    root = top.opener.top.opener;
    top.opener.close();
    top.opener = root;
    }
}

// Check if a window is a popup
function isWindowPopup(win) {
    return ((win.opener) ? true : false);
}

/*******************************************************************************
    Additional functions
*******************************************************************************/

function openImagePopupID(id, width, height, title){
    var url = base_href + 'resize.php?name=' + id + '&w=' + width + '&h=' + height + '&noconstrain=3';
    return openImagePopup(url, width, height, "image_" + id, title, 0);
}

function openImagePopupId(id, width, height, title){
    var url = base_href + 'resize.php?name=' + id;
    return openImagePopup(url, width, height, "image_" + id, title, 0);
}

function openFlashPopupID(id, width, height, title){
    var url = base_href + 'get.php?i.' + id;
    return openImagePopup(url, width, height, "image_" + id, title, 1);
}

function openImagePopup(url, width, height, window_name, title, flash){
    document.getElementById('popimage').onload=function()
    {
        document.getElementById('pop').style.display='';
        if (mouseY+document.getElementById('pop').offsetHeight>=$(window).height()+$(window).scrollTop() || mouseY+height>=$(window).height()+$(window).scrollTop())
        {
            document.getElementById('pop').style.top=($(window).height()+$(window).scrollTop())-(document.getElementById('pop').offsetHeight*1.1)+"px";
        }
        else
        {
            document.getElementById('pop').style.top=mouseY-15+"px";
        }
        if (mouseX+document.getElementById('pop').offsetWidth>=$(window).width()+$(window).scrollLeft())
        {
            document.getElementById('pop').style.left=($(window).width()+$(window).scrollLeft())-(document.getElementById('pop').offsetWidth*1.1)+"px";
        }
        else
        {
            document.getElementById('pop').style.left=mouseX+35+"px";
        }
        //document.getElementById('pop').style.left=(document.width/2)-(document.getElementById('pop').offsetWidth/2)+"px";
    };
    document.getElementById('popimage').src='';
    document.getElementById('popimage').src=url;
    /* if(title + "" == "undefined"){
        title = " ";
    }
    
    var dontFit = 0;
    
    if(width > screen.availWidth){
        width = screen.availWidth - 20;
        var dontFit = 1;
    }
    
    if(height > screen.availHeight){
        height = screen.availHeight - 30;
        var dontFit = 1;
    }
    
    if(dontFit){
        win = openPositionedWindow("", window_name, parseInt(width), parseInt(height), 0, 0, 0, 1, {}, window);
    }else{
        win = openCenteredWindow("", window_name, parseInt(width), parseInt(height), 0, 0, "");
    }
    
    var body = "<html><head><title>"+title+"</title></head><body onload='FitPic();'topmargin=0 leftmargin=0 marginwidth=0 marginheight=0>";
    if(!width && !height){
        body += '<script language=javascript>';
      body += 'var arrTemp=self.location.href.split("?"); ';
      body += 'var picUrl = (arrTemp.length>0)?arrTemp[1]:""; ';
        body += 'var NS = (navigator.appName=="Netscape")?true:false; ';
    
        body += 'function FitPic() { ';
        body += 'iWidth = (NS)?window.innerWidth:document.body.clientWidth; ';
        body += 'iHeight = (NS)?window.innerHeight:document.body.clientHeight; ';
        body += 'iWidth = document.images[0].width - iWidth; ';
        body += 'iHeight = document.images[0].height - iHeight; ';
        body += 'window.resizeBy(iWidth, iHeight); ';
        body += 'window.moveTo((screen.availWidth-document.images[0].width)/2, (screen.availHeight-document.images[0].height)/2); ';
      body += 'self.focus(); ';
        body += '}; ';
        body += '</script>';
    }else{
        body += '<script>function FitPic(){}</script>';
    }
    if(!flash){
        body += "<img style=\"\" onclick=\"window.close();\" alt=\""+title+"\" src=\""+url+"\">";
    }
    else{
        body += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+width+'" height="'+height+'"><param name="movie" value="'+url+'"><param name="quality" value="high"><param name="menu" value="false"><embed src="'+url+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width='+width+' height='+height+'></embed></object>';
    }
    body += "</body></html>";
    
    win.document.write(body);
    win.document.close(); */
}

function openWinInModal(url, width, height, params, title, charset){ // IE 5.5+ (because encodeURI used)
    url = base_href + "index.php?page=services/window_frame&title=" + encodeURIComponent(title) + "&charset=" + charset + "&url=" + encodeURIComponent(url);
    return openDialog(url, width, height, params, params["scrollbars"], params["resizable"]);
}

function openDialog(url, width, height, param, scroll, resizable){
    if(scroll + "" == "undefined"){ scroll = 0 }
    if(resizable + "" == "undefined"){ resizable = 0 }
    
    var rnd = Math.round((Math.random()*999)+1);
    
    if(typeof browser != 'undefined'){ // If we have browser detected
        
        if(browser.isIE && browser.versionMajor < 7){ // for browsers IE<7 we should add borders and status widths
            width += 6;
            height += 47;
        }
    }
    
    
    var r = openCenteredWindow(url + "&r=" + rnd, 'aa', width, height, 1, 1, 'modal=yes,dependent=yes');//param, 'dialogWidth:' + width + 'px;dialogHeight:' + height + 'px;center:1;dialogHide:0;help:0;edge:raised;status:1;unadorned:1;resizable:' + resizable + ';scroll:' + scroll);
    
    if (typeof(r) != "undefined")
        return r;
    else 
        return true;
}

if(!window.encodeURI){// Emulating encodeURI
    eval('function encodeURI(str){ return escape(str); }');
}

if(!window.encodeURIComponent){// Emulating encodeURIComponent
    eval('function encodeURIComponent(str){ return escape(str); }');
}
