var gallery;
function round(that){// add rounded borders by image position
	if((!$.support.borderRadius || $.browser.mozilla)) {// FF 3.6 still doesn't support border-radius for images
		var t = Math.round(that.position().top);
		var l = Math.round(that.position().left);
		var w = Math.round(that.width());
		var h = Math.round(that.height());
		var t2 = t + h - 5;
		var l2 = l + w - 5;
		$('body').append('<div class="round tl" style="top: ' + t + 'px; left: ' + l + 'px;"></div>\
		<div class="round tr" style="top: ' + t + 'px; left: ' + l2 + 'px;"></div>\
		<div class="round br" style="top: ' + t2 + 'px; left: ' + l2 + 'px;"></div>\
		<div class="round bl" style="top: ' + t2 + 'px; left: ' + l + 'px;"></div>');
	} else if (!($().galleria)) {
		that.css({
			'border-radius': '5px',
			'-moz-border-radius': '5px',
			'-webkit-border-radius': '5px',
			'-khtml-border-radius': '5px'
		});
	}
}

$(function() {
	$.support.borderRadius = false;
	$.each(['BorderRadius','MozBorderRadius','WebkitBorderRadius','KhtmlBorderRadius'], function() {
		if(document.body.style[this] !== undefined) $.support.borderRadius = true;
		return (!$.support.borderRadius);
	});
});


$(document).ready(function(){

	/*
	 * Fixes
	 */

	$('hr').replaceWith('<div class="hr"><hr /></div>'); // style <hr /> - fixes issues for ie7
	$('img[align]').each(function(){ // fix image align issues for ie7
		if ($(this).attr('align') == 'left' || $(this).attr('align') == 'right') {
			$(this).css({'float': $(this).attr('align')}).removeAttr('align');
		}
	});
	$(window).load(
	    function() {
        	$('img:not(.square):not(#gallery *)').each(function(){
				round($(this));
			});
	    }
	);
	$(window).bind('resize', function(){
		if (!($().galleria)) {
			$('.round.tl, .round.tr, .round.br, .round.bl').remove();
			setTimeout('$(\'img:not(.square):not(#gallery *)\').each(function(){round($(this));});', '10');
		}
	});
	if ($.browser.webkit) {
		$('.wrapper .main').css({'width':'1064px'});
	}
	// tables
	$('table').each(function(index) {
		var border = $(this).attr("border");
		if (border) {
			$(this).find("td").css('border', border + 'px solid #4f85c0');
		}
		var cellspacing = $(this).attr('cellspacing');
		if (cellspacing != '' && cellspacing > '0') { //hack for IE6/7
			$(this).css({'border-spacing': cellspacing+'px', 'border-collapse': 'separate', 'border-style': 'solid'});
		}
	});


	/*
	 * Features
	 */

	$('#search #terms').bind('click', function(){ // remove default search input text on click
		$(this).addClass('modified').val('').unbind('click');
	});

	$('#search').bind('submit', function(e){ // disable submit if search terms aren't provided
		$(this).find('#terms').hasClass('modified') && $(this).find('#terms').val() != '' ? true : e.preventDefault(e);
	});


	if($().galleria) {// check if plugin is loaded and fire Galleria plugin
		$('#galleria').galleria({
			image_position: '0 0',
			thumb_crop: true,
			image_crop: false,
			show_counter: false,
			transition: 'slide',
			extend: function(options) {
				gallery = this; // "this" is the gallery instance
				$('.show-next').bind('click', function(e){
					gallery.next();
					e.preventDefault(e);
				});
				$('.show-prev').bind('click', function(e){
					gallery.prev();
					e.preventDefault(e);
				});
	        }

		});
	}


});

