/*
 * 3ip.js
 * 
 * Default functions
 *
 * Author: Klaas Schaafsma <klaas <at> 3ip <dot> nl>
 * Copyright: 2008 - 3IP Web Solutions B.V.
 */
 
//new PeriodicalExecuter(changeBackground, 8);
 
function changeBackground()
{
    // The backgrounds
    this.backgrounds = ["images/home/6.jpg",
    "images/home/7.jpg",
    "images/home/8.jpg",
    "images/home/9.jpg",
    "images/home/10.jpg"];
 
    // Fetch random image
    var random = Math.floor(Math.random()*this.backgrounds.length);
    
    var image = this.backgrounds[random];
    
    $('container').setStyle({
        background: '#fff url('+ image +') no-repeat 5px 5px'
    });
    
}

/**
 * slideShow(container, timeout)
 *
 * Create a slideshow from a div containing div's with images
 *
 * Script taken from blog.remvee.net
 *
 */
function Slideshow(slideshow, timeout) {
    this.slides = [];
    var nl = $(slideshow).getElementsByTagName('div');
    for (var i = 0; i < nl.length; i++) {
        if (Element.hasClassName(nl[i], 'slide')) {
            this.slides.push(nl[i]);
        }
    }
    this.timeout = timeout;
    this.current = 0;

    for (var i = 0; i < this.slides.length; i++) {
        this.slides[i].style.zIndex = this.slides.length - i;
    }

    Element.show(slideshow);
    setTimeout((function(){
        this.next();
    }).bind(this), this.timeout + 1000);
}

Slideshow.prototype = {
    next: function() {
        for (var i = 0; i < this.slides.length; i++) {
            var slide = this.slides[(this.current + i) % this.slides.length];
            slide.style.zIndex = this.slides.length - i;
        }

        Effect.Fade(this.slides[this.current], {
            afterFinish: function(effect) {
                effect.element.style.zIndex = 0;
                Element.show(effect.element);
                Element.setOpacity(effect.element, 1);
            }
        });
    
        this.current = (this.current + 1) % this.slides.length;
        setTimeout((function(){
            this.next();
        }).bind(this), this.timeout + 1000);
    }
}


function tellafriend()
{
    location.href = "index.php?m=tellafriend&url="+escape(location.href);
    return false;
}

function sendForm(elementId)
{
    var myForm = $(elementId);
    while (!(myForm.nodeType == '9' || (myForm.nodeType == '1' && myForm.nodeName == 'FORM'))) {
        myForm = myForm.parentNode;
    }

    if (myForm.nodeName == 'FORM') {
        var url = myForm.action;
        reqmethod = myForm.method;
        var pars = 'ajax=true&'+myForm.serialize();

        var myAjax = new Ajax.Request(
            url,
            {
                method: reqmethod,
                postBody: pars,
                onComplete: processResult
            }
            );
        return false;
    }
    return false;
}

function formSent(t)
{
    Effect.Shrink('contentform',{
        afterFinish: function () {
            Effect.Appear('message')
            }
        });
$('message').innerHTML = t;
}

function processResult(t) {
    if (t.responseText.search(/^index.php/) < 0) {
        if (t.responseText.length > 0) {
            try {
                eval(t.responseText);
            } catch(err) {
                if (t.responseText.length > 0) {
                    window.alert(t.responseText);
                }
            }
        }
    } else {
        window.location.href = t.responseText;
    }
}

function routePlanner() {
    var sPrp = ''
    sPrp = 'width=630,height=430,history=yes,resizable=no,status=yes,scrollbars=yes,menubar=no,toolbar=no,location=no,directories=no,screenX=50,screenY=155,top=155,left=50'
    sUrl = 'http://tools.locatienet.com/location/map.asp?config_id=5173&language=dutch&street=Huiswaarderplein&zipcode=1823+CP&city=Alkmaar&country=NL&zoom=2&markertext=Mobi+Design+BV&returntext=Terug naarMobi&returnurl=javascript:window.close()'

    window.open(sUrl, "NewWin", sPrp);
}

function checkEnter(e) {
    e = e || window.event;
    var characterCode = e.which || e.keyCode;
    if(characterCode == 13){ 
        redir();
        return false
    } else{
        return true
    }
}

function clearValue(field) 
{ 
    if (field.defaultValue == field.value) 
        field.value = ""; 
} 

function redir() {
    var testStr = '';
    testStr = document.getElementById('searchinput').value;      
    window.location = "index.php?m=search&sq="+testStr;      
}

