$(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() { 
	

	

	// 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");
	});

	$(".tool-favourites a").click(function() {
	    bookmark();
	});

});


/*
 * 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 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;
}

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, "");
}