// Copyright (C) 2005-2008 Ilya S. Lyubinskiy. All rights reserved.
// Technical support: http://www.php-development.ru/
//
// YOU MAY NOT
// (1) Remove or modify this copyright notice.
// (2) Re-distribute this code or any part of it.
//     Instead, you may link to the homepage of this code:
//     http://www.php-development.ru/javascripts/popup-window.php
//
// YOU MAY
// (1) Use this code on your website.
// (2) Use this code as part of another product.
//
// NO WARRANTY
// This code is provided "as is" without warranty of any kind.
// You expressly acknowledge and agree that use of this code is at your own risk.

// USAGE
//
// function popup_show(id, drag_id, exit_id, position, x, y, position_id)
//
// id          - id of a popup window;
// drag_id     - id of an element within popup window intended for dragging it
// exit_id     - id of an element within popup window intended for hiding it
// position    - positioning type:
//               "element", "element-right", "element-bottom", "mouse",
//               "screen-top-left", "screen-center", "screen-bottom-right"
// x, y        - offset
// position_id - id of an element relative to which popup window will be positioned


// ***** Variables *************************************************************

var siteUrl="http://www.steinbrucke.org/";
var popup_dragging = false;
var popup_target;
var popup_mouseX;
var popup_mouseY;
var popup_mouseposX;
var popup_mouseposY;
var popup_oldfunction;
var Gid;
var gdrag_id;
var gexit_id;
var gposition;
var gx;
var gy;
var gelid;
var gposition_id;
var gurl;
var img1;


// ***** popup_mousedown *******************************************************

function popup_mousedown(e)
{
  var ie = navigator.appName == "Microsoft Internet Explorer";

  popup_mouseposX = ie ? window.event.clientX : e.clientX;
  popup_mouseposY = ie ? window.event.clientY : e.clientY;
}


// ***** popup_mousedown_window ************************************************

function popup_mousedown_window(e)
{
  var ie = navigator.appName == "Microsoft Internet Explorer";

  if ( ie && window.event.button != 1) return;
  if (!ie && e.button            != 0) return;

  popup_dragging = true;
  popup_target   = this['target'];
  popup_mouseX   = ie ? window.event.clientX : e.clientX;
  popup_mouseY   = ie ? window.event.clientY : e.clientY;

  if (ie)
       popup_oldfunction = document.onselectstart;
  else popup_oldfunction = document.onmousedown;

  if (ie)
       document.onselectstart = new Function("return false;");
  else document.onmousedown   = new Function("return false;");
}


// ***** popup_mousemove *******************************************************

function popup_mousemove(e)
{
  var ie      = navigator.appName == "Microsoft Internet Explorer";
  var element = document.getElementById(popup_target);
  var mouseX  = ie ? window.event.clientX : e.clientX;
  var mouseY  = ie ? window.event.clientY : e.clientY;

  if (!popup_dragging) return;

  element.style.left = (element.offsetLeft+mouseX-popup_mouseX)+'px';
  element.style.top  = (element.offsetTop +mouseY-popup_mouseY)+'px';

  popup_mouseX = ie ? window.event.clientX : e.clientX;
  popup_mouseY = ie ? window.event.clientY : e.clientY;
}

// ***** popup_mouseup *********************************************************

function popup_mouseup(e)
{
  var ie      = navigator.appName == "Microsoft Internet Explorer";
  var element = document.getElementById(popup_target);

  if (!popup_dragging) return;

  popup_dragging = false;

  if (ie)
       document.onselectstart = popup_oldfunction;
  else document.onmousedown   = popup_oldfunction;
}

// ***** popup_exit ************************************************************

function popup_exit(e)
{
  var ie      = navigator.appName == "Microsoft Internet Explorer";
  var element = document.getElementById(popup_target);

  //popup_mouseup(e);
  element.style.display = 'none';
}


// ***** popup_show ************************************************************
function ajax_popup(url)
{
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open("GET",url,false);
	xmlhttp.send(null);
	return xmlhttp.responseText;
}

function popup_translate(url)
{
	document.getElementById('popup_tra').innerHTML=ajax_popup(url);
}

function $_GET(q,s) {
    s = (s) ? s : window.location.search;
    var re = new RegExp('&amp;'+q+'=([^&amp;]*)','i');
    return (s=s.replace(/^\?/,'&amp;').match(re)) ?s=s[1] :s='';
}

function popup_imgShow(id, drag_id, exit_id, position, x, y,elid,position_id)
{
	var url=siteUrl+"libs/php/pages/ImageShow.php?lang="+$_GET('lang')+"&tr="+elid+"&pict="+document.getElementById(elid).getAttribute("src");
	var defaUrl=siteUrl+"libs/php/pages/defImage.php";
	//popup_showG(id,drag_id,exit_id,position,x,y,elid,position_id,defaUrl);
	var imgSrc=document.getElementById(elid).getAttribute("src");
	var imgSpl=imgSrc.split("/thumb");
	var newSrc=siteUrl+imgSpl[0]+"/low"+imgSpl[1];
	
	Gid=id;
	gdrag_id=drag_id;
	gexit_id=exit_id;
	gposition=position;
	gx=x;
	gy=y;
	gelid=elid;
	gposition_id=position_id;
	gurl=url;
    img1 = new Image();
	img1.src = newSrc;
	timeOut();
	
}
function timeOut()
{

	if(img1.complete)
	{
	popup_showG(Gid,gdrag_id,gexit_id,gposition,gx,gy,gelid,gposition_id,gurl);
	}
	else
	{
		setTimeout("timeOut()",10);
	}
}
function popup_show(id, drag_id, exit_id, position, x, y,trid,position_id)
{
	var url=siteUrl+"libs/php/pages/popup.php?lang="+$_GET('lang')+"&tr="+trid;
	popup_showG(id, drag_id, exit_id, position, x, y,trid,position_id,url);
}

function popup_course_details(id, drag_id, exit_id, position, x, y,trid,position_id)
{
	// alert('popup_course_details');
	var url=siteUrl+"libs/php/pages/popup_course_details.php?lang="+$_GET('lang')+"&cr_det_id="+trid;
	popup_showG(id, drag_id, exit_id, position, x, y,trid,position_id,url);
}

function popup_img(id, drag_id, exit_id, position, x, y,trid,position_id,Width,Height,Type)
{
	var url=siteUrl+"libs/php/pages/upload_crop.php?lang="+$_GET('lang')+"&Img="+position_id+"&Wdth="+Width+"&Hgth="+Height+"&Type="+Type;
	popup_showG(id, drag_id, exit_id, position, x, y,trid,position_id,url);
}
function popup_basic(id, drag_id, exit_id, position, x, y,trid,position_id,Width,Height,Type)
{
	var url=siteUrl+"libs/php/pages/upload_basic.php?lang="+$_GET('lang')+"&Img="+position_id+"&Wdth="+Width+"&Hgth="+Height+"&Type="+Type;
	popup_showG(id, drag_id, exit_id, position, x, y,trid,position_id,url);
}

function popup_uploading(id, drag_id, exit_id, position, x, y,trid,position_id)
{

var doccc=document.getElementById("userfile").value;
alert(doccc);
	var url=siteUrl+"libs/php/pages/upload_popup_action.php?lang="+$_GET('lang')+"&pat="+doccc;
	popup_showG(id, drag_id, exit_id, position, x, y,trid,position_id,url);

}

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;
}

function popup_showG(id, drag_id, exit_id, position, x, y,trid,position_id,url)
{
popup_translate(url);

//alert('hidhgfhgdhf');
	
  var element      = document.getElementById(id);
  var drag_element = document.getElementById(drag_id);
  var exit_element = document.getElementById(exit_id);

  var width        =f_clientWidth();
  var height       = f_clientHeight();

  element.style.position = "absolute";
  element.style.display  = "block";

  if (position == "element" || position == "element-right" || position == "element-bottom")
  {
    var position_element = document.getElementById(position_id);

    for (var p = position_element; p; p = p.offsetParent)
      if (p.style.position != 'absolute')
      {
        x += p.offsetLeft;
        y += p.offsetTop;
      }

    if (position == "element-right" ) x += position_element.clientWidth;
    if (position == "element-bottom") y += position_element.clientHeight;

    element.style.left = x+'px';
    element.style.top  = y+'px';
  }

  if (position == "mouse")
  {
    element.style.left = (document.documentElement.scrollLeft+popup_mouseposX+x)+'px';
    element.style.top  = (document.documentElement.scrollTop +popup_mouseposY+y)+'px';
  }

  if (position == "screen-top-left")
  {
    element.style.left = (document.documentElement.scrollLeft+x)+'px';
    element.style.top  = (document.documentElement.scrollTop +y)+'px';
  }

  if (position == "screen-center")
  {
    element.style.left = (f_scrollLeft()+(width -element.clientWidth )/2+x)+'px';
    element.style.top  = (f_scrollTop()+(height-element.clientHeight)/2+y)+'px';
  }

  if (position == "screen-bottom-right")
  {
    element.style.left = (document.documentElement.scrollLeft+(width -element.clientWidth )  +x)+'px';
    element.style.top  = (document.documentElement.scrollTop +(height-element.clientHeight)  +y)+'px';
  }

  drag_element['target']   = id;
  drag_element.onmousedown = popup_mousedown_window;

  exit_element.onclick     = popup_exit;
}

function popup_post()
{
	var url=siteUrl+"libs/php/pages/popup_process.php?tr="+document.getElementById('trid').value;
	var result=ajax_popup(url);
	alert(result);
	popup_exit(document.getElementById(id));
}

function showUpload()
{
document.getElementById('newDiv').style.display='block';
}
function directShow(id)
{
window.location.replace(siteUrl+"libs/php/pages/demo-crop.php?ImgId="+id);
}
// ***** Attach Events *********************************************************

if (navigator.appName == "Microsoft Internet Explorer")
     document.attachEvent   ('onmousedown', popup_mousedown);
else document.addEventListener('mousedown', popup_mousedown, false);

if (navigator.appName == "Microsoft Internet Explorer")
     document.attachEvent   ('onmousemove', popup_mousemove);
else document.addEventListener('mousemove', popup_mousemove, false);

if (navigator.appName == "Microsoft Internet Explorer")
     document.attachEvent   ('onmouseup', popup_mouseup);
else document.addEventListener('mouseup', popup_mouseup, false);




