/*------------------------------------------------------------------------------
    JS Document

    project:    Vediorbis - Randstadt
    created:    2008-11-21
    author:     yvg

    summary:    CONSTANTES
                UTILITIES
                WINDOW.ONLOAD
                DOMContentLoaded
                hasJS
                HORIZONTAL_SCROLLS
                SEARCHFIELDS
                DATEPICKER
----------------------------------------------------------------------------- */

/*  =CONSTANTES
----------------------------------------------------------------------------- */
var d = document;
var w = window;
window.config = {
    debug : false,
    tabs1 : {
        tabName    : 'tabs',
        tabContent : 'tab-content'
    },
    tabs2 : {
        tabName : 'directionsTabs'
    },
    scrollOverflow : {
        scrollName : 'scroll-x'
    },
    searchFields : {
        fields : ['sn-what', 'sn-where']
    },
	toggleItems : {
        name   : 'item-open',
        target : 'to-open'
    },
	datePicker : {
        pickerName : '.date-pick',
        defaultValue : ''
        /*,
        defaultValue : 'JJ/MM/AAAA'*/
    }
};


/*  =UTILITIES
----------------------------------------------------------------------------- */
/* getElementsByClassName when not supported */
var matchAll = function() {
    var node = arguments[1] || document;
    var elms = node.getElementsByTagName('*');
    var className = arguments[0];
    if (d.getElementsByClassName)
        return node.getElementsByClassName(className);
    else {
		var regExp = new RegExp("(^|\\s)" + className + "(\\s|$)");
        var array = [];
        for (var i = 0; i < elms.length; i++) {
            var current = elms[i];
            if (current.className.match(regExp)) {
                array.push(current);
            }
        }
        return array;
    }
};

/* logs into a textarea for IE and in the console for others */
var debuger = function() {
    if (window.config.debug && !debug && typeof console == 'undefined') {
        var parent = d.getElementsByTagName('div')[0];
        var body   = d.getElementsByTagName('body')[0];
        var debug  = d.createElement('textarea');
        debug.setAttribute('rows', 10);
        debug.setAttribute('cols', 80);
        debug.setAttribute('name', 'debug');
        body.insertBefore(debug, parent);
        window.debug = debug;
        log('Debug mode : on');
    }
};
var log = function(x) {
    if (typeof console != 'undefined')
        console.log(x);
    else if (typeof debug != 'undefined') {
        debug.value += x + '\n';
        debug.scrollTop = debug.scrollHeight;
    }
};


/*  =WINDOW.ONLOAD
----------------------------------------------------------------------------- */
$j(document).ready(function(){
    debuger();
    hasJS();
    tabs();
    tabs2();
    scrollOverflow();
    searchFields();
	toggleItems();
	initDatePicker();
    selectPermis();
});

/*  =hasJS
----------------------------------------------------------------------------- */
var hasJS = function() {
    var htmlTag = d.getElementsByTagName('html')[0];
    htmlTag.className = (htmlTag.className + ' ' || '') + 'hasJS';
};


/*  =TABS
----------------------------------------------------------------------------- */
var tabs = function() {

    var tabContent = window.config.tabs1.tabContent;
    var tabName    = window.config.tabs1.tabName;

    var constructPointer = function(current) {
        var pointer = d.createElement('span');
        var parent  = current;
        pointer.className = 'tab-pointer';
        current.appendChild(pointer);
    };

    var setPointer = function(current) {
        var posLeft = current.parentNode.offsetLeft;
        var parent  = current.parentNode.parentNode.parentNode;
        var pointer = matchAll('tab-pointer', parent)[0];
        pointer.style.marginLeft = posLeft + 'px';
    };

    var toggleTabs = function(id) {
        var item = d.getElementById(id);
        var parent = item.parentNode;
        var items = matchAll(tabContent, parent);
        for (var i = 0, length = items.length; i < length; i++) {
            var current = items[i];
            current.style.display = 'none';
        }
        item.style.display = 'block';
    };

    var setTabsStyle = function(item) {
        var parent = item.parentNode.parentNode;
        var tabs = parent.getElementsByTagName('li');
        for (var i = 0, length = tabs.length; i < length; i++) {
            var current = tabs[i];
            if (current.className.match(/\bactif\b/)) {
                current.className = current.className.replace(/actif/,'');
            }
        }
        item.parentNode.className += ' actif';
    };

    var init = function() {
        var tabs = matchAll(tabName);
        if (tabs.length == 0)
            return;

        for (var i = 0, length = tabs.length; i < length; i++) {
            var current = tabs[i];
            constructPointer(current);
            var items = current.getElementsByTagName('a');
            for (var j = 0, length2 = items.length; j < length2; j++) {
                var current2 = items[j];

                // Capture the click event
                current2.onclick = function() {
                    var id = this.href.split('#')[1];
                    setTabsStyle(this);
                    setPointer(this);
                    toggleTabs(id);
                    return false;
                };

                // Don't show all divs
                var id = current2.href.split('#')[1];
                if (j != 0 && id != undefined) {
                    d.getElementById(id).style.display = 'none';
                }

                // Set the first on
                if (j == 0) {
                    current2.parentNode.className += ' actif';
                }
            }
        }
    }();
};

var tabs2 = function() {
    var tabs  = w.config.tabs2.tabName;
    var items = matchAll(tabs);
    var toggleFade = function(block) {
        $j(block).siblings().fadeOut('fast');
        $j(block).fadeIn('slow');
    };
    var setTabs = function(list) {
        var items = list.getElementsByTagName('a');
        for (var i = 0, length = items.length; i < length; i++) {
            var current = items[i];
            current.onclick = function() {
                var block = '#' + this.href.split('#')[1];
                $j(list).children('li').children('a').removeClass('actif');
                $j(this).addClass('actif');
                toggleFade(block);
                return false;
            };
            if (i == 0) {
                current.className = 'actif';
            }
            if (i > 0) {
                var block = current.href.split('#')[1];
                d.getElementById(block).style.display = 'none';
            }
        }
    };
    var init  = function() {
        for (var i = 0, length = items.length; i < length;i ++) {
            var current = items[i];
            setTabs(current);
        }
    }();
};

/*  =HORIZONTAL_SCROLLS
----------------------------------------------------------------------------- */
var scrollOverflow = function() {
    var scrollName  = window.config.scrollOverflow.scrollName;
    var items = matchAll(scrollName);
    if (items.length == 0)
        return;
    var init = function() {
        for (var i = 0, length = items.length; i < length; i++) {
            var current = items[i];
            var inner   = matchAll('scroll-inner', current)[0];
            if (!inner)
                return;
            var divs    = inner.getElementsByTagName('div');
            var totalWidth = 0;

            for (var j = 0, length2 = divs.length; j < length2; j++) {
                var current2 = divs[j];
                var calcWidth = current2.offsetWidth;
                totalWidth = parseInt(totalWidth + calcWidth + 2 ,10);
            }
            if (navigator.platform.match(/\bWin32\b/)) {
                // some kind of a scrollWidth difference between Mac and Win
                totalWidth = parseInt(totalWidth - 4 ,10);
            }
            inner.style.width = totalWidth + 'px';
            if (totalWidth > current.offsetWidth) {
                current.style.overflowX = 'scroll';
            }
            else {
                current.className += ' no-scroll';
            }
         }
    }();
};


/*  =SEARCHFIELDS
----------------------------------------------------------------------------- */
var searchFields = function() {
    var fields = window.config.searchFields.fields;
    if (!d.getElementById(fields[0]))
        return;
    var setField = function(current) {
        var label   = current.parentNode.getElementsByTagName('label')[0];
        var title   = label.title;
        current.value = title;
        current.className = 'state-0';
        current.state = 0;
    };
    var init = function() {
        for (var i = 0, length = fields.length; i < length; i++) {
            var current = d.getElementById(fields[i]);
            setField(current);
            current.onkeypress = function() {
                this.state = 1;
            };
            current.onfocus = function() {
                if (this.state != 1) {
                    this.value = '';
                    this.className = '';
                }
            };
            current.onblur = function() {
                if (this.state == 0 || this.value == '') {
                    setField(this);
                }
            };
        }
    }();
};

/*  =TOGGLEITEMS
----------------------------------------------------------------------------- */
var toggleItems = function() {
    var toggleItems = window.config.toggleItems.name;
    var targetItems = window.config.toggleItems.target;

    var IEZoomBug = function(elm, zoom) {
        // IE rendering bug
        var form = elm.getElementsByTagName('form')[0];
        if (form)
            var x = setTimeout(function(){form.style.zoom = zoom;},0);
        // end
    };

    var init = function() {
        var items = matchAll(toggleItems);
        if (items.length == 0)
            return;

        for (var i = 0, length = items.length; i < length; i++) {
            var current = items[i];
            var parent = current.parentNode;
            var targetSibling = matchAll(targetItems, parent)[0];
            current.onclick = function() {
                var parent = this.parentNode;
                var targetSibling = matchAll(targetItems, parent)[0];

                if (!targetSibling)
                    return;
                if (targetSibling.className.match(/\bis-open\b/)) {
                    targetSibling.className = targetSibling.className.replace('is-open','');
                    this.className = this.className.replace('actif','');
                    if (jQuery.browser.msie && jQuery.browser.version <= 7) {
                        IEZoomBug(targetSibling, 0);
                    }
                }
                else {
                    targetSibling.className += ' is-open';
                    if (parent.className.match(/\bopt-blank\b/)) {
                        this.className += ' actif';
                    }
                    if (jQuery.browser.msie && jQuery.browser.version <= 7) {
                        IEZoomBug(targetSibling, 1);
                    }
                }
            };
            if (targetSibling.className.match(/\bis-open\b/)) {
                current.className += ' actif';
            }
        }
    }();
};

/*  =DATEPICKER
----------------------------------------------------------------------------- */
var initDatePicker = function() {
    var pickerName = w.config.datePicker.pickerName;
    var defaultVal = w.config.datePicker.defaultValue;
	if ($j(pickerName)[0]) {
	    $j(pickerName).datePicker().trigger('change');
	    $j(pickerName).click(function(){
	        if ($j(this).val() == defaultVal) {
	            $j(this).val('');
	        }
	    });
	    $j(pickerName).blur(function(){
	        if ($j(this).val() == '') {
	            $j(this).val(defaultVal);
	        }
	    });
	}
};


/*  =SELECTPERMIS
----------------------------------------------------------------------------- */
var selectPermis = function() {
	var hasDrivingLicence = false;
	$j(".hasDrivingLicense .form-checkboxes input[@type='checkbox']").each(function() {
		if (this.checked == true)
			hasDrivingLicence = true;
	});
	if (hasDrivingLicence)
		$j('#sn-permis').attr("checked", "checked");
	else
		$j(".hasDrivingLicense .form-checkboxes input[@type='checkbox']").each(function() { this.disabled = true; });
	$j("#sn-permis").click(function() {
		if ($j('#sn-permis').is(':checked'))
			$j(".hasDrivingLicense .form-checkboxes input[@type='checkbox']").each(function() {	this.disabled = false; });
		else
			$j(".hasDrivingLicense .form-checkboxes input[@type='checkbox']").each(function() { this.checked = false; this.disabled = true; });
	});
};
