$(document).ready(function() {
    screenshotPreview();
    //applyRoundStyles();		//The rounding JS library breaks all borders in IE
    applyRounding();
    tableRowStriping();
    $('#home-menu a[href=' + window.location + ']').addClass('active');
    animateHeadingBars();
});

$(window).load(function() {
    resizeLightboxImages();
    resizeImage($('#listing .mainPhoto img'), 350, 350);
});

function animateHeadingBars()
{
    var $bottomRedBar = $('#bottomRedBar');
    var $topRedBar = $('#topRedBar');
    $bottomRedBar.animate({marginRight: parseInt($bottomRedBar.css('marginRight'),10) == 0 ? $bottomRedBar.outerWidth() : 0},1000);
    $topRedBar.animate({marginLeft: parseInt($topRedBar.css('marginLeft'),10) == 0 ? $topRedBar.outerWidth() : 0},1000);
}

function resizeLightboxImages()
{
    $('img.small').each(function() {
		var maxWidth = 150; // Max width for the image
		var maxHeight = 200;    // Max height for the image
		var ratio = 0;  // Used for aspect ratio
		var width = $(this).width();    // Current image width
		var height = $(this).height();  // Current image height
		
		// Check if the current width is larger than the max
		if(width > maxWidth){
		    ratio = maxWidth / width;   // get ratio for scaling image
		    $(this).css("width", maxWidth); // Set new width
		    $(this).css("height", height * ratio);  // Scale height based on ratio
		    height = height * ratio;    // Reset height to match scaled image
		    width = width * ratio;    // Reset width to match scaled image
		}
	
		// Check if current height is larger than max
		if(height > maxHeight){
		    ratio = maxHeight / height; // get ratio for scaling image
		    $(this).css("height", maxHeight);   // Set new height
		    $(this).css("width", width * ratio);    // Scale width based on ratio
		    width = width * ratio;    // Reset width to match scaled image
		}
    });
}

function resizeImage($el, $maxWidth, $maxHeight)
{
   $($el).each(function() {
		var maxWidth = $maxWidth; // Max width for the image
		var maxHeight = $maxHeight;    // Max height for the image
		var ratio = 0;  // Used for aspect ratio
		var width = $(this).width();    // Current image width
		var height = $(this).height();  // Current image height
	
		// Check if the current width is larger than the max
		if(width > maxWidth){
		    ratio = maxWidth / width;   // get ratio for scaling image
		    $(this).css("width", maxWidth); // Set new width
		    $(this).css("height", height * ratio);  // Scale height based on ratio
		    height = height * ratio;    // Reset height to match scaled image
		    width = width * ratio;    // Reset width to match scaled image
		}
	
		// Check if current height is larger than max
		if(height > maxHeight){
		    ratio = maxHeight / height; // get ratio for scaling image
		    $(this).css("height", maxHeight);   // Set new height
		    $(this).css("width", width * ratio);    // Scale width based on ratio
		    width = width * ratio;    // Reset width to match scaled image
		}
    });

}

/*
 * Url preview script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */
 
this.screenshotPreview = function(){	
	/* CONFIG */
		
		xOffset = 10;
		yOffset = 30;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.screenshot").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='screenshot'><img src='"+ this.rel +"' alt='url preview' />"+ c +"</p>");								 
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#screenshot").remove();
    });	
	$("a.screenshot").mousemove(function(e){
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

function tableRowStriping()
{
	 
	$("tbody tr:even").css("background-color", "#EBF4D9");
	$("tbody tr:odd").css("background-color", "#FFFFFF");	
	
}

function applyRoundStyles()
{
	$("#bottomContent h2").addClass("roundSm");
	//$(".contactCard").addClass("roundSm");	//applying this rounding this way breaks IE borders (no borders at all)
}

function applyRounding()
{
	// Round any small corners
	//$('.roundSm').corner("round,top bottom, 5px");
	// Round any med corners
	//$('.round, .roundMd').corner("round,top bottom, 10px");
	// Round any lg corners
	//$('.roundLg').corner("round,top bottom, 15px");
	
}

