function mask() {
	$height = Math.max(($("#rightColumn").height() + 150), $('body').height(), 950);
	$("#maskOverlay").css({ height: $height,  display: arguments[0]});
}

function showMore() {
	mask('block');
	$orientation = arguments[1];
	$("#morePanel").load("index.php", {'action': 'more', 'id': arguments[0], 'orientation': arguments[1] }, function() {
		$height = $orientation == 'portrait' ? 465 : 345;
		$width = $orientation == 'portrait' ? 345 : 465;
		$("#morePanelImage").css({height: $height});
		$panelHeight = $orientation == 'portrait' ? $height+180 : $height+150;
		$("#morePanel").css({height: $height, width: $width});
		$("#morePanelWrapper").css({height: ($panelHeight+40), width: ($width+45)});
		$("#morePanelClose").css({width: ($width+45)});
		$("#morePanelWrapper").center();
		$("#morePanelWrapper").css({display: 'block'});
		$panelHeight = $height + $(".moreDetails").outerHeight() + 10;
		$("#morePanel").css({height: $panelHeight});
	});
	$moreOpen = true;
}

function closeMore() {
	mask('none');
	$("#morePanelWrapper").css({display: 'none'});
	$("#morePanel").html("");
	$moreOpen = false;
}

function browseFor() {
	$action = arguments[0] == 'tid' ? 'tid' : 'browse';
	$("#rightColumn").load('index.php', { 'action': $action, 'id': arguments[1] } );
	closeMore();
	updateMenu('search');
}

function lightBox() {
	$("#lightBox").css({ display: 'block' });
	$("#lightBoxContent").load("index.php", { 'action': 'lightBox', 'imageID': arguments[0], 'do': arguments[1], 'orientation': arguments[2] } );
	$("#"+arguments[0]).load("index.php", { 'action': 'imageThumb', 'imageID': arguments[0] } );
	if($moreOpen == true) {
		$("#morePanelAction").load('index.php', {'action': 'tickleMore', 'imageID': arguments[0], 'orientation': arguments[2] } );
	}
}

function minLightBox() {
	$("#lightBox").effect("size", { to: { width: 400, height: 40 }, scale: 'box' }, 1000, function() {
		$("#lightBoxContent").css({ display: 'none' });
	});
}

function maxLightBox() {
	$("#lightBoxContent").css({ display: 'block' });
	$("#lightBox").effect("size", { to: { width: 400, height: 500 }, scale: 'box' }, 1000);
	
}

function clearLightbox() {
	$args = arguments;
	$("#lightBoxContent").load('index.php', {'action': 'lightBox', 'do': 'clear', 'imageID': '', 'orientation': '' }, function() { 
		for($i=0; $i<$args.length; $i++) {
			if($("#"+$args[$i])) {
				$("#"+$args[$i]).load("index.php", { 'action': 'imageThumb', 'imageID': $args[$i] } );
			}
		}
	});
}

function processCategory() {
	$action = arguments[0] == 'categoryManager' ? 'categoryManager' : 'default';
	if($('#'+arguments[1]) && $('#'+arguments[1]).val() == '') {
		$action = false;
		alert('You must provide a category to modify');
	}
	switch($action) {
		case 'categoryManager':
			switch(arguments[2]) {
				case 'showKeywords':
					$("#categoryManager").load("index.php", {'action': 'manageCategories', 'cid': $('#'+arguments[1]).val(), 'type': arguments[2]});
				break;
				case 'addKeyword':
					$("#categoryManager").load("index.php", {'action': 'manageCategories', 'data': $('#'+arguments[1]).val(), 'type': arguments[2], 'cid': arguments[3]});
				break;
				case 'deleteKeyword':
					$("#categoryManager").load("index.php", {'action': 'manageCategories', 'data': $('#'+arguments[1]).val(), 'type': arguments[2], 'cid': arguments[3]});
				break;
			}
			
		break;
		case 'default':
			$focus = arguments[0];
			$("#categoryManager").html("");
			$("#popupPanel").load("index.php", {'action': 'popup', 'type': arguments[0], 'id': $('#'+arguments[1]).val()}, function() {
				$("#popupPanel").css({ display: 'block' });
				$("#"+$focus).focus();
			});
		break;
	}
}



function modifyCategory() { 
	if($("#"+arguments[0]).val()) {
		$("#popupPanelContent").load("index.php?action=manageCategories&type="+arguments[0]+'&'+arguments[0]+'='+escape($("#"+arguments[0]).val()),'', function() {
			$("#browseBox").load("index.php", {'action': 'reloadBrowse'});
		});
	} else {
		alert('You must provide a category to modify');
	}
}

function closePopup() {
	if(arguments[0]) {
		switch(arguments[0]) {
			case 'reloadCategories':
				$("#rightColumn").load("index.php", {'action': 'manageCategories', 'selected': arguments[1]});
				$("#browseBox").load("index.php", {'action': 'reloadBrowse'});
			break;
		}
	}
	$("#popupPanel").css({ display: 'none' }); 
}

function loadLogin() {
	$("#rightColumn").load("index.php?&action=user&userAction="+arguments[0]);
}

function doLogin() {
	$("#rightColumn").load("index.php?"+$("#loginForm").serialize());
}

function showHideLightbox() {
	if(arguments[0] == 'off') $lightBox = 'on';
	switch($lightBox) {
		case 'off':
			if(typeof($lightBoxPosition) != 'undefined') { 
				if($lightBoxPosition.top > $(window).height()) {
					$("#lightBox").css({ top: '95px' });
				}
			}
			$("#lightBox").css({ display: 'block' });
			$lightBox = 'on';
		break;
		case 'on':
			$lightBoxPosition = $("#lightBox").position();
			$("#lightBox").css({ display: 'none' });
			$lightBox = 'off';		
		break;
	}
	
}

function importImages() {
	if($("select#archive").val() != '') {
		$("#importButton").html('<img src="assets/graphics/loadingSmall.gif" />');
		$("#rightColumn").load("index.php?action=processImport&archive="+$("select#archive").val());
	}
}

function performSearch() {
	if($("#keyword0").val() == '' && $("#description").val() == '' && $("#filename").val() == '') {
		alert ("Please enter at least one keyword, description or filename term to search for");
	}
	else {
		closeMore();
		$("#searchButtonInput").html('<img src="assets/graphics/loadingSmall.gif" />');
		$("#rightColumn").load("index.php",$("#searchForm").serializeArray(), function() {
			$("#searchButtonInput").html('<input type="submit" id="search" value="Search" />');
			updateMenu('search');
		});
	}
}

function paginate() {
	$("#rightColumn").load("index.php", {'action': arguments[0], 'page': arguments[1]});
	if(arguments[2]) {
		switch(arguments[2]) {
			case 'menu':
				updateMenu('browse');
			break;
		}
	}
}

function updateMenu() {
	switch(arguments[0]) {
		case 'browse':
			$("#menuSearch").html('<a href="#">search</a>');
			$("#menuBrowse").html('>browse all images');
		
		break;
		case 'search':
			$("#menuSearch").html('>search');
			$("#menuBrowse").html('<a href="javascript:void(0)" onclick="paginate(\'browse\',\'\',\'menu\');">browse all images</a>');
		break;
	}
}

function deleteImage() {
	$("#rightColumn").load("index.php", {'action': 'deleteImage', 'imageID': arguments[0]} );
}

function ajaxCall() {
	mask('none');
	$doIt = true;
	if(arguments[1] == 'deleteImages') {
		$doIt = confirm('Are you sure you wish to delete the entire found set? Please note that there is no way to undo this request. The images and their keywords will be removed from the database and can only be retreived by re-importing');
	}
	if($doIt == true) { 
		$("#"+arguments[0]).load("index.php?action="+arguments[1]+"&"+arguments[2]);
	}
}

$(document).ready(function(){
    $("#lightBox").draggable({handle:'#lightBoxHeader', containment: 'document' });
    $lightBox = 'off'; $moreOpen = false;
    $("#keyword0").autocomplete("assets/includes/autoComplete.php", { mustMatch: true} );
    $additionalKeywords = 0;
});

function addKeyword() {
  $additionalKeywords++;
  $html = '<p id="keywords'+$additionalKeywords+'">'+
  					 '  <label for="keyword'+$additionalKeywords+'">AND</label><input type="text" id="keyword'+$additionalKeywords+'" name="keywords[]" value="" />'+
					 '<a href="javascript:removeKeyword('+$additionalKeywords+')"><img src="assets/graphics/iconMinusSmall.gif" alt="-" title="Delete item" class="top5" /></a><a href="javascript:addKeyword()"><img src="assets/graphics/iconPlusSmall.gif" alt="+" title="Add keyword" class="top5" /></a>'; //+
					 '</p>';
  $('#keywordX').append($html);
  $("#keyword"+$additionalKeywords).autocomplete("assets/includes/autoComplete.php", { extraParams: {restrict: $("#keyword"+($additionalKeywords - 1)).val()},  mustMatch: true} );
}

function removeKeyword() {
  $('#keywords'+arguments[0]).remove();
}

function fadeOut() {
	//$('#'+arguments[0]).animate({width: "450px", height: "0px"}, 500, 'linear'}); 
	//alert('test');
}

function initCategoryManager() {
 	$(document).ready(function(){
    	//$("#category").change(processCategory('categoryManager','category','showKeywords'));	
	});
}

function initKeywordFinder() {
 	$(document).ready(function(){
    	$("#newKeyword").autocomplete("assets/includes/autoComplete.php", {mustMatch: true});	
	});
}

jQuery.fn.center = function() {
    this.css("position","absolute");
    $top = ($(window).height() - this.height() ) / 2 + $(window).scrollTop() < 50 ? 50 : ($(window).height() - this.height() ) / 2 + $(window).scrollTop();
    this.css( {"top" :  $top + "px" } );
    this.css( {"left" : ( $(window).width()  - this.width()  ) / 2 + $(window).scrollLeft() + "px" } );
    return this;
}

