/* 
Berkshire Bank styles
author: Marcel Moreau for Bridgeline Software: http://bridgelinesw.com

Contents
- GLOBAL VARIABLES
- SUCKERFISH DROPDOWNS
- INPUT CLEAR
- ABOUT US HOVERS
- COMPARISON TABLES
- EXTERNAL LINKS

/*  GLOBAL VARIABLES
------------------------------------*/
var inEditMode = false;
var popUpVisible = false;

/*  SUCKERFISH DROPDOWNS
------------------------------------*/
// -- utility nav
sfHover = function() {
	var sfEls = document.getElementById("dd").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

/*  INPUT CLEAR
------------------------------------*/
function clearText(thefield){
	if (thefield.defaultValue==thefield.value)
	thefield.value = "";
	$('.search input').css('color','#494742');
} 

function resetText(thefield){
	if (thefield.value=="") {
		thefield.value = thefield.defaultValue;
    	$('.search input').css('color','#b2b2b2');
	}
}


/* wait for body to load */
$(document).ready(function() {

/*  MAIN NAV BACKGROUND IMAGE AND LAST IMAGE PIPE
------------------------------------*/
	var nav = $('#navMain');
	var listitem = $('#navMain > li');
	var tab = $('#navMain li a');
	var selected = $('#navMain li.selected');
	var count = listitem.index($('#navMain li.selected'));

	$(listitem).hover(
		function(){
			$(this).addClass('hover');
			$(listitem).removeClass('selected');
		}
		,
		function(){
			$(this).removeClass('hover');
			if (selected.length == 1) {
				$(listitem[count]).addClass('selected');
				$('#navMain').addClass('subnav');
			}
		 }
	);
	
	// show subnav's background image on hover of anywhere in area
	$(listitem).hover(function() {
        $(nav).addClass('subnav');
        $(nav).css('width','690px');
    }, function() {
        if (selected.length != 1){
		    $(nav).removeClass('subnav');
		    $(nav).css('width','');
	    }	
    })
	
	if (selected.length == 1){
		$(nav).addClass('subnav');
		$(nav).css('width','690px');
	}	
	
	$('#navMain li li:last-child').addClass('none');

/*  ABOUT US HOVERS
------------------------------------*/
	if (inEditMode){
        $('.blurb').css({'position' : 'static', 'display' : 'block', 'clear' : 'both'});  
        $('.edit').css({'display' : 'block'});
	}
	else {
	    var person = $('#hovers > div');
    	
	    $(person).hover( 
		    function(){$(this).children('.blurb').fadeIn(200)}
		    ,
		    function(){$(this).children('.blurb').fadeOut(200)}
	    );
	}

/*  COMPARISON TABLES
------------------------------------*/
	$(".comparison td:nth-child(even), .comparison th:nth-child(even)").addClass("zebra");
	$(".comparison tr td:first-child").addClass("label");

/*  LAST FOOTER PIPE
------------------------------------*/
	$(".navFooter li:last").css({"background" : "none"});
	
	
    /*  EXTERNAL LINKS
    ------------------------------------*/
    $('a,area').filter(function() {
            // filter email links and apply a seperate click function
            var linkPath = $(this).attr('href');
            if(linkPath != undefined){
                var mailToCheck = linkPath.split('mailto');
                if(mailToCheck.length >= 2){
                    $(this).click(function(){
                        $('div.popUpEmail').css('visibility','visible');
                        $('div#coverLayer').css('visibility','visible');
                        $('div.popUpEmail span.continueBtn a').attr('href', $(this).attr('href'));
                        setWindowSize();
                        return false;
                    });
                }
            }
            
            // filter external addresses which are not on the safe list and display pop up
            return this.hostname && this.hostname != location.hostname && IsValidHostName(this.hostname) && IsNotPartnerUrl(this.hostname);
        })
        .click(function () {
            $('div.popUpLink').css('visibility','visible');
            $('div#coverLayer').css('visibility','visible');
            $('div.popUpLink span.continueBtn a').attr('href', $(this).attr('href'));
            setWindowSize();
        return false;
    });
    
    
    /*  POP UP SCRIPT
    ------------------------------------*/
	$('div.popUpWrapper span.returnBtn a').click(function(){
        $('div.popUpWrapper').css('visibility','hidden');
        $('div#coverLayer').css('visibility','hidden');
	});
	
	$(window).scroll(function() {
	    if ($('div.popUpWrapper').css('visibility') == 'visible') {
	        setWindowSize();
	    }	    
	})

});

function IsNotPartnerUrl(thisUrl){
    //list of partners
    partnerUrls = new Array("berkshirebank.mortgagewebcenter.com","www.berkshirebankib.com","berkshirebankib.com","www.candidatemanager.net","candidatemanager.net","www.cmdemo.com","cmdemo.com","www.diproductsite.com","diproductsite.com","www.commonwealth.com","commonwealth.com","home.commonwealth.com","snl.com","www.snl.com");
    //if URL is not in array, function returns -1 and we return false.
    if($.inArray(thisUrl, partnerUrls) == -1){
        return true;
    }else{
        return false;
    }
}

function IsValidHostName(thisUrl){
    return (thisUrl.indexOf('.com') > 0 || thisUrl.indexOf('.net') > 0 || thisUrl.indexOf('.edu') > 0 || thisUrl.indexOf('.org') > 0 || thisUrl.indexOf('.info') > 0 || thisUrl.indexOf('.gov') > 0);
}

// Opacity Focused Layer
function setWindowSize() {
    var popUpWidth = $('div.popUpWrapper').outerWidth();
    var popUpHeight = $('div.popUpWrapper').outerHeight();
    var docWidth = 0;
    var docHeight = 0;
    var marginWidth = 0;
    var marginTop = 0;
    var currentScrollPos = 0;

	// returns width of window $('body').width();
	// returns width of contents minimum and widow width max $(document).width();

	if($('body').width() > $('#site').width()){
	    docWidth = $('body').width();
	} else {
	    docWidth = $('#site').width();
	}

    // get height of the document whichever is taller
	if($(window).height() > ($('#site').height()+70)){
	    docHeight = $(window).height()
	} else {
	    docHeight = $('#site').height()+70;
	}
    
    $('div#coverLayer').width(docWidth);
    $('div#coverLayer').height(docHeight);

    // Center window horizontally
    if(docWidth > popUpWidth){
        marginWidth = Math.round((docWidth - popUpWidth)/2);
    }
    
    // Center window vertically
	if (document.body.scrollTop>0) {
		currentScrollPos = document.body.scrollTop;		
	} else {
		currentScrollPos = document.documentElement.scrollTop;	
	}

    
    // Center window vertically
    if(($(window).height()-popUpHeight)>0){
        marginTop = ($(window).height()-popUpHeight)/2 + currentScrollPos;
    } else {
        marginTop = currentScrollPos;
    }
	
    $('div.popUpWrapper').css("margin-left", marginWidth);
    $('div.popUpWrapper').css("margin-top", marginTop);
}

var resizeTimer = null;
$(window).bind('resize', function() {
    if (resizeTimer) clearTimeout(resizeTimer);    
    resizeTimer = setTimeout(setWindowSize, 50);
});

/****** Calculator IFrame resize *****/
var iFrameHeight = 1;
$(document).ready(function() {
    calcIFrame = $('#calculatorIFrame').get(0);
    if(calcIFrame != null){
        iFrameHeight = calcIFrame.contentWindow.document.body.offsetHeight;
        $('#calculatorIFrame').height(iFrameHeight + 20);
        // For other good browsers.
	    $('#calculatorIFrame').load(function() {
	    	iFrameHeight = this.contentWindow.document.body.offsetHeight;
	    	$('#calculatorIFrame').height(iFrameHeight + 20);
	    });
	}
});
