
/**
 * Set cookie with automatically escaped value (= php urlencode)
 */
function Set_Cookie( name, value, expires, path, domain, secure ) {
    Set_Raw_Cookie(name, escape( value ), expires, path, domain, secure);
}

/**
 * set cookie without escaping the cookies' value 
 */ 
function Set_Raw_Cookie( 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 + "=" + value +
        ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
        ( ( path ) ? ";path=" + path : "" ) +
        ( ( domain ) ? ";domain=" + domain : "" ) +
        ( ( secure ) ? ";secure" : "" );
}


// Image Rollover + Preload
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function getInnerWindowHeight()
{
    var window_Height = 0;
    if ( typeof( window.innerHeight ) == 'number' )
       window_Height = window.innerHeight;
    else
    if (
         document.documentElement
      && document.documentElement.clientHeight
       )
       window_Height = document.documentElement.clientHeight;
    else
    if (
         document.body
      && document.body.clientHeight
       )
       window_Height = document.body.clientHeight;

    return window_Height;
}

function getInnerWindowWidth() {
    return document.body.clientWidth;
    var myWidth = 0;
    if (typeof window.innerWidth != 'undefined')
    {
      myWidth = window.innerWidth;
    }

    // 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)
    {
       myWidth = document.documentElement.clientWidth;
    }

    // older versions of IE

    else
    {
        myWidth = document.getElementsByTagName('body')[0].clientWidth;
    }

  return myWidth;
}

// Location Quick Select
function showLocationQuickSelect ()
{
    $('selectLocationLink').style.visibility = 'hidden';
    $('locationQuickSelect').style.display = 'block';
}

Event.observe(window, 'load', function(){
    if ($('locationQuickSelect') == null) {
        return;
    }
    hideOnMouseout($('locationQuickSelect'), function(){
        $('selectLocationLink').style.visibility = 'visible';
    });
});

// STARTSEITE (Location Auswahl)
function showHideCities ( city )
{
    var welcome                =    $("containerWelcome");

    var hamburg                =     $("hamburgOver");
    var containerHamburg     =     $("containerHamburg");

    var newyork                =     $("newyorkOver");
    var containerNewYork     =     $("containerNewyork");

    var paris                 =     $("parisOver");
    var containerParis         =     $("containerParis");


    function killTheCities()
    {
        welcome.style.display = "none";
        hamburg.style.display = "none";
        containerHamburg.style.display = "none";
        newyork.style.display = "none";
        containerNewYork.style.display = "none";
        paris.style.display = "none";
        containerParis.style.display = "none";
        MM_swapImgRestore();
    }


    switch ( city )
    {
        case "hamburg":
            killTheCities();
            hamburg.style.display = "block";
            containerHamburg.style.display = "block";
            break;
        case "newyork":
            killTheCities();
            newyork.style.display = "block";
            containerNewYork.style.display = "block";
            break;
        case "paris":
            killTheCities();
            paris.style.display = "block";
            containerParis.style.display = "block";
            break;
    }
}

// JS init on window load; put all windows.onload() - stuff here

Event.observe( window, 'load', function() {
    // Preload Images
    MM_preloadImages('images/konstrukt/locations/hamburg/europe-hamburg_over.gif','images/konstrukt/locations/newyork/newyork_over.gif','images/konstrukt/locations/paris/paris_over.gif');
});


/**
 * Sets style="display: none;" when mouse leaves the outer edge of the element
 *
 * @param domelement elem - element to hide when it is left
 * @param function whatelseshouldidomylord - function to be executed after hiding of elem
 */
function hideOnMouseout(elem, whatelseshouldidomylord) {
    var elem = $(elem);
    Event.observe(elem, 'mouseout', function(e){
        var tg = Event.element(e);

        if ( tg != elem ) {
            return;
        }

        var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;

        // check that the target is not inside our layer
        if ($(reltg).descendantOf(tg)) {
            return;
        }
        elem.style.display = 'none';
        if (whatelseshouldidomylord) {
            whatelseshouldidomylord();
        }
    });
}

/**
 * Function used to forward to the image details page for a clicked image,
 * called from the pages 'searchResult' and the different 'lightboxes'
 */

function showImageDetail(id_image, location, wherefrom) {
    document.searchResults.action = 'index.php?cont=imageDetail&location=' + location + '&id_image=' + id_image + '&wherefrom=' + wherefrom;
    document.searchResults.submit();
}
