min_right_col_top_height = 100; /*130*530/670 = about 102*/
h_original = 0;
w_original = 0;
is_animating = false;
//text_minimized = 
$(document).ready( function(){
	if (!$.browser.msie) {
		//fix padding on firefox and safari
		if ( $('#freya_right_col_bottom *:last-child')[0] && ($('#freya_right_col_bottom *:last-child')[0].tagName.toLowerCase() == 'tbody' || $('#freya_right_col_bottom *:last-child')[0].tagName.toLowerCase() == 'table')){
			$('#freya_right_col_bottom').append( '<div style="overflow:hidden; height:15px;">&nbsp;</div>' );
		} else {
			$('#freya_right_col_bottom').append( '<div style="overflow:hidden; height:1px;">&nbsp;</div>' );
		}
	} else if ( $.browser.version < 7 ) {
		$(window).resize(function(){
			document.location.reload(true);
		});	
	}
	if (imagePage) { 
	
		//Updated 8/13/2010 to fight the gwProxy issue with FCK editor and firefox plugins
		if (!userIsLoggedIn) {
			$('#freya_right_col_bottom').css( {overflow:'hidden'} );
			$('#gwProxy, #jsProxy, #refHTML').css( { display:'none' });
		}
		
		createImages(); 
		var yourQS = location.search;		
		// now you need to eliminate that ? mark
		yourQS = location.search.substring(1);
		
		// name=value string, so pairs(0) = "food=eggs"
		var getpairs = yourQS.split("&");
		// now you can loop through the array and split again!
		var tmpAry;
		for(var i=0; i<getpairs.length; i++){
			tmpAry = getpairs[i].split("=");
			if (tmpAry[0] == 'img') showImageFromUrl(tmpAry[1]);
		}
	}
	if (!itsAnImageOverviewPage && !specialPage) {
		//if javascript is turned on, adjust the height of the right col
		$('#freya_right_col_top').css( {height:"auto"} );
		right_col_top_h = document.getElementById('freya_right_col_top').offsetHeight;
		if (right_col_top_h < min_right_col_top_height) {
			right_col_top_h = min_right_col_top_height;
			$('#freya_right_col_top').css( {height:(min_right_col_top_height-30)+"px"} );
		}
		right_col_bottom_h = 530 - 15 - 30 - right_col_top_h; //total - space between - padding_on_bottom - top_height_including_padding
		$('#freya_right_col_bottom').css( {height:right_col_bottom_h+"px"} );
		$('#freya_right_col_bottom').css( 'padding','15px'); //For IE7
		
		if (!imagePage) { //expand text stuff
			$('#freya_large_image_region').click(function(){
				window.location = $('#freya_large_image').next().attr('href');
				//alert($('#freya_large_image').next().attr('href'));
				//alert('go to paintings');
			});
			if (!text_minimized && !itsTheHomePage) {
				expand_text(false);
			}
			$('#freya_footer_link_2').click( function(){ expand_or_minimize_text(); } );
		}
	} else if (itsAnImageOverviewPage) {
		// $('#freya_right_col_bottom').css( {overflow:'hidden'} ); // DFC Added Feb 26 2010
		$('.overview_block').hover(
			function() {
				$(this).css('cursor','pointer');
				$(this).find('.overview_label_link').css('color','white');
				//link_location = ( $(this).find('a.overview_label_link').attr('href') );
				$(this).click( function() { location = $(this).find('a.overview_label_link').attr('href'); } )
			},
			function() {
				$(this).find('a.overview_label_link').css('color','#959595');
			}
		);

	}
})

function expand_text(animate) {
		if (!h_original) {
			h_original = parseInt(( $('#freya_large_image').css('height')));
			w_original = parseInt(( $('#freya_large_image').css('width')));
		}
		if (document.getElementById('freya_right_col_top').offsetHeight>min_right_col_top_height) {
			h_small = document.getElementById('freya_right_col_top').offsetHeight;
		} else {
			h_small = min_right_col_top_height;
		}
		w_small = 130;
		small_ratio = h_small/w_small;
		ratio = h_original/w_original;
		if (ratio > small_ratio) {
			w_new = w_small;
			h_new = Math.round(h_original*w_new/w_original);
			w_delta = 0;
			h_delta = Math.floor((h_new-h_small)/2);
		} else {
			h_new = h_small;
			w_new = Math.round(w_original*h_new/h_original);
			h_delta = 0;
			w_delta = Math.floor((w_new-w_small)/2);
		}
		if (animate) {
			$('#freya_large_image').animate( {width:w_new+"px", height:h_new+"px", marginTop:"-"+h_delta+"px", marginLeft:"-"+w_delta+"px"}, "fast" );
			$('#freya_right_col').animate( {width:"990px",marginLeft:"0px"}, "normal" );
			$('#freya_right_col_top').animate( {width:"815px",marginLeft:"145px"}, "fast" );
			$('#freya_right_col_bottom').animate( {width:"960px",marginLeft:"0px"}, "fast" );
			$('#freya_large_image_region').animate( {width:w_small+"px", height:h_small+"px" }, "normal", "linear", function(){
				is_animating = false;
			} );
		} else {
			$('#freya_large_image').css( {width:w_new+"px", height:h_new+"px", marginTop:"-"+h_delta+"px", marginLeft:"-"+w_delta+"px"} );
			$('#freya_right_col').css( {width:"990px",marginLeft:"0px"} );
			$('#freya_right_col_top').css( {width:"815px",marginLeft:"145px"} );
			$('#freya_right_col_bottom').css( {width:"960px",marginLeft:"0px"} );
			$('#freya_large_image_region').css( {width:w_small+"px", height:h_small+"px" } );
			$('#freya_screen').hide();
		}
		$('#freya_footer_link_2').addClass( 'freya_text_expanded' );
		$('#freya_footer_link_2').removeClass( 'freya_text_minimized' );
		$('#freya_footer_link_2').html('minimize&nbsp;text');
		text_minimized = false;
		$('#freya_large_image_region').unbind('click');
		$('#freya_large_image_region').click(function(){
			expand_or_minimize_text();
		});
		//Save state
		$.ajax({
			type: "POST",
			url: "/config_central/freya_scripts/layout_state.php",
			data: 'text_minimized=f' //,dataType: "json",success: function(json){ alert( $(json).attr("text_minimized") ); }
		});
}

function minimize_text() {
		$('#freya_right_col').animate( {marginLeft:"685px",width:"305px"}, "fast" );
		$('#freya_right_col_top').animate( {width:"275px",marginLeft:"0px"}, "fast" );
		$('#freya_right_col_bottom').animate( {width:"275px",marginLeft:"0px"}, "fast" );
		$('#freya_large_image').animate( {width:w_original+"px", height:h_original+"px", marginTop:"0px", marginLeft:"0px"}, "normal", "linear", function(){
			is_animating = false;
			if ($.browser.mozilla && $.browser.version.substr(0,3) < 1.9 ) {
				//fix for firefox 2 refresh problems
				//could this be the problem: see http://drupal.org/node/243638 and http://us2.php.net/manual/en/function.ob-start.php#81667
				$('#freya_large_image_region').html( $('#freya_large_image_region').html() );
			}
		} );
		$('#freya_large_image_region').animate( {width:"670px", height:"530px"} );
		$('#freya_footer_link_2').addClass( 'freya_text_minimized' );
		$('#freya_footer_link_2').removeClass( 'freya_text_expanded' );
		$('#freya_large_image_region').unbind('click');
		$('#freya_large_image_region').click(function(){
			window.location = $('#freya_large_image').next().attr('href');
		});	
		$('#freya_footer_link_2').html('expand&nbsp;text');
		text_minimized = true;
		//Save state
		$.ajax({
			type: "POST",
			url: "/config_central/freya_scripts/layout_state.php",
			data: 'text_minimized=t'
		});
}

function expand_or_minimize_text() {
	if (!is_animating) {
		is_animating = true;
		if (text_minimized) expand_text(true);
		else minimize_text();
	}
}