var domReady = false;
var test_box_top = 0;
var test_box_bot = 0;
var browser_height = 0;
var browser_scroll_height = 0;
var delay;
window.addEvent('domready', function () {
    domReady = true;
    //FAMILY DROPDOWN
    if ($('header_menu_dropdown')) {
        $('header_menu_dropdown').addEvents({
            mouseenter: function () {
                //GRAB THE HEIGHT OF OUR DROPDOWN
                var height1 = $('family_slide').getStyle('height').split("px");
                var height2 = this.getStyle('height').split("px");
                var height = Number(height1[0]) + Number(height2[0]) + 10;
                //EXPAND THE BOX TO OUR DETERMINED HEIGHT
                this.set('tween', {
                    duration: 1000,
                    transition: Fx.Transitions.Bounce.easeOut
                }).tween('height', height);
                $$(".splash_hide").setStyle("z-index", 0);
            },
            mouseleave: function () {
                this.set('tween', {
                    onComplete: function () {
                        $$(".splash_hide").setStyle("z-index", 999);
                    }
                }).tween('height', '27px');
                //this.set('tween', {}).tween('height', '27px');
            }
        });
    }
});
function fixPhone(e, allow, line) {
    if (e.keyCode == 8 || e.keyCode == 9) { return; }
    var lenL7 = document.getElementById(line).value;
    var k = document.all ? parseInt(e.keyCode) : parseInt(e.which);
    var u = String.fromCharCode(k);
    if (lenL7.length == 3) {
        document.getElementById(line).value = document.getElementById(line).value + '-';
    }
    if (lenL7.length == 7) {
        document.getElementById(line).value = document.getElementById(line).value + '-';
    }
    return (allow.indexOf(String.fromCharCode(k)) != -1);
}
function fixZip(e, allow, line) {
	if (e.keyCode == 8 || e.keyCode == 9) { return; }
	var lenL7 = document.getElementById(line).value;
	var k = document.all ? parseInt(e.keyCode) : parseInt(e.which);
	var u = String.fromCharCode(k);
	//    alert(u.charCodeAt(0));
	if (u.charCodeAt(0) == 0 || u.charCodeAt(0) == 13 || u.charCodeAt(0) == 8) { return; }
	else { return (allow.indexOf(String.fromCharCode(k)) != -1); }
}
function fixEmail(line) {
    document.getElementById(line).value = document.getElementById(line).value.toLowerCase().trim();
    document.getElementsByName("errEmail").innerHTML = "";
    return;
}
function tip_over(e) {
    if (domReady) {
        var e_con = $(e).className.split(" ");
        var n = e_con[1];
        if ($chk($("bottips_" + e_con[1]))) {
            //THE TOOLTIP HAS BEEN CREATED - JUST UNHIDE IT
            positionContainer(e, $("bottips_" + e_con[1]), $("arrow_" + e_con[1]));
            delay = (function () {
                $("bottips_" + e_con[1]).setStyle("visibility", "visible");
                $("arrow_" + e_con[1]).setStyle("visibility", "visible");
            }).delay(500);
        } else {
            //CREATE OUR TIP CONTAINER
            var container = new Element('div').set({
                'class': "bottips_container",
                'id': "bottips_" + e_con[1],
                'styles': { "visibility": "hidden" }
            });
            //CREATE THE PARTS OF OUR TIP
            var arrow = new Element('div', { 'id': 'arrow_' + e_con[1], 'class': 'bottips_arrow', 'styles': { "visibility": "hidden" }});
            var top = new Element('div', { 'class': 'bottips_top' });
            var content = new Element('div', { 'class': 'bottips_content' });
            var bottom = new Element('div', { 'class': 'bottips_bottom' });
            //GRAB THE CONTENT FROM OUR SPECIFIED DIV TAG AND SET OUT CONTENT ELEMENT WITH THE HTML
            content.set({ 'html': $(n).get('html') });
            //REMOVE THE TAG AND ALL CHILDS
            $(n).destroy();
            //PUT ALL THE PARTS IN OUR CONTAINER
            container.adopt(top, content, bottom);
            //ADD THE TIP TO THE DOCUMENT
            //$(e).getParent().adopt(container);
			$(document.body).adopt(container);
			$(document.body).adopt(arrow);
            //
            positionContainer(e, container, arrow);
            //
            delay = (function () {
                //
                positionContainer(e, container, arrow);
                $("bottips_" + e_con[1]).setStyle("visibility", "visible");
                $("arrow_" + e_con[1]).setStyle("visibility", "visible");
            }).delay(500);
        }
		
    }
}
function tip_out(e) {
    if (domReady) {
        //HIDE OUR TOOLTIP
        delay = $clear(delay);
        var e_con = $(e).className.split(" ");
        $("bottips_" + e_con[1]).setStyle("visibility", "hidden");
        $("arrow_" + e_con[1]).setStyle("visibility", "hidden");
    }
}
function positionContainer(e, container, arrow) {
    //
		browser_height = getBrowserHeight();
		browser_width = getBrowserWidth();
		browser_scroll_height = getBrowserScrollHeight();
		test_box_top = browser_scroll_height;
		test_box_bot = browser_height + browser_scroll_height;
		var body_size = $(document.body).getSize();
    //POSITION OUR CONTAINER
		var container_size = container.getSize();
		var container_pos = e.getPosition();
		var e_size = e.getSize();
		//928 = width of our body
		//35 extra padding
		var container_x = 938 - e_size.x - container_size.x - 30; 
		var container_y = container_pos.y - (container_size.y/2);
		container.setStyles({
			'top': container_y + 'px',
			'left': container_x - 8 + 'px'
		});
	//POSITION ARROW
   		var arrow_size = arrow.getSize();
		var arrow_pos_x = 938 - e_size.x - 46; 
		//var arrow_pos_x = container_pos.x - (container_size.x);
   	 	var arrow_pos_y = container_pos.y - (arrow_size.y / 4);
		arrow.setStyles({
			'top': arrow_pos_y + 'px',
			'left': arrow_pos_x + 'px'
		});
	//
    var container_position = container.getPosition();
    var container_bottom = container_position.y + container_size.y;
    var container_top = container_position.y;
    //
    if (container_bottom > test_box_bot) {
        //BOX IS OUTSIDE OF THE BOTTOM PART OF THE BROWSER
        var container_top = Number(container.getStyle("top").replace("px", ""));
        container.setStyles({ 'top': container_top + (browser_height + browser_scroll_height) - (container_size.y + container_position.y) + 'px'});
        container_top = Number(container.getStyle("top").replace("px", ""));
        //var arrow_top = Number(arrow.getStyle("top").replace("px", ""));
        //arrow.setStyles({'top': arrow_top + Math.abs(Math.abs(arrow_top) - Math.abs(container_top)) + 'px'});
    } else if (container_top < test_box_top) {
        //BOX IS OUTSIDE OF THE TOP PART OF THE BROWSER
        var container_top = Number(container.getStyle("top").replace("px", ""));
        container.setStyles({
            'top': (test_box_top) + 'px'
        });
        //container_top = Number(container.getStyle("top").replace("px", ""));
        //var arrow_top = Number(arrow.getStyle("top").replace("px", ""));
        //arrow.setStyles({ 'top': 0});
    }

}
/*function positionContainer(e, container, arrow) {
    //
    browser_height = getBrowserHeight();
    browser_scroll_height = getBrowserScrollHeight();
    test_box_top = browser_scroll_height;
    test_box_bot = browser_height + browser_scroll_height;
    //POSITION OUR CONTAINER
    var container_size = container.getSize();
    var arrow_size = arrow.getSize();
    var container_y = container_size.y / 2;
    var arrow_pos_x = container_size.x;
    var arrow_pos_y = (container_size.y / 2) + (arrow_size.y / 2);
    container.setStyles({
        'top': '-' + container_y + 'px'
    });
    arrow.setStyles({
        'top': arrow_pos_y - 22 + 'px',
        'left': arrow_pos_x - 9 + 'px'
    });
    //
    var container_position = container.getPosition();
    var container_bottom = container_position.y + container_size.y;
    var container_top = container_position.y;
    //
    if (container_bottom > test_box_bot) {
        //BOX IS OUTSIDE OF THE BOTTOM PART OF THE BROWSER
        var container_top = Number(container.getStyle("top").replace("px", ""));
        container.setStyles({ 'top': container_top + (browser_height + browser_scroll_height) - (container_size.y + container_position.y) + 'px', 'z-index': 1000000 });
        container_top = Number(container.getStyle("top").replace("px", ""));
        var arrow_top = Number(arrow.getStyle("top").replace("px", ""));
        arrow.setStyles({ 'top': arrow_top + Math.abs(Math.abs(arrow_top) - Math.abs(container_top)) + 'px'
        });
    } else if (container_top < test_box_top) {
        //BOX IS OUTSIDE OF THE TOP PART OF THE BROWSER
        container.setStyles({
            'top': (browser_scroll_height - test_box_top) + 'px',
			'z-index': 1000000
        });
        container_top = Number(container.getStyle("top").replace("px", ""));
        var arrow_top = Number(arrow.getStyle("top").replace("px", ""));
        arrow.setStyles({ 'top': 0
        });
    }
}*/
function getBrowserScrollHeight() {
    var scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
    return scrollTop;
}
function getBrowserWidth() {
    var viewportwidth;
    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
    if (typeof window.innerWidth != 'undefined') {

        viewportwidth = window.innerWidth;
    }
    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
    else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
        viewportwidth = document.documentElement.clientWidth;
    }
    // older versions of IE
    else {
        viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
    }
    //
    return viewportwidth;
}
function getBrowserHeight() {
    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
    if (typeof window.innerHeight != 'undefined') {
        viewportheight = window.innerHeight
    }
    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
    else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientHeight != 'undefined' && document.documentElement.clientHeight != 0) {
        viewportheight = document.documentElement.clientHeight
    }
    // older versions of IE
    else {
        viewportheight = document.getElementsByTagName('body')[0].clientHeight
    }
    return viewportheight;
}

/***********************************************************************************************/


