/**
 * Screen aand mouse class
 *
 * Main js functions for works with screen and mouse
 *
 * @package general
 * @author  Eugene Cherepanov (Exmajor)
 * @copyright 2010
 * @version 5.0.1
 *
 */

/*
 * SCREEN RESOLUTION AND REDIRECTING
 *
 */
function screenRes(flagsize) {
var res = (flagsize == "WINDOW") ? winSize() : screenSize();
if(res[0] == 0) res = screenSize();
return res[0];
}


function screenRedirect(scrwd) {

var retval;

if (scrwd > 200 && scrwd <= 360) {
            retval = 240;
        } else
            if (scrwd > 360 && scrwd <= 800) {
                retval = 800;
            } else
                if(scrwd > 800) {
                    retval = 900;
                }
return retval;
}

function is_numeric (mixed_var) {
    if (mixed_var === '') {
        return false;
    }
    return !isNaN(mixed_var * 1);
}


function GoToRightRes(flag, use_res) {
    var cur_res = screenRes(flag);
    var to_res = screenRedirect(cur_res);
    var url = window.location.href;
    if(use_res != to_res) {
        window.location = "/redirect.asp?res=" + to_res + "&url=" + url;
    }
}

/*
 * COORDINATES OF MOUSE ON SCREEN
 * ------------------------------
 * Exampl of use: onmouseover='preseefoto(" . $f['good_id'] . ", mousePageXY(event), " . $size[0] . ", " . $size[1] .")'
 * 
 */
function mousePageXY(e)
{
var x = 0, y = 0;
var coord = new Array();
  if (!e) e = window.event;
  if (e.pageX || e.pageY)
  {
	x = e.pageX;
        y = e.pageY;
  }
  else if (e.clientX || e.clientY)
  {
      x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
      y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
  }
  coord[0] = x;
  coord[1] = y;
  return coord;
}

/*
 * VISIBLE DEMENTIONS OF WINDOW IN CLIENT
 */

function winSize() {
  var sz = new Array;
  var wWidth = 0, wHeight = 0;

if( typeof( window.innerWidth ) == 'number' )
{
    //Non-IE
    wWidth = window.innerWidth;
    wHeight = window.innerHeight;

} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
  {
    //IE 6+ in 'standards compliant mode'
    wWidth = document.documentElement.clientWidth;
    wHeight = document.documentElement.clientHeight;

} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
  {
    //IE 4 compatible
    wWidth = document.body.clientWidth;
    wHeight = document.body.clientHeight;
  }

	sz[0] = wWidth;
	sz[1] = wHeight;
	return sz;
}


/*
 * SCREEN SIZE
 */
function screenSize() {
    var sz = new Array;
    var sWidth = 0, sHeight = 0;
    if( typeof( screen.width ) == 'number' )
    {
        sWidth = screen.width;
        sHeight = screen.height;

    }
    
   	sz[0] = sWidth;
	sz[1] = sHeight;
	return sz;

}



/*
 * WIDTH AND HEIGHT OF HTML DOCUMENT
 */
function winSize_blk() {
  var sz = new Array;
	sz[0] = (document.body.scrollWidth > document.body.offsetWidth) ? document.body.scrollWidth : document.body.offsetWidth;
	sz[1] = (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight;
	return sz;
}

/*
 * LEFT-TOP POSITION IN CURRENT SCROLLING DOCUMENT
 */
function getScrollWin () {
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

/*
 * Progress bar INDICATOR
 */
// PROGRESSBAR CLASS
function progressbar() {

this.show = function show_pbar() {
    var Lera_pbar = document.getElementById("pbar").style;
    var wsz = winSize();
    var from_top = getScrollWin();
    var z_left = Math.round((wsz[0] - 32)/2);
    var z_top = Math.round((wsz[1] - 32)/2) + from_top;
    Lera_pbar.clear = "both";
    Lera_pbar.left = z_left+"px";
    Lera_pbar.top = z_top+"px";
    Lera_pbar.width = "32px";
    Lera_pbar.height = "32px";
    Lera_pbar.visibility = "visible";
    Lera_pbar.position = "absolute";
}

this.hide = function hide_pbar() {
var Lera_pbar = document.getElementById("pbar").style;
Lera_pbar.visibility = "hidden";
}

this.ins = function insert_pbar(todiv, clean) {
var divcontent;
    if(clean == 1) {
        divcontent = "";
    } else {
        divcontent = document.getElementById(todiv).innerHTML;
    }
    var indicator = '<img src="/img/icons/indicator_small_ring.gif" width="16" height="16" border="0" alt="">';
    document.getElementById(todiv).innerHTML = divcontent + " " + indicator;
}

}

          function GetBrowserLang() {
              var n = navigator;
              var BrowLang = n.language ? n.language : n.browserLanguage ? n.browserLanguage : null;
              if(null != BrowLang && BrowLang != "") {
                  //to lower case
                  BrowLang = BrowLang.toLowerCase();
                  //get only first two letters (for some crasy browsers)
                  BrowLang = BrowLang.substr(0,2);

              } else {
                  BrowLang = "en";
              }

              return BrowLang;
          }

// DATABOX Class:
function databoxclass() {
	this.show = function show_databox(wdth, hght, x_pos, y_pos) {
		var Lera_databox = document.getElementById("databox").style;
		Lera_databox.clear = "both";
		Lera_databox.visibility = "visible";
		Lera_databox.position = "absolute";
		var wsz = winSize();
		var from_top = getScrollWin();
		//alert(hght);
		var z_left = Math.round((wsz[0] - wdth)/2);
		var z_top = Math.round((wsz[1] - hght)/2) + from_top;
		Lera_databox.left = (false === x_pos) ? z_left+"px" : x_pos+"px";
		Lera_databox.top = (false === y_pos) ? z_top+"px" : y_pos+"px";
		Lera_databox.width = wdth+"px";
		Lera_databox.height = hght+"px";
		//Lera_databox.height = "auto";
		Lera_databox.overflow = "hidden";
	}
	this.hide = function hide_databox() {
		var Lera_databox = document.getElementById("databox").style;
		Lera_databox.visibility = "hidden";
                document.getElementById("databox").innerHTML = "";
	}
}
// CLASS BLACK FON ON PAGE
function fon_blackout() {
this.show = function show(wdth, hght, x_pos, y_pos) {
    var Lera_blackout = document.getElementById("blackout").style;
    var glas = 4;
    var ws = winSize_blk();
    var wszz = winSize();
    //var ww = ws[0];
    //if(ws[0] < wszz[0]) ww = wszz[0];

    var hh = ws[1];
    if(ws[1] < wszz[1]) hh = wszz[1];

    Lera_blackout.visibility="visible";
    Lera_blackout.position="absolute";
    Lera_blackout.left = (false === x_pos) ? "0px" : x_pos + "px";
    Lera_blackout.top = (false === y_pos) ? "0px" : y_pos + "px";
    //Lera_blackout.width = (false === wdth) ? ww+"px" : wdth + "px";
    Lera_blackout.width = (false === wdth) ? 100+"%" : wdth + "px";
    Lera_blackout.height = (false === hght) ? hh+"px" : hght + "px";
    Lera_blackout.opacity = glas/7;
    Lera_blackout.filter = 'alpha(opacity=' + glas*7 + ')';
}
this.hide = function hide() {
    var Lera_blackout = document.getElementById("blackout").style;
    Lera_blackout.visibility = "hidden";
}
}


function ConfirmDelete(delobj, act, id) {
    var asks = conffrase + '\n' + delobj;
    if ( true === confirm(asks)) window.location = act + "&id=" + id;
}

// Init
var pbar = new progressbar();
var fon = new fon_blackout();
var dbox = new databoxclass();

// Functions for inserts photo

function ShowDataBox (wdth, hght, ttl, showForm, userId, insertINto, tableInDB, fieldInTable) {
    var x_pos = false;
    var y_pos = false;
    fon.show( false, false, x_pos, y_pos);
    dbox.show(wdth, hght, x_pos, y_pos);
    var contentDbox = '<div id="dbox-top" class="clearfix">\n' +
        '<div id="dbox-top-l">' + ttl + '</div>\n' +
        '<div id="dbox-top-r">\n' +
        '<input type="button" value="X" name="clsfrm" style="width: 16px; height: 16px; border: 1px solid #fff; ' +
        'background: #f00; color: #fff; font-weight: bold; cursor: pointer;" onClick="CloseDataBox(); return false">\n' +
        '</div>\n' +
        '</div>\n' +
        '<div id="databox-data" class="clearfix"></div>\n';
    document.getElementById("databox").innerHTML = contentDbox;
    document.getElementById("databox-data").innerHTML = GetFormContent(showForm, userId, insertINto, tableInDB, fieldInTable);
//        '<a href="#"><img src="/img/icons/ico_del.png" width="16" height="16" ' +
//        'alt="' + close_ttl + '" title="' + close_ttl + '" border="0" onClick="CloseDataBox(); return false"></a>\n' +

}

function CloseDataBox() {
    dbox.hide();
    fon.hide();
    if(pbar.hide()) pbar.hide();
}

function GetProductInfo(wdth, hght, ttl, showForm, userId, insertINto, tableInDB, fieldInTable) {
ShowDataBox(wdth, (hght + 20), ttl, showForm, userId, insertINto, tableInDB, fieldInTable);
pbar.show();
    JsHttpRequest.query(
            'files-action.php', // backend
            {
                'userId': userId,
                'insertINto': insertINto,
                'tableInDB': tableInDB,
                'act': "getinfo"
            },
            // Function is called when an answer arrives.
            function(result, errors) {
                // Write the answer.
                if (result) {
                    document.getElementById(insertINto).innerHTML = result["backres"];
                    if(pbar.hide()) pbar.hide();
                }
            },
            true //false  // do not disable caching
        );
}
/**
 * Content form function
 */
function GetFormContent(showForm, userId, insertINto, tableInDB, fieldInTable) {
    var ret;
    if(showForm == 'upload') {
    ret = '<form method="post" enctype="multipart/form-data" onsubmit="return false">\n'+
    '<input type="hidden" id="max_file_size" name="max_file_size" value="10240000">\n'+
    '<input name="upfile" id="upfile" type="file" style="margin: 30px 0 0 30px;" size="45" maxlength="255">\n'+
    '<input type="hidden" value="'+ userId +'" name="userId" id="userId">\n'+
    '<input type="hidden" value="'+ tableInDB +'" name="tableInDB" id="tableInDB">\n'+
    '<input type="hidden" value="'+ fieldInTable +'" name="fieldInTable" id="fieldInTable">\n'+
    '<input name="btn_send" style="clear: both; float:left; margin-top: 30px; margin-left: 25%; width: 50%; margin-bottom:30px;" type="submit" ' +
    'value="' + upfile_btn + '" onclick="pbar.show();doLoad(\'' + insertINto + '\');"\n>' +
    '</form>\n';
    } else if(showForm == 'moveit') {
    ret = '<form method="post" onsubmit="return false">\n'+
    '<span style="float:left; margin-top: 20px; width: 100%; text-align: center;">' + intoTTL +'</span>\n' +
    '<div style="float:left; margin-top: 5px; width: 100%; text-align: center;" id="selectto">\n</div>\n' +
    '<input type="hidden" value="'+ userId +'" name="userId" id="userId">\n'+
    '<input type="hidden" value="'+ tableInDB +'" name="tableInDB" id="tableInDB">\n'+
    '<input type="hidden" value="'+ fieldInTable +'" name="fieldInTable" id="fieldInTable">\n'+
    '<input name="btn_send" style="clear: both; float:left; margin-top: 20px; margin-left: 25%; width: 50%; margin-bottom:30px;" type="submit" ' +
    'value="' + moveto_btn + '" onclick="pbar.show();SetMove();"\n>' +
    '</form>\n';
    } else if(showForm == 'infodata') {
        ret = '<div id="infodata" class="clearfix"></div>';
    } else if(showForm == 'media_opis') {
    ret = '<form method="post" onsubmit="return false">\n'+
    '<div style="float:left; margin-top: 20px; width: 100%;" class="clearfix">\n' +
    '<span style="float: left; width: 100px; margin-left: 20px; text-align: right;">' + ttl_ttl + '\n' +
    '</span>\n' + 
    '<input type="text" name="fileTTL" id="fileTTL" size="20" class="ln" style="float:right; margin-right: 20px; width: 340px;">\n' +
    '</div>\n' +
    '<input type="hidden" value="'+ userId +'" name="userId" id="userId">\n'+
    '<input type="hidden" value="'+ tableInDB +'" name="tableInDB" id="tableInDB">\n'+
    '<input type="hidden" value="'+ fieldInTable +'" name="fieldInTable" id="fieldInTable">\n'+
    '<input name="btn_send" style="clear: both; float:left; margin-top: 20px; margin-left: 25%; width: 50%; margin-bottom:30px;" type="submit" ' +
    'value="' + record_btn + '" onclick="pbar.show();SetFileOpis();">\n' +
    '</form>\n';
    }else {
        ret = '';
    }
    return ret;
}
/**
 * Delete media file
 */
function DeleteMediaFile(ttl, id, tableInDB) {
    var asks = conffrase + '\n\n' + ttl;
    if (true === confirm(asks)) {
        pbar.show();
        JsHttpRequest.query(
            'files-action.php', // backend
            {
                'userId': id,
                'tableInDB': tableInDB,
                'act': "DelMediaFile"
            },
            // Function is called when an answer arrives.
            function(result, errors) {
                // Write the answer.
                if (result) {
                    CloseDataBox();
                    window.location = window.location;
                }
            },
            false  // do not disable caching
        );
    }
}

/**
 * Make form to correct descibes of media file
 */
function MakeFileForm(wdth, hght, ttl, showForm, userId, insertINto, tableInDB, fieldInTable, FileTTL) {
ShowDataBox (wdth, hght, ttl, showForm, userId, insertINto, tableInDB, fieldInTable);
document.getElementById("fileTTL").value = FileTTL;
}

/**
 * To record media file information (ttl, opis)
 */
function SetFileOpis() {
        JsHttpRequest.query(
            'files-action.php', // backend
            {
                'userId': document.getElementById("userId").value,
                'tableInDB': document.getElementById("tableInDB").value,
                'fieldInTable': document.getElementById("fieldInTable").value,
                'ttl': document.getElementById("fileTTL").value,
                'act': "SetFileOpis"
            },
            // Function is called when an answer arrives.
            function(result, errors) {
                // Write the answer.
                if (result) {
                    CloseDataBox();
                    window.location.reload();
                }
            },
            false  // do not disable caching
        );

}


function doLoad(insertINto) {
        JsHttpRequest.query(
            'files-action.php', // backend
            {
                'userId': document.getElementById("userId").value,
                'tableInDB': document.getElementById("tableInDB").value,
                'fieldInTable': document.getElementById("fieldInTable").value,
                'insertINto': insertINto,
                'upfile': document.getElementById("upfile"),
                'act': "add"
            },
            // Function is called when an answer arrives.
            function(result, errors) {
                // Write the answer.
                if (result) {
                    document.getElementById(insertINto).innerHTML = result["backres"];
                    CloseDataBox();
                    window.location = window.location;
                }
            },
            false  // do not disable caching
        );
}

function ConfirmDeleteFile(userId, insertINto, tableInDB, fieldInTable) {
    var asks = conffrase;
    if ( true === confirm(asks)) {
        pbar.show();
        DelFile(userId, insertINto, tableInDB, fieldInTable);
    }
}

function DelFile(userId, insertINto, tableInDB, fieldInTable) {
        JsHttpRequest.query(
            'files-action.php', // backend
            {
                'userId': userId,
                'tableInDB': tableInDB,
                'fieldInTable': fieldInTable,
                'insertINto': insertINto,
                'act': "del"
            },
            // Function is called when an answer arrives.
            function(result, errors) {
                // Write the answer.
                if (result) {
                    document.getElementById(insertINto).innerHTML = result["backres"];
                    if(pbar.hide()) pbar.hide();
                }
            },
            false  // do not disable caching
        );
    
}

// flag for click-action on ico
var click_flag = false;

//Fullsize ico and photo
function ShowFullsizeIco(fileName, crd, wdth, hght) {
click_flag = false;
var insertINto = "databox";
var x_pos = crd[0] + 15;
var y_pos = crd[1] - 15 - hght;
fon.show(wdth, hght, (x_pos + 7), (y_pos + 7));
dbox.show(wdth, hght, x_pos, y_pos);
pbar.show();

document.getElementById("databox").innerHTML = '<div id="pbar_red">Loading...</div>';
var pbr = document.getElementById("pbar_red").style;
pbr.margin = ((hght / 2) - 8) + "px " + "0 0 " + ((wdth / 2) - 50) + "px";


        JsHttpRequest.query(
            'files-action.php', // backend
            {
                'userId': fileName,
                'insertINto': insertINto,
                'act': "previewico"
            },
            // Function is called when an answer arrives.
            function(result, errors) {
                // Write the answer.
                if (result) {
                    document.getElementById(insertINto).innerHTML = result["backres"];
                    if(pbar.hide()) pbar.hide();
                }
            },
            true //false  // do not disable caching
        );
    
}


function CloseFullsizeIco() {
    if(!click_flag || false === click_flag) CloseDataBox();
}

function ShowFullsize(userId, ttl, wdth, hght, tableInDB, fieldInTable) {
    click_flag = true;
    var insertINto = "databox-data";
    hght = hght + 20;
    CloseDataBox();
    ShowDataBox (wdth, hght, ttl, "blank", userId, insertINto, tableInDB, fieldInTable);
    pbar.show();
    
        JsHttpRequest.query(
            'files-action.php', // backend
            {
                'userId': userId,
                'tableInDB': tableInDB,
                'fieldInTable': fieldInTable,
                'insertINto': insertINto,
                'act': "fullsize"
            },
            // Function is called when an answer arrives.
            function(result, errors) {
                // Write the answer.
                if (result) {
                    document.getElementById(insertINto).innerHTML = result["backres"];
                    if(pbar.hide()) pbar.hide();
                }
            },
            true //false  // do not disable caching
        );

}

function MoveInTo(wdth, hght, ttl, showForm, userId, insertINto, tableInDB, fieldInTable) {
    
    ShowDataBox (wdth, hght, ttl, showForm, userId, insertINto, tableInDB, fieldInTable);
    insertINto = "selectto";
    GetLevelContent(0, insertINto, tableInDB, fieldInTable);
}

function SetMove() {
    JsHttpRequest.query(
            'files-action.php', // backend
            {
                'userId': document.getElementById("userId").value,
                'tableInDB': document.getElementById("tableInDB").value,
                'fieldInTable': document.getElementById("fieldInTable").value,
                'insertINto': document.getElementById("select_moveto").value,
                'act': "setmove"
            },
            // Function is called when an answer arrives.
            function(result, errors) {
                // Write the answer.
                if (result) {
                //alert(result["backres"]);
                CloseDataBox();
                AfterMove(result["backres"])
                }
            },
            false  // do not disable caching
        );

}

function GetLevelContent(userId, insertINto, tableInDB, fieldInTable) {
        pbar.show();
        JsHttpRequest.query(
            'files-action.php', // backend
            {
                'userId': userId,
                'tableInDB': tableInDB,
                'fieldInTable': fieldInTable,
                'insertINto': insertINto,
                'act': "getselect"
            },
            // Function is called when an answer arrives.
            function(result, errors) {
                // Write the answer.
                if (result) {
                    document.getElementById(insertINto).innerHTML = result["backres"];
                    if(pbar.hide()) pbar.hide();
                }
            },
            false  // do not disable caching
        );
    
}
function AfterMove(to) {
                if(false !== to) {
                    window.location = "index.php?pid=" + to;
                } else {
                    alert(errmove);
                }

}

// SLIDER!
//<![CDATA[
        var Slider = {
                glas: 8,            //transparent % (x*10)
                bgcolor: "#c00",    //Background color
                fontcolor: "#fff",  //Font color
                direct: "up",       //UP or Down direction
                moveto: 0,          //Move up/down to this value
                cssclass: "slider-box", // name CSS class
                hrefurl: "#",       //OnClick() goto URL
                Set: function(num) {
                    var slid = "slider" + num;
                    // Get a out layer position according to height
                    // own height
                    var sl = document.getElementById(slid);
                    var slstyle = sl.style;
                    var h = sl.clientHeight;
                    // parent height
                    var ph = sl.parentNode.clientHeight;
                    var phTOP = sl.parentNode.clientTop;
                    var moveto;
                    if(this.direct == "top") {
                        moveto = phTOP - h;
                    } else {
                        moveto = ph;
                    }
                    slstyle.top = moveto + "px";
                    slstyle.opacity = this.glas / 10,
                    slstyle.filter = 'alpha(opacity=' + this.glas * 10 + ')',
                    slstyle.background = this.bgcolor;
                    slstyle.color = this.fontcolor;
                    this.SetAct(num, this.hrefurl);
                },
                SetAct: function(num, hrefurl) {
                    var actdivhtml = '<div id="actiondiv' + num + '" class="slider-action" onmouseover="Slider.In(' + num + ');" ' +
                        'onmouseout="Slider.Out(' + num + ');"';
                    actdivhtml = (url.substr(0,1) == "/") ? actdivhtml + ' onclick="gotoURL(\'' + hrefurl + '\');"' :
                        actdivhtml + ' onclick="' + hrefurl + '"';
                    actdivhtml = actdivhtml + '></div>';
                    document.write(actdivhtml);
                    var actlayer = document.getElementById("actiondiv"+num);
                    var actlstyle = actlayer.style;
                    actlstyle.top = (-1 * actlayer.parentNode.clientHeight) + "px";
                    actlstyle.left = "0px"; // for Opera only!
                },
                In: function(num) {
                    var slid = "slider" + num;
                    var sl = document.getElementById(slid);
                    var h = sl.clientHeight;
                    var ph = sl.parentNode.clientHeight;
                    var phTOP = sl.parentNode.clientTop;
                    var start;
                    var moveto;
                    if(this.direct == "top") {
                        start = phTOP - h;
                        moveto = phTOP;
                    } else {
                        start = ph;
                        moveto = ph - h;
                    }
                    this.Into(num, start, moveto);
                },

                Out: function(num) {
                    var slid = "slider" + num;
                    var sl = document.getElementById(slid);
                    var h = sl.clientHeight;
                    var ph = sl.parentNode.clientHeight;
                    var phTOP = sl.parentNode.clientTop;
                    var start;
                    var moveto;
                    if(this.direct == "top") {
                        start = phTOP;
                        moveto = phTOP - h;
                    } else {
                        start = ph - h;
                        moveto = ph;
                    }
                    this.Into(num, start, moveto);
                },

                Into: function(num, start, moveto) {
                      var slid = "slider" + num;
                      var slstyle = document.getElementById(slid).style;
                      var t;
                       if(start != moveto) {
                            start = (start < moveto) ? start + 1 : start - 1;
                            slstyle.top = start + "px";
                            t = window.setTimeout(function(){Slider.Into(num, start, moveto)}, 10);
                        }
                        else
                        {
                            window.clearTimeout(t);
                        }
                }
        }

        function gotoURL(url) {
              window.location = url;
        }


