
var downloadBinPath = '/media/image-search/download-bin/';

$(document).ready(function() {
	
	setClickable();
	loadImageList();
	setupBinPosition();
	TB_init();
	
	// wrap event calendar in a static height
	$("#supplement object").wrap('<div class="calendarHoldingTank" style="height:136px;"></div>');
	
});

function setupBinPosition () {
	$(window).scroll(binPosition);
}

function binPosition () {
		var scrollYOffset = getScrollXY()[1] - 415;
		
		// min
		if (scrollYOffset < 0)
			scrollYOffset = 0;
		
		//$("#imageDownloadBin").css('top',scrollYOffset + 'px');
		if (scrollYOffset > 0) {
			$('#imageDownloadBin').css('position','fixed');
			$('#imageDownloadBin').css('top','0px');
			$('#imageDownloadBin').css('margin-top','10px');
		} else {
			$('#imageDownloadBin').css('position','static');
			$('#imageDownloadBin').css('top','0px');
			$('#imageDownloadBin').css('margin-top','25px');
		}
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getDocumentWidthHeight() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth, myHeight];
}

function setClickable () {
	
	// image list page clickable
	$('#imageLibraryList .imageLibraryItem').each(function(i) {
		
		var id = $(this).find('.imageLibraryData .id').text();
		var inDownloadBin = $(this).find('.imageLibraryData .inDownloadBin').text();
		
		var indicatorContainer = $(this).find('div.imageLibraryItemDownloadLink');
		var addLink = $(this).find('.imageLibraryItemAddLink');
		var removeLink = $(this).find('.imageLibraryItemRemoveLink');
		
		addLink.find('a').click(function () {
				removeLink.addClass('loadingImgActive');
				addLink.addClass('loadingImgActive');
				addImageToDownloadList(id, indicatorContainer, addLink, removeLink);				
				
				this.blur();
				return false;
		});
		
		removeLink.find('a').click(function () {
			removeLink.addClass('loadingImgActive');
			addLink.addClass('loadingImgActive');
			removeImageFromDownloadList(id, indicatorContainer, addLink, removeLink);
			
			this.blur();
			return false;
		});
		
	});
	
	// download page clickable
	$('#download-form .imageLibraryItem').each(function(i) {
		
		var id = $(this).find('.imageLibraryData .id').text();
		
		var imageBox = $(this);
		var removeLink = $(this).find('.imageLibraryItemRemoveLink');
		
		removeLink.find('a').click(function () {
			removeLink.addClass('loadingImgActive');
			removeImageFromDownloadPageList(id, imageBox);
						
			this.blur();
			return false;
		});
		
	});
	
	stripeDownloadPageList();
	
}



function addImageToDownloadList (id, indicatorContainer, addLink, removeLink) {
	
	var imageID = parseInt(id,10);
	
	if (isNaN(imageID)) {
		return;
	}
	
	// server request:
	$('div#imageDownloadList').load(downloadBinPath, {id:imageID, action:'add'}, function () {
		addLink.hide();
		removeLink.show();
		addLink.removeClass('loadingImgActive');
		removeLink.removeClass('loadingImgActive');
		TB_download_bin_init();
		setBinClickable();
	});
			
	

	
}

function removeImageFromDownloadList (id, indicatorContainer, addLink, removeLink) {	
	
	var imageID = parseInt(id,10);
	
	if (isNaN(imageID)) {
		return;
	}
	
	// server request:
	$('div#imageDownloadList').load(downloadBinPath, {id:imageID, action:'remove'}, function () {
		removeLink.hide();
		addLink.show();
		addLink.removeClass('loadingImgActive');
		removeLink.removeClass('loadingImgActive');
		
		TB_download_bin_init();
		setBinClickable();
		
	});
			
	
}

function removeImageFromDownloadPageList (id, imageBox) {	
	
	var imageID = parseInt(id,10);
	
	if (isNaN(imageID)) {
		return;
	}
	
	$.post(downloadBinPath, {id:imageID, action:'remove'}, function (data) {
		imageBox.hide();
		
		stripeDownloadPageList();
		
		// reload the page if nothing else is there
		if ($('#download-form .imageLibraryItem:visible').length == 0)
			location.reload();
	});
			
	
}

function stripeDownloadPageList () {
	
	$('#download-form .imageLibraryItem:visible').each(function(i) {
		if (i%4 == 0 || i%4 == 1) {
			$(this).addClass('stripedItem');
		}
	});
	
}

function removeImageFromDownloadBin (id, indicatorContainer) {	
	
	var imageID = parseInt(id,10);
	
	if (isNaN(imageID)) {
		return;
	}
	
	// server request:
	$('div#imageDownloadList').load(downloadBinPath, {id:imageID, action:'remove'}, function () {
		
		// search and remove from the list
		$('#imageLibraryList div.imageLibraryItem').each(function(i) {
			
			var listID = $(this).find('.imageLibraryData .id').text();
			var inDownloadBin = $(this).find('.imageLibraryData .inDownloadBin').text();
			
			if (imageID == listID) {				
				var addLink = $(this).find('.imageLibraryItemAddLink');
				var removeLink = $(this).find('.imageLibraryItemRemoveLink');
				
				removeLink.hide();
				addLink.show();
				addLink.removeClass('loadingImgActive');
				removeLink.removeClass('loadingImgActive');
			}

		});
		
		TB_download_bin_init();
		setBinClickable();
	});
			
	
}

function setBinClickable () {
	
	$('div.imageDownloadBinListItem').each(function(i) {
		
		var id = $(this).find('.imageLibraryData .id').text();
		
		var indicatorContainer = $(this).find('.removeIcon');
		var removeLink = $(this).find('a.removeLinkIcon');
		
		removeLink.click(function () {
			indicatorContainer.addClass('loadingBinIcon');
			removeImageFromDownloadBin(id, indicatorContainer);
			
			this.blur();
			return false;
		});
		
	});
	
}


function loadImageList () {
		
		// server request:
		$('div#imageDownloadList').load(downloadBinPath, {}, function () {
				$('div#imageDownloadList').fadeIn();
				TB_download_bin_init();
				setBinClickable();
		});
			
}

