﻿var $loadingAnimation = null;

function showFavoritesFolders() {
	var div = $('#favoritesFolders');
	if (div) {
		div.toggleClass('ie6_position_fixed', true);
		div.centerScreen();
		div.show();
		
		$("#favoritesFoldersContent").load("/0_281.aspx?rnd=" + Math.random());
		
		$loadingAnimation = $('<img src="/images/loadingAnimation.gif" width="208" height="13" style="position: absolute; left: -5000px; top: -5000px; z-index: 400;"/>').appendTo('body');
	}
}

function showInqueryDialog() 
{
	var div = $('#inqueryDialog');
	if (div) 
	{
		div.toggleClass('ie6_position_fixed', true);
		div.centerScreen();
		div.show();
		$("#inqueryResult").hide().html('');
		$("#inqueryWait").hide();
		$("#inqueryMessageHolder").show();
	}
}

function sendInquery(productID)
{
	$("#inqueryMessageHolder").hide();
	$("#inqueryResult").hide();
	$("#inqueryWait").show();
	jQuery.get("/0_303_product=" + productID + ".aspx",
		{
			'message': $("#inqueryMessage").val(),
			'rnd': Math.random()
		},
		function(data) 
		{
			$("#inqueryResult").html(data);
			$("#inqueryWait").hide();
			$("#inqueryResult").show();
		}
	);
	
}

function hideInqueryDialog()
{
	var div = $('#inqueryDialog');
	if (div) 
	{
		div.toggleClass('ie6_position_fixed', false);
		div.hide();
		$("#inqueryResult").html('');
	}
}

function radioGroupValue(radioGroup) {
	for(i=0; i < radioGroup.length; i++) {
		if (radioGroup[i].checked == true) 
			return radioGroup[i].value;
	}
	
	return null;
}

function applyFavoritesFolders(folder) {
	var product = document.forms.folderList.productId.value;
	
	$folders = $('#favoritesFolders');
	if ($loadingAnimation) {
		$loadingAnimation.css({
			left: $folders.position().left + Math.round($folders.width() / 2 - 90),
			top: $folders.position().top + Math.round($folders.height() / 2) 
		});
	}
	
	if (folder && product) {
		jQuery.get(
			'/0_282.aspx', 
			{
				'action': 'AddItem',
				'folderId': folder,
				'itemId': product,
				'hash': new Date()
			}, 
			function(data) {
				if ($loadingAnimation) {
					$loadingAnimation.css({
						left: '-5000px',
						top: '-5000px' 
					});
				}
			
				if (!data) {
					alert('An error has occurred while processing ajax request');
				} else {
					hideFavoritesFolders();
				}
			}
		)
	}
}

function hideFavoritesFolders() {
	var div = $('#favoritesFolders');
	if (div) {
		div.toggleClass('ie6_position_fixed', false);
		div.hide();
	}
}

function addFolderDialog() {
	jQuery('#favoritesFolders').css('z-index', '10');
	jQuery.facebox('<h3 style="padding-top: 0.3em; color: black">Add new folder</h3><br /><form><label for="folderName" style="color: black">Folder name</label><br /><input type="text" name="folderName" id="folderName" style="width: 100%;"/><br /><br /><center><input type="button" value="OK" onclick="addFolderRequest(this.form.folderName.value);" style="width: 60px; margin-right: 20px;"/><input type="button" value="Cancel" onclick="hideAddFolderDialog();" style="width: 60px;"/></center></form>');
	jQuery('#facebox').css({
		width: '360px',
		left:	$(window).width() / 2 - 190
	});
}

function hideAddFolderDialog() {
	jQuery.facebox.close(function () { 
		jQuery('#favoritesFolders').css('z-index', '200'); 
	});
}

function addFolderRequest(name) {
	jQuery.get(
		'/0_282.aspx', 
		{
			'action': 'AddFolder',
			'folderName': name,
			'hash': new Date()
		}, 
		function(data) {
			if (data > 0) {
				if ($('#favoritesFoldersContent a').length == 0)
					$('#favoritesFoldersContent').empty();

				jQuery(new StringBuilder('<a href="#" onclick="applyFavoritesFolders(', data, '); return false;"><img src="/images/b/icon-folder-black.gif" width="14" height="12" border="0" alt="" style="vertical-align: middle; margin-right: 7px;"/>', name, '</a><br />').ToString()).appendTo("#favoritesFoldersContent");
			} else {
				alert('An error has occurred while processing ajax request');
			}
			
			hideAddFolderDialog();
		}
	)
}


/* tooltips */
var timerIDs = new Object();
var tooltipstate = new Object();

jQuery(document).ready(function() {
	$('.tooltips-enabled').mouseenter(function(event) {
		showHint(this, event);
	});
	$('.tooltips-enabled').mousemove(function(event) {
		moveHint(this, event);
	});
	$('.tooltips-enabled').mouseleave(function(event) {
		hideHint(this, event);
	});
});

function showHint(owner, event) {
	tooltipstate[owner] = 1;
	timerIDs[owner] = setTimeout(function() { 
		tooltipstate[owner] = 2;
		$('.tooltips-body', $(owner)).fadeIn('slow'); 
	}, 700);
}

function moveHint(owner, event) {
	if (tooltipstate[owner] == 1) {
		$('.tooltips-body', $(owner)).css({
			'left': event.pageX,
			'top': event.pageY
		})
	}
}

function hideHint(owner) {
	clearTimeout(timerIDs[owner]);
	 $('.tooltips-body', $(owner)).hide();
	 tooltipstate[owner] = 0;
}