/* Paricida, sacrilege, diva amabilis... */
function killChildren(whose)
{
    while (whose.childNodes.length > 0) {
        whose.removeChild(whose.childNodes[0]);
    }
}

/* Ad meliorem, in aeternum */
function getText(node)
{
    var result = '';
    for (var i=0; i<node.childNodes.length; ++i) {
        var n = node.childNodes[i];
        if (3 == n.nodeType) {  //Text node
            result += n.nodeValue;
        }
        else if (1 == n.nodeType && 'script' != n.nodeName.toLowerCase()) { //Element node
            result += getText(n);
        }
    }
    return result;
}


/** @params
*** 
*** path=url 
*** winname=window 
*** name 
*** W=width 
*** H=height 
*** M=menubar 
*** S=scrollbars 
*** R=resizeable
**/

function popup(path,winname,W,H,M,S,b,f, fullscreen, resizable)
{
    var brd = b||0;
    var tsz = 20;
    f = f||0;
    fullscreen =  fullscreen||0;
    resizable = resizable||0;
    var c=(document.all&&navigator.userAgent.indexOf("Win")!=-1)?1:0
    var w=window.screen.width / 2; var h=window.screen.height / 2;
    var W=W||w; W=(typeof(W)=='string'?Math.ceil(parseInt(W)*w/100):W); W+=(brd*2+2)*c; W += f;
    var H=H||h; H=(typeof(H)=='string'?Math.ceil(parseInt(H)*h/100):H); H+=(tsz+brd+2)*c; H += f;
    var X=X||Math.ceil((w-W/2))
    var Y=Y||Math.ceil((h-H/2))
    
    var hPopup = window.open(path, null, "fullscreen="+fullscreen+",left="+X+",top="+Y+",width="+W+",height="+H+",menubar="+M+",toolbar=0,scrollbars="+S+",resizable=" + resizable); 
    hPopup.focus();
}


function showSlideshow(author, galleryid)
{
	if(typeof(_gaq) !== "undefined")
	{
		_gaq.push(['_trackEvent', 'Show Slideshow', 'Show Slideshow']);
	}
    var url = base_href + '/' + author  + '/'  + galleryid +  '/' + 'slideshow';
    if(window.navigate){
    	window.navigate(url);
    } else if(window.location)
    {
    	window.location = url;
    }
    //popup(url, 'slideshow'+ author  + galleryid,(screen.width-5), (screen.height-30), 0 , 0, 0, 0, 1, 1);
}

function showMySpace(id)
{
    var url = base_href + '/myspace.php?id=' + id
    popup(url, 'MySpace', 700, 375);
}


function share(id)
{
    var url = base_href + '/share.php?id=' + id
    popup(url, 'ShareWithFriend', 400, 500 , 0 , 1);
}


function galleryProtect(id)
{
    var url = base_href + '/gallery_protect.php?id=' + id
    popup(url, 'ProtectGallery', 400, 200);
}

function bookMark(id, mid)
{
	if( mid != ''){
    	xajax_bookmark(id);
	} else {
		addToFavorites();
	}
    return void(0);
}



function addToFavorites() {
	if ((window.sidebar) && (window.sidebar.addPanel)) {
        window.sidebar.addPanel(document.title,window.location,""); 
	} else if (window.external) {
		window.external.AddFavorite(window.location, document.title ) 
	} else { 
		alert(browser_support); 
	}
 }
function checkType()
{
    var name = document.uf.img.value;
    var md   = document.getElementById('upmes');

    if (name == '')
    {
        return false;
    }

    var t = name.split('.');
    var extention = t[(t.length - 1)];
    var check = false;

    for (i in allowed)
    {
        if (allowed[i] == extention.toLowerCase())
        {
            check = true;
        }
    }

    if (!check)
    {
        md.innerHTML = unsupported_type;
    }
    else
    {
        md.innerHTML = '';
    }
    return check;
}



function deleteGalleryCallback(reloadPage)
{
	var adr = new String(window.location);
	if(reloadPage)
	{
		window.location = adr;
		return;
	}
	
	var page = adr.gsub(".*fotoalbum\.eu/[a-zA-Z]+/?", "");
	galleryArray = $$('#galleries .album')
	if(galleryArray.size() == 1)
	{
		var firstGallery  = galleryArray[0];
		var firstId = firstGallery.id.gsub("gallery_", "");
		var linksHolder = $('moveLinksHolder_'+firstId);
		linksHolder.innerHTML = '';
	}
	
	if(galleryArray.size() > 1)
	{
		var firstGallery  = galleryArray[0];
		var firstId = firstGallery.id.gsub("gallery_", "");
		var moveUpImg = $('img_up_'+firstId);
		if(moveUpImg != null && !(page > 1))
		{
			moveUpImg.hide();
		}

		var lastGallery  = galleryArray[galleryArray.size()-1];
		var lastId = lastGallery.id.gsub("gallery_", "");
		var moveDownImg = $('img_down_'+lastId);
		var moveDownLink = $('link_down_'+lastId);
		if(moveDownImg != null && globalLastPage)
		{
			moveDownImg.hide();
		}
		if(moveDownLink != null && globalLastPage)
		{
			moveDownLink.id = 'link_up_'+lastId;
			moveDownLink.href = "javascript:moveGal("+lastId+",0, 'up');"
		}
	}
	if(galleryArray.size() == 0){
		window.location.href = returnal;
	}
}


function getViewport(){
	 var viewportwidth;
	 var viewportheight;
	 
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 
	 if (typeof window.innerWidth != 'undefined'){
	      viewportwidth = window.innerWidth - document.body.scrollLeft,
	      viewportheight = window.innerHeight - document.body.scrollTop
	 }
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	
	 else if (typeof document.documentElement != 'undefined'
	     && typeof document.documentElement.clientWidth !=
	     'undefined' && document.documentElement.clientWidth != 0)
	 {
	       viewportwidth = document.documentElement.clientWidth,
	       viewportheight = document.documentElement.clientHeight
	 }
	 
	 // older versions of IE
	 
	 else{
	       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
	       viewportheight = document.getElementsByTagName('body')[0].clientHeight
	 }
	 
	 return {width:viewportwidth,height:viewportheight};
}

function closeModal() {
	// Opera and IE6 fix.
	// Test with Opera and IE6 if the following lines change.
	$('popup').setStyle({top: '-10000px'});
	$('shadow').hide();
}

function adjustModalPosition() {
	var popup = $('popup');

	if (popup.getStyle('position') == 'absolute')
	{
		if (popup)
		{
			popup.setStyle({
				marginTop: (document.documentElement.scrollTop+(-popup.getHeight()/2))+'px'
			});
		}
		
		$('shadow').setStyle({
			width: document.documentElement.clientWidth + 'px',
			height: (document.documentElement.scrollTop + 20 + document.documentElement.clientHeight) + 'px'});
	}
	else
	{
		if (popup)
		{
			popup.setStyle({
				marginTop: (-popup.getHeight()/2)+'px'
			});
		}
	}
}

function submitForm(form)
{
	if (form.onsubmit)
	{
		form.onsubmit();
	}
	else
	{
		form.submit();
	}
}

function confirmAndAction(message, callback, params)
{
	if (confirm(message))
	{
		callback.apply(this, params);
	}
}

function showBrowser()
{
	  alert(Object.toJSON(Prototype.Browser));
}

