/******************************************************************************
*
* Filename:     js.js
*
* Description:  Javascript for popup windows, uploading indication, show/hide of
*               information and resizing photos to fit the browser.
*   
*
* Author:       David Burton
* Date:         19/02/2006
* Project:      DWW Gallery
*
* Revision:     1.0B
*
*
* URL:          http://gallery.dave-webwizard.com
*
* Copyright:    Copyright David Burton 2006
*
* License:      This file is part of the DWW Gallery
*
*               You should have received a copy of the GNU General Public
*               License along with the DWW Gallery if not,
*               write to the Free Software Foundation, Inc., 59 Temple
*               Place, Suite 330, Boston, MA  02111-1307  USA
*
*               If you require a different license for commercial or other
*               purposes, please contact the author: dave@dave-webwizard.com
*
******************************************************************************/
var newwindow = '';
function popup(url, fullscreen,name)
{
	if(!name)name="popup";
	var woptions='height=500,width=700'
	if(fullscreen){
		woptions='channelmode=yes,location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no,top=0,left=0';
		if(screen.pixelDepth)woptions+=',width='+screen.width+',height='+screen.height;
	}
	if (!newwindow.closed && newwindow.location)
	{
		newwindow.location.href = url;
	}
	else
	{
		newwindow=window.open(url,name,woptions);
		if (!newwindow.opener) newwindow.opener = self;
	}
	if (window.focus) {newwindow.focus()}
	return false;
}
var tt=Array();
function uploading(pos,dir)
{
	document.getElementById('error'+pos).innerHTML='';
	elem=document.getElementById('uploading'+pos);
	if(!dir){
		setOpacity(elem,10);
		dir=-1;
		elem.style.display='block';
		elem.style.marginTop='1.1em';
	}
	opac=getOpacity(elem);
	if(opac>10)dir=-1;
	if(opac<4)dir=+1;
	setOpacity(elem,opac+dir);
	tt[pos]=setTimeout("uploading("+pos+","+(dir)+")",300);
}

function clearUpload(id,pos){
	document.getElementById(id+pos).style.display='none';
	document.getElementById(id+pos).style.marginTop='0';
	clearTimeout(tt[pos]);
	tt[pos]=null;
}

function callUpload(form,galleryid, pos, rf){
	if(!tt[pos]){
		form.target='handleriframe'+pos;
		form.action='uploadphoto.php?id='+galleryid+'&pos='+pos+'&rf='+rf;
		uploading(pos);
		return true;
	}
	else return false;
}
function swapDisplay(div, link)
{
	if(div.style.display=='none'){
		div.style.display='block';
		link.className='clicked';
		link.title='Click here to contract';
	}
	else {
		div.style.display='none';
		link.className='';
		link.title='Click here to expand';
	}
}
function swaprev(div, link)
{
	if(div.style.display=='block' ){
		div.style.display='none';
		link.className='clicked';
	}
	else {
		div.style.display='block';
		link.className='';
	}
}

function resizer(photo,width,height){
var avail=winsize();
var ratio=photo.width/photo.height;
finalwidth=width;
if(width>avail[0]){
	finalwidth=avail[0];
	if((finalwidth/ratio)>avail[1])
		finalwidth=avail[1]*ratio;
}
if(height>avail[1]){
	finalwidth=avail[1]*ratio;
	if(finalwidth>avail[0])finalwidth=avail[0];
}
if(finalwidth>width)return;
if(finalwidth>570 || (finalwidth/ratio>450))photo.width=parseInt(finalwidth);
}

function winsize(){
    var availWidth;var availHeight;
    if (self.innerWidth) {availWidth = window.innerWidth-20;availHeight=window.innerHeight-18;}
    else {availWidth=document.documentElement.clientWidth-6;availHeight=document.documentElement.clientHeight-10;}
    return Array(availWidth,availHeight);
}
function inpage(elem,id,width,height){
	var preview=document.getElementById('preview');
	var output='';
	output+='<div id="previewmenu" oncontextmenu="return false;">';
	output+='<a href="javascript:;" onclick="document.getElementById(\'preview\').innerHTML=\'\';">Close</a>'
	output+=' <a href="showphoto.php?id='+id+'" onclick="if(!!document.getElementById(\'blog\')&&document.getElementById(\'blog\').value!=\'\')return confirm(\'Note: this may lose anything you have written.\');"\/>View<\/a>';
	output+=' <a href="exifview.php?id='+id+'" onclick="if(!!document.getElementById(\'blog\')&&document.getElementById(\'blog\').value!=\'\')return confirm(\'Note: this may lose anything you have written.\');" \/>Exif<\/a>';
	output+=' <a href="fullsize.php?id='+id+'" onclick="if(!!document.getElementById(\'blog\')&&document.getElementById(\'blog\').value!=\'\')return confirm(\'Note: this may lose anything you have written.\'_;" \/>Fullsize<\/a>';
	output+=' <a href="phpphoto.php?id='+id+'&amp;action=download" \/>Download<\/a>';
	output+='<\/div><a href="javascript:;" title="Click to close, right click for options" onclick="document.getElementById(\'preview\').innerHTML=\'\';" oncontextmenu="div=document.getElementById(\'previewmenu\');div.style.top=event.clientY+\'px\';div.style.left=event.clientX+\'px\';div.style.display=\'block\';return false;">';
	output+='<img src="'+elem.href+'" border="0" onload="resizer(this,'+width+','+height+');this.parentNode.style.width=this.width+\'px\';this.style.display=\'block\'" style="margin-left:auto;margin-right:auto;display:none;"\/><\/a>';
	preview.style.top=document.documentElement.scrollTop+'px';
	preview.innerHTML=output;
}
function dropdown(){
	
	if(document.getElementById('gallerylist').style.display=='' || document.getElementById('gallerylist').style.display=='none'){
	document.getElementById('gallerylist').style.display='block';}
	else {
	document.getElementById('gallerylist').style.display='none';			
	}
	
}

function selected(elem,url){
	document.location=url+elem.getElementsByTagName('option')[elem.selectedIndex].value;
}

f=null;
function fadeIn(elem){
	elem=document.getElementById(elem);
	elem.style.display='block';
	setOpacity(elem,0);
	var func=new function(){}
	f=window.setTimeout("fade('"+elem.id+"');",550);
}

function fadeOut(elem){
	elem=document.getElementById(elem);
	elem.style.display='none';
	clearTimeout(f);
}

function fade(elem,opac){
	elem=document.getElementById(elem);
	if(opac>10)return;
	if(typeof(opac)=='undefined'){
		setOpacity(elem,1);
		window.setTimeout("fade('"+elem.id+"',2);",20);
	} else {
		setOpacity(elem,opac);
		window.setTimeout("fade('"+elem.id+"',"+(opac+1)+");",30);
	}
}

function setOpacity(elem,opac){
	elem.style.opacity=''+(opac/10);
	elem.style.filter='alpha(opacity='+(10*opac)+')';		
}

function getOpacity(elem){
	return elem.style.opacity*10;	
}

function async(url, action, vars){
	var httpReq = getXMLHTTPObject();
	param='action='+action+'&variables='+vars;
	httpReq.open("POST", url,false);
    httpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	httpReq.readyState;
    try{httpReq.send(param);}
	catch (e){};
	httpReq.readyState;
	httpReq.status;
    if (httpReq.status == 200) {
    	if(httpReq.responseText)return httpReq.responseText;
        return true;
    } else {
        return false;
    }
}

function getXMLHTTPObject(){
    var httpReq = false;
    if (typeof XMLHttpRequest!='undefined') {
        httpReq = new XMLHttpRequest();
    } else {
        try {
            httpReq = new ActiveXObject("Msxml2.XMLHTTP.4.0");
        } catch (e) {
            try {
                httpReq = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (ee) {
                try {
                    httpReq = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (eee) {
                    httpReq = false;
                }
            }
        }
    }
    return httpReq;
}

function forceInt(elem,max,def){
	value=elem.value;
	value=value.replace(/\D/g,'');
	if(value.charAt(0)=='0')value=elem.value.substr(1);
	if(max && value>max)value=def;
	if(elem.value!=value)elem.value=value;
}

function checkenter(event,func){
	var keycode=event.keyCode;
	if(keycode==0)keycode=event.which;
	if(keycode==13){
		if(typeof(func)=='string')eval(func);
		else func();
		return false;}
	return true;
}
function forcefilesize(elem,val){
	if(!elem.value.match(/^\d+[M|K|B]$/g)){
		elem.value=elem.prev;
	}
}

