/*
 * file:	functions.js
 * autor:	sven.georgi@unister-gmbh.de
 *
 */
// behavior to save the cookie on window closing
var bodyRules = {
    'window': function(element){
        element.onclose = function(){
            //cookie.save();
            if ($('engine')) 
                $('engine').value = '';
            if ($('category')) 
                $('category').value = '';
        }, element.onresize = function(){
        
            windowResize();
        }
    }
}
Attitude.append(bodyRules);

// behavior to change the background an save value in cookie  user/changebgr.tpl
var bgrImgRules = {
    '#backgrounds a': function(element){
        element.onclick = function(){
            if (element.id != 'prevbackground' && element.id != 'nextbackground' && element.id != 'uploadimg') {
                // Src des Child-Img's auslesen
                var imgSrc = $(element).childNodes[0].src.toString();
                
                imgSrc = imgSrc.slice(imgSrc.indexOf('?') + 1, imgSrc.indexOf(','));
                //alert(imgSrc);
                
                // changes the background of the whole body
                imgSrc = imgSrc.replace(/images\//, "");
                imgSrc = imgSrc.replace(/image\//, "");
                /*
                 imgSrc = imgSrc.replace(/images/, "images/backgrounds/" + getWindowWidth());
                 if (document.body){
                 document.body.style.backgroundImage='url('+ home_url + imgSrc + ')';
                 }*/
                new Ajax.Request(home_url + 'Background/set/width/' + screen.width + '/image/' + imgSrc + '/', {
                    method: 'get',
                    onComplete: function(response){
                        //alert(response.responseText);
                        document.body.style.background = response.responseText;
						 //cookie.save(response.responseText);
                        
                    }
                });
                
                //changeBgrImg(imgSrc);
               
                return false;
            }
        }
    },
    'a#prevbackground': function(element){
        element.onclick = function(){
            var url = $('prevbackground').href + '&ajax=true';
            var parameters = '';
            new Loader('bgrloader', '', url, parameters);
            return false;
        }
    },
    'a#nextbackground': function(element){
        element.onclick = function(){
            var url = $('nextbackground').href + '&ajax=true';
            var parameters = '';
            new Loader('bgrloader', '', url, parameters);
            return false;
        }
    }
}
//Attitude.append(bgrImgRules);

// behaviors of the search form in index/searchform.tpl
var formRules = {
    /*
     'form#search' : function(form) {
     form.onsubmit = function(){
     var url = this.action + "?ajax=true";
     //	this.action = this.action + "?" + Form.serialize(this);
     $('pagelink').value="";
     var parameters = Form.serialize(this);
     new Loader('loader', '', url, parameters);
     return false;
     }
     }, */
    'form#search2': function(form){
        form.onsubmit = function(){
            loadme();
            $('pagelink').value = "";
            $('stext').value = $('stext2').value;
            if ($F('stext') != "") {
                var oldhref = this.href;
                
                var params = Form.serialize('search');
                var act = home_url + "search/?" + params;
                location.href = act;
                return false;
            }
        }
    }
}

var pagingRules = {
    '#pagination a': function(element){
        element.onclick = function(){
            loadme();
            var value = this.href.substr(this.href.indexOf('pagelink') + 9);
            var oldhref = this.href;
            $('pagelink').value = value;
            var params = Form.serialize('search');
            var act = home_url + "search/?" + params;
            location.href = act;
            return false;
        }
    }
}
Attitude.append(pagingRules);
// behaviors of elements in index/menu_search.tpl
var engineRules = {
    '#engines a': function(element){
    
        element.onclick = function(){
        
            if ($(element).id == 'home') 
                return true;
            for (i = 0; i < engines.length; i++) {
                var eek = engines[i];
                if ($(engines[i])) 
                    $(engines[i]).className = "";
            }
            $(element).className = "active";
            $('engine').value = this.id;
            
            if ($F('stext') != "") {
                //$('search').action = $('search').action + "&" + Form.serialize('search');
                loadme();
                var params = Form.serialize('search');
                
                //$('form#search').submit();
                var act = home_url + "search/?" + params;
                //$('search').submit();
                //alert($('search').action);
                document.location = act;
                //$('search').onsubmit();
                //document.search.submit();
            }
            return false;
        }
    },
    '#categories a': function(element){
        element.onclick = function(){
        
            for (t = 0; t <= 3; t++) {
                $("category" + t).style.fontWeight = "normal";
            }
            
            if (this.id != 'category0') {
                $('mix').onclick();
                
            }
            
            $(element).style.fontWeight = "bold";
            $('category').value = this.id.substr(this.id.length - 1);
            
            
            if ($F('stext') != "") {
                loadme();
                var params = Form.serialize('search');
                var act = home_url + "search/?" + params;
                document.location = act;
            }
            
            return false;
        }
    },
    'a#advanced': function(element){
        element.onclick = function(){
            if ($('advancedopts').style.display == 'block') {
                $('advancedopts').style.display = "none"
            }
            else {
                $('advancedopts').style.display = "block"
            }
            return false;
        }
    }
}
//Attitude.append(engineRules);	

var engineRules2 = {
    'a#advanced2 a': function(element){
        element.onclick = function(){
            if ($('advancedopts2').style.display == 'block') {
                $('advancedopts2').style.display = "none"
            }
            else {
                $('advancedopts2').style.display = "block"
            }
            return false;
        }
    }
}

function getWindowWidth(){
    if (document.body) {
        width = getPageSize();
        /*
         if (width[0]<=800)
         widthNew = 800;
         else if (width[0] <=1024)
         widthNew = 1024;
         else if (width[0] <=1280)
         widthNew = 1280;
         else if (width[0] <=1440)
         widthNew = 1440;
         else if (width[0] <=1680)
         widthNew = 1680;
         else if (width[0] <=1920)
         widthNew = 1920;
         else
         widthNew = 2560;
         */
        if (width[0] <= 800) 
            widthNew = 800;
        else 
            if (width[0] <= 800) 
                widthNew = 1024;
            else 
                if (width[0] <= 1024) 
                    widthNew = 1280;
                else 
                    if (width[0] <= 1280) 
                        widthNew = 1440;
                    else 
                        if (width[0] <= 1440) 
                            widthNew = 1680;
                        else 
                            if (width[0] <= 1680) 
                                widthNew = 1920;
                            else 
                                widthNew = 2560;
        return widthNew;
    }
}/*

function test(){
    // stretch overlay to fill page and fade in
    var arrayPageSize = getPageSize();
    $('overlay').width = arrayPageSize[0];
    $('overlay').height = arrayPageSize[1];
    
    new Effect.SlideDown('overlay', {
        duration: 0.2,
        from: 0.0,
        to: 0.8
    });
}
*/
function loadme(){
    $('loadme').style.display = 'block';
}

function getPageSize(){

    var xScroll, yScroll;
    
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    }
    else 
        if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        }
        else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }
    
    var windowWidth, windowHeight;
    
    if (self.innerHeight) { // all except Explorer
        if (document.documentElement.clientWidth) {
            windowWidth = document.documentElement.clientWidth;
        }
        else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    }
    else 
        if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        }
        else 
            if (document.body) { // other Explorers
                windowWidth = document.body.clientWidth;
                windowHeight = document.body.clientHeight;
            }
    
    // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    }
    else {
        pageHeight = yScroll;
    }
    
    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {
        pageWidth = xScroll;
    }
    else {
        pageWidth = windowWidth;
    }
    
    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
    return arrayPageSize;
}
