/*!
* http://www.it-ok.co.uk/
* Copyright 2011, IT-OK Limited
*/


$(document).ready(function () {
    initCufon();
    clearFormFields({
        clearInputs: true,
        clearTextareas: true,
        passwordFieldText: true,
        addClassFocus: "focus",
        filterClass: "default"
    });
    InitGallery();
    InitFadeDrop();
});
function InitFadeDrop() {
    var holder = $('#nav >li'), _speed = 400;
    holder.each(function (_i) {
        var _t;
        var hold = $(this),
			drop = hold.find('.drop');
        if (jQuery.browser.msie) {
            drop.hide();
        } else { drop.hide().css('opacity', 0); }

        hold.mouseenter(function () {
            if (_t) clearTimeout(_t);
            if (jQuery.browser.msie) {
                drop.show();
                $(this).addClass('hover');
            } else {
                drop.show().animate({ opacity: 1 }, { queue: false, duration: _speed });
            }
        }).mouseleave(function () {
            if (_t) clearTimeout(_t);
            if (jQuery.browser.msie) {
                drop.hide();
                $(this).removeClass('hover');
            } else {
                _t = setTimeout(function () {
                    drop.animate({ opacity: 0 }, { queue: false, duration: _speed, complete: function () { $(this).hide() } });
                }, 20);
            }
        })
    });
};
function InitGallery() {
    $('.visual').Gallery({
        speed: 800,
        duration: 7000,
        list: 'ul.fader>li',
        pager: 'ul.switcher',
        ieVersion: 6
    });
};
jQuery.fn.Gallery = function (_options) {
    // default options
    var _options = jQuery.extend({
        speed: 1200,
        duration: 6000,
        list: 'ul.fade>li',
        prev: 'a.prev',
        next: 'a.next',
        pager: 'ul.switcher',
        pause: '.pause',
        ieVersion: 6
    }, _options);

    return this.each(function () {
        // options
        var _hold = jQuery(this);
        var _speed = _options.speed;
        var _duration = _options.duration;
        var _list = _hold.find(_options.list);
        var _prev = _hold.find(_options.prev);
        var _next = _hold.find(_options.next);
        var _pause = _hold.find(_options.pause);
        var _ie = _options.ieVersion;
        var _f = true;
        var _a = _list.index(_list.filter('.active:eq(0)'));
        if (_a == -1) { _a = 0; _list.eq(_a).addClass('active') }
        var _i, _old = _a, _t;
        /*--------CREATING THUMBNAILS----------*/
        var _num = _hold.find(_options.pager);
        var _thumb = _num.find('li');
        _thumb.eq(_a).addClass('active');
        /*-------------------------------------------------*/
        if (jQuery.browser.msie && jQuery.browser.version < _ie) {
            _list.hide().eq(_a).show();
        } else { _list.show().css({ opacity: 0 }).eq(_a).css({ opacity: 1 }); }
        Run(_a);

        function Run(_a) {
            _t = setTimeout(function () {
                _a++; if (_a >= _list.length) { _a = 0 }
                ChangeFade(_a);
            }, _duration);
        };
        function ChangeFade(_new) {
            if (_new != _old) {
                if (jQuery.browser.msie && jQuery.browser.version < _ie) {
                    _list.eq(_old).removeClass('active').hide();
                    _list.eq(_new).addClass('active').show();
                } else {
                    _list.eq(_old).removeClass('active').animate({ opacity: 0 }, { queue: false, duration: _speed });
                    _list.eq(_new).addClass('active').animate({ opacity: 1 }, { queue: false, duration: _speed });
                }
                _thumb.eq(_old).removeClass('active');
                _thumb.eq(_new).addClass('active');
                _old = _new; _a = _new;
                if (_t) clearTimeout(_t);
                if (_f) { Run(_new); }
            };
        };
        _hold.mouseenter(function () {
            _f = false;
            clearTimeout(_t);
        }).mouseleave(function () {
            _f = true;
            Run(_a);
        });
        _thumb.hover(function () {
            _i = _thumb.index($(this));
            ChangeFade(_i);
            _a = _i;
            return false;
        });
        _next.hover(function () {
            _a++; if (_a == _list.length) { _a = 0 }
            ChangeFade(_a);
            return false;
        });
        _prev.hover(function () {
            _a--; if (_a == -1) { _a = _list.length - 1 }
            ChangeFade(_a);
            return false;
        });
    });
};
function clearFormFields(o) {
    if (o.clearInputs == null) o.clearInputs = true;
    if (o.clearTextareas == null) o.clearTextareas = true;
    if (o.passwordFieldText == null) o.passwordFieldText = false;
    if (o.addClassFocus == null) o.addClassFocus = false;
    if (!o.filter) o.filter = "default";
    if (o.clearInputs) {
        var inputs = document.getElementsByTagName("input");
        for (var i = 0; i < inputs.length; i++) {
            if ((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass)) {
                inputs[i].valueHtml = inputs[i].value;
                inputs[i].onfocus = function () {
                    if (this.valueHtml == this.value) this.value = "";
                    if (this.fake) {
                        inputsSwap(this, this.previousSibling);
                        this.previousSibling.focus();
                    }
                    if (o.addClassFocus && !this.fake) {
                        this.className += " " + o.addClassFocus;
                        this.parentNode.className += " parent-" + o.addClassFocus;
                    }
                }
                inputs[i].onblur = function () {
                    if (this.value == "") {
                        this.value = this.valueHtml;
                        if (o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
                    }
                    if (o.addClassFocus) {
                        this.className = this.className.replace(o.addClassFocus, "");
                        this.parentNode.className = this.parentNode.className.replace("parent-" + o.addClassFocus, "");
                    }
                }
                if (o.passwordFieldText && inputs[i].type == "password") {
                    var fakeInput = document.createElement("input");
                    fakeInput.type = "text";
                    fakeInput.value = inputs[i].value;
                    fakeInput.className = inputs[i].className;
                    fakeInput.fake = true;
                    inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
                    inputsSwap(inputs[i], null);
                }
            }
        }
    }
    if (o.clearTextareas) {
        var textareas = document.getElementsByTagName("textarea");
        for (var i = 0; i < textareas.length; i++) {
            if (textareas[i].className.indexOf(o.filterClass)) {
                textareas[i].valueHtml = textareas[i].value;
                textareas[i].onfocus = function () {
                    if (this.value == this.valueHtml) this.value = "";
                    if (o.addClassFocus) {
                        this.className += " " + o.addClassFocus;
                        this.parentNode.className += " parent-" + o.addClassFocus;
                    }
                }
                textareas[i].onblur = function () {
                    if (this.value == "") this.value = this.valueHtml;
                    if (o.addClassFocus) {
                        this.className = this.className.replace(o.addClassFocus, "");
                        this.parentNode.className = this.parentNode.className.replace("parent-" + o.addClassFocus, "");
                    }
                }
            }
        }
    }
    function inputsSwap(el, el2) {
        if (el) el.style.display = "none";
        if (el2) el2.style.display = "inline";
    }
}

