/* Scripts for k20survey.module */

var root = 'connectivity';
$(document).ready(function() {
  $("#data_source").change(function() {
    var v = $(this).val();
    var s = baseURI + root + '/';
    if (v.charAt(0) == 's') s += 'state/' + v.substr(2);
    else if (v.charAt(0) == 't') s += 'type/' + v.substr(2);
    else s += v;
    document.location.href=s;
  });
  $(".legendtab:not(.currenttab)").click(function() {
    clickMenu(this);
  });
  $(".orgprogress a").click(function() {
    return markComplete(this);
  });
});

function markComplete(el) {
  $.ajax({
    dataType: "json",
    url: $(el).attr('href'),
    success: function(data) {
	  $('.orgprogress .orgprogresstext').text(data.msg);
    }
  });
  return false;
}

function clickMenu(el) {
  $(".legendtab").unbind("click");
  var oldblkid=$("span.currenttab").attr('id');
  oldblkid=oldblkid.substring(0, oldblkid.length-3);
  $("span.currenttab").removeClass('currenttab');
  $(el).addClass('currenttab');
  var newblkid=el.id.substring(0, el.id.length-3);
  
  $("#" + oldblkid).slideUp(750, function() {
	$("#" + newblkid).slideDown(750, function() {
	  $(".legendtab:not(.currenttab)").click(function() {clickMenu(this);});
	});
  });

  return false;
}

function reload(val) {
  if(val!=0) location.href=baseURI+root+'/state/'+val+'/edit';
}

var oldtype;
function selType(typeval) {
  if (typeval > 0) $('#type_' + typeval).css('display', 'inline');
  if (oldtype > 0) $('#type_' + oldtype).hide();
  oldtype = typeval;
}

var currtype;
var prevtype;
function selOrg(typeval) {
  if (typeval > 0) {
    currtype = typeval;
    selPage(1);
  } else {
    selPage(0);
    prevtype = 0;
  }
}

var hidechanged = false;
function selPage(pageval) {
  if (prevtype > 0) {
    $('#orgpage_' + prevtype).hide();
    $('#orghide_' + prevtype).hide();
  }
  if (pageval > 0) {
    $('#orghide_' + currtype).show();
    $('#orgpage_' + currtype).css('display', 'inline');
    $('#orgsection').html('<img src="/misc/autocomplete.gif" />').css('display', 'inline');
    var curl=location.href;
    if (location.href.indexOf("?") != -1) curl=curl.substr(0,curl.indexOf("?"));
    curl+=(location.href.charAt(location.href.length-1)=='/') ? "":"/";
    curl+="ajaxload";
    var hide = ($("#edit-type-"+currtype+"-hide").is(":checked")) ? 1 : 0;
    var str="type="+currtype+"&page="+pageval+"&hide="+hide;
    $.ajax({dataType: "json", data: str, url: curl, type: "POST", success: function(response) {
      $('#orgsection').html(response.output);
      if (response.override && !hidechanged) {
        for (x = 0; x < response.override.length; x++) {
          var val = $('#edit-exclude').val();
          if (val.indexOf(";;" + response.override[x] + ";;") == -1) {
            $("#edit-exclude").val(val + response.override[x] + ';;');
          }
        }
      }
      setCheckboxes(false, $("#edit-type-"+currtype+"-hide")[0].checked);
      initLytebox();
    }});
    prevtype = currtype;
  } else $('#orgsection').hide();
}

function showPrompt(all, on) {
  var ret = false;
  var msg = "This will override ALL existing organization display settings";
  if (!all) msg += " for this type";
  msg +=".  Are you sure you want to continue?";
  if (confirm(msg)) {
    ret = true;
    $("#edit-exclude").val(';;');
    hidechanged = true;
    setCheckboxes(all, on);
  }
  return ret;
}

function showText(obj, txt, color) {
  obj.html(' '+txt);
  obj.css("color", color);
  obj.show();
}

function setCheckboxes(all, on) {
  if (all) {
    $("#edit-hide")[0].defaultChecked = on;
    $('#edit-orgtypeselect option').each(function() {
    	this.defaultChecked = on;
    	this.checked = on;
    });
  }
  if ($("#orgsection").css('display') == 'inline') {
	$('#orgsection input:checkbox').each(function() {
		var id = $(this).attr('id');
		if(id.indexOf('segp') == -1) {
			var org = id.substring(id.indexOf('_') + 1, id.lastIndexOf('_'));
			if($('#edit-exclude').val().indexOf(';;' + org + ';;') == -1) {
				this.defaultChecked = on;
				this.checked = on;
			}
		}
	});
  }
}

function saveField(el) {
  if(el.type == "checkbox") {
    var val=(el.checked)?1:0;
    if(val==el.defaultChecked) return;
  } else {
    var val=$(el).val();
    if(val==el.defaultValue) return;
  }
  var org=el.id.substring(el.id.indexOf("_") + 1, el.id.lastIndexOf("_"));
  var block=$(el).parents(".orgblock"), st=block.find(".ajaxsavetext"), ic=block.find(".ajaxsaveicon");
  $("#org_"+org+"_stamp").hide();
  st.hide();
  ic.show();
  
  var st=$('#ajaxtxt-'+el.id);
  if(el.id.indexOf('-url') != -1) val = val.replace(/&/g, "%26");
  var curl=location.href;
  if(location.href.indexOf("?") != -1) curl=curl.substr(0,curl.indexOf("?"));
  curl+=(location.href.charAt(location.href.length-1)=='/') ? "":"/";
  var str="field="+el.id+"&data="+val;
  if(el.type == "checkbox" && el.id.indexOf("segp") == -1) {
    var hide = ($("#edit-type-"+currtype+"-hide").is(':checked')) ? 1 : 0;
    str += "&hide="+hide;
  } else if(el.id.indexOf("_bitrate") != -1) {
    str += "&prev=" + $("#" + el.id)[0].defaultValue + "&type=" + currtype;
  }
  $.ajax({dataType: "json", data: str, url: curl+"ajaxsave", type: "POST", success: function(response) {
	  var e=$("#org_"+org), ic=e.find(".ajaxsaveicon"), st=e.find(".ajaxsavetext");
	  ic.hide();
	  var showStamp=false;
	  if(response.error==0) {
	    if(el.type == "checkbox") {
		  el.defaultChecked=val;
		  if(el.id.indexOf("segp") == -1) {
			var val = $('#edit-exclude').val();
		    if(val.indexOf(";;" + response.org + ";;") == -1) {
			  if(response.override) $("#edit-exclude").val(val + response.org + ';;');
		    } else {
			  if(!response.override) $("#edit-exclude").val(val.replace(response.org + ';;', ''));
		    }
		  }
	    } else {
		  el.defaultValue=val;
		  if(response.stamp) {
			$('#org_' + response.org + '_stamp').html(response.stamp);
			if(checkWidth()) showStamp=true;
		  }
		  if(response.connection!=null) $("#org_"+response.org+"_connection").val(response.connection);
	    }
		showText(st, 'Saved', '#007711');
		setTimeout("$('#org_"+org+"').find('.ajaxsavetext').hide()", 2000);
		if(showStamp) setTimeout("$('#org_"+org+"_stamp').show()", 2000);
		$('.orgprogress .orgprogresstext').html("You are currently entering survey data for this type.<br /><a href='" + curl + "complete/" + $('#edit-orgtypeselect').val() + "' onclick='return markComplete(this);'>Mark as complete</a>");
	  } else showText(st, response.message, '#771100');
  }});
}

