/////////////////// get screen size function /////////////////////////////
function GetViewportSize(dim)
{
	var e = window, a = 'inner';
	if ( !( 'innerWidth' in window ) ) {
		a = 'client';
		e = document.documentElement || document.body;
	}
	return e[ a + dim ];
}

/////////////////// nav bar resizing from screen size /////////////////////////////
function SetNavBarHeight() {
	var winHeight = GetViewportSize('Height');
	document.getElementById('navHolder').style.height = winHeight + 'px';	
	
}

/////////////////// search bar resizing from screen size /////////////////////////////
function ResizeSearchBarWidth() {
	var winWidth = GetViewportSize('Width');
	var contentWidth = winWidth -20;
	var collectionWidth = winWidth - 230 - 20 -20;
	document.getElementById('contentHolder').style.width = contentWidth + 'px';
	document.getElementById('collectionInfo').style.width = collectionWidth + 'px';
	
}

/////////////////// image resizing from screen size /////////////////////////////
function ResizeImageViewer() {
	
	var img = new Image();
	img.src = document.getElementById('artworkimage').src;
	
	var winWidth = GetViewportSize('Width');
	var winHeight = GetViewportSize('Height');
	
	var availWidth = winWidth - 230 - 40; //320 is width of nav bar, 40 is 2 x 20 spacing margin of image
	var availHeight = winHeight - 170; // 
	
	//check if portrait or landscape
	if (img.width > img.height) {
	
		if ((img.width / img.height) > (availWidth / availHeight))	{
			
			//set to full screen width, height will be less than max so centre vertically
			document.getElementById('artworkimage').style.width = availWidth + 'px';
			
			var newMargin = (availHeight - document.getElementById('artworkimage').height) / 2;
			document.getElementById('artworkimage').style.marginTop = newMargin + 'px';
				
		} else {
			
			//set to full screen height, width will shrink so centre
			document.getElementById('artworkimage').style.height = availHeight + 'px';
					
			var newMargin = (availWidth - document.getElementById('artworkimage').width) / 2;
			document.getElementById('artworkimage').style.marginLeft = newMargin + 'px';
		}
		
	} else {
		
		if ((img.height / img.width) > (availHeight / availWidth))	{
			
			//set to full screen height, width will be less than max so centre horizontally
			document.getElementById('artworkimage').style.height = availHeight + 'px';
			
			var newMargin = (availWidth - document.getElementById('artworkimage').width) / 2;
			document.getElementById('artworkimage').style.marginLeft = newMargin + 'px';
			
		} else {
			
			//set to full screen width, height will shrink so centre vertically
			document.getElementById('artworkimage').style.width = availWidth + 'px';
					
			var newMargin = (availHeight - document.getElementById('artworkimage').height) / 2;
			document.getElementById('artworkimage').style.marginTop = newMargin + 'px';
			
		}
		
	}
		
}

/////////////////// background images and info box /////////////////////////////
function LoadBackgroundImage() {
	
	$.ajax({
		type: 'POST',
		url: '/inc/func.fry-backgrounds.php',
		success:function(data) {
			$responseArray = data.split("###");
			
			//load background image
			var images = ["/images/backgrounds/low/" + $responseArray[0], "/images/backgrounds/high/" + $responseArray[0]];
			var index = 0;
			
			$.backstretch(images[index], {speed: 1000});
						
			var intID = setInterval(function() {
				index = 1;
				clearInterval(intID);
       			$.backstretch(images[index]);
    		}, 2000);
				
			//display artist text and link
			document.getElementById('artistPainting').innerHTML = $responseArray[1];
						
		},
		error:function() {
			alert("ERROR");	
		}
	});
	
}


/////////////////////////// word search functions /////////////////////////////////////////
function SubmitWordSearch() {
	
	$("#form-search").validate();
	if ($('#form-search').valid()) {
		var searchText = document.getElementById('txtKeywords').value;
		location.href='/the-collection/search-results/word/' + searchText + '/' + searchText;
	} else {
		document.getElementById('txtKeywords').value = '';
	}
}


/////////////////// artist biog and artwork description popups /////////////////////////////
function ShowPopup(popup) {
	if (popup == 'artist') {
		document.getElementById('artist-biography').style.display = 'block';
		document.getElementById('artwork-description').style.display = 'none';
	} else if (popup == 'artwork') {
		document.getElementById('artwork-description').style.display = 'block';
		document.getElementById('artist-biography').style.display = 'none';
	}
}

function ClosePopup() {
	document.getElementById('artist-biography').style.display = 'none';
	document.getElementById('artwork-description').style.display = 'none';
}


///////////////////// navigating artwork viewer //////////////////////////////////////////////
function NavArtworkButton(dir) {
	
	var fullURL = document.URL;
	var params = fullURL.substr(fullURL.indexOf('search-viewer') + 14); //14 is length of search-viewer/
	var paramArray = params.split('/');
	
	var artworkID = paramArray[0];
	var searchType = paramArray[1];
	var searchID = paramArray[2];
	var searchName = paramArray[3];
	
	var queryString = "artworkID=" + artworkID + "&searchType=" + searchType + "&searchID=" + searchID 
	+ "&searchName=" + searchName + "&direction=" + dir;
	
	$.ajax({
		type: 'POST',
		url: '/inc/func.fry-nav-collection.php?' + queryString,
		
		success:function(data) {
			
			$responseArray = data.split("###");
			/*alert($responseArray[0]);*/
			/*alert($responseArray[1]);
			alert($responseArray[2]);
			alert($responseArray[3]);*/
			/*alert($responseArray[4]);*/
			location.href='/the-collection/search-viewer/' +  $responseArray[0] + '/' + $responseArray[1] + '/' + $responseArray[2] + '/' + $responseArray[3];
			
		},
		error:function() {
			alert("ERROR");	
		}
	});
		
}

//////////////////////// prevent right click //////////////////////////////////////
function PreventImageRightClick() {
	alert("Please contact Fry Art Gallery for permission if you wish to copy an image.");	
}


////////////////////////// get url variables ////////////////////////////////////
function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}

