﻿'use strict';
var $j = jQuery;

// Extensions

$j.fn.setFocus = function () {
  var $this = $j(this),
      $inputToFocus;

  if ($this.is(':input')) {
    $inputToFocus = $this;
  } else {
    $inputToFocus = $this.find(':input:visible:enabled:first');
  }

  if (!$inputToFocus.is(':radio') && !$inputToFocus.is(':checkbox') && !$inputToFocus.is('select')) {
    $inputToFocus.select();
  } else {
    $inputToFocus.focus();
  }
}

// End of Extensions

var site = (function ($) {

	var groupVirtualName = 'dpgs';

	function _log(message) {
		if (typeof console !== 'undefined' && console.log) {
			console.log(message);
		}
	}

	function _invoke(params, confirmation, callback) {
		if (typeof confirmation === 'function') { callback = confirmation; }
		if (typeof confirmation === 'string') { if (!confirm(confirmation)) { return false; } }
		$j.post(params.InvokeUrl ? params.InvokeUrl : window.location.path, params, function (data, textStatus) {
			if (typeof callback === 'function') { callback(data, textStatus); }
		});
		return true;
	}

	return {
		invoke: _invoke,
		log: _log,
		configuration: {
			maximumPictureSize: 4 * 1024 * 1024,
			picturePaths: {
				fullUserProfile: 'http:\/\/dpg-storage.s3.amazonaws.com\/' + groupVirtualName + '\/images\/fullprofilepage\/'
			}
		}
	};
})();

$j(function () {
  var currentClass = 'current';

  $j('.expandable-section .title').live('click', function () {
    var $title = $j(this),
					$section = $title.closest('.expandable-section'),
					$sectionBody = $section.find('.body'),
					$expandableGroup = $section.closest('.expandable-group');

    if ($sectionBody.is(':hidden')) {
      $expandableGroup.find('.expandable-section').removeClass(currentClass).find('.body').slideUp();
      $sectionBody.slideDown();
      $section.addClass(currentClass);
    }
  });

  $j('.expandable-section .body').slideUp();
});
//function Post(form) {
//  form = jQuery(form);
//  $j.post(form.attr('action'), form.serialize());
//}


//function Invoke(params, confirmation, callback) {
//  params.InvokeUrl = '\/cms\/controler.aspx';
//  site.invoke(params, confirmation, callback);
//}
function Post(form, green, callback) {
  var f = $j(form);
  var formClass = 'form' + randomString(8);
  var formClass2 = 'form2' + randomString(8);
  var serializedForm = f.serialize();
  var tagsToReplace = f.find('.button-panel');

  if (tagsToReplace.length == 0) {
    tagsToReplace = f.find('input[type="submit"],button[type="submit"]');
  }

  f.addClass(formClass);
  f.addClass(formClass2);
  StartSpinning(formClass2, tagsToReplace, f.find('.in-progress-message').val(), green);
  if (serializedForm) {
    serializedForm += '&FormClass=' + formClass;
  }
  else {
    serializedForm = 'FormClass=' + formClass;
  }
  jQuery.post(f.attr('action'), serializedForm, function () {
    StopSpinning(formClass2);
    f.removeClass(formClass2);
  });

  return false;
}

function StartSpinning(formClass, control, customMessage, green) {
  var DEFAULT_MARGIN_LEFT = 5;
  var DEFAULT_MARGIN_TOP = 2;

  if ($j('.' + formClass).find('.progress-spinner').length == 0) {

    if (customMessage) {
      if (customMessage == 'no-text') customMessage = '';
    } else {
      customMessage = 'In Progress...';
    }
    if (green == true) {
      $j(control).after('<div class="progress-spinner"><img src="/local/css/images/loader_green.gif" style="vertical-align: middle;"/> ' + customMessage + '</div>');
    } else {
      $j(control).after('<div class="progress-spinner"><img src="/local/css/images/loader.gif" style="vertical-align: middle;"/> ' + customMessage + '</div>');
    }
    var marginLeft = $j(".progress-spinner").css('margin-left');
    var marginTop = $j(".progress-spinner").css('margin-top');

    if (marginLeft == DEFAULT_MARGIN_LEFT + 'px' && marginTop == DEFAULT_MARGIN_TOP + 'px') {
      var sumbitBtnMarginLeft = $j(control).css('margin-left');
      var sumbitBtnMarginTop = $j(control).css('margin-top');

      sumbitBtnMarginLeft = sumbitBtnMarginLeft.substring(0, sumbitBtnMarginLeft.indexOf('px'))
      sumbitBtnMarginTop = sumbitBtnMarginTop.substring(0, sumbitBtnMarginTop.indexOf('px'))

      if (sumbitBtnMarginLeft == '') { sumbitBtnMarginLeft = '0'; }
      if (sumbitBtnMarginTop == '') { sumbitBtnMarginTop = '0'; }

      $j(".progress-spinner").css('margin-left', (parseInt(DEFAULT_MARGIN_LEFT) + parseInt(sumbitBtnMarginLeft)) + 'px');
      $j(".progress-spinner").css('margin-top', (parseInt(DEFAULT_MARGIN_TOP) + parseInt(sumbitBtnMarginTop)) + 'px');
    }

  }
  else {
    $j('.' + formClass).find('.progress-spinner').show();
  }

  $j(control).hide().addClass('in-progress');
  $j('.' + formClass).find('.form-result').empty();
}

function ShowFormMessage(formClass, message) {
  $j('.' + formClass).find('.form-result').html(message).show();
}

function StopSpinning(formClass) {
  $j('.' + formClass).find('.in-progress').show().removeClass('.in-progress');
  $j('.' + formClass).find('.progress-spinner').hide();
}
function Update(expression, url, data) {
  jQuery.post(url, data, function (responseText, status) { jQuery(expression).html(responseText); });
}

function Invoke(params, confirmation) {
  if (typeof confirmation === 'string') { if (!confirm(confirmation)) { return; } }
  jQuery.post(params.InvokeUrl ? params.InvokeUrl : window.location.path, params);
}

function randomString(string_length) {
  var randomstring = '';

  for (var i = 0; i < string_length; i++) {
    randomstring += Math.floor(Math.random() * 10); ;
  }

  return randomstring;
}
