/*
 * @author Catherine Garnier
 * @desc Custom Code for Opening Fine Art CMS, an early-adopter.com product
*/ 
 
//REQUIRES jquery.js and jquery.dimensions.min.js
//REQUIRES Yahoo Maps API

//everything within the $jj(document).ready function call is fired when page is fully loaded
var $jj = jQuery.noConflict();

$jj(document).ready(function()
{
	//alert ("READY");
	
	if ( jQuery.browser.safari && document.readyState != "complete" )
	{
		//console.info('ready...');
		setTimeout( arguments.callee, 100 );
		return;
	} 

	var logoHeight = $jj("#logo").height();
	var logoWidth = $jj("#logo").width();

	// sets single image view on those pages containing image view toggle
	if ( $jj('#single_image') != "null" )
	{
		imageToggle("single_image_link");
	}

	//sets the image toggle links to switch the views when clicked

	$jj(".image_toggle > a").click(function(event)
	{ 
		//alert($jj(this).attr("id"));
		event.preventDefault();
		imageToggle($jj(this).attr("id"));
	});

	//sets the image links to swap the large image when clicked, only invoked on individual artwork page. not working on PC right now

	if ( $jj("body").attr("id") == "individual_artwork_exhibition" || "individual_artwork_artist" )
	{
		$jj(".img_link").click(function(event)
		{ 
			//alert($jj(this).attr("href"));
			event.preventDefault();
			//alert($jj("#single_image > img").attr("src"));
			$jj("#single_image > img").attr("src",$jj(this).attr("href")); 
		});
	}

	//hides all subnavs
	$jj("ul.subnav").hide();
	
	//sets associated submenu to display on mouseover of main nav link
	var submenu;

	$jj("#nav > li").hover(function()
	{
		//alert(this.id);

		if ( this.id == "exhibitions_nav" )
		{
			//alert("exhibitions_nav");
			submenu = "#exhibitions_subnav";
		}
		else if ( this.id == "gallery_nav" )
		{
			//alert("gallery_subnav");
			submenu = "#gallery_subnav";
		}
		else
		{
			submenu = null;
		}
		
		//alert(over);

		if ( submenu != null )
		{
			// alert ("over");
			$jj(submenu).fadeIn("normal");
		}
	},
	function()
	{
		if ( submenu != null )
		{
			//alert("out");
			$jj(submenu).fadeOut("normal");
		}
	});

	// vertical centering of selected elements

	verticalCenter(".display","#image");
	verticalCenter(".exhibition_details",".exhibition_info");
	verticalCenter(".artist_details",".artist_info");
	verticalCenter(".artwork_details",".artwork_info");
	verticalCenter(".checklist_details",".checklist_info");
	verticalCenter(".fair_details",".fair_info");

	// calls map ONLY if body id is gallery_info - note, there is a bug on IE 6/7, map does not load on refresh of page
	if ( $jj("body").attr("id") == "gallery_info" )
	{
		startMap();
	}
});
	
	
/* CUSTOM FUNCTIONS	*/

// verticallly align centers of any two elements by setting the top padding value of the "child" element
function verticalCenter( child, parent )
{
	if ( $jj(parent).height() > $jj(child).height() )
	{
		var childPadding = ($jj(parent).height() - $jj(child).height()) / 2;
		//alert($jj(parent).height()+" "+$jj(child).height()+" "+childPadding);
		$jj(child).css( "padding-top", childPadding );
	}
}


// Creates a map object - dependent on Yahoo Map API 
function startMap()
{
	var map = new YMap(document.getElementById('map'));  
	
	// Add map type control  
	map.addTypeControl();  
	
	// Add map zoom (long) control  
	map.addZoomLong();  
	
	// Add the Pan Control  
	//map.addPanControl();  
	
	// Set map type to either of: YAHOO_MAP_SAT, YAHOO_MAP_HYB, YAHOO_MAP_REG  
	map.setMapType(YAHOO_MAP_REG);  
	
	// Display the map centered on a geocoded location  
	map.drawZoomAndCenter("275 Park Avenue, Brooklyn, NY, 11205", 3);  
			
	//map.addMarker("275 Park Avenue, Brooklyn, NY, 11205");
	map.addOverlay(new YMarker("275 Park Avenue, Brooklyn, NY, 11205"));
}

// swaps image views from single to thumbnail and back again	
function imageToggle( view )
{
	if ( view == 'single_image_link' )
	{
		$jj('#single_image').css("display","block");
		$jj('#thumbnails').css("display","none");
		$jj('#single_image_link').css("backgroundPosition","-22px 0");
		$jj('#thumbnails_link').css("backgroundPosition","0 -22px");
	}
	else
	{ 
		$jj('#single_image').css("display","none");
		$jj('#thumbnails').css("display","block"); 
		$jj('#single_image_link').css("backgroundPosition","-22px -22px");
		$jj('#thumbnails_link').css("backgroundPosition","0 0");
	}
	
  	return false;
}
