﻿loadFuncs = [];

$(document).ready(function() {
	pageLoaded("ready");
});

$(window).load(function(){
  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(pageLoaded);
  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(checkExpandedState);
});
function pageLoaded(sender, args) {
	try {
		$('a.deleteLink').confirm({ 'msg': 'Confirm: ', 'wrapper': '<span class="deleteLink"></span>' });
	} catch (e) { }

	var qtipOptions = { 'position': { 'my': 'top right', 'target': 'mouse', 'adjust': { 'x': 0, 'y': 3} }, 'style': { 'classes': 'popup'} }
	//$('.tip[title]').qtip({ 'position': { 'my': 'top left', 'target': 'mouse', 'adjust': { 'x': 10, 'y': 10 } }, 'style': { 'classes': 'popup'} }); // add tooltips to elements with the 'tip' class and titles
	// hack because links that are disabled don't receive the hover event or something...
	$('.tip[title]').each(function() {
		var e = $(this);
		if (e.attr("disabled")) {
			e.parent().attr({ 'title': e.attr('title') }).addClass('tip').qtip(qtipOptions);
			e.removeAttr('title').removeClass('tip');
		}
		else
			e.qtip(qtipOptions);
	});

	if (!(typeof(sender) == "string" && sender == "ready"))
	{
		$('input[type="text"], textarea').each(function() {
			if(this.value.length == 0)
				this.value = "enter text...";
		});        
		$('input[type="text"], textarea').focus(function() {
			if (this.value == "enter text...")
				this.value = '';
		});
		$('input[type="text"], textarea').blur(function() {
			if ($.trim(this.value) == '')
				this.value = "enter text...";
		});
	}

	for (var i = 0; i < loadFuncs.length; i ++)
		if (typeof(loadFuncs[i]) == "function")
			loadFuncs[i]();
}

function checkExpandedState()
{
    for (var key in collapseHashState) {
		$('#' + key).css({ 'display': collapseHashState[key] });
		try {
			$('#' + key).parent().find('a.collapsible.collapsed').removeClass('collapsed');
		} catch(e) { }
	}
        
}

collapseHashState = {};
function toggleCollapse(element)
{
	var e = $(element);
	if (e.hasClass('collapsed'))
		e.removeClass('collapsed');
	else
		e.addClass('collapsed');

	var collapsibles = e.parent().parent().find('div.collapsible');
	collapsibles.each(function(){
		$(this).slideToggle({ complete: function(){ if (this.id){ collapseHashState[this.id] = $(this).css('display'); } } });
	});
}

function addRow() {
   var baseRow = document.getElementById('cloneTemplate');
   var newRow = baseRow.cloneNode(true);
   var rows = $(baseRow).parent().children($(newRow).get(0).tagName).length;
   newRow.id = '';
   if ($(baseRow).parent().get(0).tagName.toLowerCase() == 'table' || $(baseRow).parent().get(0).tagName.toLowerCase() == 'tbody') {
      newRow.style.display = 'table-row';
      $(newRow).removeClass('hide');
      $(baseRow).parent().append(newRow);
   } else {
      newRow.style.display = 'block';
      $(baseRow).siblings('a:first').before(newRow);
   }
   $(newRow).find(':radio').attr('name', 'sc' + rows);
   return newRow;
}

function deleteRow() {
   var tag = $('#cloneTemplate').get(0).tagName;
   if ($('#cloneTemplate').attr('class') != '')
      tag += '.' + $('#cloneTemplate').attr('class').substr(0, $('#cloneTemplate').attr('class').indexOf(' '));
   if($('#cloneTemplate').parent().children(tag + ':visible').length > 1)
      $('#cloneTemplate').parent().children(tag + ':visible:last').remove();
}

// Toolbar popover helper function
var popupTimeout, popupBottom, popupBody;
function showPopover(elem, title, text) {
   $('.popover').remove();
   $('.popover-icon').remove();
   var img = $(elem).children('img');
   var src = $(img).attr('src');
   var id = src.substr(src.indexOf('_') + 1, src.indexOf('.') - src.indexOf('_') - 1) + 'Popover';
   $('.body').append('<div class="popover" id="' + id + '">');
   $('#' + id).append('<img src="/images/popover_top.png">');
   $('#' + id).append('<div class="popover-body"><div class="popover-title">' + title + '</div>' + text + '</div>');
   $('#' + id).append('<div class="popover-bottom"><div class="popover-bottom-inner"><img src="' + src + '"></div></div>');
   $('#' + id).append('<div class="clr"></div>');
   $('.popover-body').children(':last').css('margin-bottom', 0);
   $('#' + id).offset({ top: $(elem).offset().top - $('#' + id).height() + 19 + $(elem).height(), left: $(elem).offset().left - 264.5 + $(elem).width() });
   clearTimeout(popupTimeout);
   popupBody = 0;
   popupBottom = 1;
   $('#' + id + ' .popover-body').mouseenter(function(){
      clearTimeout(popupTimeout);
      popupBody = 1;
   }).mouseleave(function(){
      if (!jQuery.data($(this).parent(), 'bottom')) {
         clearTimeout(popupTimeout);
         popupTimeout = setTimeout(function(){ $('.popover').children().remove(); }, 100);
      }
      popupBody = 0;
   });
   $('#' + id + ' .popover-bottom-inner').mouseenter(function(){
      clearTimeout(popupTimeout);
      popupBottom = 1;
   }).mouseleave(function(){
      if (!popupBody) {
         clearTimeout(popupTimeout);
         popupTimeout = setTimeout(function(){ $('.popover').remove(); }, 100);
      }
      popupBottom = 0;
   });
}

function logout()
{
	$.getJSON("/REST.aspx", {'key': 'logout'}, function(data){ document.location.href = '/' });
}

