/**
 * @name: Niceforms -> rfForms
 * @version: 2.0.razorfish.1
 * @author: Lucian Slatineanu, Dennis Hall
 * @url: http://www.emblematiq.com/projects/niceforms/
 * @description: Allows form elements to be skinned by hiding
 *  default browser form elements and wiring replacement elements to them.
 *  Portions copyright Lucian Slatineanu.
 * @changelog:
 *  supports IE6!
 *  all function-s encapsulated in the rfForms object (better namespacing)
 *  makes a little use of jQuery.
 *  made a few public aliases of private function-s (which were all just global before anyway..)
**/

rfForms = new(function(){

    //Theme Variables - edit these to match your theme
    var selectRightWidthSimple = 19,
        selectRightWidthScroll = 2,
        selectMaxHeight = 200,
        textareaTopPadding = 10,
        textareaSidePadding = 10,
        d = document,
        c = function(s){return d.createElement(s);},
        NF = [],
        isIE = false,
        resizeTest = 1;
    ///
    
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
        var ieversion = new Number(RegExp.$1);
        //~ if (ieversion < 7) {
            //~ return false;
        //~ } //exit script if IE6
        isIE = true;
    }
    
    //Initialization function
    function NFInit(){
        try {
            d.execCommand('BackgroundImageCache', false, true);
        } 
        catch (e) {
        }
        if (!d.getElementById) {
            return false;
        }
        NFDo('start');
    }

    function NFDo(what){
        var niceforms = d.getElementsByTagName('form');
        var identifier = new RegExp('(^| )' + 'niceform' + '( |$)');
        for (var q = 0; q < niceforms.length; q++) {
            if (identifier.test(niceforms[q].className)) {
                if (what == "start") { //Load Niceforms
                    NF[q] = new niceform(niceforms[q]);
                    niceforms[q].start();
                }
                else { //Unload Niceforms
                    niceforms[q].unload();
                    NF[q] = "";
                }
            }
        }
    }

    function NFFix(){
        NFDo('stop');
        NFDo('start');
    }
    
    function niceform(nf){
        nf._inputText = [];
        nf._inputRadio = [];
        nf._inputCheck = [];
        nf._inputSubmit = [];
        nf._inputFile = [];
        nf._textarea = [];
        nf._select = [];
        nf._multiselect = [];
        nf.add_inputText = function(obj){
            this._inputText[this._inputText.length] = obj;
            inputText(obj);
        };
        nf.add_inputRadio = function(obj){
            this._inputRadio[this._inputRadio.length] = obj;
            inputRadio(obj);
        };
        nf.add_inputCheck = function(obj){
            this._inputCheck[this._inputCheck.length] = obj;
            inputCheck(obj);
        };
        nf.add_inputSubmit = function(obj){
            this._inputSubmit[this._inputSubmit.length] = obj;
            inputSubmit(obj);
        };
        nf.add_inputFile = function(obj){
            this._inputFile[this._inputFile.length] = obj;
            inputFile(obj);
        };
        nf.add_textarea = function(obj){
            this._textarea[this._textarea.length] = obj;
            textarea(obj);
        };
        nf.add_select = function(obj){
            this._select[this._select.length] = obj;
            selects(obj);
        };
        nf.add_multiselect = function(obj){
            this._multiselect[this._multiselect.length] = obj;
            multiSelects(obj);
        };
        nf.start = function(){
            //Separate and assign elements
            var allInputs = this.getElementsByTagName('input');
            for (var w = 0; w < allInputs.length; w++) {
                switch (allInputs[w].type) {
                    case "text":
                    case "password":{
                        //this.add_inputText(allInputs[w]);
                        break;
                    }
                    case "radio":{
                        this.add_inputRadio(allInputs[w]);
                        break;
                    }
                    case "checkbox":{
                        this.add_inputCheck(allInputs[w]);
                        break;
                    }
                    case "submit":
                    case "reset":
                    case "button":{
                        this.add_inputSubmit(allInputs[w]);
                        break;
                    }
                    case "file":{
                        this.add_inputFile(allInputs[w]);
                        break;
                    }
                }
            }
            var allButtons = this.getElementsByTagName('button');
            for (var w = 0; w < allButtons.length; w++) {
                this.add_inputSubmit(allButtons[w]);
            }
            var allTextareas = this.getElementsByTagName('textarea');
            for (var w = 0; w < allTextareas.length; w++) {
                this.add_textarea(allTextareas[w]);
            }
            var allSelects = this.getElementsByTagName('select');
            for (var w = 0; w < allSelects.length; w++) {
                if (allSelects[w].size == "1") {
                    this.add_select(allSelects[w]);
                }
                else {
                    this.add_multiselect(allSelects[w]);
                }
            }
            //Start
            for (w = 0; w < this._inputText.length; w++) {
                this._inputText[w].init();
            }
            for (w = 0; w < this._inputRadio.length; w++) {
                this._inputRadio[w].init();
            }
            for (w = 0; w < this._inputCheck.length; w++) {
                this._inputCheck[w].init();
            }
            for (w = 0; w < this._inputSubmit.length; w++) {
                this._inputSubmit[w].init();
            }
            for (w = 0; w < this._inputFile.length; w++) {
                this._inputFile[w].init();
            }
            for (w = 0; w < this._textarea.length; w++) {
                this._textarea[w].init();
            }
            for (w = 0; w < this._select.length; w++) {
                this._select[w].init(w);
            }
            for (w = 0; w < this._multiselect.length; w++) {
                this._multiselect[w].init(w);
            }
        };
        nf.unload = function(){
            //Stop
            for (w = 0; w < this._inputText.length; w++) {
                this._inputText[w].unload();
            }
            for (w = 0; w < this._inputRadio.length; w++) {
                this._inputRadio[w].unload();
            }
            for (w = 0; w < this._inputCheck.length; w++) {
                this._inputCheck[w].unload();
            }
            for (w = 0; w < this._inputSubmit.length; w++) {
                this._inputSubmit[w].unload();
            }
            for (w = 0; w < this._inputFile.length; w++) {
                this._inputFile[w].unload();
            }
            for (w = 0; w < this._textarea.length; w++) {
                this._textarea[w].unload();
            }
            for (w = 0; w < this._select.length; w++) {
                this._select[w].unload();
            }
            for (w = 0; w < this._multiselect.length; w++) {
                this._multiselect[w].unload();
            }
        };
    }

    function inputText(el){ //extent Text inputs
        el.oldClassName = el.className;
        el.left = c('div');
        el.left.className = "NFTextLeft";
        el.right = c('div');
        el.right.className = "NFTextRight";
        el.dummy = c('div');
        el.dummy.className = "NFTextCenter";
        el.onfocus = function(){
            this.dummy.className = "NFTextCenter NFh";
            this.left.className = "NFTextLeft NFh";
            this.right.className = "NFTextRight NFh";
        };
        el.onblur = function(){
            this.dummy.className = "NFTextCenter";
            this.left.className = "NFTextLeft";
            this.right.className = "NFTextRight";
        };
        el.init = function(){
            this.parentNode.insertBefore(this.left, this);
            this.parentNode.insertBefore(this.right, this.nextSibling);
            this.dummy.appendChild(this);
            this.right.parentNode.insertBefore(this.dummy, this.right);
            this.className = "NFText";
        };
        el.unload = function(){
            this.parentNode.parentNode.appendChild(this);
            this.parentNode.removeChild(this.left);
            this.parentNode.removeChild(this.right);
            this.parentNode.removeChild(this.dummy);
            this.className = this.oldClassName;
        };
    }

    function inputRadio(el, initializeNow){ //extent Radio buttons
        el.oldClassName = el.className;
        el.dummy = c('div');
        el.dummyWrap = c('div');
        el.dummyWrap.className = 'NFRadioWrapper';
        el.dummy.className = "NFRadio" + (el.checked ? " NFh" : "");
        el.dummy.ref = el;
        /*
        if (isIE == false) {
            el.dummy.style.left = findPosX(el) + 'px';
            el.dummy.style.top = findPosY(el) + 'px';
        }
        else {
            el.dummy.style.left = findPosX(el) + 4 + 'px';
            el.dummy.style.top = findPosY(el) + 4 + 'px';
        }
        */
        el.dummy.onclick = function(){
            if (!el.checked) {
                var siblings = getInputsByName(el.name);
                for (var q = 0; q < siblings.length; q++) {
                    var sibling = siblings[q];
                    sibling.checked = false;
                    sibling.dummy.className = "NFRadio";
                    $('label[for='+sibling.id+']').removeClass('selected');
                }
                el.checked = true;
                this.className = "NFRadio NFh";
                $('label[for='+el.id+']').addClass('selected');
            }
        };
        el.onclick = function(){
            if (this.checked) {
                var siblings = getInputsByName(this.name);
                for (var q = 0; q < siblings.length; q++) {
                    var sibling = siblings[q];
                    //~ console.log(siblings[q]);
                    siblings[q].dummy.className = "NFRadio";
                    $('label[for='+sibling.id+']').removeClass('selected');
                }
                this.dummy.className = "NFRadio NFh";
                $('label[for='+el.id+']').addClass('selected');
            }
        };
        el.onfocus = function(){
            this.dummy.className += " NFfocused";
        };
        el.onblur = function(){
            this.dummy.className = this.dummy.className.replace(/ NFfocused/g, "");
        };
        el.init = function(){
            this.parentNode.insertBefore(this.dummyWrap, this);
            this.dummyWrap.appendChild(this.dummy);
            el.className = "NFhidden";
        };
        el.unload = function(){
            this.parentNode.removeChild(this.dummy);
            this.className = this.oldClassName;
        };
        
        // give it some nice user-feedback in the form of hovering,
        // for both the radio itself and any associated label.
      //  $(el.dummyWrap).hoverify(); // commented out, causing error with new SNI Ad code. Rick Farmer will have to take a look
        $('label[for='+el.id+']')
            .click(function(){
                el.dummy.onclick();
            })
            .hover(function(){$(el.dummyWrap).add(this).addClass('hover');}, function(){$(el.dummyWrap).add(this).removeClass('hover');})
            .addClass(el.checked?'selected':'')
            .parent().addClass('NFapplied');
        ///
        if(initializeNow){
            el.init();
        }
    }

    function inputCheck(el, initializeNow){ //extend Checkboxes
        el.oldClassName = el.className;
        el.dummy = c('div');
        el.dummyWrap = c('div');
        el.dummyWrap.className = 'NFCheckWrapper';
        el.dummy.className = "NFCheck" + (el.checked ? " NFh" : "");
        el.dummy.ref = el;
        /*
        if (isIE == false) {
            el.dummy.style.left = findPosX(el) + 'px';
            el.dummy.style.top = findPosY(el) + 'px';
        }
        else {
            el.dummy.style.left = findPosX(el) + 4 + 'px';
            el.dummy.style.top = findPosY(el) + 4 + 'px';
        }
        */
        el.dummy.onclick = function(){
            //~ console.log('inputCheck dummy.onclick .. this.ref.checked = ', this.ref.checked);
            this.ref.checked = !this.ref.checked;
            this.className = "NFCheck" + (this.ref.checked ? " NFh" : "");
            //~ console.log('inputCheck dummy.onclick .. this.ref.checked = ', this.ref.checked);
        };
        el.onclick = function(){
            //~ console.log('inputCheck el.onclick .. this.checked = ', this.checked);
            this.dummy.className = "NFCheck" + (this.checked ? " NFh" : "");
        };
        el.onfocus = function(){
            this.dummy.className += " NFfocused";
        };
        el.onblur = function(){
            this.dummy.className = this.dummy.className.replace(/ NFfocused/g, "");
        };
        el.init = function(){
            this.parentNode.insertBefore(this.dummyWrap, this);
            this.dummyWrap.appendChild(this.dummy);
            el.className = "NFhidden";
        };
        el.unload = function(){
            this.parentNode.removeChild(this.dummy);
            this.className = this.oldClassName;
        };
        
        // give it some nice user-feedback in the form of hovering,
        // for both the radio itself and any associated label.
        $(el.dummyWrap).hoverify();
        $('label[for='+el.id+']')
            //~ .click(function(){
                //~ el.dummy.onclick();
            //~ })
            .hover(function(){$(el.dummyWrap).addClass('hover');}, function(){$(el.dummyWrap).removeClass('hover');})
            .parent().addClass('NFapplied');
        ///
        if(initializeNow){
            el.init();
        }
    }

    function inputSubmit(el){ //extend Buttons
        /*
        el.oldClassName = el.className;
        el.left = c('div');
        el.left.className = "NFButtonLeft";
        el.right = c('div');
        el.right.className = "NFButtonRight";
        el.onmouseover = function(){
            this.className = "NFButton NFh";
            this.left.className = "NFButtonLeft NFh";
            this.right.className = "NFButtonRight NFh";
        };
        el.onmouseout = function(){
            this.className = "NFButton";
            this.left.className = "NFButtonLeft";
            this.right.className = "NFButtonRight";
        };
        */
        el.init = function(){
        /*
            this.parentNode.insertBefore(this.left, this);
            this.parentNode.insertBefore(this.right, this.nextSibling);
            this.className = "NFButton";
        */
        };
        el.unload = function(){
        /*
            this.parentNode.removeChild(this.left);
            this.parentNode.removeChild(this.right);
            this.className = this.oldClassName;
        */
        };
    }

    function inputFile(el){ //extend File inputs
        el.oldClassName = el.className;
        el.dummy = c('div');
        el.dummy.className = "NFFile";
        el.file = c('div');
        el.file.className = "NFFileNew";
        el.center = c('div');
        el.center.className = "NFTextCenter";
        el.clone = c('input');
        el.clone.type = "text";
        el.clone.className = "NFText";
        el.clone.ref = el;
        el.left = c('div');
        el.left.className = "NFTextLeft";
        el.button = c('div');
        el.button.className = "NFFileButton";
        el.button.ref = el;
        el.button.onclick = function(){
            this.ref.click();
        }
        el.init = function(){
            var top = this.parentNode;
            if (this.previousSibling) {
                var where = this.previousSibling;
            }
            else {
                var where = top.childNodes[0];
            }
            top.insertBefore(this.dummy, where);
            this.dummy.appendChild(this);
            this.center.appendChild(this.clone);
            this.file.appendChild(this.center);
            this.file.insertBefore(this.left, this.center);
            this.file.appendChild(this.button);
            this.dummy.appendChild(this.file);
            this.className = "NFhidden";
            this.relatedElement = this.clone;
        };
        el.unload = function(){
            this.parentNode.parentNode.appendChild(this);
            this.parentNode.removeChild(this.dummy);
            this.className = this.oldClassName;
        };
        el.onchange = el.onmouseout = function(){
            this.relatedElement.value = this.value;
        };
        el.onfocus = function(){
            this.left.className = "NFTextLeft NFh";
            this.center.className = "NFTextCenter NFh";
            this.button.className = "NFFileButton NFh";
        };
        el.onblur = function(){
            this.left.className = "NFTextLeft";
            this.center.className = "NFTextCenter";
            this.button.className = "NFFileButton";
        };
        el.onselect = function(){
            this.relatedElement.select();
            this.value = '';
        };
    }

    function textarea(el){ //extend Textareas
        el.oldClassName = el.className;
        el.height = el.offsetHeight - textareaTopPadding;
        el.width = el.offsetWidth - textareaSidePadding;
        el.topLeft = c('div');
        el.topLeft.className = "NFTextareaTopLeft";
        el.topRight = c('div');
        el.topRight.className = "NFTextareaTop";
        el.bottomLeft = c('div');
        el.bottomLeft.className = "NFTextareaBottomLeft";
        el.bottomRight = c('div');
        el.bottomRight.className = "NFTextareaBottom";
        el.left = c('div');
        el.left.className = "NFTextareaLeft";
        el.right = c('div');
        el.right.className = "NFTextareaRight";
        el.init = function(){
            var top = this.parentNode;
            if (this.previousSibling) {
                var where = this.previousSibling;
            }
            else {
                var where = top.childNodes[0];
            }
            top.insertBefore(el.topRight, where);
            top.insertBefore(el.right, where);
            top.insertBefore(el.bottomRight, where);
            this.topRight.appendChild(this.topLeft);
            this.right.appendChild(this.left);
            this.right.appendChild(this);
            this.bottomRight.appendChild(this.bottomLeft);
            el.style.width = el.topRight.style.width = el.bottomRight.style.width = el.width + 'px';
            el.style.height = el.left.style.height = el.right.style.height = el.height + 'px';
            this.className = "NFTextarea";
        };
        el.unload = function(){
            this.parentNode.parentNode.appendChild(this);
            this.parentNode.removeChild(this.topRight);
            this.parentNode.removeChild(this.bottomRight);
            this.parentNode.removeChild(this.right);
            this.className = this.oldClassName;
            this.style.width = this.style.height = "";
        };
        el.onfocus = function(){
            this.topLeft.className = "NFTextareaTopLeft NFh";
            this.topRight.className = "NFTextareaTop NFhr";
            this.left.className = "NFTextareaLeftH";
            this.right.className = "NFTextareaRightH";
            this.bottomLeft.className = "NFTextareaBottomLeft NFh";
            this.bottomRight.className = "NFTextareaBottom NFhr";
        };
        el.onblur = function(){
            this.topLeft.className = "NFTextareaTopLeft";
            this.topRight.className = "NFTextareaTop";
            this.left.className = "NFTextareaLeft";
            this.right.className = "NFTextareaRight";
            this.bottomLeft.className = "NFTextareaBottomLeft";
            this.bottomRight.className = "NFTextareaBottom";
        };
    }

    var selectsCallNumber = 0; //the number of times that the 'selects' function has been called - used to create unique ids.
    function selects(el, initializeNow){ //extend Selects
        el.oldClassName = el.className;
        el.dummy = c('div');
        el.dummy.className = "NFSelect";
        if(el.id){
            el.dummy.id = "NFSelect-"+el.id;
        }
        el.dummy.style.width = el.offsetWidth + 'px';
        el.dummy.ref = el;
        el.left = c('div');
        el.left.className = "NFSelectLeft";
        el.right = c('div');
        el.right.className = "NFSelectRight";
        el.txt = d.createTextNode(el.options[0].text);
        el.bg = c('div');
        el.bg.className = "NFSelectTarget";
        el.scrollPane = c('div');
        el.scrollPane.className = "NFScrollPane";
        el.scrollPaneContainer = c('div');
        el.scrollPaneContainer.className = "NFScrollPaneContainer";
        el.opt = c('ul');
        el.opt.className = "NFSelectOptions";
        el.scrollPane.id = "NFSelectOptions_" + (++selectsCallNumber);
        el.scrollPaneContainer.style.width = el.offsetWidth + 'px';
        el.scrollPane.style.width = (el.offsetWidth - 8) + 'px';
        /*
        el.dummy.style.left = findPosX(el) + 'px';
        el.dummy.style.top = findPosY(el) + 'px';
        */
        el.opts = new Array(el.options.length);
        el.init = function(pos){
            pos = pos || 0;
            this.dummy.appendChild(this.left);
            this.right.appendChild(this.txt);
            this.dummy.appendChild(this.right);
            this.bg.appendChild(this.scrollPaneContainer);
            this.scrollPaneContainer.appendChild(this.scrollPane);
            this.scrollPane.appendChild(this.opt);
            this.dummy.appendChild(this.bg);
            if(isIE && ieversion == 6){
                this.opt.style.zoom = 1;
            }
            var prevParent = el,
                numLabels = 0;
                optgroup = {};
            ///
            for (var q = 0, l = this.options.length; q < l; q++) {
                var optionEl = this.options[q];
                //optgroup logic..
                if(optionEl.parentNode != prevParent){
                    numLabels++;
                    //~ prevParent = optionEl.parentNode;
                    //~ optgroup.className = prevParent.className;
                    //~ optgroup.id = prevParent.id;
                    //~ optgroup.label = prevParent.label;
                    
                    var li = c('LI'),
                        h6 = c('H6');
                    ///
                    h6.appendChild(d.createTextNode(optionEl.parentNode.label));
                    li.className = 'label';
                    if(isIE && ieversion == 6){
                        li.style.zoom = 1;
                    }
                    li.appendChild(h6);
                    this.opt.appendChild(li);
                }
                this.opts[q] = new option(optionEl, q); //NOTE: lowercase 'o' in 'option' = OUR option constructor
                //~ if(q == l-1){
                    //~ optionEl.li.className = 'last';
                //~ }
                if(isIE && ieversion == 6){
                    optionEl.li.style.zoom = 1;
                }
                this.opt.appendChild(optionEl.li);
                optionEl.lnk.onclick = function(){
                    this._onclick();
                    this.ref.dummy.getElementsByTagName('div')[1].innerHTML = '<span>'+this.ref.options[this.pos].text+'</span>';
                    this.ref.options[this.pos].selected = "selected";
                    //both the dummy element and the original <select> element will fire "onchange" events:
                    $(this.ref).add(this.ref.dummy).trigger('change');
                    for (var w = 0; w < this.ref.options.length; w++) {
                        this.ref.options[w].lnk.className = "";
                    }
                    this.ref.options[this.pos].lnk.className = "NFOptionActive";
                }
                prevParent = optionEl.parentNode;
            }
            
            var selectedIndex = this.options.selectedIndex || 0;
            this.dummy.getElementsByTagName('div')[1].innerHTML = '<span>'+this.options[selectedIndex].text+'</span>';
            this.options[selectedIndex].lnk.className = "NFOptionActive";
            
            //this.dummy.style.zIndex = 999 - pos; //what the devil was this for?
            this.parentNode.insertBefore(this.dummy, this);
            this.className = "NFhidden";
            //this.opt.style.margin = '0';
            /*
            this.opt.style.position = 'relative';
            this.opt.style.overflow = 'auto';
            this.bg.style.overflow = 'visible';
            */
            var optsHeight = this.opt.offsetHeight;
            this.scrollPaneContainer.style.height = optsHeight < 200 ? optsHeight + 'px' : "200px";
            el.scrollPane.style.height = this.scrollPaneContainer.style.height;
            //this.opt.style.position = 'relative';
            
            CSBfleXcroll(this.scrollPane.id);
            el.bg.style.display = "none";
        };
        el.unload = function(){
            this.parentNode.removeChild(this.dummy);
            this.className = this.oldClassName;
        };
        //TODO: would like to make this 'mousedown' and make it so the text displayed is unselectable.
        el.dummy.onclick = function(e){
            
            //"clicking" .. which turned out to be nothing more than interacting with the scrollbar..
            // would trigger this onclick function, and we don't want to execute this function if
            // arriving here from that click-vector
            if(!e){
                var e = window.event;
            }
            var targ = e.srcElement || e.target, i=0;
            while(targ && ++i<10){
                var className = targ.className || '';
                if(className.indexOf('scrollgeneric') > -1){
                    e.preventDefault && e.preventDefault();
                    e.cancelBubble = true;
                    return;
                }
                targ = targ.parentNode;
            }
            /// end click-vector check
            
            var allDivs = d.getElementsByTagName('div');
            for (var q = 0; q < allDivs.length; q++) {
                if ((allDivs[q].className.indexOf("NFSelectTarget")>-1) && (allDivs[q] != this.ref.bg)) {
                    allDivs[q].style.display = "none";
                    //jQuery(el.dummy).removeClass('open');
                }
                if ((allDivs[q].className.indexOf("NFSelect")>-1) && (allDivs[q] != this.ref)){
                    //allDivs[q].className = allDivs[q].className.replace(' open', '');
                    $(allDivs[q]).removeClass('open');
                }
            }
            if (this.ref.bg.style.display == "none") {
                this.ref.bg.style.display = "block";
                jQuery(el.dummy).addClass('open');
            }
            else {
                this.ref.bg.style.display = "none";
                jQuery(el.dummy).removeClass('open');
            }
            //~ if (this.ref.opt.offsetHeight > selectMaxHeight) {
                //~ this.ref.bg.style.width = this.ref.offsetWidth - selectRightWidthScroll + 33 + 'px';
                //~ this.ref.opt.style.width = this.ref.offsetWidth - selectRightWidthScroll + 'px';
            //~ }
            //~ else {
                //~ this.ref.bg.style.width = this.ref.offsetWidth - selectRightWidthSimple + 33 + 'px';
                //~ this.ref.opt.style.width = this.ref.offsetWidth - selectRightWidthSimple + 'px';
            //~ }
        };
        jQuery(el.dummy).hoverify();
        ///
        el.onfocus = function(){
            this.dummy.className += " NFfocused";
        };
        el.onblur = function(){
            this.dummy.className = this.dummy.className.replace(/ NFfocused/g, "");
        };
        el.onkeydown = function(e){
            if (!e) 
                var e = window.event;
            var thecode = e.keyCode;
            var active = this.selectedIndex;
            switch (thecode) {
                case 40: //down
                    if (active < this.options.length - 1) {
                        for (var w = 0; w < this.options.length; w++) {
                            this.options[w].lnk.className = "";
                        }
                        var newOne = active + 1;
                        this.options[newOne].selected = "selected";
                        this.options[newOne].lnk.className = "NFOptionActive";
                        this.dummy.getElementsByTagName('div')[1].innerHTML = '<span>'+this.options[newOne].text+'</span>';
                    }
                    return false;
                    break;
                case 38: //up
                    if (active > 0) {
                        for (var w = 0; w < this.options.length; w++) {
                            this.options[w].lnk.className = "";
                        }
                        var newOne = active - 1;
                        this.options[newOne].selected = "selected";
                        this.options[newOne].lnk.className = "NFOptionActive";
                        this.dummy.getElementsByTagName('div')[1].innerHTML = '<span>'+this.options[newOne].text+'</span>';
                    }
                    return false;
                    break;
                default:
                    break;
            }
        };
        (initializeNow && el.init());
    }

    function multiSelects(el){ //extend Multiple Selects
        el.oldClassName = el.className;
        el.height = el.offsetHeight;
        el.width = el.offsetWidth;
        el.topLeft = c('div');
        el.topLeft.className = "NFMultiSelectTopLeft";
        el.topRight = c('div');
        el.topRight.className = "NFMultiSelectTop";
        el.bottomLeft = c('div');
        el.bottomLeft.className = "NFMultiSelectBottomLeft";
        el.bottomRight = c('div');
        el.bottomRight.className = "NFMultiSelectBottom";
        el.left = c('div');
        el.left.className = "NFMultiSelectLeft";
        el.right = c('div');
        el.right.className = "NFMultiSelectRight";
        el.init = function(){
            var top = this.parentNode;
            if (this.previousSibling) {
                var where = this.previousSibling;
            }
            else {
                var where = top.childNodes[0];
            }
            top.insertBefore(el.topRight, where);
            top.insertBefore(el.right, where);
            top.insertBefore(el.bottomRight, where);
            this.topRight.appendChild(this.topLeft);
            this.right.appendChild(this.left);
            this.right.appendChild(this);
            this.bottomRight.appendChild(this.bottomLeft);
            el.style.width = el.topRight.style.width = el.bottomRight.style.width = el.width + 'px';
            el.style.height = el.left.style.height = el.right.style.height = el.height + 'px';
            el.className = "NFMultiSelect";
        };
        el.unload = function(){
            this.parentNode.parentNode.appendChild(this);
            this.parentNode.removeChild(this.topRight);
            this.parentNode.removeChild(this.bottomRight);
            this.parentNode.removeChild(this.right);
            this.className = this.oldClassName;
            this.style.width = this.style.height = "";
        };
        el.onfocus = function(){
            this.topLeft.className = "NFMultiSelectTopLeft NFh";
            this.topRight.className = "NFMultiSelectTop NFhr";
            this.left.className = "NFMultiSelectLeftH";
            this.right.className = "NFMultiSelectRightH";
            this.bottomLeft.className = "NFMultiSelectBottomLeft NFh";
            this.bottomRight.className = "NFMultiSelectBottom NFhr";
        };
        el.onblur = function(){
            this.topLeft.className = "NFMultiSelectTopLeft";
            this.topRight.className = "NFMultiSelectTop";
            this.left.className = "NFMultiSelectLeft";
            this.right.className = "NFMultiSelectRight";
            this.bottomLeft.className = "NFMultiSelectBottomLeft";
            this.bottomRight.className = "NFMultiSelectBottom";
        };
    }

    function option(el, pos, o){ //extend Options
        o = o || {};
        el.li = c('li');
        if(o.className || o.id){
            el.li.className = (o.id ? 'childOf_'+ o.id + ' ' : '') + o.className;
        }
        el.txt = d.createTextNode(el.text);
        var span = c('SPAN'); //modified..
        span.appendChild(el.txt); //modified..
        el.lnk = c('a');
        el.lnk.href = "javascript:;";
        el.lnk.ref = el.parentNode;
        //adjust for optgroup
        if(el.lnk.ref.nodeName.toUpperCase() == 'OPTGROUP'){
            el.lnk.ref = el.lnk.ref.parentNode;
        }
        el.lnk.pos = pos;
        el.lnk._onclick = el.onclick || function(){};
        el.lnk.appendChild(span); //modified..
        el.li.appendChild(el.lnk);
    }

    //Get Position
    function findPosY(obj){
        var posTop = 0;
        do {
            posTop += obj.offsetTop;
        }
        while (obj = obj.offsetParent);
        return posTop;
    }

    function findPosX(obj){
        var posLeft = 0;
        do {
            posLeft += obj.offsetLeft;
        }
        while (obj = obj.offsetParent);
        return posLeft;
    }

    //Get [Radio] Siblings
    function getInputsByName(name){
        var inputs = d.getElementsByTagName("input");
        var w = 0;
        var results = [];
        for (var q = 0; q < inputs.length; q++) {
            if (inputs[q].name == name) {
                results[w] = inputs[q];
                ++w;
            }
        }
        return results;
    }

    //Add events
    /*
    var existingLoadEvent = window.onload || function(){};
    var existingResizeEvent = window.onresize || function(){};
    window.onload = function(){
        existingLoadEvent();
        NFInit();
    }
    window.onresize = function(){
        if (resizeTest != d.documentElement.clientHeight) {
            existingResizeEvent();
            NFFix();
        }
        resizeTest = d.documentElement.clientHeight;
    }
    */

    return {
        init: NFInit
        ,resize: NFFix
        ,selectify: selects
        ,radioify: inputRadio
        ,checkboxify: inputCheck
    };

})();







/**
 * @name: flexcroll
 * @version: 1.9.5
 * @author: Emrah Baskaya
 * @url: http://www.hesido.com/web.php?page=customscrollbar
 * @description: allows scrollbar skinning by overlays that obscure the browser's default scrollbars.
**/
var fleXenv = {
    fleXcrollMain : function(dDv) {
        //Main code begin
        var dC = document, wD = window, nV = navigator;
        var isIE = false;
        if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
            var ieversion = new Number(RegExp.$1);
            //~ if (ieversion < 7) {
                //~ return false;
            //~ } //exit script if IE6
            isIE = true;
        }
        if (!dC.getElementById || !dC.createElement)
            return;
        if (typeof (dDv) == 'string')
            dDv = document.getElementById(dDv);
        if (dDv == null
                || nV.userAgent.indexOf('OmniWeb') != -1
                || ((nV.userAgent.indexOf('AppleWebKit') != -1 || nV.userAgent.indexOf('Safari') != -1) && !(typeof (HTMLElement) != "undefined" && HTMLElement.prototype))
                || nV.vendor == 'KDE'
                || (nV.platform.indexOf('Mac') != -1 && nV.userAgent.indexOf('MSIE') != -1))
            return;
        if (dDv.scrollUpdate) {
            dDv.scrollUpdate();
            return;
        }
        if (!dDv.id || dDv.id == '') {
            var sTid = "flex__", c = 1;
            while (document.getElementById(sTid + c) != null) {
                c++;
            }
            dDv.id = sTid + c;
        }
        var targetId = dDv.id;
        dDv.fleXdata = {};
        var sC = dDv.fleXdata;
        sC.keyAct = {
            _37 : [ '-1s', 0 ],
            _38 : [ 0, '-1s' ],
            _39 : [ '1s', 0 ],
            _40 : [ 0, '1s' ],
            _33 : [ 0, '-1p' ],
            _34 : [ 0, '1p' ],
            _36 : [ 0, '-100p' ],
            _35 : [ 0, '+100p' ]
        };
        sC.wheelAct = [ "-2s", "2s" ];
        sC.baseAct = [ "-2s", "2s" ];
        var cDv = createDiv('contentwrapper', true),
            mDv = createDiv('mcontentwrapper', true),
            tDv = createDiv('scrollwrapper', true),
            pDv = createDiv('copyholder', true),
            iDv = createDiv('domfixdiv', true),
            fDv = createDiv('zoomdetectdiv', true),
            stdMode = false;
        // pDv.sY.border='1px solid blue';
        pDv.fHide();
        dDv.style.overflow = 'hidden';
        fDv.sY.fontSize = "12px";
        fDv.sY.height = "1em";
        fDv.sY.width = "1em";
        fDv.sY.position = "absolute";
        fDv.sY.zIndex = "-999";
        fDv.fHide();
        var brdHeight = dDv.offsetHeight,
            brdWidth = dDv.offsetWidth;
        copyStyles(dDv, pDv, '0px', [ 'border-left-width', 'border-right-width', 'border-top-width', 'border-bottom-width' ]);
        var intlHeight = dDv.offsetHeight,
            intlWidth = dDv.offsetWidth,
            brdWidthLoss = brdWidth - intlWidth,
            brdHeightLoss = brdHeight - intlHeight,
            oScrollY = (dDv.scrollTop) ? dDv.scrollTop : 0,
            oScrollX = (dDv.scrollLeft) ? dDv.scrollLeft : 0,
            urlBase = document.location.href,
            uReg = /#([^#.]*)$/,
            focusProtectList = [ 'textarea', 'input', 'select' ];
        ///
        
        sC.scroller = [];
        sC.forcedBar = [];
        sC.containerSize = sC.cntRSize = [];
        sC.contentSize = sC.cntSize = [];
        sC.edge = [ false, false ];
        sC.reqS = [];
        sC.barSpace = [ 0, 0 ];
        sC.forcedHide = [];
        sC.forcedPos = [];
        sC.paddings = [];
        while (dDv.firstChild) {
            cDv.appendChild(dDv.firstChild);
        }
        cDv.appendChild(iDv);
        dDv.appendChild(mDv);
        dDv.appendChild(pDv);
        if (getStyle(dDv, 'position') != 'absolute'){
            dDv.style.position = "relative";
        }
        var dAlign = getStyle(dDv, 'text-align');
        dDv.style.textAlign = 'left';
        mDv.sY.width = "100px";
        mDv.sY.height = "100px";
        mDv.sY.top = "0px";
        mDv.sY.left = "0px";
        copyStyles(dDv, pDv, "0px", [ 'padding-left', 'padding-top', 'padding-right', 'padding-bottom' ]);
        var postWidth = dDv.offsetWidth,
            postHeight = dDv.offsetHeight,
            mHeight;
        ///
        mHeight = mDv.offsetHeight;
        // mDv.sY.borderBottom="2px solid black";
        if (mDv.offsetHeight >= mHeight)
            stdMode = true;
        mDv.sY.borderBottomWidth = "0px";
        copyStyles(pDv, dDv, false, [ 'padding-left', 'padding-top', 'padding-right', 'padding-bottom' ]);
        findPos(mDv);
        findPos(dDv);
        sC.paddings[0] = mDv.yPos - dDv.yPos;
        sC.paddings[2] = mDv.xPos - dDv.xPos;
        dDv.style.paddingTop = getStyle(dDv, "padding-bottom");
        dDv.style.paddingLeft = getStyle(dDv, "padding-right");
        findPos(mDv);
        findPos(dDv);
        sC.paddings[1] = mDv.yPos - dDv.yPos;
        sC.paddings[3] = mDv.xPos - dDv.xPos;
        dDv.style.paddingTop = getStyle(pDv, "padding-top");
        dDv.style.paddingLeft = getStyle(pDv, "padding-left");
        var padWidthComp = sC.paddings[2] + sC.paddings[3],
            padHeightComp = sC.paddings[0] + sC.paddings[1];
        ///
        mDv.style.textAlign = dAlign;
        copyStyles(dDv, mDv, false, [ 'padding-left', 'padding-right', 'padding-top', 'padding-bottom' ]);
        tDv.sY.width = dDv.offsetWidth + 'px';
        tDv.sY.height = dDv.offsetHeight + 'px';
        mDv.sY.width = postWidth + 'px';
        mDv.sY.height = postHeight + 'px';
        tDv.sY.position = 'absolute';
        tDv.sY.top = '0px';
        tDv.sY.left = '0px';
        tDv.fHide();

        mDv.appendChild(cDv);
        dDv.appendChild(tDv);
        tDv.appendChild(fDv);

        cDv.sY.position = 'relative';
        mDv.sY.position = 'relative';
        cDv.sY.top = "0";
        // cDv.sY.width="100%";//fix IE7
        mDv.sY.overflow = 'hidden';
        mDv.sY.left = "-" + sC.paddings[2] + "px";
        mDv.sY.top = "-" + sC.paddings[0] + "px";
        sC.zTHeight = fDv.offsetHeight;

        sC.getContentWidth = function() {
            var cChilds = cDv.childNodes, maxCWidth = compPad = 0;
            for ( var i = 0; i < cChilds.length; i++) {
                if (cChilds[i].offsetWidth) {
                    maxCWidth = Math.max(cChilds[i].offsetWidth, maxCWidth)
                }
            }
            sC.cntRSize[0] = ((sC.reqS[1] && !sC.forcedHide[1]) || sC.forcedBar[1]) ? dDv.offsetWidth - sC.barSpace[0] : dDv.offsetWidth;
            sC.cntSize[0] = maxCWidth + padWidthComp;
            return sC.cntSize[0];
        };
        var isFirstGetContentHeightCall = true;
        sC.getContentHeight = function() {
            // there appears to be a bug in this code..
            sC.cntRSize[1] = ((sC.reqS[0] && !sC.forcedHide[0]) || sC.forcedBar[0]) ? dDv.offsetHeight - sC.barSpace[1] : dDv.offsetHeight;
            var h = cDv.offsetHeight + padHeightComp - 2;
            //~ var heights = ['num LIs = '+$(cDv).children().eq(0).children().size()];
            /**/
            // NOT SURE IF IT EVER WORKED QUITE RIGHT .. BUT HAD TO ADD THIS IN TO FIX IE7.
            // AMAZINGLY .. IE6 WORKS FINE!
            // UPDATE: I believe I made another change, which more accurately addresses the root cause,
            //  .. which makes this code block both obsolete and introduce a new premature-scrollbar bug in ie7.
            if(ieversion && ieversion == 7){
                if(isFirstGetContentHeightCall){
                    var h = 0;
                    $(cDv).children().eq(0).children().each(function(){
                        var myHeight = this.firstChild.offsetHeight + 1;//+ 1 = 1px border-top
                        h += myHeight;
                        //~ heights.push([myHeight, this.firstChild.offsetHeight]);
                    });
                    h += 12; //padding top & bottom of 6 each
                    //~ (function lambda(){
                        //~ if(window.console && console.ready){
                            //~ console.log(heights.join('|') + ' .. '+h);
                        //~ } else {
                            //~ setTimeout(lambda, 200);
                        //~ }
                    //~ })();
                    isFirstGetContentHeightCall = false;
                    cDv.style.height = h + 'px'; //BLECH!
                } else {
                    h = parseInt(cDv.style.height);
                }
            }
            /**/
            sC.cntSize[1] = h;
            return h;
        };

        sC.fixIEDispBug = function() {
            cDv.sY.display = 'none';
            cDv.sY.display = 'block';
        };
        sC.setWidth = function() {
            mDv.sY.width = (stdMode) ? (sC.cntRSize[0] - padWidthComp - brdWidthLoss) + 'px' : sC.cntRSize[0] + 'px';
        };
        sC.setHeight = function() {
            mDv.sY.height = (stdMode) ? (sC.cntRSize[1] - padHeightComp - brdHeightLoss) + 'px' : sC.cntRSize[1] + 'px';
        };

        sC.createScrollBars = function() {
            sC.getContentWidth();
            sC.getContentHeight();
            // vert
            tDv.vrt = [];
            var vrT = tDv.vrt;
            createScrollBars(vrT, 'vscroller');
            vrT.barPadding = [ parseInt(getStyle(vrT.sBr, 'padding-top')), parseInt(getStyle(vrT.sBr, 'padding-bottom')) ];
            vrT.sBr.sY.padding = '0px';
            vrT.sBr.curPos = 0;
            vrT.sBr.vertical = true;
            vrT.sBr.indx = 1;
            cDv.vBar = vrT.sBr;
            prepareScroll(vrT, 0);
            sC.barSpace[0] = vrT.sDv.offsetWidth;
            sC.setWidth();
            // horiz
            tDv.hrz = [];
            var hrZ = tDv.hrz;
            createScrollBars(hrZ, 'hscroller');
            hrZ.barPadding = [ parseInt(getStyle(hrZ.sBr, 'padding-left')), parseInt(getStyle(hrZ.sBr, 'padding-right')) ];
            hrZ.sBr.sY.padding = '0px';
            hrZ.sBr.curPos = 0;
            hrZ.sBr.vertical = false;
            hrZ.sBr.indx = 0;
            cDv.hBar = hrZ.sBr;
            if (wD.opera)
                hrZ.sBr.sY.position = 'relative';
            prepareScroll(hrZ, 0);
            sC.barSpace[1] = hrZ.sDv.offsetHeight;
            sC.setHeight();
            tDv.sY.height = dDv.offsetHeight + 'px';
            // jog
            hrZ.jBox = createDiv('scrollerjogbox');
            tDv.appendChild(hrZ.jBox);
            hrZ.jBox.onmousedown = function() {
                hrZ.sBr.scrollBoth = true;
                sC.goScroll = hrZ.sBr;
                hrZ.sBr.clicked = true;
                hrZ.sBr.moved = false;
                tDv.vrt.sBr.moved = false;
                fleXenv.addTrggr(dC, 'selectstart', retFalse);
                fleXenv.addTrggr(dC, 'mousemove', mMoveBar);
                fleXenv.addTrggr(dC, 'mouseup', mMouseUp);
                return false;
            };
        };

        sC.goScroll = null;
        sC.createScrollBars();
        cDv.removeChild(iDv);

        if (!this.addChckTrggr(dDv, 'mousewheel', mWheelProc) || !this.addChckTrggr(dDv, 'DOMMouseScroll', mWheelProc)) {
            dDv.onmousewheel = mWheelProc;
        }
        this.addChckTrggr(dDv, 'mousewheel', mWheelProc);
        this.addChckTrggr(dDv, 'DOMMouseScroll', mWheelProc);
        dDv.setAttribute('tabIndex', '0');

        this.addTrggr(dDv, 'keydown', function(e) {
            if (dDv.focusProtect)
                return;
            if (!e) {
                var e = wD.event;
            }
            var pK = e.keyCode;
            sC.pkeY = pK;
            sC.mDPosFix();
            if (sC.keyAct['_' + pK] && !window.opera) {
                dDv.contentScroll(sC.keyAct['_' + pK][0],
                        sC.keyAct['_' + pK][1], true);
                if (e.preventDefault)
                    e.preventDefault();
                return false;
            }
        });
        
        //make Opera Happy
        this.addTrggr(dDv, 'keypress', function(e) {
            if (dDv.focusProtect)
                return;
            if (!e) {
                var e = wD.event;
            }
            var pK = e.keyCode;
            if (sC.keyAct['_' + pK]) {
                dDv.contentScroll(sC.keyAct['_' + pK][0], sC.keyAct['_' + pK][1], true);
                if (e.preventDefault)
                    e.preventDefault();
                return false;
            }
        });

        this.addTrggr(dDv, 'keyup', function() {
            sC.pkeY = false
        });

        this.addTrggr(dC, 'mouseup', intClear);
        this.addTrggr(dDv, 'mousedown', function(e) {
            if (!e)
                e = wD.event;
            var cTrgt = (e.target) ? e.target : (e.srcElement) ? e.srcElement : false;
            if (!cTrgt || (cTrgt.className && cTrgt.className.match(RegExp("\\bscrollgeneric\\b"))))
                return;
            sC.inMposX = e.clientX;
            sC.inMposY = e.clientY;
            pageScrolled();
            findPos(dDv);
            intClear();
            fleXenv.addTrggr(dC, 'mousemove', tSelectMouse);
            sC.mTBox = [ dDv.xPos + 10, dDv.xPos + sC.cntRSize[0] - 10, dDv.yPos + 10, dDv.yPos + sC.cntRSize[1] - 10 ];
        });

        function tSelectMouse(e) {
            if (!e)
                e = wD.event;
            var mX = e.clientX,
                mY = e.clientY,
                mdX = mX + sC.xScrld,
                mdY = mY + sC.yScrld;
            sC.mOnXEdge = (mdX < sC.mTBox[0] || mdX > sC.mTBox[1]) ? 1 : 0;
            sC.mOnYEdge = (mdY < sC.mTBox[2] || mdY > sC.mTBox[3]) ? 1 : 0;
            sC.xAw = mX - sC.inMposX;
            sC.yAw = mY - sC.inMposY;
            sC.sXdir = (sC.xAw > 40) ? 1 : (sC.xAw < -40) ? -1 : 0;
            sC.sYdir = (sC.yAw > 40) ? 1 : (sC.yAw < -40) ? -1 : 0;
            if ((sC.sXdir != 0 || sC.sYdir != 0) && !sC.tSelectFunc)
                sC.tSelectFunc = wD.setInterval( function() {
                    if (sC.sXdir == 0 && sC.sYdir == 0) {
                        wD.clearInterval(sC.tSelectFunc);
                        sC.tSelectFunc = false;
                        return;
                    }
                    pageScrolled();
                    if (sC.mOnXEdge == 1 || sC.mOnYEdge == 1)
                        dDv.contentScroll((sC.sXdir * sC.mOnXEdge) + "s", (sC.sYdir * sC.mOnYEdge) + "s", true);
                }, 45);
        }
        function intClear() {
            fleXenv.remTrggr(dC, 'mousemove', tSelectMouse);
            if (sC.tSelectFunc)
                wD.clearInterval(sC.tSelectFunc);
            sC.tSelectFunc = false;
            if (sC.barClickRetard)
                wD.clearTimeout(sC.barClickRetard);
            if (sC.barClickScroll)
                wD.clearInterval(sC.barClickScroll);
        }
        function pageScrolled() {
            sC.xScrld = (wD.pageXOffset) ? wD.pageXOffset : (dC.documentElement && dC.documentElement.scrollLeft) ? dC.documentElement.scrollLeft : 0;
            sC.yScrld = (wD.pageYOffset) ? wD.pageYOffset : (dC.documentElement && dC.documentElement.scrollTop) ? dC.documentElement.scrollTop : 0;
        }

        dDv.scrollUpdate = function(recurse) {
            if (tDv.getSize[1]() === 0 || tDv.getSize[0]() === 0)
                return;
            cDv.sY.padding = '1px';
            var reqH = sC.reqS[0], reqV = sC.reqS[1], vBr = tDv.vrt, hBr = tDv.hrz, vUpReq, hUpReq, cPSize = [];
            tDv.sY.width = dDv.offsetWidth - brdWidthLoss + 'px';
            tDv.sY.height = dDv.offsetHeight - brdHeightLoss + 'px';
            cPSize[0] = sC.cntRSize[0];
            cPSize[1] = sC.cntRSize[1];
            sC.reqS[0] = sC.getContentWidth() > sC.cntRSize[0];
            sC.reqS[1] = sC.getContentHeight() > sC.cntRSize[1];
            var stateChange = (reqH != sC.reqS[0] || reqV != sC.reqS[1] || cPSize[0] != sC.cntRSize[0] || cPSize[1] != sC.cntRSize[1]) ? true : false;
            vBr.sDv.setVisibility(sC.reqS[1]);
            hBr.sDv.setVisibility(sC.reqS[0]);
            vUpReq = (sC.reqS[1] || sC.forcedBar[1]);
            hUpReq = (sC.reqS[0] || sC.forcedBar[0]);
            sC.getContentWidth();
            sC.getContentHeight();
            sC.setHeight();
            sC.setWidth();
            if (!sC.reqS[0] || !sC.reqS[1] || sC.forcedHide[0] || sC.forcedHide[1])
                hBr.jBox.fHide();
            else
                hBr.jBox.fShow();
            if (vUpReq)
                updateScroll(vBr, (hUpReq && !sC.forcedHide[0]) ? sC.barSpace[1] : 0);
            else
                cDv.sY.top = "0";
            if (hUpReq)
                updateScroll(hBr, (vUpReq && !sC.forcedHide[1]) ? sC.barSpace[0] : 0);
            else
                cDv.sY.left = "0";
            if (stateChange && !recurse)
                dDv.scrollUpdate(true);
            cDv.sY.padding = '0px';
            sC.edge[0] = sC.edge[1] = false;
        };

        dDv.commitScroll = dDv.contentScroll = function(xPos, yPos, relative) {
            var reT = [ [ false, false ], [ false, false ] ], Bar;
            if ((xPos || xPos === 0) && sC.scroller[0]) {
                xPos = calcCScrollVal(xPos, 0);
                Bar = tDv.hrz.sBr;
                Bar.trgtScrll = (relative) ? Math.min(Math.max(Bar.mxScroll, Bar.trgtScrll - xPos), 0) : -xPos;
                Bar.contentScrollPos();
                reT[0] = [ -Bar.trgtScrll - Bar.targetSkew, -Bar.mxScroll ]
            }
            if ((yPos || yPos === 0) && sC.scroller[1]) {
                yPos = calcCScrollVal(yPos, 1);
                Bar = tDv.vrt.sBr;
                Bar.trgtScrll = (relative) ? Math.min(Math.max(Bar.mxScroll, Bar.trgtScrll - yPos), 0) : -yPos;
                Bar.contentScrollPos();
                reT[1] = [ -Bar.trgtScrll - Bar.targetSkew, -Bar.mxScroll ]
            }
            if (!relative)
                sC.edge[0] = sC.edge[1] = false;
            return reT;
        };

        dDv.scrollToElement = function(tEM) {
            if (tEM == null || !isddvChild(tEM))
                return;
            var sPos = findRCpos(tEM);
            dDv.contentScroll(sPos[0] + sC.paddings[2], sPos[1] + sC.paddings[0], false);
            dDv.contentScroll(0, 0, true);
        };

        copyStyles(pDv, dDv, '0px', [ 'border-left-width', 'border-right-width', 'border-top-width', 'border-bottom-width' ]);

        dDv.removeChild(pDv);
        dDv.scrollTop = 0;
        dDv.scrollLeft = 0;
        dDv.fleXcroll = true;
        classChange(dDv, 'flexcrollactive', false);
        dDv.scrollUpdate();
        dDv.contentScroll(oScrollX, oScrollY, true);
        if (urlBase.match(uReg)) {
            dDv.scrollToElement(dC.getElementById(urlBase.match(uReg)[1]));
        }
        tDv.fShow();

        sC.sizeChangeDetect = wD.setInterval( function() {
            var n = fDv.offsetHeight;
            if (n != sC.zTHeight) {
                dDv.scrollUpdate();
                sC.zTHeight = n;
            }
        }, 2500);

        function calcCScrollVal(v, i) {
            var stR = v.toString();
            v = parseFloat(stR);
            return parseInt((stR.match(/p$/)) ? v * sC.cntRSize[i] * 0.9 : (stR.match(/s$/)) ? v * sC.cntRSize[i] * 0.1 : v);
        }
        function camelConv(spL) {
            var spL = spL.split('-'),
                reT = spL[0],
                i;
            for (i = 1; parT = spL[i]; i++) {
                reT += parT.charAt(0).toUpperCase() + parT.substr(1);
            }
            return reT;
        }
        function getStyle(elem, style) {
            if (wD.getComputedStyle)
                return wD.getComputedStyle(elem, null).getPropertyValue(style);
            if (elem.currentStyle)
                return elem.currentStyle[camelConv(style)];
            return false;
        }

        function copyStyles(src, dest, replaceStr, sList) {
            var camelList = [];
            for ( var i = 0; i < sList.length; i++) {
                camelList[i] = camelConv(sList[i]);
                dest.style[camelList[i]] = getStyle(src, sList[i], camelList[i]);
                if (replaceStr)
                    src.style[camelList[i]] = replaceStr;
            }
        }
        
        function createDiv(typeName, noGenericClass) {
            var nDiv = dC.createElement('div');
            // ,pTx=dC.createTextNode('\u00a0');
            nDiv.id = targetId + '_' + typeName;
            nDiv.className = (noGenericClass) ? typeName : typeName + ' scrollgeneric';
            nDiv.getSize = [
                function() {
                    return nDiv.offsetWidth;
                },
                function() {
                    return nDiv.offsetHeight;
                }
            ];
            nDiv.setSize = [
                function(sVal) {
                    nDiv.sY.width = sVal;
                },
                function(sVal) {
                    //~ if(location.search.indexOf('debug=true')>0){ alert( sVal ); }
                    nDiv.sY.height = sVal;
                }
            ];
            nDiv.getPos = [
                function() {
                    return getStyle(nDiv, "left");
                },
                function() {
                    return getStyle(nDiv, "top");
                }
            ];
            nDiv.setPos = [
                function(sVal) {
                    nDiv.sY.left = sVal;
                },
                function(sVal) {
                    nDiv.sY.top = sVal;
                }
            ];
            nDiv.fHide = function() {
                nDiv.sY.visibility = "hidden";
            };
            nDiv.fShow = function(coPy) {
                nDiv.sY.visibility = (coPy) ? getStyle(coPy, 'visibility') : "visible";
            };
            nDiv.sY = nDiv.style;
            // if(!noGenericClass) nDiv.appendChild(pTx);
            return nDiv;
        }
        function createScrollBars(ary, bse) {
            ary.sDv = createDiv(bse + 'base');
            ary.sFDv = createDiv(bse + 'basebeg');
            ary.sSDv = createDiv(bse + 'baseend');
            ary.sBr = createDiv(bse + 'bar');
            ary.sFBr = createDiv(bse + 'barbeg');
            ary.sSBr = createDiv(bse + 'barend');
            tDv.appendChild(ary.sDv);
            ary.sDv.appendChild(ary.sBr);
            ary.sDv.appendChild(ary.sFDv);
            ary.sDv.appendChild(ary.sSDv);
            ary.sBr.appendChild(ary.sFBr);
            ary.sBr.appendChild(ary.sSBr);
        }
        function prepareScroll(bAr, reqSpace) {
            var sDv = bAr.sDv,
                sBr = bAr.sBr,
                i = sBr.indx;
            sBr.minPos = bAr.barPadding[0];
            sBr.ofstParent = sDv;
            sBr.mDv = mDv;
            sBr.scrlTrgt = cDv;
            sBr.targetSkew = 0;
            updateScroll(bAr, reqSpace, true);

            sBr.doScrollPos = function() {
                sBr.curPos = (Math.min(Math.max(sBr.curPos, 0), sBr.maxPos));
                sBr.trgtScrll = parseInt((sBr.curPos / sBr.sRange) * sBr.mxScroll);
                sBr.targetSkew = (sBr.curPos == 0) ? 0 : (sBr.curPos == sBr.maxPos) ? 0 : sBr.targetSkew;
                sBr.setPos[i](sBr.curPos + sBr.minPos + "px");
                cDv.setPos[i](sBr.trgtScrll + sBr.targetSkew + "px");
            };

            sBr.contentScrollPos = function() {
                sBr.curPos = parseInt((sBr.trgtScrll * sBr.sRange) / sBr.mxScroll);
                sBr.targetSkew = sBr.trgtScrll - parseInt((sBr.curPos / sBr.sRange) * sBr.mxScroll);
                sBr.curPos = (Math.min(Math.max(sBr.curPos, 0), sBr.maxPos));
                sBr.setPos[i](sBr.curPos + sBr.minPos + "px");
                sBr.setPos[i](sBr.curPos + sBr.minPos + "px");
                cDv.setPos[i](sBr.trgtScrll + "px");
            };

            sC.barZ = getStyle(sBr, 'z-index');
            sBr.sY.zIndex = (sC.barZ == "auto" || sC.barZ == "0" || sC.barZ == 'normal') ? 2 : sC.barZ;
            mDv.sY.zIndex = getStyle(sBr, 'z-index');

            sBr.onmousedown = function() {
                sBr.clicked = true;
                sC.goScroll = sBr;
                sBr.scrollBoth = false;
                sBr.moved = false;
                fleXenv.addTrggr(dC, 'selectstart', retFalse);
                fleXenv.addTrggr(dC, 'mousemove', mMoveBar);
                fleXenv.addTrggr(dC, 'mouseup', mMouseUp);
                return false;
            };

            sBr.onmouseover = intClear;

            sDv.onmousedown = sDv.ondblclick = function(e) {
                if (!e) {
                    var e = wD.event;
                }
                if (e.target && (e.target == bAr.sFBr || e.target == bAr.sSBr || e.target == bAr.sBr))
                    return;
                if (e.srcElement && (e.srcElement == bAr.sFBr || e.srcElement == bAr.sSBr || e.srcElement == bAr.sBr))
                    return;
                var relPos, mV = [];
                pageScrolled();
                sC.mDPosFix();
                findPos(sBr);
                relPos = (sBr.vertical) ? e.clientY + sC.yScrld - sBr.yPos : e.clientX + sC.xScrld - sBr.xPos;
                mV[sBr.indx] = (relPos < 0) ? sC.baseAct[0] : sC.baseAct[1];
                mV[1 - sBr.indx] = 0;
                dDv.contentScroll(mV[0], mV[1], true);
                if (e.type != "dblclick") {
                    intClear();
                    sC.barClickRetard = wD.setTimeout( function() {
                        sC.barClickScroll = wD.setInterval( function() {
                            dDv.contentScroll(mV[0], mV[1], true);
                        }, 80)
                    }, 425);
                }
                return false;
            }
            sDv.setVisibility = function(r) {
                if (r) {
                    sDv.fShow(dDv);
                    sC.forcedHide[i] = getStyle(sDv, "visibility") == "hidden";
                    (sC.forcedHide[i] ? sBr.fHide() : sBr.fShow(dDv));
                    sC.scroller[i] = true;
                    classChange(sDv, "", "flexinactive");
                } else {
                    sDv.fHide();
                    sBr.fHide();
                    sC.forcedBar[i] = getStyle(sDv, "visibility") != "hidden";
                    sC.scroller[i] = false;
                    sBr.curPos = 0;
                    cDv.setPos[i]('0px');
                    classChange(sDv, "flexinactive", "");
                }
                mDv.setPos[1 - i]((sC.forcedPos[i] && (r || sC.forcedBar[i]) && !sC.forcedHide[i]) ? sC.barSpace[1 - i] - sC.paddings[i * 2] + "px" : "-" + sC.paddings[i * 2] + "px");
            };
            sDv.onmouseclick = retFalse;
        }

        function updateScroll(bAr, reqSpace, firstRun) {
            var sDv = bAr.sDv, sBr = bAr.sBr, sFDv = bAr.sFDv, sFBr = bAr.sFBr, sSDv = bAr.sSDv, sSBr = bAr.sSBr, i = sBr.indx;
            sDv.setSize[i](tDv.getSize[i]() - reqSpace + 'px');
            sDv.setPos[1 - i](tDv.getSize[1 - i]() - sDv.getSize[1 - i]() + 'px');
            sC.forcedPos[i] = parseInt(sDv.getPos[1 - i]()) === 0;
            bAr.padLoss = bAr.barPadding[0] + bAr.barPadding[1];
            bAr.baseProp = parseInt((sDv.getSize[i]() - bAr.padLoss) * 0.75);
            sBr.aSize = Math.min(Math.max(Math.min(parseInt(sC.cntRSize[i] / sC.cntSize[i] * sDv.getSize[i]()), bAr.baseProp), 45), bAr.baseProp);
            sBr.setSize[i](sBr.aSize + 'px');
            sBr.maxPos = sDv.getSize[i]() - sBr.getSize[i]() - bAr.padLoss;
            sBr.curPos = Math.min(Math.max(0, sBr.curPos), sBr.maxPos);
            sBr.setPos[i](sBr.curPos + sBr.minPos + 'px');
            sBr.mxScroll = mDv.getSize[i]() - sC.cntSize[i];
            sBr.sRange = sBr.maxPos;
            sFDv.setSize[i](sDv.getSize[i]() - sSDv.getSize[i]() + 'px');
            sFBr.setSize[i](sBr.getSize[i]() - sSBr.getSize[i]() + 'px');
            sSBr.setPos[i](sBr.getSize[i]() - sSBr.getSize[i]() + 'px');
            sSDv.setPos[i](sDv.getSize[i]() - sSDv.getSize[i]() + 'px');
            if (!firstRun)
                sBr.doScrollPos();
            sC.fixIEDispBug();
        }

        sC.mDPosFix = function() {
            mDv.scrollTop = 0;
            mDv.scrollLeft = 0;
            dDv.scrollTop = 0;
            dDv.scrollLeft = 0;
        };

        this.addTrggr(wD, 'load', function() {
            if (dDv.fleXcroll)
                dDv.scrollUpdate();
        });
        
        this.addTrggr(wD, 'resize', function() {
            if (dDv.refreshTimeout)
                wD.clearTimeout(dDv.refreshTimeout);
            dDv.refreshTimeout = wD.setTimeout( function() {
                if (dDv.fleXcroll)
                    dDv.scrollUpdate();
            }, 80);
        });

        for ( var j = 0, inputName; inputName = focusProtectList[j]; j++) {
            var inputList = dDv.getElementsByTagName(inputName);
            for ( var i = 0, formItem; formItem = inputList[i]; i++) {
                fleXenv.addTrggr(formItem, 'focus', function() {
                    dDv.focusProtect = true;
                });
                fleXenv.addTrggr(formItem, 'blur', function() {
                    dDv.focusProtect = false;
                });
            }
        }

        function retFalse() {
            return false;
        }
        
        function mMoveBar(e) {
            if (!e) {
                var e = wD.event;
            }
            var FCBar = sC.goScroll, movBr, maxx, xScroll, yScroll;
            if (FCBar == null)
                return;
            if (!fleXenv.w3events && !e.button)
                mMouseUp();
            maxx = FCBar.scrollBoth ? 2 : 1;
            for ( var i = 0; i < maxx; i++) {
                movBr = (i == 1) ? FCBar.scrlTrgt.vBar : FCBar;
                if (FCBar.clicked) {
                    if (!movBr.moved) {
                        sC.mDPosFix();
                        findPos(movBr);
                        findPos(movBr.ofstParent);
                        movBr.pointerOffsetY = e.clientY - movBr.yPos;
                        movBr.pointerOffsetX = e.clientX - movBr.xPos;
                        movBr.inCurPos = movBr.curPos;
                        movBr.moved = true;
                    }
                    movBr.curPos = (movBr.vertical) ? e.clientY - movBr.pointerOffsetY - movBr.ofstParent.yPos - movBr.minPos : e.clientX - movBr.pointerOffsetX - movBr.ofstParent.xPos - movBr.minPos;
                    if (FCBar.scrollBoth)
                        movBr.curPos = movBr.curPos + (movBr.curPos - movBr.inCurPos);
                    movBr.doScrollPos();
                } else
                    movBr.moved = false;
            }
        }

        function mMouseUp() {
            if (sC.goScroll != null) {
                sC.goScroll.clicked = false;
            }
            sC.goScroll = null;
            fleXenv.remTrggr(dC, 'selectstart', retFalse);
            fleXenv.remTrggr(dC, 'mousemove', mMoveBar);
            fleXenv.remTrggr(dC, 'mouseup', mMouseUp);
        }

        function mWheelProc(e) {
            if (!e)
                e = wD.event;
            if (!this.fleXcroll)
                return;
            var scrDv = this, vEdge, hEdge, hoverH = false, delta = 0, iNDx;
            sC.mDPosFix();
            hElem = (e.target) ? e.target : (e.srcElement) ? e.srcElement : this;
            if (hElem.id && hElem.id.match(/_hscroller/))
                hoverH = true;
            if (e.wheelDelta)
                delta = -e.wheelDelta;
            if (e.detail)
                delta = e.detail;
            delta = (delta < 0) ? -1 : +1;
            iNDx = (delta < 0) ? 0 : 1;
            sC.edge[1 - iNDx] = false;
            if ((sC.edge[iNDx] && !hoverH) || (!sC.scroller[0] && !sC.scroller[1]))
                return;
            if (sC.scroller[1] && !hoverH)
                scrollState = dDv.contentScroll(false, sC.wheelAct[iNDx], true);
            vEdge = !sC.scroller[1]
                    || hoverH
                    || (sC.scroller[1] && ((scrollState[1][0] == scrollState[1][1] && delta > 0) || (scrollState[1][0] == 0 && delta < 0)));
            if (sC.scroller[0] && (!sC.scroller[1] || hoverH))
                scrollState = dDv.contentScroll(sC.wheelAct[iNDx], false, true);
            hEdge = !sC.scroller[0]
                    || (sC.scroller[0] && sC.scroller[1] && vEdge && !hoverH)
                    || (sC.scroller[0] && ((scrollState[0][0] == scrollState[0][1] && delta > 0) || (scrollState[0][0] == 0 && delta < 0)));
            if (vEdge && hEdge && !hoverH)
                sC.edge[iNDx] = true;
            else
                sC.edge[iNDx] = false;
            if (e.preventDefault)
                e.preventDefault();
            return false;
        }

        function isddvChild(elem) {
            while (elem.parentNode) {
                elem = elem.parentNode;
                if (elem == dDv)
                    return true;
            }
            return false;
        }

        function findPos(elem) {
            //modified from firetree.net
            var obj = elem, curleft = 0, curtop = 0, monc = "";
            if (obj.offsetParent) {
                while (obj) {
                    curleft += obj.offsetLeft;
                    curtop += obj.offsetTop;
                    obj = obj.offsetParent;
                    monc += curtop + " ";
                }
            } else if (obj.x) {
                curleft += obj.x;
                curtop += obj.y;
            }
            elem.xPos = curleft;
            elem.yPos = curtop;
        }

        function findRCpos(elem) {
            var obj = elem, curleft = 0, curtop = 0;
            while (!obj.offsetHeight && obj.parentNode && obj != cDv
                    && getStyle(obj, 'display') == "inline") {
                obj = obj.parentNode;
            }
            if (obj.offsetParent) {
                while (obj != cDv) {
                    curleft += obj.offsetLeft;
                    curtop += obj.offsetTop;
                    obj = obj.offsetParent;
                }
            }
            return [ curleft, curtop ];
        }

        function classChange(elem, addClass, remClass) {
            if (!elem.className)
                elem.className = '';
            var clsnm = elem.className;
            if (addClass
                    && !clsnm.match(RegExp("(^|\\s)" + addClass + "($|\\s)")))
                clsnm = clsnm.replace(/(\S$)/, '$1 ') + addClass;
            if (remClass)
                clsnm = clsnm.replace( RegExp("((^|\\s)+" + remClass + ")+($|\\s)", "g"), '$2').replace(/\s$/, '');
            elem.className = clsnm;
        }
    },
    //main code end
    globalInit : function() {
        if (fleXenv.catchFastInit)
            window.clearInterval(fleXenv.catchFastInit);
        var regg = /#([^#.]*)$/, urlExt = /(.*)#.*$/, matcH, i, anchoR, anchorList = document.getElementsByTagName("a"), urlBase = document.location.href;
        if (urlBase.match(urlExt))
            urlBase = urlBase.match(urlExt)[1];
        ///
        /* we don't care about preserving anchors with href's of "#something", and what's more, setting anchor.style.position = "relative" is hurting ie6 */
        /*
        for (i = 0; anchoR = anchorList[i]; i++) {
            if (anchoR.href && anchoR.href.match(regg)
                    && anchoR.href.match(urlExt)
                    && urlBase === anchoR.href.match(urlExt)[1]) {
                anchoR.fleXanchor = true;
                fleXenv.addTrggr(anchoR, 'click', function(e) {
                    if (!e)
                        e = window.event;
                    var clickeD = (e.srcElement) ? e.srcElement : this;
                    while (!clickeD.fleXanchor && clickeD.parentNode) {
                        clickeD = clickeD.parentNode;
                    }
                    if (!clickeD.fleXanchor)
                        return;
                    var tEL = document.getElementById(clickeD.href
                            .match(regg)[1]), eScroll = false;
                    if (tEL == null)
                        tEL = (tEL = document.getElementsByName(clickeD.href.match(regg)[1])[0]) ? tEL : null;
                    if (tEL != null) {
                        var elem = tEL;
                        while (elem.parentNode) {
                            elem = elem.parentNode;
                            if (elem.scrollToElement) {
                                elem.scrollToElement(tEL);
                                eScroll = elem;
                            }
                        }
                        if (eScroll) {
                            if (e.preventDefault)
                                e.preventDefault();
                            document.location.href = "#" + clickeD.href.match(regg)[1];
                            eScroll.fleXdata.mDPosFix();
                            return false;
                        }
                    }
                });
            }
        }
        */
        fleXenv.initByClass();
        if (window.onfleXcrollRun)
            window.onfleXcrollRun();
    },

    initByClass : function() {
        if (fleXenv.initialized)
            return;
        fleXenv.initialized = true;
        var fleXlist = fleXenv.getByClassName(document.getElementsByTagName("body")[0], "div", 'flexcroll');
        for ( var i = 0, tgDiv; tgDiv = fleXlist[i]; i++)
            fleXenv.fleXcrollMain(tgDiv);
    },

    getByClassName : function(elem, elType, classString) {
        //v1.1fleX
        if (typeof (elem) == 'string')
            elem = document.getElementById(elem);
        if (elem == null)
            return false;
        var regExer = new RegExp("(^|\\s)" + classString + "($|\\s)"), clsnm, retArray = [], key = 0;
        var elems = elem.getElementsByTagName(elType);
        for ( var i = 0, pusher; pusher = elems[i]; i++) {
            if (pusher.className && pusher.className.match(regExer)) {
                retArray[key] = pusher;
                key++;
            }
        }
        return retArray;
    },

    catchFastInit : window.setInterval( function() {
        var dElem = document.getElementById('flexcroll-init');
        if (dElem != null) {
            fleXenv.initByClass();
            window.clearInterval(fleXenv.catchFastInit);
        }
    }, 100),

    addTrggr : function(elm, eventname, func) {
        if (!fleXenv.addChckTrggr(elm, eventname, func) && elm.attachEvent) {
            elm.attachEvent('on' + eventname, func);
        }
    },

    addChckTrggr : function(elm, eventname, func) {
        if (elm.addEventListener) {
            elm.addEventListener(eventname, func, false);
            fleXenv.w3events = true;
            window.addEventListener("unload", function() {
                fleXenv.remTrggr(elm, eventname, func);
            }, false);
            return true;
        } else
            return false;
    },

    remTrggr : function(elm, eventname, func) {
        if (!fleXenv.remChckTrggr(elm, eventname, func) && elm.detachEvent)
            elm.detachEvent('on' + eventname, func);
    },

    remChckTrggr : function(elm, eventname, func) {
        if (elm.removeEventListener) {
            elm.removeEventListener(eventname, func, false);
            return true;
        } else
            return false;
    }

};
fleXenv.fleXcrollInit = function() {
    $(fleXenv.globalInit);
};

  
function CSBfleXcroll(targetId) {
    fleXenv.fleXcrollMain(targetId);
}

fleXenv.fleXcrollInit();
