$(function()
{
	if(window.fnInit)
	{
		fnInit();
	}
	if(window.fnInitWizard)
	{
		fnInitWizard();
	}
	
	/* divTopForm */
	$('#divTopForm input').not('[type="submit"]').focus(function()
	{
		if($(this).attr('value') == 'e-mail' || $(this).attr('value') == 'passwd')
		{
			$(this).attr('value', '').removeClass('topFormEmpty');
		}
	});
	$('#divTopForm input').not('[type="submit"]').blur(function()
	{
		if($.trim($(this).attr('value')) == '')
		{
			if($(this).is(':password'))
			{
				$(this).attr('value', 'passwd');
			}
			else
			{
				$(this).attr('value', 'e-mail');
			}
			$(this).addClass('topFormEmpty');
		}
	});
	
	$('#ulBugReport li').click(function()
	{
		$('#divBottomContent').slideUp('fast', function()
		{
			$('#divBugReportForm').slideDown('fast');
		});
	});
	
	$('#divBugReportForm form textarea').keyup(function()
	{
		if($.trim($(this).val()))
		{
			$('#divBugReportForm input:submit').removeAttr('disabled');
		}
		else
		{
			$('#divBugReportForm input:submit').attr('disabled','disabled');
		}
	});
	
	$('#divBugReportForm input:submit').click(function(oEvent)
	{
		oEvent.preventDefault();
		$('#divBugReportForm').slideUp('fast', function()
		{
			$('#divBugReportInfo').slideDown('fast');
		});
		
		$.ajax(
		{
			type:		'post',
			url:		'/bug_report',
			data:		$('#divBugReportForm form').serialize(),
			beforeSend: function()
			{
				$('#divBugReportForm form textarea').val('');
				$('#divBugReportForm input:submit').attr('disabled','disabled');
			}	
		});
	});
	
	$('#divBugReportForm button').click(function()
	{
		$('#divBugReportInfo').slideUp('fast');
		$('#divBugReportForm').slideUp('fast', function()
		{
			$('#divBottomContent').slideDown('fast');
		});
	});
	
	$('#divBugReportInfo button').click(function()
	{
		$('#divBugReportForm').slideUp('fast');
		$('#divBugReportInfo').slideUp('fast', function()
		{
			$('#divBottomContent').slideDown('fast');
		});
	});
	
	$('.click').click();
	
	oImg = new Image();
	oImg.src = '/img/v12/ajax-loader.gif';
});



/* account */
function fnHelpButtons(sButName, sDivName)
{
	$('#' + sButName).click(function()
	{
		$(this).attr('disabled', 'disabled');
		$('#' + sDivName).slideDown('fast')
	});
	
	$('#' + sDivName).find('button:first').click(function()
	{
		$('#' + sDivName).slideUp('fast')
		$('#' + sButName).removeAttr('disabled');
	});
}



/* account: wizard */
function fnLeaveWizardButton()
{
	$('#butLeaveWizard').click(function()
	{
		window.location.href = '/account/wizard_leave';
	});
}



/* account: banned */
function fnBannedButton(sConfirmText)
{
	$('#divAccount td button').click(function()
	{
		if(window.confirm(sConfirmText))
		{
			window.location.href = '/account/banned_action/' + $(this).attr('name');
		}
	});
}



/* account: site, link */
function fnSiteLinkTabs()
{
	var cTrDivTab = $('.trDivTab');
	cTrDivTab.find('div > div').hide();
	$('button.refresh').hide();
	
	$.each($('.trUlTab > td > ul'), function(i, oUl)
	{
		$.each($(this).children('li'), function(j, oLi)
		{
			$(this).click(function()
			{
				$(oUl).children('li').removeClass('selected');
				$(oLi).addClass('selected');
				
				var oTr = cTrDivTab.eq(i);
				var oDiv = oTr.find('div > div:eq(' + j + ')');
				
				if(oTr.is(':hidden'))
				{
					oTr.show();
				}
				if(oDiv.is(':hidden'))
				{
					oTr.find('div > div:visible').slideUp('fast');
					oDiv.slideDown('fast');
					
					/* site_installation*/
					if(oDiv.find('input[name^="installSiteType"]').is('.jsRunFnSiteInstallation'))
					{
						oDiv.find('input[name^="installSiteType"]').change();
					}
					
					/* link_exchanged */
					if(oDiv.is('.link_tab_exchanged'))
					{
						oTr.find('button.refresh').show().click(function()
						{
							_fnGetLinkExchanged(oDiv, 'exchanged');
						});
						
						if(oDiv.is(':empty'))
						{
							_fnGetLinkExchanged(oDiv, 'exchanged');
						}
					}
					else
					{
						oTr.find('button.refresh').hide();	
					}
				}				
			});
		});
	
		cTrDivTab.eq(i).find(':button:first').click(function()
		{
			$(oUl).children('li').removeClass('selected');
				
			cTrDivTab.eq(i).find('div > div:visible').slideUp('fast', function()
			{
				cTrDivTab.eq(i).hide();
			})
		});
	});
}



/* account: site */
function fnSiteInstallation()
{
	$('.trDivTab').each(function(i)
	{
		$(this).find('input[name^="installSiteType"]').change(function()
		{
			var oDiv = $(this).parent().parent().parent();
			if($(this).val() == 'site')
			{
				oDiv.css('min-height', oDiv.height());
				oDiv.find('ol:last').slideUp('fast', function()
				{
					oDiv.find('ol:first').slideDown('fast');
				});
			}
			else if($(this).val() == 'generator')
			{
				oDiv.css('min-height', oDiv.height());
				oDiv.find('ol:first').slideUp('fast', function()
				{
					oDiv.find('ol:last').slideDown('fast');
					var iSiteId = oDiv.find('ol:last li:first img').attr('name');
					if(oDiv.find('ol:last li:first img').attr('name'))
					{
						$.ajax(
						{
							type:		'get',
							url:		'/account/site/get_generator_form/' + iSiteId,
							success: function(sHtml)
							{
								if($.trim(sHtml) == 'signin')
								{
									window.location.href = '/signin';
									return;
								}
								
								oDiv.find('ol:last li:first').html(sHtml);
								oDiv.find('ol:last li:first form').submit(function(oEvent)
								{
									oEvent.preventDefault();
									_fnSiteGetGenerator(this, iSiteId);
								});
							},
							error: function(XMLHttpRequest, textStatus, errorThrown)
							{
								oDiv.find('ol:last li:first').text(document.domain + ': ' + XMLHttpRequest.status + ' ' + textStatus);
							}
						});
					}
				});
			}
			oDiv.find('.info_installation').slideDown('fast');
		});
	});
}



/* account: site */
function _fnSiteGetGenerator(oForm, iSiteId)
{
	var oP1 = $(oForm).next()
	var oP2 = $(oP1).next()
	
	$.ajax(
	{
		type:		'post',
		url:		'/account/site/set_generator',
		data:		$(oForm).serialize(),
		beforeSend: function()
		{
			oP2.slideUp('fast');
			oP1.slideDown('fast');
		},
		success: function(sHtml)
		{
			if($.trim(sHtml) == 'signin')
			{
				window.location.href = '/signin';
				return;
			}
			
			var aHtml = sHtml.split('|||');
			oP1.slideUp('fast');
			oP2.removeClass('notice_ok');
			oP2.removeClass('notice_error');
			oP2.addClass('notice_' + $.trim(aHtml[0])).html($.trim(aHtml[1])).slideDown('fast');
			
			if($.trim(aHtml[0]) == 'ok')
			{
				window.location.href = '/account/site/get_generator_skrypt/' + iSiteId;
				return;
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			oP1.slideUp('fast');
			oP2.html(document.domain + ': ' + XMLHttpRequest.status + ' ' + textStatus).slideDown('fast');
		}
	});
}



/* account: site */
function fnSiteDownload()
{
	$('.trDivTab').each(function(i)
	{
		$(this).find('ol :button:first').click(function()
		{
			window.location.href = '/account/site/download/' + $(this).attr('name');
		});
	});
}



/* account: site */
function fnSiteInstall(bIsItWizard)
{
	$('.trDivTab').each(function(i)
	{
		$(this).find('ol > li:last-child :button').click(function()
		{
			var oP1 = $(this).parent().parent().parent().find('ol:last').next();
			var oP2 = oP1.next();
			var sUrl = '';
			if(bIsItWizard)
			{
				sUrl = '/account/wizard/site_install/' + $(this).attr('name');
			}
			else
			{
				sUrl = '/account/site/install/' + $(this).attr('name');
			}
			
			
			
			$.ajax(
			{
				type:		'get',
				url:		sUrl,
				beforeSend: function()
				{
					oP2.slideUp('fast');
					oP1.slideDown('fast');
				},
				success: function(sHtml)
				{
					if($.trim(sHtml) == 'signin')
					{
						window.location.href = '/signin';
						return;
					}
					
					var aHtml = sHtml.split('|||');
					
					if($.trim(aHtml[0]) == 'ok' && bIsItWizard)
					{
						window.location.href = '/account/wizard/link_add';
					}
					else
					{
						oP1.slideUp('fast');
					
						if(aHtml.length >= 4)
						{
							$('#tdStatusImg' + i).children('img').attr('src', '/img/v12/install/' + $.trim(aHtml[2]) + '.png');
							if(aHtml.length == 4)
							{
								$('#tdStatusInfo' + i).html($.trim(aHtml[3]));
							}
							else
							{
								$('#tdStatusInfo' + i).html($.trim(aHtml[3]) + '<br />' + $.trim(aHtml[4]));
							}
						}
						
						oP2.removeClass('notice_ok');
						oP2.removeClass('notice_error');
						oP2.addClass('notice_' + $.trim(aHtml[0])).html($.trim(aHtml[1])).slideDown('fast');
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown)
				{
					oP1.slideUp('fast');
					oP2.html(document.domain + ': ' + XMLHttpRequest.status + ' ' + textStatus).slideDown('fast');
				}
			});
		});
	});
}



/* account: site */
function fnSiteForm()
{
	$('form[target^="iframe"]').submit(function()
	{
		$(this).attr('action', $(this).find('input[type="text"]').val());
	});
}



/* account: site, link */
function fnSiteLinkDelete(sConfirmText, sType, sPageNumber)
{
	$('.trDivTab').each(function()
	{
		$(this).find('button:last').click(function()
		{
			if(window.confirm(sConfirmText))
			{
				window.location.href = '/account/' + sType + '/delete/' + $(this).attr('name') + '/' + sPageNumber;
			}
		});
	});
}



/* account: link_exchanged */
function _fnGetLinkExchanged(oDiv, sAction, sPageNumber, iSiteId)
{
	var	iId = oDiv.attr('id').replace('divLink_', '');
	
	$.ajax(
	{
		type:		'get',
		url:		'/account/link/' + sAction + '/' + iId + (iSiteId ? '/' + iSiteId : '') + (sPageNumber ? '/' + sPageNumber : ''),
		beforeSend: function()
		{
			oDiv.css('min-height', oDiv.height())
			oDiv.html('<img src="/img/v12/ajax-loader.gif" alt="" />');
		},
		success: function(sHtml)
		{
			if($.trim(sHtml) == 'signin')
			{
				window.location.href = '/signin';
				return;
			}
			
			if($.trim(sHtml) == 'reload')
			{
				location.reload();
				return;
			}
						
			oDiv.html(sHtml);
			
			/* pagination */
			$(oDiv).find('a').not('a[target]').click(function(oEvent)
			{
				oEvent.preventDefault();
				var aUrl = $(oEvent.currentTarget).attr('href').replace('http://www.strong-links.com', '').split('/');
				
				if(aUrl.length == 6)
				{
					_fnGetLinkExchanged(oDiv, 'exchanged', aUrl[aUrl.length - 1]);
				}
				else
				{
					_fnGetLinkExchanged(oDiv, 'exchanged');
				}
			});
			
			/* ban */
			$(oDiv).find('tr td .jBan').click(function(oEvent)
			{
				if(window.confirm(sExchangedBanConfirmText))
				{
					if(sPageNumber)
					{
						_fnGetLinkExchanged(oDiv, 'exchanged_action', sPageNumber, $(this).attr('name'));
					}
					else
					{
						_fnGetLinkExchanged(oDiv, 'exchanged_action', false, $(this).attr('name'));
					}
				}
			});
			
			/* report */
			$(oDiv).find('tr td .jReport').click(function()
			{
				var oTd = $(this).parent();
				
				var oButton = $(this);
				var oPButtonHide = oTd.find('p').eq(0);
				var oPInfo = oTd.find('p').eq(1);
				var oPSent = oTd.find('p').eq(2);
				var oForm = oTd.find('form');
				
				oButton.hide();
				oPSent.slideUp('fast');
				oPButtonHide.slideUp('fast', function()
				{
					oPButtonHide.slideDown('fast');
					oPInfo.slideDown('fast');
					oForm.slideDown('fast');
				});
				
				oForm.find('textarea').keyup(function()
				{
					if($(this).val())
					{
						oForm.find('input:submit').removeAttr('disabled');
					}
					else
					{
						oForm.find('input:submit').attr('disabled','disabled');
					}
				});
				
				oPButtonHide.find('button').click(function()
				{
					oPButtonHide.slideUp('fast');
					oPInfo.slideUp('fast');
					oForm.slideUp('fast', function()
					{
						oButton.show();
					});		
				});
				
				oForm.find('input:submit').click(function(oEvent)
				{
					oEvent.preventDefault();
					
					oPButtonHide.slideUp('fast');
					oPInfo.slideUp('fast');
					oForm.slideUp('fast', function()
					{
						oButton.show();
						oPSent.slideDown('fast');
					});
					
					$.ajax(
					{
						type:		'post',
						url:		'/account/link/exchanged_report',
						data:		oForm.serialize(),
						beforeSend: function()
						{
							oForm.find('textarea').val('');
							oForm.find('input:submit').attr('disabled','disabled');
						}
					});
				});
			});
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			oDiv.html(document.domain + ': ' + XMLHttpRequest.status + ' ' + textStatus);
		}
	});
}





