// document onready event listener
jQuery(document).ready(function($){
	$('input').placeholder();
	$('div.block_foto a div img')
	// listen to the mouse over event
	.live('mouseover', function(e) {
		var id = $(this).attr('id');
		var item_id = id.replace('image', '');
		var hint = $('#hint_' + id);
		var x = e.pageX + 15;
		var y = e.pageY + 15;
		
		// in case of container already exists
		if (hint.size()) {
			hint.show();
			hint.css({position: 'absolute', top: y, left: x});
		}
		// oterwise - ajax preload and create it
		else {
			// create hint box with NO ajax animations inside it
			hint = $('<div class="info" id="hint_' + id + '"/>');
			//hint.append('<img src="i/lightbox_loading.gif"/>');

			$('#oBody').append(hint);

			hint.show();
			hint.css({position: 'absolute', top: y, left: x, overflow: 'hidden', zIndex:'99999'});
			
  		   // get contents via ajax call
			$.getJSON(window.location.href, {searchPreview: true, item_id: item_id}, function(data) {
				// render table
				var t = $('<table/>'); 
				for (var field in data['recordset']) { 
					if (field.indexOf('_IMG') != -1) continue;
					var caption = data['captions'][field]; 
					var value = data['recordset'][field]; 
					
					switch(field) { 
						case 'PC_IS_MODEL_RELEASE_REQUIRED':
						case 'PI_UNITS':
							continue;
						break;
						case 'PHOTO_IS_MODEL_RELEASE_AVAILABLE':
							if (data['recordset']['PC_IS_MODEL_RELEASE_REQUIRED'] == '0') continue;
							if (value == '1') {
								value = data['translations']['PHOTO_IS_MODEL_RELEASE_AVAILABLE'];
								
							} else {
								if (data['recordset']['PC_IS_MODEL_RELEASE_REQUIRED'] == '1') {
									value = data['translations']['PHOTO_NO_MODEL_RELEASE_AVAILABLE'];
								} else {
									continue;
								}

							}
						break;
						case 'PI_FILE_SIZE':
							value = value + ' KB';
						break;
						case 'PI_PIXEL_SIZE_WIDTH':
						case 'PI_PIXEL_SIZE_HEIGHT':
							value = value + ' Pixels';
						break;
						case 'PI_PRINT_SIZE':
							value = value + ' ' + data['recordset']['PI_UNITS'];
						break;
						case 'PHOTO_INFO_ENG':
						case 'PHOTO_INFO_DUTCH':
							if (value == '') continue;
						break;
					}

					var row = $('<tr/>');
					var cell1 = $('<td class="inf_left"></td>');
					var cell2 = $('<td class="inf_right"></td>');
					cell1.html(caption + ':');
					cell2.html(value);
					row.append(cell1);
					row.append(cell2);
					t.append(row);
				}
				hint.empty();
				hint.append(t);
			}); 
		}
	})
	// listen to the mouse out event
	.live('mouseout', function(e) {
		var id = $(this).attr('id');
		$('#hint_' + id).hide();
	})
	// listen to the mouse move event
	.live('mousemove', function(e) {
		var id = $(this).attr('id');
		var x = e.pageX + 15;
		var y = e.pageY + 15;
		
		var box = document.getElementById('hint_' + id);

		var height = $('#hint_' + id).height() + 20;
		var width =	$('#hint_' + id).width() + 20;
		
	//	var height = Element.getHeight(box) + 20;
	//	var width =  Element.getWidth(box) + 20;


		if ((window.innerWidth && (x > window.innerWidth/2+window.pageXOffset)) || (document.body.clientWidth && (x > document.body.clientWidth/2+document.body.scrollLeft)))
		{
			x = x - width;
		}

		if ((window.innerHeight && (y > window.innerHeight/2+window.pageYOffset)) || (document.body.clientHeight && (y > document.body.clientHeight/2+document.body.scrollTop)))
		{
			y = y - height; 
		}

		$('#hint_' + id).css({
			top: y,
			left: x
		});

	});
});

