$(document).ready(function() {
	
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		DD_belatedPNG.fix('h1#logo a, .shadow, .col-img, .popup-head, .next, .prev, #cboxClose');
	};
	
	$('.blink')
		.focus(function(){
			if( $(this).val() == $(this).attr('title') ) {
				$(this).val('');
			}
		}).blur(function(){
			if( $(this).val() == '' ) {
				$(this).val( $(this).attr('title') );
			}
		});
		
	$('.cols-home .box.interactive').click(function(e){
		window.location = $(this).find('a').attr('href');
		});
		
	$('.cols-home .box').hover(
		function(){
			$(this).addClass('active');
			$(this).find('img:eq(0)').hide();
			$(this).find('img:eq(1)').show();
		}, 
		function(){
			$(this).removeClass('active');
			$(this).find('img:eq(0)').show();
			$(this).find('img:eq(1)').hide();
		});
		
		
	$('.social a').each( function () { $(this).find('img:eq(1)').hide(); }  );
		
	$('.social a').hover(
		function(){
			$(this).find('img:eq(0)').hide();
			$(this).find('img:eq(1)').show();
		}, 
		function(){
			$(this).find('img:eq(0)').show();
			$(this).find('img:eq(1)').hide();
			if($(this).parent().hasClass('active'))
			{
				$(this).find('img:eq(0)').hide();
				$(this).find('img:eq(1)').show();
			}
		});
		
	$('.gbox a').each( function () { $(this).find('img:eq(1)').hide(); }  );
	$('.gbox').hover(
		function(){
			$(this).find('a').addClass('active');
			$(this).find('img:eq(0)').hide();
			$(this).find('img:eq(1)').show();
		}, 
		function(){
			$(this).find('a').removeClass('active');
			$(this).find('img:eq(0)').show();
			$(this).find('img:eq(1)').hide();
			if($(this).parent().hasClass('active'))
			{
				$(this).find('img:eq(0)').hide();
				$(this).find('img:eq(1)').show();
			}
		});

	$('.gbox a').colorbox({
		scrolling: false,
		onComplete: function(){
			$.colorbox.resize();
		}
	});
	
	$('#top-form input[type=submit]').click(function () {
		if($('#top-form input#mail').val() == 'Email Address') {
			alert('Please fill out the Email field');
			return false;
		}
		if($('#top-form field2 input').val() == 'Zip') {
			alert('Please fill out the Zip Code field');
			return false;
		}
		$.ajax({
			type: 'POST',
			url: $('#top-form').attr('action'),
			data: $("#top-form").serialize(),
			success: onSuccess
		});
		
		return false;
	});
	
	function onSuccess(data) {
		$.fn.colorbox({
			html:   data,
			open:   true,
			iframe: false
        });
	}
	if ($("#sidebar").length > 0){
		moveSidebar();
	}
});


function moveSidebar() {
	var top = $('#sidebar').offset().top - parseFloat($('#sidebar').css('marginTop').replace(/auto/, 0));
	$(window).scroll(function(event){
		var y = $(this).scrollTop();
		var newTop = $('#sidebar').offset().top;
		if (y < top) {
			$('#sidebar').css('top', 0);
			$('#sidebar').css('position', 'relative');
		}
		if (y  >= top && y < $(document).height() - 1000) {
			$('#sidebar').css('top', '0px');
			$('#sidebar').css('position', 'fixed');
		} 
		if  (y > ($(document).height() - 1050)) {
			$('#sidebar').css('top',  $('#main').height() - 750);
			$('#sidebar').css('position', 'relative');
		}

	})
}

