/*
This file is part of the business logic for

  Ann Helena Schlüter

Copyright 2007-2010 by Diedrich Vorberg <diedrich@tux4web.de>
Copyright 2007-2010 by Ann-Helena Schlüter <info@ann-helena-schlueter.de>

All Rights Reserved

The customer, Ann-Helena Schlüter, is granted the right to use this
and all related sourcecode in running and maintaining above website
now and in the future. For this purpose it may be modified at
will. However, publication and commercial distribution is restricted
as for any other peace of proprietary software.


Changelog
---------
$Log: layout.js,v $
Revision 1.16  2010-11-19 17:30:49  cvs
Isi am Telefon

Revision 1.15  2010-11-01 19:19:27  cvs
*** empty log message ***

Revision 1.14  2010-10-25 22:35:36  cvs
Funktioniert (wieder)
Die letzten Reste der alten Webseite sind unschädlich gemacht.

Revision 1.13  2010-10-20 18:07:26  cvs
Fertig (neues Design)

Revision 1.12  2010-10-16 13:58:50  cvs
Isis Aenderungen (1)

Revision 1.11  2010-07-30 15:45:56  t4w00-diedrich
Bugfixes.

Revision 1.10  2010-07-21 14:10:46  t4w00-diedrich
Aenderungen auf dem Server

Revision 1.9  2010-05-30 22:14:53  t4w00-diedrich
Letzte Änderungen

Revision 1.8  2010-05-30 14:04:29  t4w00-diedrich
Termine pflegen

Revision 1.7  2010-05-30 13:39:02  t4w00-diedrich
Fast fertig

Revision 1.6  2010-05-23 22:16:13  t4w00-diedrich
Fertig, so weit.

Revision 1.5  2010-05-23 19:47:28  t4w00-diedrich
CD Shop

Revision 1.4  2010-05-19 18:28:36  t4w00-diedrich
Fixed news database

Revision 1.3  2010-05-18 21:53:35  t4w00-diedrich
o Menu images on first level only
o ahdb

Revision 1.2  2010-05-07 21:15:20  t4w00-diedrich
Fast fertig, Termin db fehlt noch.

Revision 1.1.1.1  2010-05-04 21:19:38  t4w00-diedrich

*/
    
    
    
    
function recalc_sizes() {
    var container = document.getElementById("container");
    var content = document.getElementById("content");
    var menu = document.getElementById("menu");
    var foot = document.getElementById("foot");
    var margin = document.getElementById("margin");

    if ( margin.offsetHeight > content.offsetHeight )
    {
        content.style.height = margin.offsetHeight + "px";
    }
    
    // Is the container tall enough to fit the menu + the foot?
    if ( container.offsetHeight <
         menu.offsetTop + menu.offsetHeight + foot.offsetHeight )
    {
        container.style.height = (menu.offsetTop +
                                  menu.offsetHeight +
                                  foot.offsetHeight) + "px";
    }    
    
    
    // The foot's position
    foot.style.position = "absolute";
    foot.style.top = (container.offsetHeight - foot.offsetHeight) + "px";
    foot.style.visibility = "visible";
    
    var plummet = document.getElementById("plummet");
    var width = container.offsetWidth;
    
    container.style.background = "transparent";
}



function max(a, b) {
    if (a > b) {
		return a;
    }
    else {
		return b;
    }
}

// document.observe('dom:loaded', recalc_sizes);


/* Reload all images with the src= set to a value that contains the current
 * physical extends in pixels. If it's an RImage the server will provide a
 * nicely scaled version of the image. */


function windows_image_improver()
{
    // Get all the images
    var images = document.getElementsByTagName("img");

    // Traverse the images and find those with class= need-improvement
    for(var a = 0; a < images.length; a++)
    {
        var img = images[a];
        var cls = img.className;

        if ( cls.indexOf("no-improvement") == -1 &&
             img.width > 0 && img.height > 0 )
        {
			var scaleto = img.width + "x" + img.height;
			
			var url = img.src;
			var parts = url.split("?");
			var base = parts[0];
            var params = $H(url.toQueryParams());

            params.unset("size");
            params.unset("preview");
            params.unset("scaleto");
            
            params.set("scaleto", scaleto);
            
            // Images that are .png files or smaller than 200x200 pixels
            // in terms of area, are requested in .png format.
			if ( img.src.indexOf(".png") != -1 ||
                        img.width * img.height <= 200*200 ) {
				params.set("png", "1");
			}
			
			url = base + "?" + params.toQueryString();
            
			img.src = url;
		}
    }
}

document.observe('dom:loaded', windows_image_improver);




function player_popup()
{
    var newwindow = window.open ("/player_popup", "PopupPlayer",
        "menubar=0,scrollbars=0,status=0,toolbar=0,resizeable=0,scrollbars=0,width=279,height=226");
    
    if (window.focus) {newwindow.focus()};
}

function resize_event(body)
{
    var body = document.getElementsByTagName("body")[0];
    var plumet = document.getElementById("plummet");
    var container = document.getElementById("container");

    var cls = "";
    
    if ( container.offsetWidth < body.offsetWidth )
    {
        cls = "center";
    }
    else
    {
        cls = "left";
    }

    container.className = cls;
    plumet.className = cls;
};

function reload_menu()
{
    new Ajax.Request(context_url + "/reload_menu",
                     {  
                         method: "get",
                         asynchronous: false,
                         onSuccess: function(transport) {
                             $("menu").innerHTML = transport.responseText;
                         },
                         onFailure: function(){ }
                     });
}


function set_menu_image(img, url)
{
    img.src = url += "?scaleto=" + img.width + "x" + img.height + "&png=1";
}

function goto_month(dir)
{
    var select = $("month");
    var current_option = select.selectedIndex;
    current_option += dir;
    if ( current_option < 0 ) current_option = 0;
    if ( current_option > select.options.length - 1 )
    {
        current_option = select.options.length - 1
    }
    
    select.selectedIndex = current_option;

    update_news_overview();
    
    // var params = $H({place: $F("place"), month: $F("month")});
    // var url = portal_url + "/termine?" + params.toQueryString();
    // window.location = url;
}

function update_news_overview()
{
    var params = $H({place: $F("place"), month: $F("month")});
    
    new Ajax.Request(context_url +
                     "/overview_content?" + params.toQueryString(),
                     {  
                         method: "GET",
                         asynchronous: false,
                         onSuccess: function(response) {
                             $("news-overview").innerHTML = response.responseText;
                             recalc_sizes();
                         },
                         onFailure: function(){ alert('AJAX ERROR...') },
                     });
    
}

function update_place()
{
    var params = $H({place: $F("place")});
    window.location = context_url + "/overview?" + params.toQueryString();
}

set_cookie = function ( name, value, expires,
                        path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/* if the expires variable is set, make the correct expires time,
	the current script below will set it for x number of days, to make
	it for hours, delete * 24, for minutes, delete * 60 * 24 */
	
	if ( !expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );

}


function init_mp3player()
{
    var div = $("player");
    if ( div )
    {
        var flashvars = { skin: portal_url + "/player_skins/nobius_grey/skin.xml",
                          playlist: context_url + "/mp3playlist.xml",
                          autoplay: "false",
                          shuffle: "false",
                          repeat: "true",
                          buffertime: "2",
                          key: "XDDON4FW5X606VFOMV8F"
                        };
        
        var params = { allowScriptAccess: "always",
                       bgcolor: "#f5f5e9",
                       wmode: "transparent" };
        
        var attributes = { };
        
        var so = new swfobject.embedSWF(
            portal_url + "/ep_player.swf",
            "player",
            div.offsetWidth,
            div.offsetHeight,
            "10.0.0",
            portal_url + '/expressInstall.swf',
            flashvars,
            params,
            attributes);
    }
}

document.observe('dom:loaded', init_mp3player);

function player_popup()
{
    var newwindow = window.open (portal_url + "/player_popup.html", "PopupPlayer",
        "menubar=0,scrollbars=0,status=0,toolbar=0,resizeable=0,scrollbars=0,width=279,height=226");
    
    if (window.focus) {newwindow.focus()};
}


function ask(question, href)
{
    if (window.confirm(question))
    {
		window.location.href = href;
    }
}

function fix_position()
{
    var zenith = document.getElementById("plummet");
    var ship = document.getElementById("container");

    var width;
    if ( typeof window.innerWidth == "undefined" )
    {
        // MSIE
        width = document.body.offsetWidth;
    }
    else
    {
        // Gute Browser
        width = window.innerWidth;
    }
        

    if ( ship.offsetWidth > width ) {
        zenith.style.left = (ship.offsetWidth/2) + "px";
    } else {
        zenith.style.left = "50%";
    }            
}

document.observe("dom:loaded", fix_position);
document.observe("dom:resize", fix_position);



