/** Slide Show **/
function showPopup(address) {
	/*** Opens a small window to play the address given ***/
	h = document.documentElement.clientHeight;
	l = document.documentElement.clientWidth;
	w = window.open(address,'demo','Menubar=no,location=yes,resizable=yes,scrollbar=no,status=no,height=' + h + ',width=' + l);
	w.referringWindow = window;
}
function showZoomifyPopup(address) {
	/*** Opens a small window to play the address given with a zoomify image box ***/
	w = window.open(address,'demo','Menubar=no,location=no,resizable=yes,scrollbar=no,status=no,height=435,width=630');
	w.referringWindow = window;
}

/**
 * First step of the slideshow - just do the preloading; we're already showing an image
 */
function slideshow_first() {
	preloadNextImage();
	setTimeout("slideshow_rest()", slideshow_getDuration());
}

/**
 * Every other step of the slideshow
 */
function slideshow_rest() {
	showNextImage();
	preloadNextImage();
	setTimeout("slideshow_rest()", slideshow_getDuration());
}

/**
 * Get the appropriate duration from the dropdown, in msec
 */
function slideshow_getDuration() {
	return document.getElementById("Duration").value * 1000;
}

/**
 * Returns the string of the user selected dimension enum either fullscreen or thumbnail
 */
function slideshow_getDimension(){
	return dimension = document.getElementById("PictureSize").value;
}

var _NEXT_IMAGE;
var _CURRENT_IMAGE_INDEX = 0;

function preloadNextImage() {
	// Increment the current image index
		_CURRENT_IMAGE_INDEX = (_CURRENT_IMAGE_INDEX+1)%src.length;
		
	// Get the next image source.  Use _CURRENT_IMAGE_INDEX, and don't forget to choose the right size!
	// Set up _NEXT_IMAGE as an Image() object
		
	
		_NEXT_IMAGE = new Image();
		_NEXT_IMAGE.src = src[_CURRENT_IMAGE_INDEX].replace("800,600",slideshow_getDimension());
		
		tempBr = new Array();
		tempBr = slideshow_getDimension().split(",");
		window.moveTo(0,0);
		window.resizeTo(parseInt(tempBr[0]),parseInt(tempBr[1]));
		
		document.getElementById("SlideShowText").innerHTML= info[_CURRENT_IMAGE_INDEX];
}

function gotoLink(){
	// Used by the SlideShow to link to the appropriate artwork.
	try{
		window.referringWindow.location.href = url[_CURRENT_IMAGE_INDEX-1]; // it was already calculating the next image so -1 to get current one
		window.close();
	}catch(ex){
		window.location.href = url[_CURRENT_IMAGE_INDEX-1]; // it was already calculating the next image so -1 to get current one
		//If the reffering window cannot be found, send the current page to the selected artwork
	}
}

/**
 * Updates the Image source from the global _NEXT_IMAGE
 */ 
function showNextImage(){
	if (_NEXT_IMAGE != null) {
		document.getElementById("SlideShowImage").src = _NEXT_IMAGE.src;
	}
}

/** Email to Friend Form **/

var showDiv;
var oldDiv = 1;

function showChild( el ){
	if(oldDiv == 1){
	//the first time you call this function
		showDiv = document.getElementById(el);
		showDiv.style.display = "block";
		oldDiv = showDiv;
	}
	else{
		showDiv = document.getElementById(el);
		if( showDiv == oldDiv ){
			if(oldDiv.style.display == "none"){	showDiv.style.display = "block";}
			else { showDiv.style.display = "none";}
			oldDiv = showDiv;
			// if its the same div as before, hide the same div, unless its the first time you use the function
		}else{
			oldDiv.style.display = "none";
			showDiv.style.display = "block";
			oldDiv = showDiv;
			// if its a different div, hide the first one, and show the second one
		}
	}
}

var showDiv;
function showAbstract( el ){
	if(showDiv == null){
		showDiv = document.getElementById(el);
		showDiv.style.display = "block";		
	} else {
		showDiv.style.display = "none";
		showDiv = null;
	}
}

function checkSearch( el ){

	if( el.value == "Search" ){
		el.value= ""
	}
	
	el.focus();
	el.select();
}

function positionFooter(){
	showAbstract('SendForm');
}

var _InquiryOn = 0;
var _MoreOn = 0;

function showSwitch(toshow,tohide){
	if(toshow == "InquireToBuy"){
		if(_InquiryOn)
		{
			hide(toshow);
			_InquiryOn = 0;
		}
		else
		{
			var toshowDiv = document.getElementById(toshow);
			toshowDiv.style.display = "block";
			_InquiryOn = 1;
			hide(tohide);
			_MoreOn = 0;
		}
	}
	if(toshow == "MoreInfo"){
		if(_MoreOn)
		{	hide(toshow);
			_MoreOn = 0;
		}
		else
		{
			var toshowDiv = document.getElementById(toshow);
			toshowDiv.style.display = "block";
			_MoreOn = 1;
			hide(tohide);
			_InquiryOn = 0;
		}
	}
}

/*
 * Update the sort direction captions.  They're referenced by HTML IDs
 */
function updateSortDirectionTitles(sortBy, calledOnLoad) {
	var asc = document.getElementById('ascTitle');
	var desc = document.getElementById('descTitle');

	var typeCheckboxes = document.getElementById('ArtworkTypeSelector').getElementsByTagName('input');
	
	switch (sortBy) {
	case 'Created':
		asc.innerHTML = 'oldest first'; 
		desc.innerHTML = 'newest first';
		if(typeof calledOnLoad == 'undefined') desc.parentNode.onclick();
		break;
	case 'MaxValue':
	case 'MinValue':
		asc.innerHTML = 'cheapest first'; 
		desc.innerHTML = 'most expensive first';
		if(typeof calledOnLoad == 'undefined') asc.parentNode.onclick();
		break;

	case 'T_Artist.txt_Surname':
	case 'MediumID':
	case 'T_Medium.txt_Title':
		asc.innerHTML = 'a - z';
		desc.innerHTML = 'z - a';
		if(typeof calledOnLoad == 'undefined') asc.parentNode.onclick();
		break;
	
	case 'AuctionCloseDate':
		asc.innerHTML = 'closing soon first';
		desc.innerHTML = 'closing soon last';

		typeCheckboxes[0].checked = true;
		typeCheckboxes[1].checked = false;
		
		if(typeof calledOnLoad == 'undefined') asc.parentNode.onclick();
		break;
	}
}

// No use now. Ticket 16 / item 5
/*function searchArtworkTypeChanged_simpleSearch(el) {
	var typeSet = document.getElementById("OtherArtworkType").getElementsByTagName("input");
	if(el.value == "Auction" &&  el.checked)
	{
		typeSet[1].checked = false;
		typeSet[2].checked = false;
	}
	else if(el.value == "Gallery" && el.checked ||el.value == "Exhibition" && el.checked)
	{
		typeSet[0].checked = false;
	}
}*/

function searchArtworkTypeChanged(el) {
	
		
	var typeSet = document.getElementById("ArtworkType").getElementsByTagName("input");
	if(el.value == "Auction" &&  el.checked)
	{
		typeSet[1].checked = false;
		typeSet[2].checked = false;
		var another = typeSet[1];
	}
	else if(el.value == "Gallery" && el.checked ||el.value == "Exhibition" && el.checked)
	{
		typeSet[0].checked = false;
		var sortField = document.AdvancedSearchForm_AdvancedSearch.sort;
		if(sortField.value == "AuctionCloseDate") {
			sortField.value = "Created";
			updateSortDirectionTitles(sortField.value);
		}
		var another = typeSet[0];
	}
	updateCategoryList()
	/*--------------------------------------------------------------------
	 * Fishers change the logic here, so it is not used anymore, but we keep
	 * it here as it is, just in case ...
	 *--------------------------------------------------------------------
	if(el.value != "Exhibition")
	{
		if((el.value == "Auction" && !el.checked) || (el.value == "Gallery" && !el.checked)) {
			var sortField = document.AdvancedSearchForm_AdvancedSearch.sort;
			if(sortField.value == "AuctionCloseDate") {
				sortField.value = "Created";
				updateSortDirectionTitles(sortField.value);
			}
		}
		
		var typeSet = document.getElementById("ArtworkType").getElementsByTagName("input");
		if(el.value == "Gallery" && el.checked)
		{
			typeSet[2].checked = false;
		}
		
		if(el.value == "Auction")
		{
			var another = typeSet[1];
		}
		else
		{
			var another = typeSet[0];
		}
		
		updateCategoryList(el, another);
	}
	else
	{
		if(el.checked = true)
		{
			var typeSet = document.getElementById("ArtworkType").getElementsByTagName("input");
			typeSet[1].checked = false;
			searchArtworkTypeChanged(typeSet[1]);
		}
	}
	*----------------------------------------------------------------------
	*/
}

function updateCategoryList(){
	
		
	
		var typeSet = document.getElementById("ArtworkType").getElementsByTagName("input");
		
		var categoryTag = document.getElementById("AdvancedSearchForm_AdvancedSearch_category");
		var categories = categoryTag.getElementsByTagName("option");
		
		for(var i=1; i<categories.length;){
			categoryTag.removeChild(categories[i]);
		}
		
		if(typeSet[0].checked||typeSet[1].checked||typeSet[2].checked){
				var showtype, hiddentype;
				if(typeSet[0].checked){
					showtype=typeSet[0].value;
				}else if(typeSet[1].checked||typeSet[2].checked){
					showtype=typeSet[1].value;
				}
				
				
				for(var i=1; i<_DUMMY_LIST_LENGTH; i++)
				{
					if(_DUMMY_LIST[i].label == showtype)
					{
						categoryTag.appendChild(_DUMMY_LIST[i]);
					}
				}
			}else{
				for (var i=1; i<_DUMMY_LIST_LENGTH; i++)
				{
					categoryTag.appendChild(_DUMMY_LIST[i]);
				}
			}
		
	/*--------------------------------------------------------------------
	 * Fishers change the logic here, so it is not used anymore, but we keep
	 * it here as it is, just in case ...
	 *--------------------------------------------------------------------
	var categories = document.getElementById("AdvancedSearchForm_AdvancedSearch_category").getElementsByTagName("option");

	
	if(me.checked&&!another.checked||!me.checked&&another.checked)
	{
		
		if(me.checked)
		{
			var hiddentype = another.value;
			var showtype = me.value;
		}
		else if(another.checked)
		{
			var hiddentype = me.value;
			var showtype = another.value;
		}
		
		for(var i=1; i<categories.length; i++)
		{
			if(categories[i].label == showtype)
			{
				categories[i].style.display = "block";
			}
			if(categories[i].label == hiddentype)
			{
				categories[i].style.display = "none";
			}
		}
	}
	
	else // both Type are checked or unchecked
	{
		for (var i=1; i<categories.length; i++)
		{
			categories[i].style.display = "block";
		}
	}
	*--------------------------------------------------------------------
	*/
}

function sortdirection_onload() {
	var searchForm = document.getElementById('AdvancedSearchForm_AdvancedSearch');
	if(!searchForm) searchForm = document.AdvancedSearchForm_AdvancedSearch;
	if(searchForm && searchForm.elements.sort) {
		updateSortDirectionTitles(searchForm.elements.sort.value, true);
	}
}

function InquiryNMore_onload() {

	if(window.location.href.match(/#inquiry_form$/g))
	{
		showSwitch("InquireToBuy", "MoreInfo");
	}
	else if(window.location.href.match(/#moreinfo_form$/g))
	{
		showSwitch("MoreInfo", "InquireToBuy");
	}
}

function toggleArtImage( el ){
	newImage = document.getElementById(el);
	if(newImage.className == 'hide'){
		images = document.getElementById('Images').getElementsByTagName('li');
		for(i=0;i<images.length;i++){
			if(images[i].id != newImage.id){
				images[i].className = 'hide';
			}else{
				images[i].className = '';
			}
		}
	}
}

function updateReserve(el){
	if(el.value > 3000) document.getElementById('ListingFee').innerHTML = 50;
		else document.getElementById('ListingFee').innerHTML = 20;
}

function defaultSearchArtworkType_onload(){
	var type;
	if(type = document.getElementById("ArtworkType")){
		var typeSet = type.getElementsByTagName("input");
		for(var i = 0; i< typeSet.length; i++){
			if(typeSet[i].checked){
				searchArtworkTypeChanged(typeSet[i]);
				return true;
			}
		}
	}
}

var _DUMMY_LIST;
var _DUMMY_LIST_LENGTH;

function makeCategoryDummyList(){
	
		var categoryTag = document.getElementById("AdvancedSearchForm_AdvancedSearch_category");

		if(categoryTag){
			var categories = categoryTag.getElementsByTagName("option");
		
		_DUMMY_LIST_LENGTH = categories.length;
		
		_DUMMY_LIST = Array();
		for(var i=1; i<_DUMMY_LIST_LENGTH; i++){
			_DUMMY_LIST[i]=categories[1];
			categoryTag.removeChild(categories[1]);
		}
		
		updateCategoryList();
	}
}


appendLoader(function() {
	if(document.getElementById('LastAccountActionMessage')) {
		setTimeout("document.getElementById('LastAccountActionMessage').style.display = 'none'", 5000);
	}
});

appendLoader(InquiryNMore_onload);
appendLoader(sortdirection_onload);
appendLoader(makeCategoryDummyList);
appendLoader(defaultSearchArtworkType_onload);
