﻿var $jQ = jQuery.noConflict();

jQuery(window).load(function () {
    initFancyBox();
	
	//open print pages in a new window
   $jQ('a.print').click(function () {
    	window.open(this.href, "printPage", "width=800,height=600,menubar=yes,scrollbars=1").focus();
    	return false;
  	});
	

})
jQuery(function () {
    initCufon();
    initPage();
    initHoverIE();
    flyoutResize()
    menu();
    inputDefaultValue();

    //initDropDown();
});

function flyoutResize() {
    var imgWidth = 7;
    $jQ('.absContainer').each(function () {
        var me = $jQ(this);
        var resize = me.width() - imgWidth * 2;
        $jQ('.bottomMiddle', me).width(resize);
    })
}

function inputDefaultValue() {
    el = $jQ('#nav :text');

    el.bind('focusin', function (e) {
        $jQ(this)
            .data('content', $jQ(this).val())
                .val('');
    })
  .bind('focusout', function (e) {
      if ($jQ(this).val() == '') {
          $jQ(this).val($jQ(this).data('content'));
      }
  });
}

function menu() {
    var event = false;

    var el = $jQ('#nav select');

    el.bind('focus', function () {
        event = true;
    })
    el.bind('blur', function () {
        event = false;
    })

    $jQ('#nav li').hover(function () {
        event = false;
        var me = $jQ(this);
        $jQ('a', me).css({ backgroundPosition: '0px -88px' })
        $jQ('ul', me).show();
    },
	function () {
	    var me = $jQ(this);
	    if (!event) {
	        $jQ('a', me).css({ backgroundPosition: '0px 0px' })
	        $jQ('ul', me).hide();
	    }
	});
}

function initCufon() {
	Cufon.replace('.content .title h2', { fontFamily: 'ClarendonLT ExtraBold' });
	Cufon.replace('.text p a', { hover: true, fontFamily: 'ClarendonLT ExtraBold' });
	Cufon.replace('.dynamicContent .title h2', { fontFamily: 'ClarendonLT ExtraBold' });
    Cufon.replace('.dynamicContent p a', { hover: true, fontFamily: 'ClarendonLT ExtraBold' }); //ExtraBold can also be set to medium if need be
}
function initHoverIE() {
    jQuery('body').NVhoverIE({
        hoverItem: '.btn input'
    });
}
function initFancyBox() {
    var _opener = jQuery('#fancy-opener');
    _opener.simpleLightbox({
        faderOpacity: 0.7,
        faderBackground: '#000000'
    });
    _opener.trigger('click');
}
function initPage() {
    clearFormFields({
        clearInputs: true,
        clearTextareas: true,
        passwordFieldText: false,
        addClassFocus: "focus",
        filterClass: "default"
    });
}
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.filterClass) o.filterClass = "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) == -1) {
                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) == -1) {
                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";
    }
}
jQuery.fn.NVhoverIE = function (_options) {
    var _options = jQuery.extend({
        hoverItem: 'div',
        hoverClass: 'hover'
    }, _options);

    return this.each(function () {
        var _this = jQuery(this);
        var _hoverItem = jQuery(_options.hoverItem, _this);
        var _hoverClass = _options.hoverClass;
        if (jQuery.browser.msie && jQuery.browser.version < 7) {
            _hoverItem.each(function () {
                jQuery(this).hover(
					function () {
					    jQuery(this).addClass(_hoverClass)
					},
					function () {
					    jQuery(this).removeClass(_hoverClass)
					}
				)
            })
        }
    });
}
function initDropDown() {
    var nav = document.getElementById("nav");
    if (nav) {
        var lis = nav.getElementsByTagName("li");
        for (var i = 0; i < lis.length; i++) {
            if (lis[i].getElementsByTagName("ul").length > 0) {
                lis[i].className += " has-drop-down"
                lis[i].getElementsByTagName("a")[0].className += " has-drop-down-a"
            }
            lis[i].onmouseover = function () {
                this.className += " hover";
            }
            lis[i].onmouseout = function () {
                this.className = this.className.replace(" hover", "");
            }
        }
    }
}

jQuery.fn.simpleLightbox = function (_options) {
    var _options = jQuery.extend({
        lightboxContentBlock: '.lightbox',
        faderOpacity: 0.5,
        faderBackground: '#ffffff',
        closeLink: 'a.close-btn',
        href: true,
        onClick: null
    }, _options);

    return this.each(function (i, _this) {
        var _this = jQuery(_this);
        if (!_options.href)
            _this.lightboxContentBlock = _options.lightboxContentBlock;
        else _this.lightboxContentBlock = _this.attr('href');
        if (_this.lightboxContentBlock != '' && _this.lightboxContentBlock.length > 1) {
            _this.faderOpacity = _options.faderOpacity;
            _this.faderBackground = _options.faderBackground;
            _this.closeLink = _options.closeLink;
            var _fader;
            var _lightbox = $jQ(_this.lightboxContentBlock);
            if (!jQuery('div.lightbox-fader').length)
                _fader = $jQ('body').append('<div class="lightbox-fader"></div>');
            _fader = jQuery('div.lightbox-fader');
            _lightbox.css({
                'zIndex': 991
            });
            _fader.css({
                opacity: _this.faderOpacity,
                backgroundColor: _this.faderBackground,
                display: 'none',
                position: 'absolute',
                top: 0,
                left: 0,
                zIndex: 990,
                textIndent: -9999
            }).text('$nbsp');
            _lightbox.shownFlag = false;
            _this.click(function () {
                if (jQuery.isFunction(_options.onClick)) {
                    _options.onClick.apply(_this);
                }
                _lightbox.shownFlag = true;
                _lightbox.hide();
                jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
                _fader.fadeIn(300, function () {
                    _lightbox.fadeIn(400);
                    jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
                });
                jQuery('span.playButton').click();
                return false;
            });
            jQuery(_this.closeLink).click(function () {
                _lightbox.fadeOut(400, function () {
                    _fader.fadeOut(300);
                    _scroll = false;
                });
                return false;
            });
            _fader.click(function () {
                _lightbox.fadeOut(400, function () {
                    _fader.fadeOut(300);
                });
                return false;
            });
            var _scroll = false;
            jQuery.fn.simpleLightbox.positionLightbox = function (_lbox) {
                if (!_lbox.shownFlag) return false;
                var _height = 0;
                var _width = 0;
                var _minWidth = $jQ('body > div:eq(0)').outerWidth();
                if (window.innerHeight) {
                    _height = window.innerHeight;
                    _width = window.innerWidth;
                } else {
                    _height = document.documentElement.clientHeight;
                    _width = document.documentElement.clientWidth;
                }
                var _thisHeight = _lbox.outerHeight();
                var _page = jQuery('body');
                if (_lbox.length) {
                    if (_width < _minWidth) { _fader.css('width', _minWidth); } else { _fader.css('width', '100%'); }
                    if (_height > _page.innerHeight()) _fader.css('height', _height); else _fader.css('height', _page.innerHeight());
                    if (_height > _thisHeight) {
                       if ($jQ.browser.msie && $jQ.browser.version < 7) {
                            _lbox.css({
                                position: 'absolute',
                                top: (document.documentElement.scrollTop + (_height - _thisHeight) / 2) + "px"
                            });
                        } else {
                            _lbox.css({
                                position: 'fixed',
                                top: ((_height - _lbox.outerHeight()) / 2) + "px"
                            });
                        }
                    }
                    else {
                        var _fh = parseInt(_fader.css('height'));
                        if (!_scroll) {
                            if (_fh - _thisHeight > parseInt($jQ(document).scrollTop())) {
                                _fh = parseInt($jQ(document).scrollTop())
                                _scroll = _fh;
                            } else {
                                _scroll = _fh - _thisHeight;
                            }
                        }
                        _lbox.css({
                            position: 'absolute',
                            top: _scroll
                        });
                    }
                    if (_width > _lbox.outerWidth()) _lbox.css({ left: ((_width - _lbox.outerWidth()) / 2 + 10) + "px" });
                    else _lbox.css({ position: 'absolute', left: 0 });
                }
            }

            jQuery(window).resize(function () {
                if (_lightbox.is(':visible'))
                    jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
            });
            jQuery(window).scroll(function () {
                if (_lightbox.is(':visible'))
                    jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
            });

            jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
            $jQ(document).keydown(function (e) {
                if (!e) evt = window.event;
                if (e.keyCode == 27) {
                    _lightbox.fadeOut(400, function () {
                        _fader.fadeOut(300);
                    });
                }
            });
        }
    });
}
