$(window).load(function() {
    // find all containers with the imageAdjust class and center images horizonally and vertically
    $(".imageAdjust").each(function(i) {
        var heightDiff = $(this).find("img").height() - $(this).height();
        var widthDiff = $(this).find("img").width() - $(this).width();

        // set image to be positioned centered/middle of container
        $(this).find("img").css("marginTop", -heightDiff / 2).css("marginLeft", -widthDiff / 2);

    });

});


$(document).ready(function() {

    Cufon.replace('.cufon', { fontFamily: 'helvetica_neue' });
    Cufon.replace('.helvetica_neue_light', { fontFamily: 'Helvetica 45 Light' });
    /*    $('a.info').click(function() {
    $('#' + $(this).attr('rel')).dialog({
    buttons: { "Ok": function() { $(this).dialog("close"); } },
    bgiframe: true,
    height: $('#' + $(this).attr('rel')).height(),
    width: 580,
    title: $(this).attr('title'),
    modal: true
    });
    return false;
    });*/
    $('div.info').hide();
    //detect browser size or when browser has been re-sized
    try {
        $(window).resize(setWidth);
        $(document).ready(setWidth);
    } catch (e) {
    }


    function setWidth() {
        var de = document.documentElement;
        var w = (window.innerWidth || (de && de.clientWidth) || document.body.clientWidth);

        // for the 2nd flyout to fit, browser needs to be atleast 1375px wide
        // if browser too small, display 2nd flyout to left (thats what id=browser-too-small does in css )
        if (w <= "1375") {
            $('.pn-6').attr({ "id": "flyout-two-right" });

            // for the 1nd flyout to fit, browser needs to be atleast 1025px wide
            // if browser too small, move 1st flyout to the left as well
            if (w <= "1025") {
                $('.pn-6').attr({ "id": "flyout-one-right" });
            }
            return false;
        }
        else (w >= "1375")
        {
            // reset the id back to nothing so flyout is back to normal
            $('.pn-6').attr({ "id": " " });
        }
    }


    $(".mySelect").styleSelect({ styleClass: "selectDark" });

    // attach Google page tracker function to all outbound links
    $("a[href^='http']").each(function(i) {
        var $this = $(this);

        $this.bind("click", function() {
            pageTracker._trackPageview("/outgoing/" + $this.attr('href'));
        });
    });


    // attach Google page tracker function to all downloads
    $("a[href$='pdf'],[href$='doc'],[href$='xls'],[href$='jpg'],[href$='gif'],[href$='ppt']").each(function(i) {
        var $this = $(this);
        $this.bind("click", function() {
            pageTracker._trackPageview("/downloads/" + $this.attr('href'));
        });
    });


    $(".form-holder input").focus(function() {
        $(this).addClass("has-focus");
    });

    $(".form-holder textarea").focus(function() {
        $(this).addClass("has-focus");
    });

    $(".form-holder input").blur(function() {
        $(this).removeClass("has-focus");
    });

    $(".form-holder textarea").blur(function() {
        $(this).removeClass("has-focus");
    });


});

function resizeIframePage(){
 	$(".section-membership form").attr("style","height:100%;");
 //	$(".section-membership #container").attr("style","height:100%;");
 	$(".section-membership #main").attr("style","height:100%;");
 	$(".section-membership #cols").attr("style","height:100%;");
 //	$(".section-membership #col-main").attr("style","height:100%;");

	
}

/*
 * Popup window script 
 *  
 * Example markup 
 * <a href="mypage.html" onclick="popUpWin(this.href,'console',660,520);return false;" title="Open popup window">Link text</a>
 */
 
var newWindow = null;
function closeWin(){
	if (newWindow != null){
		if(!newWindow.closed)
			newWindow.close();
	}
}

function popUpWin(url, type, strWidth, strHeight){
	closeWin();
	if (type == "fullScreen"){
		strWidth = screen.availWidth - 10;
		strHeight = screen.availHeight - 160;
	}
	var tools="";
	if (type == "standard" || type == "fullScreen") tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width="+strWidth+",height="+strHeight+",top=0,left=0";
	if (type == "console") tools = "resizable,toolbar=no,location=no,scrollbars=yes,width="+strWidth+",height="+strHeight+",left=0,top=0";
	newWindow = window.open(url, 'newWin', tools);
	newWindow.focus();
}


function ShowSection(parentElement, selectedElement)
{
	alert(parentElement + " " + selectedElement.value);
	
	// hide all child div's inside section_area div 
	$("#" + parentElement +" > div").hide();	
	// show the element	
	$("#" + selectedElement.value).show();
	
}

/****************** EMAIL DECRYPTION *********************/
function decrypt_string(crypted_string) {

	var numbers = crypted_string.split(' ');			
	n = numbers[0];	decryption_key = numbers[1];			
	numbers[0] = ""; numbers[1] = "";				
	crypted_string = numbers.join(" ").substr(2);


	var decrypted_string = '';
	var crypted_characters = crypted_string.split(' ');
	var i;
	
	for(i = 0; i< crypted_characters.length; i++) {
		var current_character = crypted_characters[i];
		var decrypted_character = exponentialModulo(current_character,n,decryption_key);
		decrypted_string += String.fromCharCode(decrypted_character);
	}
	
	return decrypted_string;
}


// Finds base^exponent % y for large values of (base^exponent)
function exponentialModulo(base,exponent,y) {
	if (y % 2 == 0) {
		answer = 1;
		for(var i = 1; i <= y/2; i++) {
			temp = (base*base) % exponent;
			answer = (temp*answer) % exponent;
		}
	} else {
		answer = base;
		for(var i = 1; i <= y/2; i++) {
			temp = (base*base) % exponent;
			answer = (temp*answer) % exponent;
		}
	}
	return answer;
}
function bookmark() {
    bookmarkurl = window.location;
    bookmarktitle = document.title;
    if (document.all)
        window.external.AddFavorite(bookmarkurl, bookmarktitle)
    else if (window.sidebar) // firefox
        window.sidebar.addPanel(bookmarktitle, bookmarkurl, "");
}
function sendEmail(dat)
{
    document.location.href='mailto:' + do_decrypt ( dat );
}


function do_decrypt(dat)
{ 
	return decrypt_string ( dat, 0, 0 ,'');
}



function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode == 13 || charCode == 46 || (charCode >= 96 && charCode <= 105) || (charCode >= 37 && charCode <= 40)) {
        return true;
    } else
        if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}

$(document).ready(function() {


    var defaultValues = new Array();

    $(".watermarked").each(function(i) {
        var $this = $(this);

        defaultValues[i] = $this.attr("style");

        $this.bind("focus", function() {
        if (this.id == "login_loginPassword" || this.id == "_ctl0_login_non_member_loginPassword") {
                $this.attr("style", "");
            }
            else if (this.id == "login_loginUserName" || this.id == "_ctl0_login_non_member_loginUserName") {
                $this.attr("style", "");
            }

        });

        $this.bind("blur", function() {
            if (!($this.val())) {
               
                    $this.attr("style", defaultValues[i]);
                
            }
        });
    });

});


$(document).ready(function() {
	var $frames = $('iframe.mainiframe');
	try{
		var framedoc = $($('iframe.mainiframe')[0].contentWindow.document);
		$(framedoc).ready(function(){
		var h = framedoc.find('body').height();
		var w = framedoc.find('body').width();
		if(h>800){
			$($('iframe.mainiframe')[0]).height(h);
			$($('iframe.mainiframe')[0]).width(w);
		}
		});
	}catch(e)
	{
	}
	
});