// JavaScript Document

function printPage() {
		if (window.navigator.userAgent.indexOf('Netscape') > 0) {
			window.setTimeout(window.print,1);
		} 
		else {
			window.print();
		}
	}
	
	
function intEmailContent(URL,Title) {
	if (!URL) {
		URL = escape(window.location);
	}
	if (!Title) {
		Title = escape(document.title);
	}
	window.location = 'mailto:?subject=' + Title + '&body=Look%20what%20I%20found%20on%20IntegrityWindows.com:%0A%0A' + Title + '%0A' + URL;
}

function galleryPopUp(url) {
window.open(url,'','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=660,height=660,left=100,top=100');
}

function popupImage(sTitle, sIgnore, url, intWidth, intHeight) {
// window.open(url,'popup','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=' + (intWidth + 40) + ',height=' + (intHeight + 40) + ',left=100,top=100');
window.open('popupIntegrity.aspx?image=' + url + '&title=' + sTitle,'popup','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=660,height=660,left=100,top=100');
}




// -------------------------------------------------------------------------------
//
// Old Ham in the Fridge Functions 
//
function clearText(thefield) { 
	if (thefield.defaultValue == thefield.value) { 
		thefield.value = "" 
		thefield.style.color = "#464646"
	}
	else {
		thefield.value = thefield.defaultValue
		thefield.style.color = "#7c7c7c" 
	}
}



/* Google Maps */

function locatorSearchAttributes() {
    var ret = parseInt($('table.searchattributes input:checked').val());
    if (!ret || ret == 0) {
        ret = '';
    } else {
        ret += parseInt($.gMapsLocator.locatorFlag);
    };
    return ret;
}
function geocode(address, city, state, postalCode, range, geocodeProvider, url) {
	if ((address.length + city.length + state.length + postalCode.length) == 0) {
		window.location = url;
		return false;
	};
	if (postalCode.length == 0 && (state.length != 0 && city.length == 0 || city.length != 0 && state.length == 0)) {
		alert('Enter a city and state/province');
		return false;
	};
	if (postalCode.length != 0 && !postalCode.match(/^\d{5}-\d{4}$|^\d{5}$|^[A-Z]\d[A-Z] \d[A-Z]\d$/)) {
		alert('Enter a valid postal code');
		return false;
	};
	switch (geocodeProvider) {
		case 'Google':
			var addr = address + ' ' + city + ' ' + state + ' ' + postalCode;
			window.status = 'searching ' + addr
			var geocoder = new google.maps.Geocoder();
			geocoder.geocode({ 'address': addr.trim() }, function (results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					window.location = url + '&p1=' + address + '&p2=' + city + '&p3=' + state + '&p4=' + postalCode + '&p5=' + range + '&p6=' + locatorSearchAttributes() + '&ll=' + results[0].geometry.location;
				} else {
					//alert("Geocode was not successful for the following reason: " + status);
					window.location = url;
				}
			});
			geocoder = null;
			break;
		case 'MapPoint':
			window.location = url + '&p1=' + address + '&p2=' + city + '&p3=' + state + '&p4=' + postalCode + '&p5=' + range + '&p6=' + locatorSearchAttributes();
			break;
	};
};

var locatorWidget = function () {
	return {
		init: function (geocodeProvider, locatorResultsPage, postalCodeInput, searchButton) {
//            widget = !widget ? '.locator-widget' : widget;
			postalCodeInput = !postalCodeInput ? '.locator-postal-code' : postalCodeInput;
			searchButton = !searchButton ? '.locator-go' : searchButton;
			var postalPrompt = 'Enter Zip/Postal Code';
//console.log(widget, $(widget).length);
//            $(widget).each(function () {
//                var w = $(this);
				$(searchButton).each(function () {
					var s = $(this);
//console.log(s.length);
					var p = $(s.siblings(postalCodeInput));
//console.log(w.attr('class'), s.attr('class'), p.attr('class'));
					var qs = new Querystring();
					if (qs.get('directions', '').length == 0) {
						p.val(qs.get('p4', ''));
						if (p.val().length == 0) {
							p.val(postalPrompt)
						};
					};
					s.click(function () {
//console.log(w.attr('class'), s.attr('class'), p.attr('class'));
						if (p.val() != '' && p.val() != postalPrompt) {
							geocode('', '', '', p.val(), '25', geocodeProvider, locatorResultsPage);
							return false;
						};
					});
					p.focus(function () {
//console.log(w.attr('class'), s.attr('class'), p.attr('class'));
						if (p.val() == postalPrompt) {
							p.val('');
						};
					});
					p.blur(function () {
						if (p.val().length == 0) {
							p.val(postalPrompt);
						};
					});
					p.keypress(function (event) {
						if (event.keyCode == 13 || event.which == 13) {
//console.log(w.attr('class'), s.attr('class'), p.attr('class'));
							s.click();
							return false;
						};
					});
				});
//            });

		}
	};
} ();

//Was seeing this error popup so added as stub. Most if not all instances calling this should be gone now.
function swapImage(o) { };

var topNavigation = function () {
    var primary;
    var secondary;
    var hider;
    var expanded = true;
    var expandItem = -1;
    return {
        init: function (effect, speed, delay, hoverWhenEmpty) {
            effect = !effect ? 'down' : effect;
            speed = !speed ? 'fast' : speed;
            delay = !delay ? 500 : delay;
            hoverWhenEmpty = !hoverWhenEmpty ? true : hoverWhenEmpty;
            primary = $('.master-menu-item');
            secondary = $('.master-menu-expanded');
            hider = $('<div></div>');
            hider.css('position', 'absolute');
            //hider.css('background-color', '#00ff00');
            hider.insertAfter(secondary[secondary.length - 1]);
            primary.each(function (index) {
                var p = $(this);
                //make sure whole LI area looks and acts clickable
                p.css('cursor', 'pointer');
                p.click(function () {
                    window.location = ($(this).children('a')).attr('href');
                });
                //current page primary nav effect
                var location = window.location.href;
                var link = p.children('a');
                var matchRef = new RegExp(link.attr('href').replace('\?', '\\?'), 'i');
                if (location.match(matchRef)) {
                    p.addClass(p.attr('class').split(' ')[0] + '-selected');
                    p.addClass('selected');
                    p.css('border', 'solid 3px #ee2424');
                    p.css('border-left', 'none');
                    p.css('border-right', 'none');
                    p.css('position', 'relative');
                    p.css('top', -5);
                    p.css('height', p.height() + 4);
                    link.css('color', '#ff6666');
                    link.css('margin-top', Number(link.css('margin-top').replace('px', '')) + 2);
                };
                //primary hover effect (background/border color + open secondary - if exists)
                p.hover(function () {
                    hider.mouseover();
                    var active = $(this);
                    var expandee = $(secondary[index]);
                    if (!active.hasClass('selected') && (hoverWhenEmpty || expandee.html().trim().length != 0)) {
                        var primaryLinks = primary.children('a');
                        var activeLink = active.children('a')
                        primaryLinks.css('border-color', '#cccccc');
                        primaryLinks.css('filter', 'alpha(opacity=40)');
                        primaryLinks.css('opacity', '0.4');
                        active.css('background-color', '#ee2424');
                        active.prev().children('a').css('border-color', 'transparent');
                        activeLink.css('border-color', '#ee2424')
                        activeLink.css('filter', '');
                        activeLink.css('opacity', '');
                        if (active.css('background-image') != 'none') {
                            active.css('background-position', -10000);
                            activeLink.css('padding-left', '');
                        }
                    };
                    //secondary expand effect
                    if (expandee.html().trim().length != 0) {
                        expandee.css('z-index', '1000000');
                        switch (effect) {
                            case 'wipe': //wipe tl->br
                                h = expandee.height();
                                w = expandee.width();
                                expandee.height(0);
                                expandee.width(0);
                                expandee.toggle(true);
                                expandee.delay(delay).animate({ height: h, width: w }, speed, function () {
                                    hider.css('left', expandee.offset().left - 40);
                                    hider.css('top', expandee.offset().top);
                                    hider.css('width', expandee.width() + 80);
                                    hider.css('height', expandee.height() + 40);
                                    hider.css('z-index', expandee.css('z-index') - 1);
                                    expanded = true;
                                });
                                break;
                            default: //straight down 
                                expandee.delay(delay).slideDown(speed, function () {
                                    hider.css('left', expandee.offset().left - 40);
                                    hider.css('top', expandee.offset().top);
                                    hider.css('width', expandee.width() + 80);
                                    hider.css('height', expandee.height() + 40);
                                    hider.css('z-index', expandee.css('z-index') - 1);
                                    expanded = true;
                                });
                        };
                    };
                }, function () {
                });
                //secondary hover effect - swap image
                //                var li = $(secondary[index]).find('li');
                //                li.hover(function () {
                //                    var i = $(this).find('img');
                //                    src = i.attr('src');
                //                    if (src) {
                //                        i.attr('src', src.replace('tn_', 'tn_glow_'));
                //                    };
                //                }, function () {
                //                    var i = $(this).find('img');
                //                    src = i.attr('src');
                //                    if (src) {
                //                        i.attr('src', src.replace('tn_glow_', 'tn_'));
                //                    };
                //                });
            });
            hider.each(function () {
                $(this).mouseover(function () {
                    if (expanded) {
                        hider.css('left', -10000);
                        primary.each(function () {
                            var p = $(this);
                            var l = p.children('a');
                            p.css('background-color', '');
                            l.css('border-color', '#ffffff')
                            l.css('filter', '');
                            l.css('opacity', '');
                            if (p.css('background-image') != 'none') {
                                p.css('background-position', 0);
                                l.css('padding-left', 10000);
                            }
                        });
                        secondary.stop(true, true).hide();
                    };
                });
            });
            $('#master-header, #content, #master-menu-search').each(function () {
                $(this).hover(function () {
                    hider.mouseover();
                }, function () {
                });
            });
            hider.mouseover();
            if (expandItem >= 0) {
                $($('.master-menu-item')[expandItem]).mouseover();
            };
        }
    };
} ();


/* SubNav Topic Selector */
// Horizontal menu about 500 pixels from the top
// Uses a list and no anchor links
// Shows one div.topic and hides all other div.topic divs
// List item 3 displays the third div.topic, li 2 displays div 2, etc.
// Expects: 
// div.subnav ul
//   li
//   li ...
// div.topic
// div.topic ...
var subNavTopicSelector = function () {
    var selectedIndex = 0;
    return {
        init: function () {
            // var top = $('.topic-selector');
            var li = $('.subnav li');
            // var img = $('.subnav li img');
            var topics = $('.topic');
            var topicsRelated = $('.topic-related-module-custom');
            li.each(function () {
                // var el = $(this);
                var menuListItem = $(this);
                menuListItem.hover(function () {
                    if (selectedIndex != menuListItem.index()) {
                        menuListItem.css('text-decoration', 'underline')
                        menuListItem.css('cursor', 'pointer')
                    };
                }, function () {
                    menuListItem.css('text-decoration', 'none');
                });
                menuListItem.click(function () {
                    selectedIndex = menuListItem.index();
                    var sibs = menuListItem.siblings('li');
                    var vis = $(topics[selectedIndex]);
                    var visibleRelated = $(topicsRelated[selectedIndex]);
                    // top.css('background-image', 'url(\'' + $(img[selectedIndex]).attr('src') + '\')');
                    vis.toggle(true);
                    vis.siblings('div.topic').toggle(false);
                    visibleRelated.toggle(true);
                    visibleRelated.siblings('div.topic-related-module-custom').toggle(false);
                    menuListItem.attr('class', 'topic-selected');
                    menuListItem.css('text-decoration', 'none');
                    sibs.attr('class', '');
                });
            });
            $(li[selectedIndex]).click();
        }
    };
} ();




$(window).load(function () {
    var anchor = decodeURI(self.document.location.hash.substring(1));
    if (anchor != "" && $('.subnav').length>0) {
        $('.subnav li')[$('.topic').index($('.topic.' + anchor))].click();
    }
});
