﻿
function pageLoad()
{
	$('#LoginUsername').focus();
	var userid = window.location.search.substring(1);
	var arrayQS = new Array();
	arrayQS = userid.split('&');
	userid = arrayQS[0].replace("id=","");
	
	//Homepage JQuery
	$(".Boxes").sortable({ 
		cursor: "move", opacity: .7, handle: ".dragitem", placeholder: 'selectedcss',forcePlaceholderSize:true, tolerance:'intersect',
		receive: function(e, ui){
			$('.TemplateID').val($(ui.item).attr('id'));
			var templateid = $(".Templates option:selected").val();
			if (templateid=="6b5c1822-2b50-4d99-9eb5-cf0214559607" || templateid=="ee02a163-305a-4568-b9e5-0c6450719279" || templateid=="db9e4432-c59d-4fd5-8689-6d1cfadef76e")
			{
				$(".HeightsRow").hide();
				$(".EntriesRow").show();
			}
			$(this).npModal({ containerTarget: '#WidgetPopup', width: '320px' });
			//$('.npClose').click(function(){ location.href=location.href; });
		},
		update: function(e, ui){
			if (!$(ui.sender).hasClass('Templates'))
			{
				//if (userid!="") $(this).npModal({ containerTarget: '.ProgressIndicator', width: '200px' });
				
				$.ajax({
					type: "POST", dataType: "json",
					url: rootpath + "Update.asmx/SaveOrder",
					data: "{ UserId:'" + userid + "', Order:'" + $('.Boxes').sortable('toArray') + "' }",
					contentType: "application/json; charset=utf-8",
					success: function(msg) {
						//if (userid!="")	window.location.href = window.location.href;
					},
					error: function(xhr, msg)
					{
						alert("fail: " + msg + '\n ' + xhr.responseText);
					}
				});
			}
		}
	});
	
	$(".AddSection").click(function(){
		$('.TemplateID').val( $(".Templates option:selected").val());
			var templateid = $(".Templates option:selected").val();
			if (templateid=="6b5c1822-2b50-4d99-9eb5-cf0214559607" || templateid=="ee02a163-305a-4568-b9e5-0c6450719279" || templateid=="db9e4432-c59d-4fd5-8689-6d1cfadef76e")
			{
				$(".HeightsRow").hide();
				$(".EntriesRow").show();
			}
			
		$(this).npModal({ containerTarget: '#WidgetPopup', width: '320px' });
		$('.npClose').click(function(){ location.href=location.href; });
	});
	
	$("#BtnAddWidget").click(function(){
		var templateid = $(".Templates option:selected").val();
		templateid = $('.TemplateID').val();
		var width = $(".Widths option:selected").val();
		var height = $(".Heights option:selected").val();
		var entries = $(".Entries").val();
			
		if (templateid=="6b5c1822-2b50-4d99-9eb5-cf0214559607" || templateid=="ee02a163-305a-4568-b9e5-0c6450719279" || templateid=="db9e4432-c59d-4fd5-8689-6d1cfadef76e")
			height = "0";
		else
			entries = "0";
		
		$.ajax({
			type: "POST", dataType: "json",
			url: rootpath + "Update.asmx/AddWidget",
			data: "{ UserId:'" + userid + "', TemplateID:'" +templateid + "', Columns:'" +width + "', Height:'" +height + "', Entries:'" +entries + "', Order:'" + $('.Boxes').sortable('toArray') + "' }",
			contentType: "application/json; charset=utf-8",
			success: function(msg) {
				if (userid!="")	window.location.href = window.location.href;
				else
					window.location.href = "index.aspx";
			},
			error: function(xhr, msg)
			{
				//alert("fail: " + msg + '\n ' + xhr.responseText);
			}
		});
	});

	$(".BtnDeleteWidget").click(function() {
	  if (confirm(["Are you sure you want to delete?"])) {
	    var parent = $(this).parents(".Widget");
	    var id = parent.attr("id");

	    if (userid != "") $(this).npModal({ containerTarget: '#ProgressIndicator', width: '200px' });

	    $.ajax({
	      type: "POST", dataType: "json",
	      url: rootpath + "Update.asmx/RemoveWidget",
	      data: "{ UserId:'" + userid + "', ID:'" + id + "' }",
	      contentType: "application/json; charset=utf-8",
	      success: function(msg) {
	        if (msg.d == "Saved") parent.remove();
	        if (userid != "") window.location.href = "member.aspx?id=" + userid;
	      },
	      error: function(xhr, msg) {
	        //alert("fail: " + msg + '\n ' + xhr.responseText);
	      }
	    });
	  }
	});
	
	//Toggle widget on client side
	$(".Widget img").click(
		function()
		{
			var parent = $(this).parents(".Widget");
			
			if ($(this).hasClass('accept'))
				ToggleWidget($(this), parent.attr("id"),false);
			else if ($(this).hasClass('disable'))
				ToggleWidget($(this), parent.attr("id"),true);
		}
	);
	
	//Shows or hides a widget on the client site
	function ToggleWidget(img, id, show) {
	
		$.ajax({
			type: "POST", dataType: "json",
			url: rootpath + "Update.asmx/ToggleWidget",
			data: "{ UserId:'" + userid + "', ID:'" +id + "', Show:'" + show + "' }",
			contentType: "application/json; charset=utf-8",
			success: function(msg) {
				img.toggleClass('accept').toggleClass('disable');
				if (show)
				{
					img.attr('src', 'images/icons/disable.gif');
					if (userid!="")
						img.attr('src', '../images/icons/disable.gif');
					img.attr('title', 'Hide').tooltip({delay:0, showURL:false, track:true});
				}
				else
				{
					img.attr('src', 'images/icons/accept.gif');
					if (userid!="")
						img.attr('src', '../images/icons/accept.gif');
					img.attr('title', 'Show').tooltip({delay:0, showURL:false, track:true});
				}
			},
			error: function(xhr, msg)
			{
				alert("fail: " + msg + '\n ' + xhr.responseText);
			}
		});
	};

	$(".EditContent").tooltip({ track: true, delay: 0, showURL: false, showBody: ' - ' }).dblclick(
		function() {

		  var parent = $(this).parents(".Widget");
		  var date = new Date();
		  $('#ifmf').attr('src', 'radeditor.aspx?id=' + parent.attr("id") + "&time=" + date.getTime());
		  $(this).npModal({
		    containerTarget: '#CustomEditor',
		    targetClass: '#CustomEditorContent',
		    animate: true
		  });
		}
	);
	//End Homepage JQuery


	$(".slider").click( function() { $(this).nextAll("div:first").slideToggle(400); });
	
	//Shows or hides ticket info
	$(".SellTickets").change(
		function()
		{
			$(".SellTicketsArea").slideToggle();
		}
	);
	
	//Shows or hides list of companies
	$(".Affiliation").change(
		function()
		{
			$(".AffiliationArea").slideToggle();
		}
	);

		$(".EventInfoPopup").click(
		function() {
		    //$('#EventInfoContent').css('background-color', 'white');
		    //$('#EventInfoContent').load('eventinfo.aspx?id=' + $(this).attr("id")).dialog({ modal: true });
		    $('#EventFrame').attr('src','eventinfo.aspx?id=' + $(this).attr("id"));
		    $(this).npModal({ containerTarget: '#EventInfo', height: '160px', width: '300px', animate:true});
		}
	);

	//Adds new member detail popup
	$('.NewDetailBtn').click(function(){
		$(this).npModal({
			containerTarget: '#NewMemberDetail',
			width: '320px'
		});
	});
	
	$(".Loading").click(function(){
		$(this).npModal({ containerTarget: '#ProgressIndicator', width: '200px' });
	});
	
	$(".noborders").focus(
		function()
		{
			$(this).css("background", "#D1D3D4");
		}
	);
	
	$(".noborders").blur(
		function()
		{
			$(this).css("background", "");
		}
	);
	$(".buttonemail").click(function() {
	  $(this).npModal({ containerTarget: '#EmailModal', width: '300px', height: '120px'});

  })
	
	//Adds a new member gallery
	$('#BtnGalleryPopup').click(function(){
		$(this).npModal({
			containerTarget: '#GalleryPopup',
			width: '320px'
		});
	});
	
	$(".BtnAddGallery").click(
		function()
		{
			var templateid = $("#Templates option:selected").val();
			var width = $(".Widths option:selected").val();
			var height = $(".Heights option:selected").val();
			
			Update.AddWidget(userid, templateid, width, height,
				function(result, eventArgs)
				{
					if (userid!="")
						window.location.href = "member.aspx?id=" + userid;
					else
						window.location.href = "index.aspx";
				}
			);
		}
	);
	
	$('.AddPhotos').click(function(){
    $(this).npModal({containerTarget:"#PhotoPopup", toTop:true, width:'400px', enableDrag:false, isModal:true, animate:true}); return false;
  });
  
  $(".Photos").sortable({ 
		items: ".GalleryPhotoWrapper", 
		containment: ".Widget",
		cursor: "move", 
		opacity: .7,
		update: function(e, ui)
		{
			var IsAdmin = false;
			if (location.href.indexOf("admin",0)>0) IsAdmin = true;
			Update.SavePhotoOrder($('.Photos').sortable('toArray'), IsAdmin);
		}
	});

	$(".trash .boxcontent1").droppable({
	  accept: '.GalleryPhotoWrapper',
	  drop: function(ev, ui) {
	    var $that = $(this);
	    ui.draggable.fadeOut('slow', function() {
	      ui.draggable
					.appendTo($that)
					.animate({ width: '0', height: '0' }, 'slow', function() {
					  $(this).remove();
					});
	    });

	    var IsAdmin = false;
	    if (location.href.indexOf("admin", 0) > 0) IsAdmin = true;
	    Update.RemovePhoto(ui.draggable.attr('id'), IsAdmin);
	  }
	});
  
	$(".winnerImageTrash .boxcontent1").droppable({
	  accept: '.GalleryPhotoWrapper',
	  drop: function(ev, ui) {
	    var $that = $(this);
	    ui.draggable.fadeOut('slow', function() {
	      ui.draggable
					.appendTo($that)
					.animate({ width: '0', height: '0' }, 'slow', function() {
					  $(this).remove();
					});
	    });

	    Update.RemoveWinnerPhoto(ui.draggable.attr('id'));
	  }
	});
	
	$(".UpdateUserLogin").click(function() {
		 $(this).npModal({containerTarget:"#UserLoginPopup", width:'400px', enableDrag:false, isModal:true, animate:true}); return false;
	});
	$(".tooltip").tooltip({delay:0, showURL:false, track:true, showBody: " - "});
	
	$(".EditEntries").click( function() {
		 $(this).npModal({containerTarget:"#EditEntriesPopup", width:'400px', enableDrag:false, isModal:true, animate:true}); return false;
	});
	//End member gallery code
	
	$(".AddCommitteeMember").click( function() {
		 $(this).npModal({containerTarget:"#AddCommitteeMemberPopup", width:'400px', enableDrag:false, isModal:true, animate:true}); return false;
	});
	
	//Phone field masked inputs
	$(".Phone").unmask();
	$(".Phone").mask("(999) 999-9999",{placeholder:" ", completed:function() { $(this).blur(); $('#PhoneExt').focus(); } });
	$(".Fax").mask("(999) 999-9999",{placeholder:" "});
	
	$("#CompanyPhone").mask("(999) 999-9999",{placeholder:" ", completed:function() { $(this).blur(); $('#CompanyPhoneExt').focus(); } });
	$("#CompanyFax").mask("(999) 999-9999",{placeholder:" "});
	
	
	//Used to add new categories dynamically to the dropdowns
	$(".LabelDropDown").focus(
		function()
		{
			$(".LabelDropDown option:eq(2)").attr('disabled','disabled');
		}
	);
	
	$(".LabelDropDown").change(
		function()
		{
			if ($(".LabelDropDown option:selected").val()=="New")
			{
				$(".LabelDropDown option:first").attr('selected', 'true');
				$(this).npModal({
					containerTarget: '#NewLabelPopup',
					width: '320px'
				});
			}
		}
	);
	
	$(".npClose").click( function() { $('.npContainer').hide();$('.npOverlay').hide() });
	
	$("#FindCompany").click(function(){
    $(this).npModal({containerTarget:"#FindCompanyPopup", width:'400px', enableDrag:false, isModal:true, animate:true}); return false;
  });
  
  $('.FoundCompany').click(function(){
		$("#FindCompany").text($(this).text());
		$("#CompanyID").val($(this).attr("id"));
  });
}

//Resize Event Info Modal
function ResizeModal()
{
	$('#EventInfo').animate({ width: '500px', height:'350px', left:(parseInt($('#EventInfo').css('left'))-90)+'px' }, { duration:1000, complete:function(){$('#EventFrame').width(500).height(320)} } );
}

//-- This method is used to parse the returned server JSON to make sure it is safe to use --//
String.parseJSON  = (function (s) {
  var m = {
    '\b': '\\b',
    '\t': '\\t',
    '\n': '\\n',
    '\f': '\\f',
    '\r': '\\r',
    '"' : '\\"',
    '\\': '\\\\'
  };
  s.parseJSON = function (filter) {
    try {
      if (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(this)) {
          var j = eval('(' + this + ')');
          if (typeof filter === 'function') {
            function walk(k, v) {
              if (v && typeof v === 'object') {
                for (var i in v) {
                  if (v.hasOwnProperty(i)) {
                    v[i] = walk(i, v[i]);
                  }
                }
              }
              return filter(k, v);
            }
            j = walk('', j);
          }
          return j;
        }
      } catch (e) {
      }
      //throw new SyntaxError("parseJSON: filter failed");
      throw new SyntaxError("Trouble loading data please contact support@novelprojects.com");
    };
  }
) (String.prototype);
function SyntaxError(e)
{
 alert(e);
}
